Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbiEncode::encode(complex_solidity_datastruct) return wrong data #2752

Open
s3cunDa opened this issue Mar 2, 2024 · 0 comments
Open

AbiEncode::encode(complex_solidity_datastruct) return wrong data #2752

s3cunDa opened this issue Mar 2, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@s3cunDa
Copy link

s3cunDa commented Mar 2, 2024

Version
ethers v2.0.11

Platform
MacOs

Description
When meet complex struct, like:

struct A {
    address _a;
    uint8 _b;
    uint256 _c;
    bytes _d;
}
struct B {
    A[] AList;
    bytes _e;
}

When using solidity's abi.encode(), it will add a "offset prefix" to indicate the data offset in bytes, example code here:

pragma solidity ^ 0.8.0;

struct A {
    address _a;
    uint8 _b;
    uint256 _c;
    bytes _d;
}
struct B {
    A[] AList;
    bytes _e;
}
contract test{
    function testEncode() public returns (bytes memory result){
        A[] memory AList = new A[](5);
        AList[0] = A({
            _a: address(0),
            _b: 0,
            _c: 0,
            _d: hex"4141414141414141"
        });
        AList[1] = A({
            _a: address(1),
            _b: 0,
            _c: 0,
            _d: hex"4242424242424242"
        });
        AList[2] = A({
            _a: address(2),
            _b: 0,
            _c: 0,
            _d: hex"4343434343434343"
        });
        AList[3] = A({
            _a: address(3),
            _b: 0,
            _c: 0,
            _d: hex"4444444444444444"
        });
        AList[4] = A({
            _a: address(4),
            _b: 0,
            _c: 0,
            _d: hex"4545454545454545"
        });

        B memory BInstance = B({
            AList: AList,
            _e: hex"4646464646464646"
        });
        return abi.encode(BInstance);
    }
}

The corresponding(correct) result is:

0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000004c0000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002e000000000000000000000000000000000000000000000000000000000000003a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000009414141414141414141000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000009424242424242424242000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000009434343434343434343000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000009444444444444444444000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000009454545454545454545000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000094646464646464646460000000000000000000000000000000000000000000000

When I tried this code in rust, using the data struct from Abigen:

fn test_encode() {
    let mut a_list: Vec<A> = Vec::new();
    a_list.push(A { 
        _a: "0x0000000000000000000000000000000000000000".parse().unwrap(),
        _b: 0, 
        _c: U256::zero(), 
        _d: "4141414141414141".parse().unwrap()
    });
    a_list.push(A { 
        _a: "0x0000000000000000000000000000000000000001".parse().unwrap(),
        _b: 0, 
        _c: U256::zero(), 
        _d: "4242424242424242".parse().unwrap()
    });
    a_list.push(A { 
        _a: "0x0000000000000000000000000000000000000002".parse().unwrap(),
        _b: 0, 
        _c: U256::zero(), 
        _d: "4343434343434343".parse().unwrap()
    });
    a_list.push(A { 
        _a: "0x0000000000000000000000000000000000000003".parse().unwrap(),
        _b: 0, 
        _c: U256::zero(), 
        _d: "4444444444444444".parse().unwrap()
    });
    a_list.push(A { 
        _a: "0x0000000000000000000000000000000000000004".parse().unwrap(),
        _b: 0, 
        _c: U256::zero(), 
        _d: "4545454545454545".parse().unwrap()
    });
    let b_ins: B = B{
        a_list,
        _e: "4646464646464646".parse().unwrap()
    };
    println!("{:?}", (Bytes::from(AbiEncode::encode(b_ins.clone()))));
}

I got this result:

0x000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000004c0000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000002e000000000000000000000000000000000000000000000000000000000000003a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008414141414141414100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008424242424242424200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008434343434343434300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008444444444444444400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000008454545454545454500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000084646464646464646000000000000000000000000000000000000000000000000

The "offset prefix", in this case is uint256(0x20), is missing somehow.

@s3cunDa s3cunDa added the bug Something isn't working label Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant