Create and Initialize a Buffer Account using Solana Web3.js

I am trying to deploy a large program using Solana Web3.js. In order to do it, I am trying to create a Buffer Account and initialize it.

The console shows the following:

0: “Program 11111111111111111111111111111111 invoke [1]”

1: “Program 11111111111111111111111111111111 success”

2: “Program BPFLoaderUpgradeab1e11111111111111111111111 invoke [1]”

3: “Program BPFLoaderUpgradeab1e11111111111111111111111 failed: invalid instruction data”

I tried everything to solve the invalid instruction data. I would really appreciate if someone could guide me. Thank you very much.

  const PublicKey = require("@solana/web3.js").PublicKey;
  const SolanaTransaction = require('@solana/web3.js').Transaction;
  const SystemProgram = require('@solana/web3.js').SystemProgram;
  const SolanaWeb3 = require("@solana/web3.js");

  //Creating buffer account...

  let conn = new Connection("https://api.devnet.solana.com", 'confirmed');

  const bufferAccount = Keypair.generate();
  const loaderProgramId = new PublicKey('BPFLoaderUpgradeab1e11111111111111111111111');

  const rentExemption = await conn.getMinimumBalanceForRentExemption(programData.length);

  const createBufferAccountIx = SystemProgram.createAccount({
    fromPubkey: new PublicKey(data.from),
    newAccountPubkey: bufferAccount.publicKey,
    lamports: rentExemption,
    space: programData.length,
    programId: loaderProgramId
  });

Then I wrote the Transaction Instruction to initialize

  //Initializing buffer account...

  const initializeBufferIx = new SolanaWeb3.TransactionInstruction({
    programId: loaderProgramId,
    keys: [
      { pubkey: bufferAccount.publicKey, isSigner: false, isWritable: true },  // buffer
      { pubkey: new PublicKey(data.from), isSigner: false, isWritable: false }, // authority
    ],
    data: Buffer.from([0]), // 0 => InitializeBuffer
  });

  // Combine into a single transaction: create + initialize
  let transaction = new SolanaTransaction().add(createBufferAccountIx, initializeBufferIx);

  //Add recent blockhash
  let recent_blockhash = await conn.getRecentBlockhash();
  transaction.recentBlockhash = recent_blockhash.blockhash;

  //Sign transaction
  transaction.feePayer = new PublicKey(data.from);
  transaction.partialSign(data.from_keypair);
  transaction.partialSign(bufferAccount);

  //Simulate transaction
  const simulationResult = await conn.simulateTransaction(transaction);
  if (simulationResult.value.logs){
    console.log("Simulation Logs:", simulationResult.value.logs);
  }

Related Articles

how to Filter Raydium Pool with geyser

is there anyone know how to filter the raydium pool newly created pairs with geyser python, transaction { signatures: “G337364220355377210257334275137736315314$34692025425535132427620505330]f324265370Z360f27125330122321w254315351Q227 31DJ357e6243203255CL202312r27200” message { header {…