2

I have a VersionedTransaction, which has a message with some instructions of type MessageCompiledInstruction. I'm trying to decode the ComputeBudgetInstruction with the following code:

const msg = versionedTransaction.message
for (const ix of msg.compiledInstructions) {
    if (msg.staticAccountKeys[ix.programIdIndex].toBase58() == "ComputeBudget111111111111111111111111111111") {
    console.log(ix)
// e.g.
//{
//  programIdIndex: 19,
//  accountKeyIndexes: [],
//  data: <Buffer 02 ae 02 04 00>
//}

    ComputeBudgetInstruction.decodeInstructionType(ix)
  }
}

I want to decode the compute budget instructions, however decodeInstructionType takes in input a TransactionInstruction, which can be instantiated with the following constructor:

  type TransactionInstructionCtorFields = {
    keys: Array<AccountMeta>;
    programId: PublicKey;
    data?: Buffer;
  };

I'm not sure where to get the AccountMeta objects. So in short my question is how to convert a MessageCompiledInstruction to TransactionInstruction?

1 Answer 1

3

If you have a versioned transaction, you can get the instructions of that transaction by

    let ixs = TransactionMessage.decompile(
        versionedTx.message
    ).instructions;

The AccountMeta struct is used to declare the accounts the instruction reads and writes to.

Here's an example pulled from the docs with the transfer sol instruction

const transferInstruction = new TransactionInstruction({
  keys: [
    { pubkey: sender.publicKey, isSigner: true, isWritable: true },
    { pubkey: receiver.publicKey, isSigner: false, isWritable: true },
  ],
  programId: SystemProgram.programId,
  data: instructionData,
});

AccountMeta ref: https://solana.com/docs/core/transactions#accountmeta

Ideally, you wouldn't be writing every transaction instruction account as most programs on Solana build the tx for you, e.g the transfer

const transferInstruction = SystemProgram.transfer({
  fromPubkey: sender.publicKey,
  toPubkey: receiver.publicKey,
  lamports: transferAmount * LAMPORTS_PER_SOL, // Convert transferAmount to lamports
});

In your example, you can request more compute units for a program using

const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({
  units: 300_000,
});

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.