从Solana交易数据中获取程序ID/地址(solana/web3.js)

6

我想确定一笔交易是否与特定的程序ID "cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ"(即糖果机程序地址)进行了交互。

我正在使用@solana/web3.js获取下面(帖子底部)的Candy Machine铸币交易数据,使用connection.getTransaction(txn.signature)。

我如何确定该交易是否与糖果机程序进行了交互?

我已经尝试深入研究transaction.meta.innerInstructions,但我无法弄清如何将其转换为程序ID地址。

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

注意:我意识到我可以在“logMessages”字符串中搜索程序地址,但这有点作弊,我正在尝试了解所有这些是如何工作的。

与糖果机程序交互的示例铸币交易数据:

{
  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'
    ]
  }
}

感谢!

1个回答

3

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

事务对象被组织为包含所有与之交互的账户(程序是账户)列表,存储在tx.transaction.message.accountKeys中。

对于tx.meta.innerInstructions中的任何指令,您可以找到一个programIdIndex ( tx.meta.innerInstructions.instructions[0].programIdIndex ),该索引指向程序数组中该指令所涉及的程序的索引。

因此,您可以通过以下方式找到特定事务的programId:

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 } 
    // ...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接