0

I'm trying to analyze the transaction of this contract, but sometimes when I try to convert from wei to ether value of tokens I got comma in different position than expected. This is how I proceed:

1)Fetch all transactions from Moralis API.
2)Take the ABI of the contract, and from the tx_input I decode the transaction in this way:

contract = web3.eth.contract(address=Web3.toChecksumAddress(tx["to_address"]), abi=abi['result'])
func_obj, func_params = contract.decode_function_input(tx["input"])
  1. I get the wei value of token sent to the contract selecting the func_params with the following path func_params['amountsInOutMarketMaxFee'][0] in the case of Swap Function invoked.

Everything is fine unless in some case when I try to convert some numbers from wei to ether value I got different numbers than seen in explorers. For example when I fetch the following transaction from the contract above: https://polygonscan.com/tx/0xa2d59655b42f947dc619f456553476f7b5929ef1ec6da229409dde9d6fb9629d I got the following value:

13045952229932174800

That's the same amount you get from the decoded input in the blockchain explorer page. But if I try to convert the amount with the following function:

 Web3.fromWei(tx_data['amountsInOutMaxFee'][0], 'ether')

I got 13.04 while on the blockchain explorer the amount is 1.304 Why is there this lack? Thank you for the answers.

1 Answer 1

0

It seems there was a calculation error. The number that appears in the event logs is 121ada717a2ea7cd that correspond to 1304595222993217480.

And the value in the question is 13045952229932174800. It has an additional zero at the end. That number correspond to b50c886ec5d28dd0 in hexadecimal, but there's nothing like it in the transaction logs.

5
  • you're right. What could I change of my procedure in order to get the right numbers? Commented Jun 21, 2022 at 8:00
  • @giacomomaraglino How did you get the number 13045952229932174800? It is no clear from the question. If you used a script, you could share to see if it has a bug.
    – Ismael
    Commented Jun 21, 2022 at 13:44
  • Sorry. I get from Moralis APIs a JSON like this for each transaction: pastebin.com/0m8fvQW4. Then I take the "input" field and I decode the transaction input as described in the original post. From the func_obj dictonary I take the value of 'amountsInOutMaxFee' Commented Jun 21, 2022 at 14:11
  • @giacomomaraglino Either Moralis is providing erroneous data, or there's a bug in the decoding. You haven't provided a runnable script so we can't test where is the error.
    – Ismael
    Commented Jun 21, 2022 at 14:45
  • Here you can find a runnable code: pastebin.com/agaB5hR2 . I don't think it's a Moralis Bug because if you see from the explorer the decoded data you will get the same amount. Hope you can help me finding a solution. thanks. Commented Jun 21, 2022 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.