Create Transactions on the Solana Network

date
Jul 9, 2024
slug
create-transaction-on-the-solana
status
Published
tags
Solana
NodeJs
summary
All modifications to onchain data happen through transactions. Transactions are mostly a set of instructions that invoke Solana programs. Transactions are atomic, meaning they either succeed - if all the instructions have been executed properly - or fail as if the transaction hasn't been run at all
type
Post
const transaction = new Transaction() const sendSolInstruction = SystemProgram.transfer({ fromPubkey: sender, toPubkey: recipient, lamports: LAMPORTS_PER_SOL * amount }) transaction.add(sendSolInstruction)
const signature = sendAndConfirmTransaction( connection, transaction, [senderKeypair] )
Solana Explorer
All transactions on the blockchain are publicly viewable on the Solana Explorer. For example, you could take the signature returned by sendAndConfirmTransaction() in the example above, search for that signature in the Solana Explorer, then see:
  • when it occurred
  • which block it was included in
  • the transaction fee
  • and more!
console.log( `✅ Loaded our own keypair, the destination public key, and connected to Solana` ); const transaction = new Transaction(); const LAMPORTS_TO_SEND = 5000; const sendSolInstruction = SystemProgram.transfer({ fromPubkey: senderKeypair.publicKey, toPubkey, lamports: LAMPORTS_TO_SEND, }); transaction.add(sendSolInstruction); const signature = await sendAndConfirmTransaction(connection, transaction, [ senderKeypair, ]); console.log( `💸 Finished! Sent ${LAMPORTS_TO_SEND} to the address ${toPubkey}. ` ); console.log(`Transaction signature is ${signature}!`);