2

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.

1
  • started over a couple times, deleted .local_keys and my id.json file, ran solana-keygen new, ran the 1.simpleTransaction, created the .local_keys file again, but got the same error trying to run 2.complexTransaction: Transaction results in an account (2) with insufficient funds for rent
    – tanny
    Commented May 30, 2023 at 21:01

2 Answers 2

1

Increase the rent exemption for the account with ID 2. The rent exemption is the amount of funds that an account can have without having to pay rent. You can increase the rent exemption for an account by using the increaseRentExemption instruction.

Send more funds to the account with the ID 2. If the account with ID 2 does not have enough funds to pay rent, you can send more funds to the account. You can send funds to an account by using the transfer instruction. or you can airdrop some token in this account

0

That is because the account does not have enough lamports. Solana requires fee for storing data as they need to be managed by the clusters. If the account has enough lamports to pay for the data at least 2 years, the account become rent exempt. Currently solana does not allow newly created accounts to be rent payer, meaning that your account should have enough lamports to become a rent exempt. Furthurmore, if your account was rent exempt and you resized data or used some lamports so that your account can not be rent exempt anymore, transaction will be failed because it does not allow rent exempt accounts to be rent payer. If your account was originally rent payer and you try to resize data but don't have enough lamports to be a rent exempt, transaction will also fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.