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.

Rosco Kalis
  • 2,137
  • 2
  • 14
  • 26
savard
  • 448
  • 1
  • 4
  • 16

1 Answers1

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.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145