Solidity – Converting Bytes Memory to Bytes Calldata

bytessolidity

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 😀

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).

Best Answer

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);
    }
}