3

Contact A:

function redeemAssetsForBuyback(address[] calldata tokens) external {
  // ...
}

I would like to call redeemAssetsForBuyback from Contract B:

function initiateRedeem() external {
  // ...
  // I have address[] memory payload ready to be sent
  address[] memory myPayload;

// Can I transform myPayload to address[] calldata? IContractA(contractAAdrress).redeemAssetsForBuyback(myPayload); // Parameter mismatchs }

I can change the redeemAssetsForBuyback function's parameter to be memory instead of calldata and my issue resolves on JS VM at Remix but I read that I should be using calldata here.

So, can I somehow create a address[] calldata on ContractB and call ContractA's function with it? If not, is it okay to use redeemAssetsForBuyback(address[] memory tokens)?

CeamKrier
  • 131
  • 5

1 Answers1

0

This is one way to do so:

pragma solidity ^0.8.0;

contract A { event Received(bytes);

function redeemAssetsForBuyback(address[] calldata) external{
  emit Received(msg.data);
}      

}

contract B{ function initiateRedeem(address _contractA, address[] calldata address_array) external {

    (bool success, ) = _contractA.call(
        abi.encodeWithSignature("redeemAssetsForBuyback(address[])", address_array)
    );

    require(success);
}

}

If you deploy ContractA and pass the address of ContractA to initiateRedeem() in ContractB along with your array of address, it will make an call to redeemAssetsForBuyback function in contractA.

Emrah
  • 1,654
  • 2
  • 9
  • 24