Overview
Overview
solana-web3.js (
@solana/web3.js
) is among one of the most popular ways of connecting to and interacting with the Solana blockchain.Included within the functionality of solana-web3.js is the ability to interact with Solana’s StakeProgram
, enabling programmatic delegation.Today, we’ll be delegating 0.02 SOL with solana-web3.js.Environment setup
Environment setup
If you haven’t already, ensure node.js is installed.For this task, you’ll need to install two main dependencies.
@solana/web3.js
, this is what you’ll be using to interact with Solana.bs58
for converting a traditional string private key to aUint8Array
in the account definition process.
JavaScript
Connecting to Solana
Connecting to Solana
To begin, you’ll need to initialize a connection with the Solana blockchain. Like any other Web3 application, you’ll need to create a master variable with an RPC endpoint to achieve this.In this script, we’ve defined this within a variable called
connection
, and passed in a Chainstack Solana Global Elastic Node HTTPS and WSS endpoint.To launch a Solana Global Elastic Node, do the following:1
Navigate to the Chainstack console
2
Deploy a standard-configuration Solana mainnet node
3
Open the newly deployed node
4
Copy the corresponding HTTPS & WSS endpoints
5
Paste them into your
Connection
constructorJavaScript
Importing a wallet
Importing a wallet
We’ll need to import a funded Solana wallet because we’ll be spending SOL (0.02) for the delegation. (If you’d like to generate a fresh wallet, you can do so with
solanaWeb3.Keypair.generate()
)For importing an existing wallet, you’ll need to leverage the fromSecretKey
function on Keypair
. fromSecretKey
takes a Uint8Array
as a parameter, so you’ll need to convert your string private key with bs58.decode
.JavaScript
Creating the stake account
Creating the stake account
The first half of the delegation process involves creating a stake account. We’ll do this with
StakeProgram.createAccount
.You’ll first need to generate a fresh wallet with solanaWeb3.Keypair.generate()
, this will represent the keypair for the stake account.With the keypair generated, you’ll then need to create the transaction instructions; in this case, we’ll call this createStakeAccountInstruction
and pass the following parameters:fromPubkey
, the public key of our funded wallet (the delegator).stakePubkey
, the public key of the stake account we just generated.authorized
, the authorities of the new stake account. We’ll have this set to the funded wallet we previously imported.lamports
, the value of the delegation.
createStakeAccountTransaction
, then sign it and push it to the network.JavaScript
Delegating the stake
Delegating the stake
With the stake account successfully initialized, we can move on to actually delegating the 0.02 SOL.First, we’ll need to define the public key (
PublicKey
) of the validator we’d like to delegate to, we can define this in votePubkey
.From there, similar to the stake account creation, we’ll need to build the delegation instructions. We can do this in a variable called delegateInstructions
and pass the following parameters:stakePubkey
, the public key of the previously generated stake account.authorizedPubkey
, the public key of the stake authority.votePubkey
, the public key of the validator you’d like to stake with. We already defined this, so we’ll just pass it directly.
JavaScript