ethereum / eth-abi

Ethereum ABI utilities for python
MIT License
247 stars 269 forks source link

Wrong encoding output when using uint256[] #197

Closed dynalz closed 1 year ago

dynalz commented 1 year ago

If this is a bug report, please fill in the following sections. If this is a feature request, delete and describe what you would like with examples.

What was wrong?

Issues encoding uint256[] property. Not sure what's wrong. I am currently on version 2.1.1 and this issue happens, updated to latest version to see if issue is fixed but still persists

Code that produced the error

from eth_abi import encode
args_types=["uint256[]","bool"]
args_list=[[0, 0, 1000000000000000, 0], False]
print(encode(args_types, args_list).hex())

Full error output (Not error, just wrong output)

0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000038d7ea4c680000000000000000000000000000000000000000000000000000000000000000000

Expected Result

This section may be deleted if the expectation is "don't crash".

0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000038d7ea4c680000000000000000000000000000000000000000000000000000000000000000000

Where I found the issue: 1) https://scope.klaytn.com/account/0x5b4ed8321ea13047195104037798f29257eac28c?tabId=contractCode 2) Click "Change ABI" tab 3) Paste the following abi:

[{"name":"calculateTokenAmount", "inputs":[{"type":"uint256[]", "name":""}, {"type":"bool", "name":""}], "outputs": [{"type":"uint256", "name":""}], "stateMutability":"view","type":"function","gas":2220}]

4) Click "Read Contract" tab 5) Click "calculateTokenAmount" function 6) Paste [0, 0, 1000000000000000, 0] and false to params 7) You can see the query works and returns a valid value, while using eth_abi lib fails (RPC returns execution reverted) due to the enconding result above not matching the expected encoded value.

fselmo commented 1 year ago

Hey @dynalz, I'm not seeing the same result. What eth-abi's encode returns is the same as Solidity. Please see this contract example:

contract Contract {
    uint256[] list_example = [0, 0, 1000000000000000, 0];

    function encodeExample() public view returns (bytes memory) {
        return abi.encode(list_example, false);
    }
}

If you deploy this contract in, say, remix, with the latest Solidity version, and run the encodeExample() method... you will get the same output as eth-abi.

I did not test out your steps to reproduce with scope.klaytn.com (not familiar with this), but since the Solidity matches it seems safe to say this isn't a bug with eth-abi. I'm going to close this but feel free to re-open if there's a misjudgement on my part.