Created
December 2, 2024 18:47
-
-
Save ducphamle2/b52f66baa3891b06fda4bdb2c1216f45 to your computer and use it in GitHub Desktop.
Pre-compute a simple Sol transfer transaction on Solana
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { | |
| signerIdentity, | |
| createSignerFromKeypair, | |
| Instruction, | |
| publicKey, | |
| } from "@metaplex-foundation/umi"; | |
| import { LAMPORTS_PER_SOL, SystemProgram, Connection } from "@solana/web3.js"; | |
| import base58 from "bs58"; | |
| import dotenv from "dotenv"; | |
| import { createUmi } from "@metaplex-foundation/umi-bundle-defaults"; | |
| import { mplTokenMetadata } from "@metaplex-foundation/mpl-token-metadata"; | |
| dotenv.config(); | |
| (async () => { | |
| // Use Playground wallet as sender, generate random keypair as receiver | |
| // const sender = Keypair.fromSecretKey(base58.decode(process.env.PRIVATE_KEY)); | |
| const umi = createUmi("https://api.mainnet-beta.solana.com", "confirmed").use( | |
| mplTokenMetadata() | |
| ); | |
| const sender = umi.eddsa.createKeypairFromSecretKey( | |
| base58.decode(process.env.PRIVATE_KEY) | |
| ); | |
| const signer = createSignerFromKeypair(umi, sender); | |
| umi.use(signerIdentity(signer, true)); | |
| // const receiver = new Keypair(); | |
| const receiver = sender; | |
| // Define the amount to transfer | |
| const transferAmount = 0.001; // 0.01 SOL | |
| // Instruction index for the SystemProgram transfer instruction | |
| const transferInstructionIndex = 2; | |
| // Create a buffer for the data to be passed to the transfer instruction | |
| const instructionData = Buffer.alloc(4 + 8); // uint32 + uint64 | |
| // Write the instruction index to the buffer | |
| instructionData.writeUInt32LE(transferInstructionIndex, 0); | |
| // Write the transfer amount to the buffer | |
| instructionData.writeBigUInt64LE( | |
| BigInt(transferAmount * LAMPORTS_PER_SOL), | |
| 4 | |
| ); | |
| const transferInstruction: Instruction = { | |
| data: instructionData, | |
| keys: [ | |
| { pubkey: sender.publicKey, isSigner: true, isWritable: true }, | |
| { pubkey: receiver.publicKey, isSigner: false, isWritable: true }, | |
| ], | |
| programId: publicKey(SystemProgram.programId.toBase58()), | |
| }; | |
| let transaction = umi.transactions.create({ | |
| version: 0, | |
| blockhash: (await umi.rpc.getLatestBlockhash()).blockhash, | |
| instructions: [transferInstruction], | |
| payer: umi.payer.publicKey, | |
| }); | |
| transaction = await umi.payer.signTransaction(transaction); | |
| console.log( | |
| "transaction signatures: ", | |
| transaction.signatures.map((sig) => base58.encode(sig)) | |
| ); | |
| const transactionSignature = await umi.rpc.sendTransaction(transaction, { | |
| commitment: "confirmed", | |
| }); | |
| console.log("transaction signature: ", transactionSignature); | |
| // Check and log balance after transfer | |
| const postBalance1 = await umi.rpc.getBalance(sender.publicKey); | |
| const postBalance2 = await umi.rpc.getBalance(receiver.publicKey); | |
| console.log("sender postbalance:", postBalance1); | |
| console.log("receiver postbalance:", postBalance2); | |
| console.log("\n"); | |
| console.log( | |
| "Transaction Signature:", | |
| `https://explorer.solana.com/tx/${base58.encode( | |
| transactionSignature | |
| )}` | |
| ); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment