1

Example:

interface IA {
    function a1(uint256) public returns();
}
import "./IA.sol";

contract A is IA {
    function a1(uint256 _a1) public returns() {
        _a = 1;
    }

    function a2(uint256 _a2) public returns() {
        _a2 = 2;
    }
}
import "./IA.sol";

contract B {
    IA a;
    function b(address _aAddress, uint256 _a2) public returns(uint256) {
        a = IA(_aAddress);
        a.a2(_a2);
        return _a2;
    }
}

Question:

When I try to interact with a contract by using interface(contractAddress), can I call one function that not declared in the interface but implemented in the contract?

For example, Can I call the a2() of contract A in the contract B in this way?

1

You have at least two options:

  1. Write your own interface which inherits from the IA and adds the missing functionality. This is by far the nicest approach

  2. Use something like call or delegateCall to call functionality in another contract without direct reference to the function. So basically you craft the call to the other function by hand. You can find more info with examples here: https://medium.com/coinmonks/delegatecall-calling-another-contract-function-in-solidity-b579f804178c

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.