0

I'm trying to create a contract (solidity version 0.7.6) that interacts with some arbitrary contract of a lower version (0.4.21). However, I have been trying to declare an interface in (0.7.6) but it does not seem to be compatible. I am trying to use the interface defined on (0.7.6) to interact with and call functions from the (0.4.21). Assume that the (0.4.21) contract is already deployed. What is the best way of doing this?

Thanks very much for your help and expertise.

4
  • Use Hardhat's multi-compiler feature. Sep 25, 2021 at 21:40
  • @PaulRazvanBerg no, that's not the issue here.
    – antioi
    Sep 26, 2021 at 11:17
  • Oh, I see. Then you have to use the >= prefix instead of ^. But note it is not always possible to do that, you might have to rewrite that contract to adhere to the latest version of Solidity. Sep 26, 2021 at 14:23
  • Can you show the interface for the 0.4 contract? It should be possible to update the interface to a 0.7 version backward compatible with 0.4.
    – Ismael
    Sep 28, 2021 at 4:38

1 Answer 1

0

Here is an example;

function invoke(address _target, bytes memory _data) internal returns (bytes memory) {
            // External contracts can be compiled with different Solidity versions
            // which can cause "revert without reason" when called through,
            // for example, a standard IERC20 ABI compiled on the latest version.
            // This low-level call avoids that issue.

            bool success;
            bytes memory _res;
            // solhint-disable-next-line avoid-low-level-calls
            (success, _res) = _target.call(_data);
            if (!success && _res.length > 0) {
                // solhint-disable-next-line no-inline-assembly
                assembly {
                    returndatacopy(0, 0, returndatasize())
                    revert(0, returndatasize())
                }
            } else if (!success) {
                revert("VM: wallet invoke reverted");
            }
            return _res;
        }

The target id the address of the contract you are calling, The data is the function call and argument encoded like this;

 bytes memory _data = abi.encodeWithSignature(
                "swap(address,address,uint256,uint256,uint256[],uint256)",
                fromToken,
                toToken,
                amount,
                minReturn,
                distribution,
                flag
            );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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