PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 25, 2022

[FIXED] How to decode the indexed string param in an event using web3.js?

 September 25, 2022     blockchain, events, javascript, solidity, web3js     No comments   

Issue

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.


Solution

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


Answered By - Tahlil
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing