I believe I got the main issue that was confusing to me.
So if you come from OOP background this what we know about interfaces:
interface IERC20 {
function totalSupply() external view returns (uint256);
}
contract XYZ is IERC20 {
// then implement totalSupply here
function totalSupply() external view returns (uint256) {
// implementiation goes here.
address public add='0x123...4'
}
So at this point you can call XYZ's totalSupply()
and you should be fine.
However, there is another way of using interfaces in Solidity. I will take this code from compound protocol as an example (https://github.com/compound-developers/compound-supply-examples)
If you see MyContracts.sol
, it has the following interface:
interface CEth {
function mint() external payable;
function exchangeRateCurrent() external returns (uint256);
function supplyRatePerBlock() external returns (uint256);
function redeem(uint) external returns (uint);
function redeemUnderlying(uint) external returns (uint);
}
However, there is no place in our contract that uses the keyword IS and implements any of the methods. So you might ask how is our interface being used ?
Now let's go back to MyContract
contract in MyContracts.sol
file and see this code under supplyEthToCompound
function:
CEth cToken = CEth(_cEtherContract);
Here we are providing CEth interface with a contract address of Compound (i.e _cEtherContract
and the contract at that address has a mint() function.)
When you call cToken.exchangeRateCurrent();
on the next line, what happens is we are basically calling a function exchangeRateCurrent on Compound Contract.
At first it seems like exchangeRateCurrent has no implementation in the file we are calling it but the implementation resides at _cEtherContract address.
I hope this clears the confusion especially if you come from traditional OOP background.
Feel free to point out anything that is misleading in my answer.