What Are Interface contracts Really?

So i remember when taking this course a little under a year ago i struggled to get the concept of “Interfaces”. I was only doing something recenty where i was only able to achieve what i wanted through the use of interfaces and i thought id write a post to explain them to anyone who sees this and still isnt sure.

An interface is basically contract of some other contract implementation that only has the function headers in the contract it doesnt have any of the function bodies implemented. No whay is this and what the hell would we want to use an interface contract for.

Well lets imagine we have a contract which is deployed on mainnet and it has 100s or thousands of lines of code. It is a lot of effor to copy this file into ur project just so we can import it to access its functions. Also it will most likely be the case that we only want to access one or two functions of the contract. This is where interfaces come in.

If we know the address of the contract and the names of the functions we want to implement in our contract then we can declare an interface which allows us to access those functions without any mess in a much more concise way. Consider the example below

pragma solidity 0.8.0;

contract Test {

  function addNums(uint num1, num2) public returns (uint) {

    uint result = num1 + num2;

    return result;
  }

  function subtractNums(uint num1, num2) public returns (uint) {

    uint result = num1 - num2;

    return result;
  }
}

contract CallTest {


  function useAddSum(uint num1, uint Num2) {

    Test(addressOfTestContract).addNums(num1, num2)
  }

  function useSubtractSum(uint num1, uint Num2) {

    Test(addressOfTestContract).subtractNums(num1, num2)
  }
}

In this example the contract whos functions we want to access is the Test contract. And we do so by calling them in our contract that we are writing, namely the callTest contract. In order to do this we need to specify the contract name and thhen call the function we want. This might not seem so bad here because the Test contract is very small. But imagine this contract was thousands of lines of code and we only wanted to call one or two functions. Well copying the entire contract into ours would wake things very cluttered and uneadable.

This is where we use the interface. Using the interface we can access the same contracts functins but we dont need to copy over the whole thing, we only need to use the declare the headers of the funtions we want to intercat with. Look how the example changes when we use an inerface suprisingly its not much

pragma solidity 0.8.0;

contract ITest {

  function addNums(uint num1, num2) external returns (uint);
  function subtractNums(uint num1, num2) external returns (uint);
}

contract CallTest {


  function useAddSum(uint num1, uint Num2) {

    ITest(addressOfTestContract).addNums(num1, num2)
  }

  function useSubtractSum(uint num1, uint Num2) {

    ITest(addressOfTestContract).subtractNums(num1, num2)
  }
}

even in this example you can see that we save so much space by using the interface as we dont need to declare the function bodies and we can still access the functions by interfaceing into the contract with ITest(addressOfTest).functionName(arg1, arg2)

This becomes really useful when the contract your interfacing into has dozens of functions and you only need access to one or two. This way you can just write an interface and include the headers of the two functions you need and leave everything else out.

So thats interfaces in a nutshell I hope anyone who reads this finds it useful. Any questions let me know

Note that when writing an interface you should always have the functions visibilities as external. This is because we are always calling the functions in an interface contract from another contract outside and enver from withtin the interface itself. This is just something to keep in mind. Its not too imprortant to remeber as soldidty will throw an error but its good to know none the less

2 Likes

So question , for a flash loan arbitrage , the address of the exchange is an interface (meaning a contract thats a short hand for the entire exchange )? I’m watching the flash loan arbitrage in defi 201 and they completely skipped over how to find or get the addresses for the exchange .

I understand finding the token address.
i understand getting the lending pool address.
I understand the functions needed for the exchange.

Where i’m lost is say token ‘a’ is at 5 dollars on coinbase , and 7 dollars on kukoin . I can swap the flash loan ( Dai for token ‘a’ ) But i don’t know how to get the interface(addresses) for coinbase or kukoin.

Please help