Solana web3 js

Riddhesh Ganatra
2 min readOct 18, 2021

--

Basic operations on Solana using web3 js.

Libraries:

var web3 = require('@solana/web3.js');
var splToken = require('@solana/spl-token');

Connection and generating keypairs:

const connection = new web3.Connection(
web3.clusterApiUrl('testnet'),
'confirmed',
);
// generate keypair
var fromWallet = web3.Keypair.generate();
var toWallet = web3.Keypair.generate();

Get balance:

    console.log(await connection.getBalance(fromWallet.publicKey));
console.log(await connection.getBalance(toWallet.publicKey));
// add some initial balance. Not possible in production.
var fromAirdropSignature = await connection.requestAirdrop(
fromWallet.publicKey,
1000000000, // lambports
);
await connection.confirmTransaction(fromAirdropSignature);
console.log(await connection.getBalance(fromWallet.publicKey));

Transaction:

var transaction = new web3.Transaction().add(
web3.SystemProgram.transfer({
fromPubkey: fromWallet.publicKey,
toPubkey: toWallet.publicKey,
lamports: 10000,
}),
);
// Sign transaction, broadcast, and confirm
var signature = await web3.sendAndConfirmTransaction(
connection,
transaction,
[fromWallet],
);
console.log('SIGNATURE', signature);
console.log(await connection.getBalance(fromWallet.publicKey));
console.log(await connection.getBalance(toWallet.publicKey));

Multi sign transaction(partialSign)

var feePayerWallet = web3.Keypair.generate();
var feePayerAirdropSignature = await connection.requestAirdrop(
feePayerWallet.publicKey,
1000000000, // lambports
);
await connection.confirmTransaction(feePayerAirdropSignature);
var fundedTransaction = new web3.Transaction({
feePayer: feePayerWallet.publicKey
}).add(
web3.SystemProgram.transfer({
fromPubkey: fromWallet.publicKey,
toPubkey: toWallet.publicKey,
lamports: 10000,
}),
);
// partially sign transaction
let blockhashObj = await connection.getRecentBlockhash();
fundedTransaction.recentBlockhash = await blockhashObj.blockhash;
fundedTransaction.partialSign(fromWallet);// encoding tranaction so that we can send it to backend
let endocdeTransction = fundedTransaction.serialize({
requireAllSignatures: false,
verifySignatures: false,
});
// can be transfered over API to backend if required
endocdeTransction = endocdeTransction.toJSON()
let transactionFromJson = web3.Transaction.from(endcodedTransction);transactionFromJson.partialSign(feePayerWallet);const wireTransaction = transactionFromJson.serialize();
const signature = await this.connection.sendRawTransaction(
wireTransaction,
);

Github repo: https://github.com/riddheshganatra/solana-tutorial

Have any suggestions? Feel free to message me on LinkedIn.

We at Code B are a team of Fullstack software developers, passionate and dedicated to growing businesses for clients.

We have experience in Web Applications(Frontend and Backend), Mobile Applications(Native and Hybrid), DevOps (AWS, GCP, Azure, Digital Ocean, and Heroku), Blockchain(Solana), and UI/UX Design(Figma).

Contact us if you need any help with Software.

--

--

Riddhesh Ganatra

Software Architect, Full Stack Web developer, MEAN/MERN stack, Microservices, etc