Home > Net >  how to test a function call with dynamic calldata array?
how to test a function call with dynamic calldata array?

Time:07-23

I have a contract function like the following

function updateDeps(uint16 parent, uint16[] calldata deps) public

And I am trying to create unit tests in remix with the following

contract TestContract is MyContract {
    uint16[] deps;
    function testUpdateDeps() {
        deps.push(uint16(2));
        deps.push(uint16(3));
        deps.push(uint16(4));

        (bool success, bytes memory result) = address(this).delegatecall(abi.encodeWithSignature("updateDeps(uint16, uint16[])", 1, deps));
        Assert.ok(success, "should succeed");
    }
}

I wrote the test like this because of the difference between fixed size array and dynamic array and that calldata cannot be a contract variable and can only be created in function call, thus the delegatecall. However, the transaction failed.

How should I test function that takes a calldata dynamic array as param?

CodePudding user response:

It is with the abi encoding string, there cannot be space between the params in the string, i.e. updateDeps(uint16,uint16[]) works but not updateDeps(uint16, uint16[])

P.S. an additional fyi, if you are using uint in your code, which is alias for uint256, in the abi signature string you have to specify uint256 but not uint.

  • Related