如何从solana交易签名中获取发送方和接收方的钱包ID?

How to get the wallet ID of sender and receiver from solana transaction signature?

我有一个 Solana 交易的签名,我想找到发送方和接收方的钱包 ID。这就是我所做的:

const connection = new Web3.Connection(Web3.clusterApiUrl("devnet"), "confirmed");
const transaction = await connection.getTransaction(signature);

当我执行 console.log(transaction) 时,它有 blockTime 等字段,但没有 tofrom 地址。有人可以帮忙吗?

调用getTransaction时,节点会return指定格式的交易。默认情况下,connection.getTransaction 将 return 使用 base58 编码的交易,因此请尝试使用 connection.getParsedTransaction。这是一个简单传输的示例:

curl https://api.devnet.solana.com -X POST -H "Content-Type: application/json" -d '
                                  {
                                    "jsonrpc": "2.0",
                                    "id": 1,
                                    "method": "getTransaction",
                                    "params": [
                                      "2YHDUWRRh4jwaAqSPJqCiu97FTy6Pu2C6XGbAzsaBbyjQeXW11z
hhF3DJHt4vDFHVND1ybdSHf6E5FxbjFXZP4gQ",
                                      "jsonParsed"
                                    ]
                                  }
                                ' | python3 -m json.tool
{
    "jsonrpc": "2.0",
    "result": {
        "blockTime": 1647001173,
        "meta": {
            "err": null,
            "fee": 5000,
            "innerInstructions": [],
            "logMessages": [
                "Program 11111111111111111111111111111111 invoke [1]",
                "Program 11111111111111111111111111111111 success"
            ],
            "postBalances": [
                23932341357,
                110000000,
                1
            ],
            "postTokenBalances": [],
            "preBalances": [
                23942346357,
                100000000,
                1
            ],
            "preTokenBalances": [],
            "rewards": [],
            "status": {
                "Ok": null
            }
        },
        "slot": 120237987,
        "transaction": {
            "message": {
                "accountKeys": [
                    {
                        "pubkey": "4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn",
                        "signer": true,
                        "writable": true
                    },
                    {
                        "pubkey": "4AUt2JyjzJYVhWkjKugXmzhWizpb4SpLHBtL2fuqPskU",
                        "signer": false,
                        "writable": true
                    },
                    {
                        "pubkey": "11111111111111111111111111111111",
                        "signer": false,
                        "writable": false
                    }
                ],
                "instructions": [
                    {
                        "parsed": {
                            "info": {
                                "destination": "4AUt2JyjzJYVhWkjKugXmzhWizpb4SpLHBtL2fuqPskU",
                                "lamports": 10000000,
                                "source": "4SnSuUtJGKvk2GYpBwmEsWG53zTurVM8yXGsoiZQyMJn"
                            },
                            "type": "transfer"
                        },
                        "program": "system",
                        "programId": "11111111111111111111111111111111"
                    }
                ],
                "recentBlockhash": "3zny2xt5wimev9Jry3TiAyK8yA2pMMcGvsPWpFN5HiL6"
            },
            "signatures": [
                "2YHDUWRRh4jwaAqSPJqCiu97FTy6Pu2C6XGbAzsaBbyjQeXW11zhhF3DJHt4vDFHVND1ybdSHf6E5FxbjFXZP4gQ"
            ]
        }
    },
    "id": 1
}

result.transaction.message.instructions[0].parsed 你会得到你想要的。

有关 getTransaction 通话的更多信息 https://docs.solana.com/developing/clients/jsonrpc-api#gettransaction