Solana web3.js version 2.x and superior: a beginner’s guide
As a typecript developer, you probably know the basics of the Solana blockchain programming. However, if you are new on web3.js, the official library to interact with the Solana network, things can be done quickly. In this article, we will guide you through a few basic examples on how to use Solana web3.js version 2.x and higher.
Installation of the library
Before diving into the code, make sure you have the latest version of Solana Cli installed on your machine. You can install it using NPM:
`bash
NPM Install Solana-Cli @ Solana / Web3js
'
Configuration of a new cluster
To use web3.js with a new cluster, create a file. Ply to the root directory and add the private key to your cluster as follows:
`'Makefile
Solara_Key = your key key snacks
'
Replace your cluster-private-Key with your private Solana Cluster key.
Connection to the cluster
Create a new JavaScript file (for example, Main.TS
) and import the library ‘Web3.js. You will need to use the object
Solanato connect to the cluster:
Typescript
Import {web3} of '@ solana / web3js';
Const Cluster = New Web3.websoCketCluster ({{
URL: "WSS: // your-cluster-url",
});
// generate a pair of keys for your account
GenerateKeyPair asynchronous function () {
Const Keypath = Process.Ev.solana_Key;
Const Key = wait web3.generatekeypairs (cheypath);
Return {Key, cluster};
}
Const {key, cluster} = wait generateKeypair ();
'
Creation of a new account
Use theWeb3object to create a new account:
Typescript
Asynchronous function createaccount () {
to try {
Constance CONTS = Wait Web3.count.createaccount (Key);
Console.log (Creation of the account: $ {account.publickey.tostring ()});
} Catch (error) {
Console.error (error);
}
}
'
Jaguer and unlock
To play your account, use theWeb3object to send a transaction:
Typescript
Async stake () function {
to try {
Const Transaction = wait web3.account.stakeaccount (Key, {Amount: '10'});
console.log (marked $ {transaction.amount} tokens);
} Catch (error) {
Console.error (error);
}
}
'
To unlock your account, use theWeb3object to send a transaction:
Typescript
async unlock () function {
to try {
Const Transaction = Await Web3.account.unlockaccount (Key);
console.log (Unlocked account: $ {transaction.publickey.tostring ()});
} Catch (error) {
Console.error (error);
}
}
'
Intelligent contracts
To interact with intelligent contracts, use the objectWeb3to send transactions and perform the functions:
Typescript
Asynchronous function deployctract () {
to try {
Const contract = wait web3.eth_contract.createcontract ({{
Contractaddress: '0x ... your contract address ...',
Accounts: ["your key accounts"],
bytecode: '0x ... by contract bytecode ...',
});
console.log ("deployed contract");
} Catch (error) {
Console.error (error);
}
}
` ‘
To start
With this basic example, you should be able to start with Solana web3.js version 2.x and higher. Do not forget to replace the spaces reserved by the details of your real cluster.
If you have problems or have any questions, don’t hesitate to ask!
Resources:
- Official Documentation of Solana Web3.js] (
- [Documentation Solana Cli] (
- [Web3.js GitHub restitory] (