Go-Ethereum

解碼輸入參數會引發無效錯誤

  • June 20, 2020

將 abi 用於我得到的智能合約,我試圖使用web3.eth.abi.decodeParameter s 函式解碼交易收據的輸入參數。logs.json 文件如下:

{
 "blockHash": "0x05578d94e81075639f393dd1252deda65c6ab244e399073b6ff946d26af0dce7",
 "blockNumber": 8945008,
 "contractAddress": "0x0E3A2A1f2146d86A604adc220b4967A898D7Fe07",
 "cumulativeGasUsed": 9263902,
 "from": "0xef536c805482FeB3D838426ED48320f77FD5F82b",
 "gasUsed": 6928008,
 "logs": [
   {
     "address": "0x0E3A2A1f2146d86A604adc220b4967A898D7Fe07",
     "topics": [
       "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0",
       "0x0000000000000000000000000000000000000000000000000000000000000000",
       "0x000000000000000000000000ef536c805482feb3d838426ed48320f77fd5f82b"
     ],
     "data": "0x",
     "blockNumber": 8945008,
     "transactionHash": "0xc599d6ea2e6d4bd9d908f887a46040338105c20d2e8a08bc457b75180a660f17",
     "transactionIndex": 28,
     "blockHash": "0x05578d94e81075639f393dd1252deda65c6ab244e399073b6ff946d26af0dce7",
     "logIndex": 38,
     "removed": false
   }
 ],
 "logsBloom": "0x00000002000000000000000000000000000000000000000000800000000000000002000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000001000000000000000000400000000000000000020000000000000000000800000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000001000000020000000000000000000000000000000000000000000000000000000000000000000",
 "status": 1,
 "to": null,
 "transactionHash": "0xc599d6ea2e6d4bd9d908f887a46040338105c20d2e8a08bc457b75180a660f17",
 "transactionIndex": 28
}

作為第一個主題呼叫的函式OwnershipTransferred的 abi是

{
   "anonymous": false,
   "inputs": [
     {
       "indexed": true,
       "internalType": "address",
       "name": "previousOwner",
       "type": "address"
     },
     {
       "indexed": true,
       "internalType": "address",
       "name": "newOwner",
       "type": "address"
     }
   ],
   "name": "OwnershipTransferred",
   "type": "event"
 }
]

我嘗試解碼數據的行是

let value = web3.eth.abi.decodeParameters(['address', 'address'], '0x');
console.log(value);

最後我得到的錯誤是

Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.
   at ABICoder.decodeParameters (/home/ethereumpc1/Desktop/abi_extraction/node_modules/web3-eth-abi/src/index.js:332:15)
   at Object.<anonymous> (/home/ethereumpc1/Desktop/abi_extraction/index.js:28:26)
   at Module._compile (internal/modules/cjs/loader.js:778:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
   at Module.load (internal/modules/cjs/loader.js:653:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
   at Function.Module._load (internal/modules/cjs/loader.js:585:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
   at startup (internal/bootstrap/node.js:283:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

請告訴我我做錯了什麼。

在 ABI 中,事件有兩個被索引的參數。索引參數不包含在數據欄位中,而是包含在主題數組中。

     "topics": [
       "0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0",
       "0x0000000000000000000000000000000000000000000000000000000000000000",
       "0x000000000000000000000000ef536c805482feb3d838426ed48320f77fd5f82b"
     ],

第一個元素是事件簽名,接下來的兩個是您的參數。

如果您想解碼事件日誌條目,我建議您web3.eth.abi.decodeLog改用。如果事件 abi 發生變化,它應該更容易和防錯。

web3.eth.abi.decodeLog(eventABI, data, topics);

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