Transactions

如何解碼我的事務日誌的日誌事件?

  • September 21, 2020

我在我的乙太坊交易日誌中收到一個交易事件。

 .on('receipt', function(receipt){             
       console.log("address: " + receipt.logs[0].address);
       console.log("receipt.logs[0].args " + receipt.logs[0].data );

 })

我的交易收據對像是:

{
"blockHash": "0x2de8d9ff07b91523359c08a0004131d5839a7d4ff5aa1bd00843777a5e096ca1",
"blockNumber": 8726746,
"contractAddress": null,
"cumulativeGasUsed": 5151494,
"from": "0xf73875991f5fe4ffaf32380e25e185f4f9a3b245",
"gasUsed": 93895,
"logs": [
   {
       "address": "0x75508672C8C155b06a0Bc82e8CA9aB967c9a17d8",
       "blockHash": "0x2de8d9ff07b91523359c08a0004131d5839a7d4ff5aa1bd00843777a5e096ca1",
       "blockNumber": 8726746,
       "data": "0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000c496c2056696e6369746f7265000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047177716500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000477657771000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003717765000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000047177657100000000000000000000000000000000000000000000000000000000",
       "logIndex": 16,
       "removed": false,
       "topics": [
           "0x8cdd8faf489944941fc651bffdc4a404b57bddc1c35eccf8531de89a4422d777"
       ],
       "transactionHash": "0xf29b396e697713937dcf56e3f0c0dd2577beeac48c67a8168ebc084f75daf7b2",
       "transactionIndex": 12,
       "id": "log_a7c1ba91"
   }
],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000400000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000",
"status": true,
"to": "0x75508672c8c155b06a0bc82e8ca9ab967c9a17d8",
"transactionHash": "0xf29b396e697713937dcf56e3f0c0dd2577beeac48c67a8168ebc084f75daf7b2",
"transactionIndex": 12
}

收據.logs

$$ 0 $$.data 是 0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000c496c2056696e6369746f72650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b3535353535353535353536000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000336363600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013600000000000000000000000000000000000000000000000000000000000000

更新-在我的智能合約中,事件是

event Winner(
   string messanger, 
   string username,
   string nome,
   string cognome,
   string email
);

emit Winner ("Il Vincitore", "111111", "2222" , "3333","4444");

結束更新-

我試圖用

       const temp= web3.eth.abi.decodeLog(receipt, receipt.logs[0].data, receipt.logs[0].topics[0]);

沒有成功。

我試過

  const temp = web3.utils.toAscii(receipt.logs[0].data);
  console.log("toAscii " + temp);

但我在需要手動解析的單個字元串中沒有字母字元。

到`Il Vincitore111111222233334444

有什麼建議嗎?

您可以使用函式web3.eth.abi.decodeParameters,例如在您的情況下:

const Web3 = require('web3');
const web3 = new Web3();

const typesArray = [
   {type: 'string', name: 'messanger'}, 
   {type: 'string', name: 'username'},
   {type: 'string', name: 'nome'},
   {type: 'string', name: 'cognome'},
   {type: 'string', name: 'email'}
];

const data = '0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000c496c2056696e6369746f72650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b3535353535353535353536000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000336363600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013600000000000000000000000000000000000000000000000000000000000000';

const decodedParameters = web3.eth.abi.decodeParameters(typesArray, data);

console.log(JSON.stringify(decodedParameters, null, 4));

這使:

{
   "0": "Il Vincitore",
   "1": "55555555556",
   "2": "666",
   "3": "6",
   "4": "6",
   "__length__": 5,
   "messanger": "Il Vincitore",
   "username": "55555555556",
   "nome": "666",
   "cognome": "6",
   "email": "6"
}

這意味著您可以像這樣讀取參數:

const messanger = decodedParameters.messanger;
const username  = decodedParameters.username ;
const nome      = decodedParameters.nome     ;
const cognome   = decodedParameters.cognome  ;
const email     = decodedParameters.email    ;

或者像這樣:

const messanger = decodedParameters[0];
const username  = decodedParameters[1];
const nome      = decodedParameters[2];
const cognome   = decodedParameters[3];
const email     = decodedParameters[4];

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