CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-solana--web3-js

Comprehensive JavaScript SDK for building Solana blockchain applications with modern architecture and type safety

93

1.29x

Evaluation93%

1.29x

Agent success when using this tile

Overview
Eval results
Files

task.mdevals/scenario-9/

Multi-Instruction Transaction Builder

Build a transaction builder that constructs Solana transactions with multiple program instructions.

Background

Solana transactions can contain multiple instructions that are executed atomically. Each instruction includes:

  • A program ID that will execute the instruction
  • A list of accounts the instruction will read from or write to
  • Instruction-specific data

Requirements

Implement a module that creates a transaction with three instructions that will:

  1. Transfer 1000 lamports from a source account to a destination account
  2. Transfer 500 lamports from the same source account to a second destination account
  3. Create a new account with 2048 bytes of space, owned by a specified program, funded by the source account

The module should:

  • Export a function buildMultiInstructionTransaction(params) that takes:
    • sourceKeypair: The keypair that will sign and pay for the transaction
    • destination1: Public key of the first transfer recipient
    • destination2: Public key of the second transfer recipient
    • newAccountKeypair: Keypair for the new account to be created
    • programId: Public key of the program that will own the new account
    • recentBlockhash: A recent blockhash to use for the transaction
  • Return a Transaction object with all three instructions added in order
  • Properly configure account metadata:
    • Mark accounts that will be written to as writable
    • Mark accounts that must sign as signers
    • Include the system program ID where needed

Test Cases

Test Case 1: Transaction Structure { .test }

Test File: transaction-builder.test.js

const { buildMultiInstructionTransaction } = require('./transaction-builder');
const { Keypair, PublicKey } = require('@solana/web3.js');

const source = Keypair.generate();
const dest1 = Keypair.generate().publicKey;
const dest2 = Keypair.generate().publicKey;
const newAccount = Keypair.generate();
const programId = Keypair.generate().publicKey;
const blockhash = 'EkSnNWid2cvwEVnVx9aBqawnmiCNiDgp3gUdkDPTKN1N';

const tx = buildMultiInstructionTransaction({
  sourceKeypair: source,
  destination1: dest1,
  destination2: dest2,
  newAccountKeypair: newAccount,
  programId: programId,
  recentBlockhash: blockhash
});

console.assert(tx.instructions.length === 3, 'Transaction should have 3 instructions');
console.assert(tx.recentBlockhash === blockhash, 'Transaction should have correct blockhash');
console.log('Test Case 1: PASSED');

Expected Output:

Test Case 1: PASSED

Test Case 2: Instruction Order { .test }

Test File: transaction-builder.test.js

const { buildMultiInstructionTransaction } = require('./transaction-builder');
const { Keypair, PublicKey, SystemProgram } = require('@solana/web3.js');

const source = Keypair.generate();
const dest1 = Keypair.generate().publicKey;
const dest2 = Keypair.generate().publicKey;
const newAccount = Keypair.generate();
const programId = Keypair.generate().publicKey;
const blockhash = 'EkSnNWid2cvwEVnVx9aBqawnmiCNiDgp3gUdkDPTKN1N';

const tx = buildMultiInstructionTransaction({
  sourceKeypair: source,
  destination1: dest1,
  destination2: dest2,
  newAccountKeypair: newAccount,
  programId: programId,
  recentBlockhash: blockhash
});

// First two instructions should be transfers to SystemProgram
console.assert(
  tx.instructions[0].programId.equals(SystemProgram.programId),
  'First instruction should target SystemProgram'
);
console.assert(
  tx.instructions[1].programId.equals(SystemProgram.programId),
  'Second instruction should target SystemProgram'
);
console.assert(
  tx.instructions[2].programId.equals(SystemProgram.programId),
  'Third instruction should target SystemProgram for account creation'
);
console.log('Test Case 2: PASSED');

Expected Output:

Test Case 2: PASSED

Dependencies { .dependencies }

@solana/web3.js { .dependency }

Provides Solana blockchain interaction capabilities including transaction construction, instruction creation, and account management.

Install with Tessl CLI

npx tessl i tessl/npm-solana--web3-js

tile.json