2

Hello you may have seen my last post, Error : TypeError: Cannot read properties of undefined (reading 'equals')

This is a follow up to that post.

I decided to experiment more with decodeMintToInstructions and decodeInstructions. To do this I found a transaction that had a MintTo instruction and was on the token program, I did this using solscan.

I made some code to get this transaction and log the instructions for it.

const { decodeMintToInstruction, decodeMintToCheckedInstruction, decodeInstruction, programId, SPL_TOKEN_PROGRAM_ID } = require('@solana/spl-token');
const { Connection, clusterApiUrl, TransactionInstruction } = require('@solana/web3.js');
const connection = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');

// WARNING  ONLY WORKS FOR LEGACY TRANSACTIONS ATM AS VERSION 0 ONES ARE COMPLCATED AND IDK HOW TO WORK WITH THEM

async function getTransactionDetails(signature) {
  try {
    const connection = new Connection(clusterApiUrl("mainnet-beta"), "confirmed");
    const rawConfig = {
      encoding: 'json',
      maxSupportedTransactionVersion: 0,
    }
    const transaction = await connection.getTransaction(signature, rawConfig);

    if (transaction) {
      console.log("Transaction :", JSON.stringify(transaction, null, 2));
    } else {
      console.log("Transaction not found.");
    }

    const instructions = transaction.transaction.message.instructions;

    instructions.forEach((instruction, index) => {
      console.log(`Instruction ${index}:`, instruction);
    });
    
  } catch (error) {
    console.error("Error :", error);

    
  }
}
 // 3rmaMeCwfsknUFTyJenmvnV8NnRBLiDQb8CXbSdGTwPazrpZxwuf1XVuqAcxi9HUQmf2J8y5LHrXWNyBQb6jcBVE version 0 transaction for testing https://solscan.io/tx/3rmaMeCwfsknUFTyJenmvnV8NnRBLiDQb8CXbSdGTwPazrpZxwuf1XVuqAcxi9HUQmf2J8y5LHrXWNyBQb6jcBVE
const transactionSignature = "25VERnHKBHfwYBbzezKXkWNZuMsqcsC2EuDNoEcP7CT2p9PLSmzZFJoyA8KcN8qBZ6N7NBDUySj9GVqCQpGoPZtG"; //legacy version https://solscan.io/tx/25VERnHKBHfwYBbzezKXkWNZuMsqcsC2EuDNoEcP7CT2p9PLSmzZFJoyA8KcN8qBZ6N7NBDUySj9GVqCQpGoPZtG
getTransactionDetails(transactionSignature);

Okay so the following is the log for that:

Transaction : {
  "blockTime": 1717180198,
  "meta": {
    "computeUnitsConsumed": 6211,
    "err": null,
    "fee": 20000,
    "innerInstructions": [],
    "loadedAddresses": {
      "readonly": [],
      "writable": []
    },
    "logMessages": [
      "Program 11111111111111111111111111111111 invoke [1]",
      "Program 11111111111111111111111111111111 success",
      "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]",
      "Program log: Instruction: MintTo",
      "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 5761 of 199850 compute units",
      "Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success",
      "Program ComputeBudget111111111111111111111111111111 invoke [1]",
      "Program ComputeBudget111111111111111111111111111111 success",
      "Program ComputeBudget111111111111111111111111111111 invoke [1]",
      "Program ComputeBudget111111111111111111111111111111 success"
    ],
    "postBalances": [
      1098870000,
      1099510000,
      1447680,
      261688945306,
      2039280,
      1,
      33361680,
      1,
      42706560,
      934087680
    ],
    "postTokenBalances": [
      {
        "accountIndex": 4,
        "mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
        "owner": "DBD8hAwLDRQkTsu6EqviaYNGKPnsAMmQonxf7AH8ZcFY",
        "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
        "uiTokenAmount": {
          "amount": "79758581385951",
          "decimals": 6,
          "uiAmount": 79758581.385951,
          "uiAmountString": "79758581.385951"
        }
      }
    ],
    "preBalances": [
      1098890000,
      1099510000,
      1447680,
      261688945306,
      2039280,
      1,
      33361680,
      1,
      42706560,
      934087680
    ],
    "preTokenBalances": [
      {
        "accountIndex": 4,
        "mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
        "owner": "DBD8hAwLDRQkTsu6EqviaYNGKPnsAMmQonxf7AH8ZcFY",
        "programId": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
        "uiTokenAmount": {
          "amount": "59758581385951",
          "decimals": 6,
          "uiAmount": 59758581.385951,
          "uiAmountString": "59758581.385951"
        }
      }
    ],
    "rewards": [],
    "status": {
      "Ok": null
    }
  },
  "slot": 269086211,
  "transaction": {
    "message": {
      "header": {
        "numReadonlySignedAccounts": 1,
        "numReadonlyUnsignedAccounts": 5,
        "numRequiredSignatures": 2
      },
      "accountKeys": [
        "8wEnkDFv5bqP1K6KeQdoVLFaHm3XukkqMFikXa1Ne4tX",
        "DLEZaNSqfSHB2RMs7yZPnMMx87igoVYGqD1xfiQXntcD",
        "4KUTSfhh7aNmXxTgoTEYjoZ2xNM498zikEjHgCchUWmQ",
        "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
        "FSxJ85FXVsXSr51SeWf9ciJWTcRnqKFSmBgRDeL3KyWw",
        "11111111111111111111111111111111",
        "2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9",
        "ComputeBudget111111111111111111111111111111",
        "SysvarRecentB1ockHashes11111111111111111111",
        "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
      ],
      "recentBlockhash": "EUR5Vc8eJiTNZMpGbgb7LphZ1nsb8QzpWruteqJJ1cLf",
      "instructions": [
        {
          "accounts": [
            2,
            8,
            0
          ],
          "data": "6vx8P",
          "programIdIndex": 5,
          "stackHeight": null
        },
        {
          "accounts": [
            3,
            4,
            6,
            0,
            1
          ],
          "data": "6An732uugsbm",
          "programIdIndex": 9,
          "stackHeight": null
        },
        {
          "accounts": [],
          "data": "3Sy41WEwNLnT",
          "programIdIndex": 7,
          "stackHeight": null
        },
        {
          "accounts": [],
          "data": "Fj2Eoy",
          "programIdIndex": 7,
          "stackHeight": null
        }
      ],
      "indexToProgramIds": {}
    },
    "signatures": [
      "25VERnHKBHfwYBbzezKXkWNZuMsqcsC2EuDNoEcP7CT2p9PLSmzZFJoyA8KcN8qBZ6N7NBDUySj9GVqCQpGoPZtG",
      "2Pq6NDcj6uDXMFW88VHkNphUWijDndjmfsicjkqn5r1rcjFcK2GBA8FfcdkGVS95rGtTuXGi4imLpBeu6Nsz6UT6"
    ]
  },
  "version": "legacy"
}
Instruction 0: {
  accounts: [ 2, 8, 0 ],
  data: '6vx8P',
  programIdIndex: 5,
  stackHeight: null
}
Instruction 1: {
  accounts: [ 3, 4, 6, 0, 1 ],
  data: '6An732uugsbm',
  programIdIndex: 9,
  stackHeight: null
}
Instruction 2: {
  accounts: [],
  data: '3Sy41WEwNLnT',
  programIdIndex: 7,
  stackHeight: null
}
Instruction 3: { accounts: [], data: 'Fj2Eoy', programIdIndex: 7, stackHeight: null }

Now then all I needed to do next was use decodeInstruction or decodeMintToInstruction on the instructions listed there, however I kept getting the error Error decoding Instruction 2: TypeError: b must be a Uint8Array If i used decodeInstruction.

And Error decoding Instruction 3: TypeError: Cannot read properties of undefined (reading 'equals') If i used decodeMintToInstruction.

So I was really confused on what to do, im not sure what the error for decodeInstructions means but the one for decodeMintToInstruction indicated that there is no programId present.

Then I got a response on the old question from @JonC I linked at the start saying that If I used jsonParsed as the encoding I would not have to decode anything. So I tried that on my new code (not the code i used in the prev question as that straight up didnt work) but this is the log:

Error : StructError: At path: transaction.message.accountKeys.0 -- Expected a string, but received: [object Object]
    at validate (C:\Users\user\node_modules\superstruct\dist\index.cjs:301:27)
    at Object.create (C:\Users\user\node_modules\superstruct\dist\index.cjs:266:24)
    at C:\Users\user\node_modules\@solana\web3.js\lib\index.cjs.js:4514:29
    at Struct.coercer (C:\Users\user\node_modules\superstruct\dist\index.cjs:861:38)
    at run (C:\Users\user\node_modules\superstruct\dist\index.cjs:123:28)
    at run.next (<anonymous>)
    at shiftIterator (C:\Users\user\node_modules\superstruct\dist\index.cjs:71:39)
    at validate (C:\Users\user\node_modules\superstruct\dist\index.cjs:299:23)
    at Object.create (C:\Users\user\node_modules\superstruct\dist\index.cjs:266:24)
    at Connection.getTransaction (C:\Users\user\node_modules\@solana\web3.js\lib\index.cjs.js:7398:29) {       
  value: {
    pubkey: '8wEnkDFv5bqP1K6KeQdoVLFaHm3XukkqMFikXa1Ne4tX',
    signer: true,
    source: 'transaction',
    writable: true
  },
  type: 'string',
  refinement: undefined,
  key: 0,
  path: [ 'transaction', 'message', 'accountKeys', 0 ],
  branch: [
    {
      blockTime: 1717180198,
      meta: [Object],
      slot: 269086211,
      transaction: [Object],
      version: 'legacy'
    },
    { message: [Object], signatures: [Array] },
    {
      accountKeys: [Array],
      instructions: [Array],
      recentBlockhash: 'EUR5Vc8eJiTNZMpGbgb7LphZ1nsb8QzpWruteqJJ1cLf'
    },
    [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
    {
      pubkey: '8wEnkDFv5bqP1K6KeQdoVLFaHm3XukkqMFikXa1Ne4tX',
      signer: true,
      source: 'transaction',
      writable: true
    }
  ],
  failures: [Function (anonymous)]
}

As far as I understand this is because accountKeys is an array of objects and not strings.

Sorry for how long this post is and thanks if you read it despite that. Im really struggling on this and I would appreciate some insight on what im doing wrong.

UPDATE: I have found an idl for the token program however im not sure how to decode the instructions using that, I know I have to use @project-serum/anchor but im not sure how to use it to decode/deserialise instructions

Thanks and have a nice day!! :3

1 Answer 1

0

I would recommend to read the technical details on how the transaction is defined:

Instructions 2 and 3 say the programIdIndex: 7 which is from the accounts listed

      "accountKeys": [
        "8wEnkDFv5bqP1K6KeQdoVLFaHm3XukkqMFikXa1Ne4tX",
        "DLEZaNSqfSHB2RMs7yZPnMMx87igoVYGqD1xfiQXntcD",
        "4KUTSfhh7aNmXxTgoTEYjoZ2xNM498zikEjHgCchUWmQ",
        "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
        "FSxJ85FXVsXSr51SeWf9ciJWTcRnqKFSmBgRDeL3KyWw",
        "11111111111111111111111111111111",
        "2wmVCSfPxGPjrnMMn7rchp4uaeoTqN39mXFC2zhPdri9",
        "ComputeBudget111111111111111111111111111111",
        "SysvarRecentB1ockHashes11111111111111111111",
        "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
      ],

So those will be the ComputeBudget111111111111111111111111111111.

Maybe you can just run the decoding within the try-catch block and ignore any exception in a way you know it won't be the Token instruction that is smoothly decodable with decodeMintToInstruction.

For the jsonParsed try to print the whole object in a more readable way (without [Object]) in getting it parsed and printed as JSON, use the JSON.stringy(...). It's good to get more insight into what was parsed in the transaction structure.

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.