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 )}` ); })();