Solana CookbookTransactions

How to Send SOL

To send SOL, invoke the SystemProgram's transfer instruction.

import {
appendTransactionMessageInstructions,
airdropFactory,
createSolanaClient,
createTransaction,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners
} from "gill";
import { getTransferSolInstruction } from "@solana-program/system";
const { rpc, rpcSubscriptions, sendAndConfirmTransaction } = createSolanaClient(
{
urlOrMoniker: "localnet"
}
);
const sender = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const LAMPORTS_PER_SOL = 1_000_000_000n;
const transferAmount = lamports(LAMPORTS_PER_SOL / 100n); // 0.01 SOL
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: sender.address,
lamports: lamports(LAMPORTS_PER_SOL), // 1 SOL
commitment: "confirmed"
});
const transferInstruction = getTransferSolInstruction({
source: sender,
destination: recipient.address,
amount: transferAmount
});
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const tx = createTransaction({
feePayer: sender,
version: "legacy",
instructions: [transferInstruction],
latestBlockhash
});
const signedTransaction = await signTransactionMessageWithSigners(tx);
await sendAndConfirmTransaction(signedTransaction, { commitment: "confirmed" });
const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("Transaction Signature:", transactionSignature);
Console
Click to execute the code.

Is this page helpful?