Your First Client dApp with Solana using Javascript

Solana is a decentralized blockchain built to enable scalable, user-friendly apps for the world. Been one of the fastest blockchain in the world and the fastest growing ecosystem in crypto, with thousands of projects spanning DeFi, NFTs, Web3 and more. In this tutorial, i will be guiding you on creating dApp using Javascript only.

No need for prior experience with Rust which is the native language used for building Solana On-chain Programs.

Get Started

To begin, have Node js installed on your machine, our mini project is a dApp called solsend. Our program should be able to send SOL token to other solana wallet address.

For the wallet used in this guide, i am using Phantom Wallet extension with chrome. Remember to change the wallet network to devnet

Building our Sender app

Navigate to your workspace, open your terminal and write the following command

npm init -y solsend

the above command initializes all you need to get started. After successfull intialization,

cd solsend

Once you are in the folder, we will install an important library which aims to provide complete coverage of Solana. The library was built on top of the Solana JSON RPC API.

npm install --save @solana/web3.js

That was easy right? We have just installed our library. The next thing is to create our javascript file where you will be writing codes.

touch index.js

Open it and import the web3 library

const web3 = require("@solana/web3.js");
async function solSend(){
//codes here
}

Everytime we run this code, we want to be able to

  1. Generate a wallet address
  2. Airdrop 1SOL into the generated wallet
  3. Transfer 0.5SOL to our wallet address from Phantom Wallet

With that in mind,

Generate Wallet

//address from phantom(public key)
const phantom_address="7dM9fjE7UvrVNmGmgSTNkMxZP9U7oHaB1gdjeAm63z6t";
//generated address(has public and private key)
const project_address= web3.Keypair.generate();
console.log("Generated Wallet Successfully")

Airdrop 1SOL

Airdrops are considered transactions, and to initiate any transaction a connection to the solana network will have to be opened. The 4 networks are mainnet, devnet, testnet and localnet. Recall that we are interacting with the devnet.

//opening connection
const connection = new web3.Connection(web3.clusterApiUrl('devnet'), 'confirmed')
//request airdrop
let airdrop = await connection.requestAirdrop(project_address.publicKey,  web3.LAMPORTS_PER_SOL);

console.log("Airdrop Successfull",airdrop)

The unit coin for solana is call lamports. 1SOL = 1 billion lamports

Now that we have successfully aidropped 1SOL to our project wallet let's go to the next step.

Transfer 0.5SOL

We are close to the finish line. Now is time to transfer some SOL from our project wallet to our Phantom wallet.

To initiate a successfull transaction,

  • Sender wallet must be funded

  • Sender must sign the transaction

Let's put it in code.

//creates an empty transaction
const transaction = new web3.Transaction();
//this adds an instruction that transfers from one account to another
transaction.add(
web3.SystemProgram.transfer({
fromPubkey: new web3.PublicKey(project_address.publicKey),
toPubkey: new web3.PublicKey(phantom_address),
lamports: 0.5* web3.LAMPORTS_PER_SOL
})
);

//sign transaction
const signature = await web3.sendAndConfirmTransaction(
                connection,
                transaction,
                [project_address]
            );

console.log('Signed transaction hash:', signature);
//call the function
solSend()

We have successfully sent a signed transaction in the Solana devnet cluster.

To run solsend, type this in your terminal

node index.js

If you enjoyed these guide, kindly drop a like and follow me to recieve notifications on my new web3 posts.