Solana Web3.js Version 2.x and Above: A Beginner’s Guide
As a TypeScript developer, you’re probably familiar with the basics of Solana blockchain programming. But if you’re new to Web3.js, the official library for interacting with the Solana network, it can quickly become confusing. In this article, we’ll walk you through some basic examples of using Solana Web3.js version 2.x and above.
Library Installation
Before we dive into the code, make sure you have the latest version of the Solana CLI installed on your computer. You can install it using npm:
“Bash
npm install solana-cli @solana/web3js
“
Setting up a new cluster
To use Web3.js with a new cluster, create a “.env” file in the root directory and add your cluster’s private key like this:
“makefile
SOLANA_KEY=your-cluster-private-key
“
Replace “your-cluster-private-key” with your actual Solana cluster private key.
Connecting to the cluster
Create a new JavaScript file (e.g. “main.ts”) and import the “web3.js” library. You need to use the “solana” object to connect to the cluster:
“Typescript
import { web3 } from ‘@solana/web3js’;
const Cluster = new web3.WebsocketCluster({
URL: ‘wss://your-cluster-url’,
});
// Generate a key pair for your account
async function genericKeyPair() {
const keyPath = process.env.SOLANA_KEY;
const key = waiting web3.generateKeypairs(keyPath);
return { key, cluster };
}
const { key, cluster } = waiting for genericKeyPair();
“
Create a new account
Use the “web3” object to create a new account:
“typescript
async function createAccount() {
try {
const account = waiting web3.account.createAccount(key);
console.log(Account created: ${account.publicKey.toString()}
);
} Catch (error) {
console.error(error);
}
}
„
Stake and Unlock
To secure your account, use the web3
object to send a transaction:
“Typescript
async function Stake() {
try {
const Transaction = waiting web3.account.stakeAccount(key, { amount: ’10’ });
console.log(${transaction.amount} staked tokens
);
} Catch (error) {
console.error(error);
}
}
„
To unlock your account, use the web3
object to send a transaction:
“Typescript
async function unlock() {
try {
const Transaction = waiting web3.account.unlockAccount(key);
console.log(Account unlocked: ${transaction.publicKey.toString()}
);
} Catch (error) {
console.error(error);
}
}
„
Smart Contracts
To interact with smart contracts, use the ‘web3’ object to send transactions and execute functions:
“typescript
async function ‘deployContract()’
try {
const contract = await web3.eth_contract.createContract({
ContractAddress: ‘0x…your-contract-address…’,
Accounts: [‘your-account-key’],
Bytecode: ‘0x…contract-bytecode…’,
});
console.log(‘contract deployed’);
} Catch (error) {
console.error(error);
}
}
“
Introduction
With this simple example, you should be able to get started using Solana Web3.js version 2.x and above. Remember to replace the placeholders with the actual details of your cluster.
If you run into any issues or have any questions, don’t hesitate to ask!
Resources:
- [Official Solana Web3.js Documentation](
- [Solana CLI Documentation](
- [Web3.js GitHub Repository](