1

Hello I hope you are all having a great day!!

I wanted to ask how I would go about decoding transaction instructions into a readable format. This is my code below

const { decodeMintToInstruction, decodeMintToCheckedInstruction, decodeInstruction } = require('@solana/spl-token');
const { Connection, clusterApiUrl } = require('@solana/web3.js');


const connection = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');


//below is for getblock https://solana-labs.github.io/solana-web3.js/types/GetBlockConfig.html
GetBlockConfig: {
  commitment: 'finalized';
  rewards: false;
  transactionDetails: "full";
}

//below is for get transactions if i need it later
GetVersionedBlockConfig: {
  commitment: 'finalized';
  maxSupportedTransactionVersion: 0;
  rewards: false;
  transactionDetails: "full";
}




async function fetchCurrentSlot() {
  const currentSlot = await connection.getSlot();
  console.log('Current slot:', currentSlot);
  return currentSlot;
}

async function fetchBlock() {
  try {
      const currentSlot = await fetchCurrentSlot();
      const rawConfig = {
        encoding: 'json',
        maxSupportedTransactionVersion: 0,
      }
      const block = await connection.getBlock(currentSlot,rawConfig );
      console.log(block);
      console.log('First transaction details:', JSON.stringify(block.transactions[0].transaction, null, 2));
      const transactionData = block.transactions[0].transaction.message.instructions[0].data
      const transactionInstructions = block.transactions[0].transaction.message.instructions[0]
      //const decodedInstructions = method to decode the instructions when i find one that works
      console.log('First transaction instructions data:',transactionData);
      //console.log('First transaction instructions decoded', decodedInstructions)
  } catch (error) {
      console.error("Error fetching block:", error);
  }
}

fetchBlock();

I want to be able to decode the transaction information but im not sure how, from what I have seen I need to convert it from base58 to unint8. Can anyone give me an example on how to do this?

Thanks and have a great day!

1 Answer 1

3

This is not an easy task in general in Solana. The instruction data is just a bag of bytes and you need to know how to interpret them.

The best case is when you have the IDL that you can use for decoding (IDL defines what byte is interpreted as what). Otherwise you would need somehow manually match bytes and find their meaning.

When IDL is available:

  • When you work only with one particular Program then you can decode the program data based on the IDL that's linked to you project.
  • When you search for a general purpose solution then I could recommend the Explorer Kit library from SolanaFM https://github.com/solana-fm/explorer-kit/tree/main#-usage

I did a tech deep dive write-up on this kind of topic here https://blog.chalda.cz/posts/decoding-solana-data (in particular about transactions is here https://blog.chalda.cz/posts/decoding-solana-data#_transaction_data_structure)

With that I have this (not a perfect) page that I use when I need to decode some data https://chalda.cz/solana-tx/ Maybe it could help.

1
  • 1
    Thanks a lot! Ill look into the explorer kit
    – Golden
    Commented May 25 at 20:56

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.