TL/DR: Transaction simulation failed: Transaction results in an account (2) with insufficient funds for rent
2.complexTransaction.ts
code:
/**
* Introduction to the Solana web3.js
* Demonstrates how to build a more complex transaction, with multiple instructions
*/
// import custom helpers for demos
import { payer, testWallet, connection, STATIC_PUBLICKEY } from "@/lib/vars";
import { explorerURL, printConsoleSeparator } from "@/lib/helpers";
import { SystemProgram, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
(async () => {
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
console.log("Payer address:", payer.publicKey.toBase58());
console.log("Test wallet address:", testWallet.publicKey.toBase58());
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
/**
* create a simple instruction (using web3.js) to create an account
*/
const space = 0; // on-chain space to allocated (in number of bytes)
// request the cost (in lamports) to allocate `space` number of bytes on chain
const balanceForRentExemption = await connection.getMinimumBalanceForRentExemption(space);
// create this simple instruction using web3.js helper function
const createTestAccountIx = SystemProgram.createAccount({
// `fromPubkey` - this account will need to sign the transaction
fromPubkey: payer.publicKey,
// `newAccountPubkey` - the account address to create on chain
newAccountPubkey: testWallet.publicKey,
// lamports to store in this account
lamports: balanceForRentExemption + 2_000_000,
// total space to allocate
space,
// the owning program for this account
programId: SystemProgram.programId,
});
// create an instruction to transfer lamports
const transferToTestWalletIx = SystemProgram.transfer({
lamports: balanceForRentExemption + 100_000,
// `fromPubkey` - from MUST sign the transaction
fromPubkey: payer.publicKey,
// `toPubkey` - does NOT have to sign the transaction
toPubkey: testWallet.publicKey,
programId: SystemProgram.programId,
});
// create an other instruction to transfer lamports
const transferToStaticWalletIx = SystemProgram.transfer({
lamports: 100_000,
// `fromPubkey` - from MUST sign the transaction
fromPubkey: payer.publicKey,
// `toPubkey` - does NOT have to sign the transaction
toPubkey: STATIC_PUBLICKEY,
programId: SystemProgram.programId,
});
/**
* build the transaction to send to the blockchain
*/
// get the latest recent blockhash
let recentBlockhash = await connection.getLatestBlockhash().then(res => res.blockhash);
// create a transaction message
const message = new TransactionMessage({
payerKey: payer.publicKey,
recentBlockhash,
instructions: [
// transfer lamports to the static wallet
transferToStaticWalletIx,
// create the test wallet's account on chain
createTestAccountIx,
// transfer lamports to the test wallet
transferToTestWalletIx,
// transfer lamports to the static wallet
transferToStaticWalletIx,
],
}).compileToV0Message();
/**
* try changing the order of the instructions inside of the message above...
* see what happens :)
*/
// create a versioned transaction using the message
const tx = new VersionedTransaction(message);
// console.log("tx before signing:", tx);
// sign the transaction with our needed Signers (e.g. `payer` and `keypair`)
tx.sign([payer, testWallet]);
// actually send the transaction
const sig = await connection.sendTransaction(tx);
/**
* display some helper text
*/
printConsoleSeparator();
console.log("Transaction completed.");
console.log(explorerURL({ txSignature: sig }));
})();
I swapped lines 80 and 82, which were reversed to display an error in the lecture. No other modifications have been made. This is the error thrown:
yarn run v1.22.19
warning ../../../package.json: No license field
$ npx ts-node -r tsconfig-paths/register ./scripts/2.complexTransaction.ts
Payer address: GYRDtmVEzJ4Qy**************dAZWhCm7c3
Test wallet address: E86AfE97mAmcS************bdyjp5nk1DYYU8
/Users/**************/Documents/GitHub/workshop-nyc-basics/node_modules/@solana/web3.js/src/connection.ts:5792
throw new SendTransactionError(
^
SendTransactionError: failed to send transaction: Transaction simulation failed: Transaction results in an account (2) with insufficient funds for rent
at Connection.sendEncodedTransaction (/Users/**************/Documents/GitHub/workshop-nyc-basics/node_modules/@solana/web3.js/src/connection.ts:5792:13)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Connection.sendRawTransaction (/Users/**************/Documents/GitHub/workshop-nyc-basics/node_modules/@solana/web3.js/src/connection.ts:5751:20)
at async Connection.sendTransaction (/Users/**************/Documents/GitHub/workshop-nyc-basics/node_modules/@solana/web3.js/src/connection.ts:5699:14)
at async /Users/**************/Documents/GitHub/workshop-nyc-basics/scripts/2.complexTransaction.ts:102:15 {
logs: [
'Program 11111111111111111111111111111111 invoke [1]',
'Program 11111111111111111111111111111111 success',
'Program 11111111111111111111111111111111 invoke [1]',
'Program 11111111111111111111111111111111 success',
'Program 11111111111111111111111111111111 invoke [1]',
'Program 11111111111111111111111111111111 success',
'Program 11111111111111111111111111111111 invoke [1]',
'Program 11111111111111111111111111111111 success'
]
}
error Command failed with exit code 1.
Thanks ahead of time for your help. Stoked to be here.
.local_keys
and myid.json
file, ransolana-keygen new
, ran the1.simpleTransaction
, created the.local_keys
file again, but got the same error trying to run2.complexTransaction
:Transaction results in an account (2) with insufficient funds for rent