Ethers.js: How to decode log data

Created on 11 Feb 2019  路  3Comments  路  Source: ethers-io/ethers.js

I fetched logs via provider.getLogs. Now I try to decode the data with this:

ethers.utils.defaultAbiCoder.decode(
    [ 'string','string','bytes32','string','address' ],
    connection.data
 )

But I get

 Error: overflow (operation="setValue", fault="overflow", details="Number can only safely store up to 53 bits", version=4.0.23)

The data comes from this event:

event newConnect (
    string indexed hashedName,
    string name,
    bytes32 connectId,
    string encrypted,
    address owner
);
discussion

Most helpful comment

Keep in mind that that indexed parameters are not included in the connection.data, but in the connection.topics. You probably want something like:

ethers.utils.defaultAbiCoder.decode(
    [ 'string', 'bytes32', 'string', 'address' ],
    connection.data
 );

without the first 'string'. You can also use the Interface API to help:

let abi = [
    "event newConnect (string indexed hashedName, string name, bytes32 connectId, string encrypted, address owner)"
];

let iface = new ethers.utils.Interface(abi)

getLogs.then((logs) => {
    logs.forEach((log) => {
        console.log(iface.parseLog(log));
    });
});

That is just typed off the top of my head, so there may be typos, but that should get you started. :)

All 3 comments

Keep in mind that that indexed parameters are not included in the connection.data, but in the connection.topics. You probably want something like:

ethers.utils.defaultAbiCoder.decode(
    [ 'string', 'bytes32', 'string', 'address' ],
    connection.data
 );

without the first 'string'. You can also use the Interface API to help:

let abi = [
    "event newConnect (string indexed hashedName, string name, bytes32 connectId, string encrypted, address owner)"
];

let iface = new ethers.utils.Interface(abi)

getLogs.then((logs) => {
    logs.forEach((log) => {
        console.log(iface.parseLog(log));
    });
});

That is just typed off the top of my head, so there may be typos, but that should get you started. :)

No typos at all and both solutions work like a charm. Thank you very much!

I have spent the last two days troubleshooting, this thread has given me the solution. I am using etherjs, I wish they had documented well that indexed parameters are not included in the connection.data, but in the connection.topics.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Neozaru picture Neozaru  路  3Comments

jochenonline picture jochenonline  路  3Comments

thegostep picture thegostep  路  3Comments

ricmoo picture ricmoo  路  3Comments

moshebeeri picture moshebeeri  路  3Comments