Solana Web3.js Version 2.x and Above: A Beginner’s Guide
As a TypeScript developer, you’re likely familiar with the basics of Solana blockchain programming. However, if you’re new to Web3.js, the official library for interacting with the Solana network, things can get confusing quickly. In this article, we’ll walk you through some basic examples on how to use Solana Web3.js version 2.x and above.
Installing the Library
Before we dive into the code, make sure you have the latest version of Solana CLI installed on your machine. You can install it using npm:
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 as follows:
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’ll need to use the solana
object to connect to the cluster:
import { web3 } from '@solana/web3js';
const cluster = new web3.WebsocketCluster({
url: 'wss://your-cluster-url',
});
// Generate a key pair for your account
async function generateKeyPair() {
const keyPath = process.env.SOLANA_KEY;
const key = await web3.generateKeypairs(keyPath);
return { key, cluster };
}
const { key, cluster } = await generateKeyPair();
Creating a New Account
Use the web3
object to create a new account:
async function createAccount() {
try {
const account = await web3.account.createAccount(key);
console.log(Created account: ${account.publicKey.toString()}
);
} catch (error) {
console.error(error);
}
}
Staking and Unlocking
To stake your account, use the web3
object to send a transaction:
async function stake() {
try {
const transaction = await web3.account.stakeAccount(key, { amount: '10' });
console.log(Staked ${transaction.amount} tokens
);
} catch (error) {
console.error(error);
}
}
To unlock your account, use the web3
object to send a transaction:
async function unlock() {
try {
const transaction = await web3.account.unlockAccount(key);
console.log(Unlocked account: ${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:
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);
}
}
Getting Started
With this basic example, you should be able to get started with Solana Web3.js version 2.x and above. Remember to replace placeholders with your actual cluster details.
If you encounter any issues or have questions, feel free to ask!
Resources:
- Official [Solana Web3.js documentation](
- [Solana CLI documentation](
- [Web3.js GitHub repository](