Wednesday, 21 June 2023

web3.js ETH events log - How to convert javascript big number into WEI

I have a js script that used to work fine with getting events in the proper format -

receipt.events.MyEvent.returnValues.marketShare

but reading events that way seems to no longer work. So I edited my script to read the logs and use web3.eth.abi.decodeLog.

var eventAbi = [
            {
                "indexed": false,
                "internalType": "uint256",
                "name": "earnings",
                "type": "uint256"
            },
            {
                "indexed": false,
                "internalType": "uint256",
                "name": "marketShare",
                "type": "uint256"
            },
    ];

var myEvent = web3.eth.abi.decodeLog(eventAbi, receipt.logs[1].data, receipt.logs[1].topics);   

This seems to return fine, however my numbers are returning as such -

earnings: 32n
marketShare: 47691443057146912922899395050909650362856399929838832537703884369061582418996n

I need it to return the WEI or ETH value (at this point I'll take either or and convert later). So I've tried -

    console.log(myEvent.marketShare.toLocaleString());

    console.log(web3.utils.fromWei(myEvent.marketShare, "ether"));

    console.log(myEvent.marketShare.toNumber());

    console.log(myEvent.marketShare.toFixed());

    console.log(web3.utils.toBN(myEvent.marketShare));

I'm not having any luck getting the correct values, any suggestions?



from web3.js ETH events log - How to convert javascript big number into WEI

No comments:

Post a Comment