Transactions – How to Decode Log Event of a Transaction Log

decodingeventslogsreceiptstransactions

I receive a transaction event in my log of my Ethereum transaction.

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

My transaction receipt object is :

    {
    "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
    }

receipt.logs[0].data is

0x00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000c496c2056696e6369746f72650000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b3535353535353535353536000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000336363600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013600000000000000000000000000000000000000000000000000000000000000

UPDATE
In my smart contract the Event is

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

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

END UPDATE

I tried to decode this hexadecimal code with

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

without success.

I tried

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

but I get no alphabetic character in a single string that need manual parsing.

à ` Il Vincitore111111222233334444

any suggestion?

Best Answer

You can use function web3.eth.abi.decodeParameters, for example in your case:

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));

Which gives:

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

Which means that you can read your parameters either like this:

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

Or like this:

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