IERC20 Interface

Hi,

So I’m on Project - Building a Decentralized Exchange, Building Wallet section. So I just started.

I’m confused that Filip doesn’t explain what an Interface is. I’m also confused that his contract compiles at all. Because mine required to be abstract, otherwise I had errors that all the functions from IERC20 Interface were not implemented. (EDIT: ok Filip is not actually inheriting from the Interface, he’s just importing it. So that’s why mine errored out about missing implementations)

I did use some Interfaces in C# in Unity and I remember whenever you inherit from an Interface, you have to implement whatever is in the interface - so that error makes sense.

So few questions:

  1. Why even use Interfaces in solidity since you will have to do your own implementations anyways? Why not instead of an Interface just use a contract with virtual functions?

  2. Also why are we using this IERC20(address), is this sth that an Interface requires? That you have to pass in an address of the caller?

  3. When calling wallet functions that call the interface functions… What am I actually calling? Because interface functions are empty and we are not implementing them here. But ERC20 does implement them. So can I just call the functions straight from ERC20 instead of IERC20? It’s just I don’t understand why even bother with the Interface at all?

Hey @JJ_FX, hope you are well.

Interfaces are used to permit external contracts to be able to interact with your contract, through the functions allowed on the interface.

Now IERC20 is an standard interface for all ERC20 contracts (if contract fulfill the standard), each contract have a address in the network, so if you want to interact with let say USDT, within your contract, you will use the interface to instantiate the USDT contract in yours, that way you can use the IERC20 interface to call functions on the USDT contract from your contract.

I do not understand this question, i think i explained above the usage of an interface.

Still let me know if you have any other question :nerd_face:

Carlos Z

1 Like

Hi @thecil,

Thanks for trying to clear things up for me. I’m still very much confused :woozy_face:

Now IERC20 is an standard interface for all ERC20 contracts (if contract fulfill the standard), each contract have a address in the network, so if you want to interact with let say USDT, within your contract, you will use the interface to instantiate the USDT contract in yours, that way you can use the IERC20 interface to call functions on the USDT contract from your contract.

Yeah, that make sense. But here in our example we’re just working on a local network, without any connection to other tokens. So why even bother with Interface? We could just inherit from ERC, not IERC and get the same result.

I do not understand this question, i think i explained above the usage of an interface.

Well ok, let’s put it this way: The only thing that says I can use the Interface is the import part. Then there is nothing in the contract itself about interface. Except you can randomly call functions from that imported interface.

So this might be a dumb question. But since we are not on a network, but on a local one, how can I just grab a function from that Interface? eg. IERC20(tokenMapping[_ticker].tokenAddress)? The mapping is a local (to the contract) variable. It doesn’t exist as a global scope it could grab from?

Or does it only exist, because we can addToken()? And once it’s added it can get the address, and that’s that?

mappings are not mean to be accessible through the interface, rather than the contract itself, if you want to request data from a mapping on a contract, you need a getter function.

Common example:
a mapping for balance mapping(address => uint) balance;

the getter function will be:

function balanecOf(address _requested) public view returns(uint) {
return balance[_requested];

Carlos Z

1 Like

Dear thecil

Really I dont understand what realy interface make
I understand that it is standart but I dont understand following code concepts.
What do you think, are these true concepts for interface issue?

https://github.com/veridelisi/basic-solidity/blob/main/interface/interface_1.sol

or the following code

pragma solidity ^0.5.0;

interface Calculator {
function getResult() external view returns(uint);
}
contract Test is Calculator {
constructor() public {}
function getResult() external view returns(uint){
uint a = 1;
uint b = 2;
uint result = a + b;
return result;
}
}

1 Like

Hey @veridelisi, hope you are well.

The interface is used to let other contract or dapps to interact with your contract.

When you deploy a contract, all you will have available is the contract address, but you will not know what functions does that contract contain, which can be used to for example call the balance of an address.

In this way, you can create an interface that contain the functions that can be used on the contract to then do something else (like a dapp).

On the example you made, the interface point to the getResult which will return the calcutation made by the contract (return result)…

Hope this helps :nerd_face:

Carlos Z

1 Like