5

I am getting the following error:

Error: Type bytes memory is not implicitly convertible to expected type bytes calldata.

So I was wondering if it was possible to convert from bytes memory to bytes calldata, as an external function I'm using requires bytes calldata but I can only decode it into bytes memory.

Thanks in advance :D

EDIT: I have made a contract which represents my issue as the actual one is quite long. The error occurs on line 9 with the error TypeError: Type bytes memory is not implicitly convertible to expected type bytes calldata..

I tried putting bytes calldata instead of bytes as the type in the abi decode part was I ended up with the error ParserError: Expected ',' but got 'calldata'.

If I replace calldata with memory when specifying the types of the variables (uint a, bytes memory b, bytes memory c), I get TypeError: Invalid type for argument in function call. Invalid implicit conversion from bytes memory to bytes calldata requested. on line 11.

At this point, I don't know what to do and I've looked through many websites to try to find the answer. Here's an example contract which is similar to the part of my contract that contains the errors.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Foo {    
    // I can't change the parameters of this function but I can change the content
    function bar (bytes calldata data) public {
        (uint a, bytes calldata b, bytes calldata c) = abi.decode(data, (uint, bytes, bytes));
        
        qux(c);
    }
    
    // I can't change this function at all
    function qux (bytes calldata data) public {
        // Do something here
    }
    
    // I can change this function, the parameters are just an example
    function baz (uint _a, bytes calldata _b, bytes calldata _c) public {
        uint a = _a;
        bytes calldata b = _b;
        bytes calldata c = _c;
        
        bytes memory data = abi.encode(
            a,
            b,
            c
        );

        bar(data);
    }
}

Baz is the function that I call externally (aka the starting point).

2
  • You should write the program that give this error. Jul 14, 2021 at 6:20
  • @AlirezaZojaji Done :D
    – Thierry P
    Jul 14, 2021 at 17:12

2 Answers 2

7

The answer was given to me on reddit so here is the fixed contract for anyone who may need it :D :

// SPDX-License-Identifier: MIT pragma solidity 0.8.0;

contract Foo {
    // I can't change the parameters of this function but I can change the content
    function bar (bytes calldata data) public {
        (uint a, bytes memory b, bytes memory c) = abi.decode(data, (uint, bytes, bytes));
        this.qux(c);
    }
    
    // I can't change this function at all
    function qux (bytes calldata data) public {
        // Do something here
    }
    
    // I can change this function, the parameters are just an example
    function baz (uint _a, bytes calldata _b, bytes calldata _c) public {
        uint a = _a;
        bytes calldata b = _b;
        bytes calldata c = _c;
        
        bytes memory data = abi.encode(
            a,
            b,
            c
        );
    
        this.bar(data);
    }
}
2
  • You saved my time! Nov 21, 2021 at 19:52
  • 3
    For those who didn't get it right away, calling the function on the 'this' object allows us to pass memory params to a function that accepts calldata params, essentially "converting" memory --> storage. Apr 19, 2022 at 9:31
1

Change all calldata statements to memory.

Solidity language ordinary uses calldata statement in interfaces for the functions input parameters instead of memory.

3
  • 1
    But I said that I can't change the parameters of 2 of the functions which require calldata...
    – Thierry P
    Jul 15, 2021 at 16:55
  • You must change ALL calldatas to memory. In this case no error is occured. Jul 17, 2021 at 8:16
  • 1
    But I can't change ALL of the calldatas to memory. I can't change one function at all that requires calldata and I can't change the parameters of another that requires calldata so how am I going to change ALL calldatas to memory?
    – Thierry P
    Jul 20, 2021 at 16:46

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.