Solana Web3.js Version 2.x and Later: A Beginner’s Guide
As a TypeScript developer, you’re probably 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 quickly get confusing. In this article, we’ll walk you through some basic examples of using Solana Web3.js version 2.x and later.
Installing the Library
Before we dive into the code, make sure you have the latest version of the 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 an .env
file in the root directory and add your cluster’s private key as follows:
SOLANA_KEY=private-key-of-your-cluster
Replace your-cluster-private-key
with your actual Solana cluster private key.
Connect to cluster
Create a new JavaScript file (eg main.ts
) and import the web3.js
library. To connect to the cluster, you must use the `salana'' object:
import { web3 } from '@solana/web3js';
const cluster = new web3.WebsocketCluster({
url: 'wss://url-of-your-cluster',
});
// Generate a key pair for your account
asynchronous function generateKeyPair() {
const keyPath = process.env.SOLANA_KEY;
const key = await web3.generateKeypairs(keyPath);
return { key, cluster };
}
const { key, cluster } = await generateKeyPair();
Create a new account
Use the web3object to create a new account:
Account created: ${account.publicKey.toString()}
asynchronous function createAccount() {
try {
const account = await web3.account.createAccount(key);
console.log(
);
} catch (error) {
console.error(error);
}
}
Stake and Unlock
To stake your account, use the web3object to send a transaction:
Staked ${transaction.amount} token
asynchronous function stake() {
try {
const transaction = await web3.account.stakeAccount(key, { amount: '10' });
console.log(
);
} catch (error) {
console.error(error);
}
}
To unlock your account, use the web3object to send a transaction:
Unlocked account: ${transaction.publicKey.toString()}
asynchronous function unlock() {
try {
const transaction = await web3.account.unlockAccount(key);
console.log(
);
} catch (error) {
console.error(error);
}
}
Smart contracts
To interact with smart contracts, use the web3` object to send transactions and execute functions:
asynchronous 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 secured');
} catch (error) {
console.error(error);
}
}
First steps
With this basic example, you should be able to get started with Solana Web3.js version 2.x and above. Don’t forget to replace the placeholders with your actual cluster details.
If you encounter any problems or have any questions, feel free to ask!
Resources:
- Official [Solana Web3.js Documentation](
- [Solana CLI Documentation](
- [Web3.js GitHub Repository](