Interfaces

  • Used to create the ABI of a referenced contract without having to actually include all the functions

    • So you say "This contract that I'm referencing has a fund() function"

    • This means it can be called, but you don't actually need to know what happens inside the function, you just need to know the name and inputs

CAN NOT:

  • have any functions implemented

  • inherit from other contracts, but they can inherit from other interfaces

  • declare a constructor

  • declare state variables

  • declare modifiers

All declared functions must be external

Using an Interface

interface NumberInterface {
  function getNum(address _myAddress) external view returns (uint);
}

contract MyContract {
  address NumberInterfaceAddress = 0xab38... 
  NumberInterface numberContract = NumberInterface(NumberInterfaceAddress);

  function someFunction() public {
    uint num = numberContract.getNum(msg.sender);
  }
}

In this way, your contract can interact with any other contract on the Ethereum blockchain, as long they expose those functions as public or external.

Example

Last updated