fre*_*dry 6 node.js solana solana-web3js
我想确定事务是否与特定程序 ID“cndyAnrLdpjq1Ssp1z8xxDsB8dxe7u4HL5Nxi2K5WXZ”(即糖果机程序地址)进行交互。
我使用@solana/web3.js 使用connection.getTransaction(txn.signature) 获取下面(帖子底部)Candy Machine mint 交易数据。
如何判断交易是否与糖果机程序交互?
我尝试深入研究 transaction.meta.innerInstructions,但我不知道如何将其转换为程序 id 地址。
[
{
'accounts': '2',
'data': '3Bxs4h24hBtQy9rw',
'programIdIndex': '8'
},{
'accounts': '2',
'data': '9krTDU2LzCSUJuVZ',
'programIdIndex': '8'
},{
'accounts': '2',
'data': 'SYXsBSQy3GeifSEQSGvTbrPNposbSAiSoh1YA85wcvGKSnYg',
'programIdIndex': '8'
},
...etc.
]
Run Code Online (Sandbox Code Playgroud)
注意:我意识到我可以只在“logMessages”字符串中搜索程序地址,但这有点作弊,我正在尝试了解这一切是如何工作的。
与 Candy Machine 程序交互的铸币交易数据示例:
{
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'
]
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
小智 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 }
// ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2680 次 |
| 最近记录: |