0

This is the event -

    event BridgeAdded(
        string indexed tokenTicker,
        string tokenName,
        string imageUrl
    );

I use web3.eth.abi.decodeParameter("string", topics[1]) to decode the indexed string param but get this error -

Error: overflow [ See: https://links.ethers.org/v5-errors-NUMERIC_FAULT-overflow ] (fault="overflow", operation="toNumber", value="105640063387051144792550451261497903460441457163918809975891088748950929433065", code=NUMERIC_FAULT, version=bignumber/5.6.2)
    at Logger.makeError (/home/rajat/bridge-server/node_modules/@ethersproject/logger/lib/index.js:233:21)
    at Logger.throwError (/home/rajat/bridge-server/node_modules/@ethersproject/logger/lib/index.js:242:20)
    at throwFault (/home/rajat/bridge-server/node_modules/@ethersproject/bignumber/lib/bignumber.js:303:19)
    at BigNumber.toNumber (/home/rajat/bridge-server/node_modules/@ethersproject/bignumber/lib/bignumber.js:151:13)
    at /home/rajat/bridge-server/node_modules/@ethersproject/abi/lib/coders/array.js:92:60
    at Array.forEach (<anonymous>)
    at unpack (/home/rajat/bridge-server/node_modules/@ethersproject/abi/lib/coders/array.js:88:12)
    at TupleCoder.decode (/home/rajat/bridge-server/node_modules/@ethersproject/abi/lib/coders/tuple.js:74:60)
    at AbiCoder.decode (/home/rajat/bridge-server/node_modules/@ethersproject/abi/lib/abi-coder.js:98:22)
    at ABICoder.decodeParametersWith (/home/rajat/bridge-server/node_modules/web3-eth-abi/lib/index.js:310:30)
    at ABICoder.decodeParameters (/home/rajat/bridge-server/node_modules/web3-eth-abi/lib/index.js:291:17)
    at ABICoder.decodeParameter (/home/rajat/bridge-server/node_modules/web3-eth-abi/lib/index.js:280:17)
    at populateDb (/home/rajat/bridge-server/eventListener/main.js:140:36)
    at onEvent (/home/rajat/bridge-server/eventListener/main.js:117:19)
    at handleLogs (/home/rajat/bridge-server/eventListener/main.js:90:32)
    at startListening (/home/rajat/bridge-server/eventListener/main.js:64:27) {
  reason: 'overflow',
  code: 'NUMERIC_FAULT',
  fault: 'overflow',
  operation: 'toNumber',
  value: '105640063387051144792550451261497903460441457163918809975891088748950929433065'
}

Please help me resolve this error.

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

1

You should use decodeLog instead, and you would also need The ABI byte code in the data field of logs (if you can get the topics array you should get data from logs as well):

let result = web3.eth.abi.decodeLog([{
    type: 'string',
    name: 'tokenTicker',
    indexed: true
    }],
    data,
    topics[1]);

But you would get the hash of the value of tokenTicker because it is indexed. This is because. If the index parameter is a string, bytes or array than the keccak-256 hash of is the topic instead. So you can't really retrieve the indexed string value. I found this out in the following way:

I deployed a contract in Rinkeby that has a simple function that emit the BridgeAdded: enter image description here

I called the above function (Here is the TX) and then I try to decode the event with web3 js. As you would see, it would return the keccak256 hash of the indexed string:

enter image description here

Even in Etherscan you can see its not able to decode the indexed string:

enter image description here

So there might be two workaround options I can think of:

Option 1: Create an index with primitive type instead of indexing string:

event BridgeAdded(
        uint256 indexed id,
        string tokenTicker,
        string tokenName,
        string imageUrl
    );

Option 2: You can create another string that holds the exact same value as the indexed string:

event BridgeAdded(
            string indexed indexedTokenTicker,
            string tokenTicker,
            string tokenName,
            string imageUrl
        );
Tahlil
  • 1,031
  • 1
  • 14
  • 26
  • I did this but the decoded form of the `string indexed` param is still in hex format. It looks like this - `tokenTicker: '0x56a4db8db8d4f3dc22c02e5cd25b4bb01274bb7d3f0a5695df5ab5a544f3c0fe'` Is there any way I can convert this to readable string format? – Rajat Saxena Aug 05 '22 at 18:28
  • Okay so I just found out "If the index parameter is a string, bytes or array than the keccak-256 hash of is the topic instead." So if you index with a string, array or bytes you can never retrieve that particular event...I have shown the detailed explanation in the answer. I had also suggested two workarounds at the end. – Tahlil Aug 06 '22 at 07:40
  • @Tahlil could you please give us a reference why is it impossible to retrive that event – Dante Aug 07 '22 at 17:46