Quick Start
Transactions and Instructions

Transactions and Instructions

  • explain transactions and instructions

Transactions

  • message/signature
  • atomic transaction execution, size limit

Instructions

  • explain components of instruction
  • programid, accounts, instruction data

How to Transfer SOL

  • simple SOL transfer

  • final code

https://beta.solpg.io/656a0ea7fb53fa325bfd0c3e

Solana Playground

  • open solana playground
  • starter code
client.ts
import {
  LAMPORTS_PER_SOL,
  SystemProgram,
  Transaction,
  sendAndConfirmTransaction,
  Keypair,
} from "@solana/web3.js";
 
const connection = pg.connection;
 
const sender = pg.wallet.keypair;
const receiver = new Keypair();

Build Transfer Instruction

client.ts
const transferAmount = 0.01;
 
const transferInstruction = SystemProgram.transfer({
  fromPubkey: sender.publicKey,
  toPubkey: receiver.publicKey,
  lamports: transferAmount * LAMPORTS_PER_SOL,
});

Add Instruction to Transaction

client.ts
const transaction = new Transaction().add(transferInstruction);

Send Transaction

client.ts
const transactionSignature = await sendAndConfirmTransaction(
  connection,
  transaction,
  [sender],
);

Run script

Terminal
run

How to Create an Account

Minimum Rent

  • example transfer below minimum rent, tx fails
client.ts
const transferInstruction = SystemProgram.transfer({
  fromPubkey: sender.publicKey,
  toPubkey: receiver.publicKey,
  lamports: 1, // 1 lamport
});
Output
Running client...
  client.ts:
    Uncaught error: failed to send transaction: Transaction simulation failed: Transaction results in an account (1) with insufficient funds for rent
  • minimum lamports for accountinfo, where data field stores 0 bytes of extra data
client.ts
// Minimum balance for rent exemption for new account
const minumumLamports = await connection.getMinimumBalanceForRentExemption(0);
 
const transferInstruction = SystemProgram.transfer({
  fromPubkey: sender.publicKey,
  toPubkey: receiver.publicKey,
  lamports: minumumLamports,
});

Notes

  • explain wallet is just system account
  • transfer sol, both using helper and manually built instruction
Last updated on