1

I am trying to test out a function similar to the one shown below:

function add (Complex calldata x, Complex calldata y) pure returns (Complex memory result) {
    result = Complex(x.a.add(y.a), x.b.add(y.b));
}

If the storage type of the parameters is memory, I have no issues. However, if the storage type is calldata, I cannot declare this type of parameters for the function, because I get this:

Type struct Complex memory is not implicitly convertible to expected type struct Complex calldata

The testing function is shown below:

function testAdd(int256 a1, int256 b1, int256 a2, int256 b2) external {
    Complex memory x = Complex(SInteger.wrap(a1), SInteger.wrap(b1));
    Complex memory y = Complex(SInteger.wrap(a2), SInteger.wrap(b2));

    ...

    // The two statements below produce: Type struct Complex memory is not implicitly
    // convertible to expected type struct Complex calldata   
     
    Complex calldata actual = x.add(y); 
    Complex calldata expected = Complex(SInteger.wrap(a1).add(SInteger.wrap(a2)), SInteger.wrap(b1).add(SInteger.wrap(b2)));

    assertEq(SInteger.unwrap(actual.a), SInteger.unwrap(expected.a));
    assertEq(SInteger.unwrap(actual.b), SInteger.unwrap(expected.b));
}

If I do instead declare x and y using memory, then the following is shown:

Member “add” not found or not visible after argument-dependent lookup in struct Complex memory.

1 Answer 1

0

Calldata is the cheapest type of storage, is read-only and can only be used for data passed into a function through the transaction input. So, it is impossible for you to modify Complex calldata actual or Complex calldata expected.

The solution is pretty simple, declare Calldata parameters as memory only for preparing the input params for the function to be tested and then pass it to the function, you do not need to convert it to calldata (not that it can be in the first place).

If you're calling the contract through a transaction, this will simply copy the calldata to the memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.