eth-brownie / brownie

A Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine.
https://eth-brownie.readthedocs.io
MIT License
2.64k stars 550 forks source link

to_bytes not working as expected #1007

Open poolpitako opened 3 years ago

poolpitako commented 3 years ago

Environment information

What was wrong?

I am working with an SNX contract which interface looks like this:

function getAddress(bytes32 name) external view returns (address);

From solidity I can do the following:

    function feePool() public view returns (IFeePool) {
        return IFeePool(IAddressResolver(resolver).getAddress("FeePool"));
    }

In a brownie console I can run strategy.feePool() and get back 0x778D2d3E3515e42573EB1e6a8d8915D4a22D9d54.

My issue is if I want to call the method from a console, I can't convert the bytes32 correctly. The address resolver is 0x823bE81bbF96BEc0e25CA13170F5AaCb5B79ba83.

So far I got it working by doing:

from eth_abi import encode_single
param = encode_single('bytes32', b'FeePool')
resolver.getAddress(param)

param is

b'FeePool\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

If I try to do it a la brownie, I don't get the proper address

from brownie import convert
param = convert.to_bytes(b'FeePool')
resolver.getAddress(param)

param is:

b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00FeePool'

wdyt?

banteg commented 3 years ago

This bugged me too when working with Maker, I did a quick patch which works like this:

>>> Contract('0x35D1b3F3D7966A1DFe207aa4514C12a259A0492B').ilks('YFI-A').dict()
{
    'Art': 27002196598742850363955981,
    'dust': 2000000000000000000000000000000000000000000000000,
    'line': 32584730719588744119147504654936718496251066860207916,
    'rate': 1021573582679307467037884636,
    'spot': 20994362788580571428571428571428
}
>>> Contract('0x823bE81bbF96BEc0e25CA13170F5AaCb5B79ba83').getAddress('FeePool')
'0x778D2d3E3515e42573EB1e6a8d8915D4a22D9d54'

Need to check it doesn't break anything.

Update: It does break everything, this needs more work.