1

I want to Modify the compute unit for Versioned Transactions but there are some issue with that.

Before i Have used it for legacy transaction but now when i want to add new compute unit size to TransactionV0 it doesn't do that like before.

General Snippet codes for addComputeUnit, Create and Send v0 transaction:

const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
    units: 1000000   });

const addPriorityFee = ComputeBudgetProgram.setComputeUnitPrice({
    microLamports: 1   });

const instruction = [
         await program.methods.breedBee(false)
           .accounts({list of accounts})
           .signers([ list of signers])
           .instruction()
         ]

const messageV0 = new TransactionMessage({
      payerKey: staging_wallet.publicKey,
      recentBlockhash: latestBlockhash.blockhash,
      instructions: instruction
    }).compileToV0Message([lookupTable]);


const transactionV0 = new VersionedTransaction(messageV0);

transactionV0.add(modifyComputeUnits); transactionV0.add(addPriorityFee);

❌ Property 'add' does not exist on type 'VersionedTransaction'.


So I changed .instruction() to .transaction() and then wanted to add modifyComputeUnits by this way:

const instruction = await program.methods.breedBee(false)
           .accounts({list of accounts})
           .signers([ list of signers])
           .transaction()

           
instruction.add(modifyComputeUnits)
instruction.add(addPriorityFee)
instruction.recentBlockhash = blockhash
instruction.feePayer = staging_wallet.publicKey

with this approach I faced with another error while passing instruction into TransactionMessage:

const messageV0 = new TransactionMessage({
      payerKey: staging_wallet.publicKey,
      recentBlockhash: latestBlockhash.blockhash,
      instructions: instruction
    }).compileToV0Message([lookupTable]);

❌ the error:

TypeError: instructions is not iterable

TypeError: Cannot read properties of undefined (reading 'header')

so it can't compile TransactionMessage.

2 Answers 2

2

Finally I could solve the issue by this way;

const instruction = await program.methods.breedBee(false)
           .accounts({list of accounts})
           .signers([ list of signers])
           .transaction()

           
instruction.add(modifyComputeUnits)
instruction.add(addPriorityFee)
instruction.recentBlockhash = blockhash
instruction.feePayer = staging_wallet.publicKey

and use the instruction in TransactionMessage by this format;

const messageV0 = new TransactionMessage({
      payerKey: staging_wallet.publicKey,
      recentBlockhash: latestBlockhash.blockhash,
      instructions: instruction.instructions
    }).compileToV0Message([lookupTable]);

const transactionV0 = new VersionedTransaction(messageV0);
transactionV0.sign([ signers ]);

const txid = await connection.sendTransaction(transactionV0, { maxRetries: 5 });

const confirmation = await connection.confirmTransaction({
    signature: txid,
    blockhash: latestBlockhash.blockhash,
    lastValidBlockHeight: latestBlockhash.lastValidBlockHeight
  });

2

You can this code:

    const ix_1 = solana.ComputeBudgetProgram.setComputeUnitLimit(
    {
        units: 300_000
    }
);
const ix_2 = solana.ComputeBudgetProgram.setComputeUnitPrice(
    {
        microLamports: 50_000
    }
);

        const block = await connection.getLatestBlockhash();
    const txMsg = new solana.TransactionMessage(
        {
            payerKey: wallet.publicKey,
            recentBlockhash: block.blockhash,
            instructions: [
                ix_1,
                ix_2
            ]
        }
    ).compileToV0Message();
    const tx = new solana.VersionedTransaction(txMsg);

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.