Send Solana via javascript functions
quick guide how you can send transaction to Solana blockchain using Solana JavaScript SDK.
Step 1: Init JavaScript Project
Step 2: Install modules
@solana/web3.js — Solana JavaScript SDK bs58 — encoding library
Step 3: Create JavaScript script file
You can use any text editor. Create an empty file with .js extension. For e.g: test.js
Step 4: Include installed modules
At the beginning of created file we will include required modules:
Step 5: Init connection and create Keypair object
As an argument for web3.Connection method we will use any Solana RPC. For e.g default mainnet RPC: https://api.mainnet-beta.solana.com
In constant privateKey we will load wallet private key (you can find it in the settings of your wallet)
We will save Keypair object to constant from.
Step 6: Create Trasnaction
At the second string we create transaction constant. We will pass to web3.Transaction().add() method result of calling web3.SystemProgram.transfer() method with following arguments:
fromPubkey — publicKey property of Keypair object
toPubkey — string with SOL address to where we want to send SOL (lamports)
lamports — amount of lamports that we want to send
fromPubkey also may be a string * toPubkey also may be a Keypair object (in this case we will use publicKey property) * lamports is a piece of SOL. We can use this converter for better understanding
signature constant contains a result of execution web3.sendAndConfirmTransaction() method where we passed connection constant, transaction data and sender Keypair object.
Expected output of following code is a transaction hash:
Last updated