Smart Contract Interfaces
Smart Contract Interfaces#
Now that we have our Truffle tools installed and the folders ready for smart contract development on a local machine, let's dive into the first part of creating an ERC-20 token - the interface smart contract.
Solidity contracts are like object-oriented classes. They have variables and methods that can persist and manipulate data. An interface can be thought of as a contract that does not implement the method. They are used as base contracts to identify how the contracts that inherit from them should behave.
Some general rules about interfaces:
Methods cannot be implemented
Structs, enums, variables, and constructors cannot be defined
They cannot inherit other interfaces / contracts
We won't be working with structs or enums for the contracts we are covering in this book, but if you are interested in learning more check out this link: Source. We will dive into variables and constructors later in this chapter.
Since we are starting with an ERC-20 token interface, let's review the Ethereum Implementation Proposal (EIP) that will guide our code writing for this chapter. The EIP can be found here.
The key features in an ERC-20 token are:
version of Solidity must be v0.4.17 or above
To start writing the interface, create a folder in the
contracts folder called
interfaces, and within that create a file called
I at the beginning of the name identifies to future developers that the contract inside is an interface.
Now that we have the interface file created, open the file and start off by specifying the version of Solidity that we will be using.
Next, you can create the interface with the following code:
The interface is very simple. Based on what was identified in the EIP standard above, we only have to specify the methods
allowance, and the two events -
Once these are all in place, will specify the method inputs and outputs.
Methods for the Interface Contract#
Let's start with the methods. First is the totalSupply method, which will return the amount of tokens in existence. This method can be created within the brackets of
IERC20 like this: