I know how to instantiate a memory array. Looks like this:
address[] memory users = new address[](1);
users[0] = address(0x0);
But I cannot create a calldata array in this same fashion. Any pointers?
An easy solution for inheriting from IFlashLoanReceiver is to create an internal function that is callable from the executeOperation
.
// SPDX-License-Identifier: ISC
pragma solidity 0.6.12;
// Interface cannot be modified
interface IFlashLoadReceiver {
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
) external returns (bool);
}
contract Demo is IFlashLoadReceiver {
// Override inherited function
function executeOperation(
address[] calldata assets,
uint256[] calldata amounts,
uint256[] calldata premiums,
address initiator,
bytes calldata params
) override external returns (bool) {
// Call real implementation
// Compiler takes care of calldata to memory convertion
return doExecuteOperation(assets, amounts, premiums, initiator, params);
}
// Real implementation callable by other internal functions
function doExecuteOperation(
address[] memory assets,
uint256[] memory amounts,
uint256[] memory premiums,
address initiator,
bytes memory params
) internal returns (bool) {
// Do something with parameters
return false;
}
}
Another solution is to be aware that calldata
and memory
are not part of the function signature. So from the outside function foo(bytes calldata x) external
has the same signature as function foo(bytes memory x) public
so you can make the function public instead of external and replace calldata
by memory
where appropriate and it should work (tm).
contract Demo {
function executeOperation(
address[] memory assets,
uint256[] memory amounts,
uint256[] memory premiums,
address initiator,
bytes memory params
) public returns (bool) {
// do something
return false;
}
}
function foo(bytes calldata a) external { /* use a here */ }
. In some cases usingmemory
instead ofcalldata
will work. You can't create acalldata
variable.uint256[] calldata data = existing_data; some_function(data);
.