Solidity Tutorial 2 — Build your own cryptocurrency (a)
Liked the previous parts? Want to explore more? If you’re just getting started, my ebook — “Blockchain programming for absolute beginners” is a perfect start. Have a look at it’s contents here!
This is part (a) of a series of tutorials to build your own token on the Ethereum Blockchain. If you haven’t already, I highly suggest you check out the previous tutorial, which is a primer on contracts.
In this tutorial we will discuss the ERC20 standard interface for building tokens on the Ethereum Blockchain. We will also go into details about concepts such as abstract contracts, events and explain the functionality of the functions in the ERC20 standard interface.Building a token is easier than you think, and a lot of fun too! Are you ready to build your ICO yet?
The ERC20 Standard Interface
First things first, below is the code snippet for the ERC20 standard interface for building tokens on the Ethereum blockchain. Tokens that are built using this protocol are said to be ERC20 compatible. In order to build the smart contract for our token, we will have to implement all the functions defined below. These would make sure that our contract is ERC20 compatible and hence can be deployed and verified by the Ethereum Mainnet Network. Don’t worry, as we move towards the end of this tutorial, we will explain what each one does and why it’s important. But first, let’s do a primer on abstract contracts.
contract ERC20Contract { /* Functions that need to be implemented */ function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success); /* Events related to the protocol */ event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);}
Abstract Contracts
If you have a look at the code above, you would notice that there are simply function declarations, but no implementations. Such a contract is perfectly valid solidity and is known as an abstract contract. If you inherit from an abstract contract and do not implement all the functions of the contract, the derived contract is also abstract. Abstract contracts are mostly useful for defining protocols for implementing a set of functions and the concept is similar to abstract classes in other languages like C++.
We can also have interfaces in solidity, as in other languages, and they are denoted by the keyword interface.(instead of contract as in the above snippet). We will make use of an interface soon.
Explaining the ERC20 functions
function totalSupply() public constant returns (uint);
This function simply is a getter for getting the total number of tokens in circulation. It is essentially so that the token builder can know how many tokens we have of our currency in circulation.
function balanceOf(address tokenOwner) public constant returns (uint balance);
This function returns the token balance of the particular address passed to it.
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
This is a bit tricky. In our implementation, we allow certain addresses to spend tokens from one account. The amount of tokens they can spend is determined by this function, called allowance, which returns the amount of tokens the spender address can spend from the balance of the tokenOwner address.
Note how all the above three functions are marked constant. It simply means that they promise not to modify the state variables of the contract. constant is an alias to the keyword view for functions is solidity, which also means the same thing. One more important keyword is pure which promises to neither modify nor read from the state of a contract.
function transfer(address to, uint tokens) public returns (bool success);
This function transfers the passed amount of tokens from to the to address, after verifying certain conditions like enough balance etc. These are spend from the calling address, that calls this function.
function approve(address spender, uint tokens) public returns (bool success);
This function is used by an address to approve the spending of a particular amount tokens by a particular address (spender here). The calling address approves the spending of certain amount of tokens for spender address.
function transferFrom(address from, address to, uint tokens) public returns (bool success);
Similar to the transfer function, except the fact that transferFrom passes in the from address as well, which implies that the balance is not transferred from the calling address, but from the from address instead.
Explaining the ERC20 Events
These are very simple.
event Transfer(address indexed from, address indexed to, uint tokens);
The Transfer event is fired when you want to alert the blockchain or client side of your app about a transfer that has taken place. You can call this both from the transfer as well as the transferFrom functions. The only difference would be the from address, which in case of the transfer function would be the msg.sender (the calling address).
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
Called when spending of a certain amount of tokens is approved by the tokenOwner, by a certain spender address.
Note the use of the indexed keyword in the events above. These allow for the events (logged), to be searched for using the parameters as filters. More on this later.
Conclusion
There you go! This concludes our part (a) of the series of tutorials on building your own crypto currency! Check out the next tutorial here! Comment your suggestions down below and don’t forget to follow!
Stay updated
Join us on Telegram : https://t.me/joinchat/G4Ls4hLyjTLLNke_3QGygQ
Join the ebook email list :- https://goo.gl/FtRCuX
Donations
If you enjoyed this tutorial, and would like to help me continue building these in the future, feel free to drop some ETH at the address below!
ETH: 0xE11aaAB8c3a6feD2843e66FaC08C13Cda8Cb0D55