This Recipe shows how you can use the solana/web3.js library to send transactions programmatically.
Overview
Environment setup
solana/web3.js
and bs58
libraries:npm install --save @solana/web3.js bs58
Get your Chainstack endpoint
The code
Connection and accounts setup
connection
const establishes a connection to the Solana blockchain using a Chainstack endpoint.The next few lines set up two accounts. The first account is created from a private key. The second account is a new account that’s generated on the fly to be a fresh recipient account.The transfer function
async
function to transfer a certain amount of Lamports, the smallest unit of the SOL token in Solana, from the first to the second account. In this case, it’s sending 0.001 SOL (LAMPORTS_PER_SOL * 0.001).It then sends the transaction and waits for confirmation.Run the code
node YOUR_SCRIPT_NAME
The transaction has been sent successfully if you don’t get any errors in the console.