0

I understand how to call another contracts function. My question is, if I want to use an interface like in this question over call(): Call function on another contract

contract Name {
    mapping(address=>string) public text;
    string public test;

    function register(string _text) {
        text[msg.sender] = _text;
    }
}

contract Proxy {
    address watch_addr = 0xEB1e2c19bd833b7f33F9bd0325B74802DF187935;
    address user_addr = msg.sender;

    function register(string _text) {
        Name name = Name(watch_addr);
        name.register(_text);
    }
}

I know you don't need to have the function definition so having only this from Contract Name would work fine:

contract Name {
    function register(string _text) {}
}

What if contract Name had other functions defined in it too? Would the interface contract being used need to implement all the functions in contract Name, or only the function being used by the Proxy contract?

Also if the function we wanted to call from contract Name had a modifer, say onlyOwner, would the interface version need to too?

Thanks for any help.

1

What if contract Name had other functions defined in it too? Would the interface contract being used need to implement all the functions in contract Name, or only the function being used by the Proxy contract?

It's not important to define the interfaces for unneeded functions. That would just increase the size of the compiled calling contract for no reason.

Also if the function we wanted to call from contract Name had a modifier, say onlyOwner, would the interface version need to too?

Modifiers are actually an implementation detail. They are not part of the interface definition that is only concerned with the ABI definition. If you think about it, modifiers are a compile-time source code roll-up and therefore part of function definition.

For example:

modifier onlyOwner {
  require(msg.sender == owner);
  _; // <=== function implementation goes here
}

function myFunction() public onlyOwner returns(bool success) { ...

is compiled as:

function myFunction() public onlyOwner returns(bool success) {
  require(msg.sender == owner);
  ...
}

A calling contract isn't concerned with that, so no need to describe it in the interface.

Hope it helps.

  • 1
    Thanks, Rob! This is a perfect answer to my questions. – savard Feb 5 at 22:29
  • 1
    Thanks for accepting the answer. It's important the site has lots of votes and accepts to maintain good standing with StackOverflow. – Rob Hitchens - B9lab Feb 5 at 22:35

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.