1

I am writing a smart contract as follow:

contract MyContract {

   function doSomething(address newAddress, interface newInterface) {
       _newAddress = newInterface(newAddress);
       _newAddress.doSomething();
    }

}

I could define my interface beforehand. But I would like to pass every-time a new one because my newInterface could CHANGE overtime.

So I am looking a bit at the different alternatives, any idea? Thanks

1 Answer 1

3

interface is reserved for defining a set of functions on a predefined contract. You could add interface function you intend to add into the external contract in the future, but this is not recommended as you likely will change what you want later.

I suggest looking into external calls directly, here's one example of how this can be done:

bytes memory payload = abi.encodeWithSignature("mintFromGamify(uint256,address)", _amount, _to);
(bool success, bytes memory result) = governanceToken.call(payload);

To extend you contract to add functionality to calling external contracts your would do something similar to the following (KEEP this data private and ownerOnly permissions):

Create a variable for the byte code: mapping(uint256 -> bytes memory) byteCode;

Generate the byte code payload, which could be generated offline via the function listed above (this will have to be coded for each function but does not require the function to be hard-coded). Then add the external call data via:

function addByteCode(bytes memory input, uint256 index) public {
    require(byteCode[index] != 0, "memory not avalible here");
    byteCode[index] = input;
}

Now that you have a collection byte codes in memory just call the appropriate index:

function externalCaller(address targetContract, uint256 index) public 
    returns (bool){
    (bool success, bytes memory result) = targetContract.call(byteCode[index]);
} 
7
  • Indeed it does solve my first issue, but my second is that what happens if my function, in your example "mintFromGamify(uint256,address)", does not take 2 arguments but instead only one? How can I manage it with the above example @FudgyDRS? And thanks for your help Commented Feb 7, 2022 at 21:22
  • Or even if at some point I do not have any params to pass anymore? I really do not want to be stuck at some point with some hedge cases Commented Feb 7, 2022 at 21:24
  • or even if I at some point I do not have arguments to pass anymore? thanks Commented Feb 7, 2022 at 21:25
  • So if you wanted less or more variables you just add or remove those elements. ("myFunction(uint256,address)", arg1, arg2) becomes ("myFunction(address)", arg1) or ("myFunction(bool,bool,bool)",arg1 arg2,arg3)
    – FudgyDRS
    Commented Feb 7, 2022 at 23:10
  • Make sure the types you are calling the external contract exactly the same and your arguments match the types of the input in the same order. As you can see the typing can change to match the functions you're call. In fact you can call functions that don't take input via ("myFunction()")
    – FudgyDRS
    Commented Feb 7, 2022 at 23:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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