Skip to content
This repository was archived by the owner on Oct 19, 2024. It is now read-only.
This repository was archived by the owner on Oct 19, 2024. It is now read-only.

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

@s3cunDa

Description

@s3cunDa

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions