简体   繁体   中英

Get program id/address from Solana transaction data (solana/web3.js)

I want to determine whether a transaction has interacted with a specific program id "cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ" (being the Candy Machine program address).

I'm using @solana/web3.js to get the below (bottom of post) Candy Machine mint transaction data using connection.getTransaction(txn.signature).

How can I determine whether the transaction interacted with the Candy Machine program?

I have tried digging into transaction.meta.innerInstructions, but I can't work out how to translate this into program id addresses.

[
  {
    'accounts': '2',
    'data': '3Bxs4h24hBtQy9rw',
    'programIdIndex': '8'
  },{
    'accounts': '2',
    'data': '9krTDU2LzCSUJuVZ',
    'programIdIndex': '8'
  },{
    'accounts': '2',
    'data': 'SYXsBSQy3GeifSEQSGvTbrPNposbSAiSoh1YA85wcvGKSnYg',
    'programIdIndex': '8'
  },
...etc.
]

Note: I realise I could just search for the program address within "logMessages" string, but that's cheating somewhat and I'm trying to learn how this all works.

Example mint transaction data which DOES interact with the Candy Machine program:

{
  blockTime: 1640299576,
  meta: {
    err: null,
    fee: 10000,
    innerInstructions: [ [Object], [Object] ],
    logMessages: [
      'Program 11111111111111111111111111111111 invoke [1]',
      'Program 11111111111111111111111111111111 success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]',
      'Program log: Instruction: InitializeMint',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2390 of 200000 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL invoke [1]',
      'Program log: Transfer 2039280 lamports to the associated token account',
      'Program 11111111111111111111111111111111 invoke [2]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Allocate space for the associated token account',
      'Program 11111111111111111111111111111111 invoke [2]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Assign the associated token account to the SPL Token program',
      'Program 11111111111111111111111111111111 invoke [2]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Initialize the associated token account',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [2]',
      'Program log: Instruction: InitializeAccount',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 3449 of 179576 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL consumed 24522 of 200000 compute units',
      'Program ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL success',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [1]',
      'Program log: Instruction: MintTo',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2879 of 200000 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ invoke [1]',
      'Program 11111111111111111111111111111111 invoke [2]',
      'Program 11111111111111111111111111111111 success',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s invoke [2]',
      'Program log: Instruction: Create Metadata Accounts',
      'Program log: Transfer 5616720 lamports to the new account',
      'Program 11111111111111111111111111111111 invoke [3]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Allocate space for the account',
      'Program 11111111111111111111111111111111 invoke [3]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Assign the account to the owning program',
      'Program 11111111111111111111111111111111 invoke [3]',
      'Program 11111111111111111111111111111111 success',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s consumed 23040 of 176519 compute units',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s success',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s invoke [2]',
      'Program log: Instruction: Create Master Edition',
      'Program log: Transfer 2853600 lamports to the new account',
      'Program 11111111111111111111111111111111 invoke [3]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Allocate space for the account',
      'Program 11111111111111111111111111111111 invoke [3]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Assign the account to the owning program',
      'Program 11111111111111111111111111111111 invoke [3]',
      'Program 11111111111111111111111111111111 success',
      'Program log: Setting mint authority',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]',
      'Program log: Instruction: SetAuthority',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2019 of 126852 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program log: Setting freeze authority',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA invoke [3]',
      'Program log: Instruction: SetAuthority',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA consumed 2021 of 122246 compute units',
      'Program TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA success',
      'Program log: Finished setting freeze authority',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s consumed 28978 of 148307 compute units',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s success',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s invoke [2]',
      'Program log: Instruction: Update Metadata Accounts',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s consumed 7238 of 116624 compute units',
      'Program metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s success',
      'Program cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ consumed 94165 of 200000 compute units',
      'Program cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ success'
    ],
    postBalances: [
         636708880,    1461600,
           2039280,    4572720,
      737969768141,    5616720,
           2853600,    1009200,
                 1, 1089991680,
        9286574880,    1141440,
           1169280,  898174080,
           1141440
    ],
    postTokenBalances: [ [Object] ],
    preBalances: [
         898690080,          0,
                 0,    4572720,
      737719768141,          0,
                 0,    1009200,
                 1, 1089991680,
        9286574880,    1141440,
           1169280,  898174080,
           1141440
    ],
    preTokenBalances: [],
    rewards: [],
    status: { Ok: null }
  },
  slot: 112965987,
  transaction: {
    message: Message {
      header: [Object],
      accountKeys: [Array],
      recentBlockhash: 'B2EuNArBL5XL1coHVZY53Gob7FxrqUxA77DsBRPJwVyd',
      instructions: [Array],
      indexToProgramIds: [Map]
    },
    signatures: [
      '5E8VZSRhfUffRuZFUwoGmDsV1hMzqq2zD5sV6hZPAVFQizXa5SeEpYKw5RWfSMX4bsxB8GyELfPJhYaeLxUn5yjT',
      '4ZaC8PSqBH95Yk8X8aSiea6dEBwostzaf9WxQQKdNNKs4VjGGUJDYw8iAJsp9SxwkgUjM68kZuD4xK1Yxwnq4jM1'
    ]
  }
}

Thanks!

https://solana-labs.github.io/solana-web3.js/modules.html#TransactionResponse

The transaction object is organized to contain a list of all the accounts (programs are accounts) which it interacts with in tx.transaction.message.accountKeys

for any instruction in tx.meta.innerInstructions you can find a programIdIndex ( tx.meta.innerInstructions.instructions[0].programIdIndex ) which refers to the index in the accounts array of the program which the instruction interacts with

so you can find the programId of a specific transaction like so,

let accs = tx.transaction.message.accountKeys; // get all accs of all ixs
for (let inner_ixs of tx.meta.innerInstructions) {
    let first_ix = inner_ixs.instructions[0] // ix of interest 
    let program_id = accs[first_ix.programIdIndex].toString(); 
            
    if (program_id != CANDY_PROGRAM_ID) { continue } 
    // ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM