Transactions

從交易雜湊中檢索 erc20 和 erc721 傳輸資訊

  • March 14, 2022

我想從事務雜湊中檢索以下資訊:

  • 如果該交易是 ERC-20 轉賬:如果是,則轉賬的是什麼 ERC20,金額是多少以及轉給誰
  • 如果該交易是 ERC-721 轉移:如果是,則轉移了什麼 erc721 nft 以及轉移給誰

範例:https ://blockscout.com/xdai/mainnet/tx/0x5f1ac5db6c0c31025ac19f6de22eef665e57262f9953567438827b339828e1a8/token-transfers

我已經在這筆交易中轉移了一些水。現在我想從我的 tx 雜湊中檢索上面的資訊。

這就是我正在做的(使用ethers.js):

       let blockchain = await Blockchain.findBy('network', 'xdai')
       let defaultProvider = await Ethers.getDefaultProvider(blockchain)

       let tx = await defaultProvider.getTransaction("0x5f1ac5db6c0c31025ac19f6de22eef665e57262f9953567438827b339828e1a8")
       let txReceipt = await defaultProvider.getTransactionReceipt("0x5f1ac5db6c0c31025ac19f6de22eef665e57262f9953567438827b339828e1a8")

結果:

{
   "tx": {
       "hash": "0x5f1ac5db6c0c31025ac19f6de22eef665e57262f9953567438827b339828e1a8",
       "blockHash": "0x4023d17a5469c9dfb13bf50a63ce40d90b07a4b8e811fdba25b063ad535ba3f2",
       "blockNumber": 21105871,
       "transactionIndex": 4,
       "confirmations": 151,
       "from": "0x52eB606f94091A2BA4f985D98C62880a9BA591B6",
       "gasPrice": {
           "type": "BigNumber",
           "hex": "0x59682f07"
       },
       "gasLimit": {
           "type": "BigNumber",
           "hex": "0x01630f"
       },
       "to": "0x6A023CCd1ff6F2045C3309768eAd9E68F978f6e1",
       "value": {
           "type": "BigNumber",
           "hex": "0x00"
       },
       "nonce": 937,
       "data": "0xa9059cbb000000000000000000000000fce3c3d51c7f3cc8c0f380be844e7cbad14c1c1d00000000000000000000000000000000000000000000000000027ca57357c000",
       "r": "0xa3ff01c0dcc17cfccf595fefe23468b80727e58003f364f793f6c8ed38da63b4",
       "s": "0x4a709844959b5858607b555a8ec66f90c76c00f10de44386ff80990737d126dd",
       "v": 1,
       "creates": null,
       "chainId": 100
   },
   "txReceipt": {
       "to": "0x6A023CCd1ff6F2045C3309768eAd9E68F978f6e1",
       "from": "0x52eB606f94091A2BA4f985D98C62880a9BA591B6",
       "contractAddress": null,
       "transactionIndex": 4,
       "gasUsed": {
           "type": "BigNumber",
           "hex": "0xead8"
       },
       "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000800000000000000000008000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000008000000000000000000000000000000000008000000000000000000010000000000000000000000000000000000000000000000044000002000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
       "blockHash": "0x4023d17a5469c9dfb13bf50a63ce40d90b07a4b8e811fdba25b063ad535ba3f2",
       "transactionHash": "0x5f1ac5db6c0c31025ac19f6de22eef665e57262f9953567438827b339828e1a8",
       "logs": [
           {
               "transactionIndex": 4,
               "blockNumber": 21105871,
               "transactionHash": "0x5f1ac5db6c0c31025ac19f6de22eef665e57262f9953567438827b339828e1a8",
               "address": "0x6A023CCd1ff6F2045C3309768eAd9E68F978f6e1",
               "topics": [
                   "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                   "0x00000000000000000000000052eb606f94091a2ba4f985d98c62880a9ba591b6",
                   "0x000000000000000000000000fce3c3d51c7f3cc8c0f380be844e7cbad14c1c1d"
               ],
               "data": "0x00000000000000000000000000000000000000000000000000027ca57357c000",
               "logIndex": 21,
               "blockHash": "0x4023d17a5469c9dfb13bf50a63ce40d90b07a4b8e811fdba25b063ad535ba3f2"
           }
       ],
       "blockNumber": 21105871,
       "confirmations": 151,
       "cumulativeGasUsed": {
           "type": "BigNumber",
           "hex": "0x168b7f"
       },
       "status": 1,
       "byzantium": true
   }
}

從交易收據我可以在這裡看到這些數據:

"logs": [
           {
               "transactionIndex": 4,
               "blockNumber": 21105871,
               "transactionHash": "0x5f1ac5db6c0c31025ac19f6de22eef665e57262f9953567438827b339828e1a8",
               "address": "0x6A023CCd1ff6F2045C3309768eAd9E68F978f6e1",
               "topics": [
                   "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                   "0x00000000000000000000000052eb606f94091a2ba4f985d98c62880a9ba591b6",
                   "0x000000000000000000000000fce3c3d51c7f3cc8c0f380be844e7cbad14c1c1d"
               ],
               "data": "0x00000000000000000000000000000000000000000000000000027ca57357c000",
               "logIndex": 21,
               "blockHash": "0x4023d17a5469c9dfb13bf50a63ce40d90b07a4b8e811fdba25b063ad535ba3f2"
           }
       ],

但是我如何找到我需要的資訊(也顯示在資源管理器上)。

我想知道轉賬的是什麼貨幣和金額。

您可以從日誌中讀取。

可以在這裡找到一篇解釋如何使用事件的好文章https://betterprogramming.pub/learn-solidity-events-2801d6a99a92

要了解“貨幣”或代幣,您可以閱讀被呼叫的智能合約。交易的“to”、“from”和“amount”都在日誌中。To 和 From 是事件的主題(請參閱上面的文章),金額在數據中。

要檢查已呼叫的函式,您可以從事務數據中讀取。它的前 4 個字節 (a9059cbb) 是函式的簽名。例如,您可以檢查簽名是否是“轉移”的簽名。

引用自:https://ethereum.stackexchange.com/questions/123811