Token Contract Constructor and Mandatory Functions

Andrei Dumitrescu
A free video tutorial from Andrei Dumitrescu
DevOps Engineer and Professional Trainer
4.5 instructor rating • 7 courses • 20,991 students

Learn more from the full course

Master Ethereum & Solidity Programming:Build Real-World Apps

In-depth Ethereum & Solidity Programming. A step-by-step approach to ERC20 Token, ICO, Auction, FundRaising,Lottery,IPFS

09:28:53 of on-demand video • Updated August 2020

  • Understand the Ethereum Blockchain Platform (accounts, addresses, ether, gas, opcodes, transactions, blocks, mining, etc)
  • Have a very good understanding of Solidity Contract Programming Language (syntax and concepts like state variables, storage, functions, visibility specifiers, mappings, structs, enums, etc)
  • Understand concepts like Events, Function Modifiers, Contract Inheritance, Abstract Contracts & Interfaces, etc
  • Design, create and deploy a fully-compliant ERC20 Token and launch an Initial Coin Offering (ICO)
  • Design, develop and deploy a smart contract for a decentralized Lottery
  • Design, develop and deploy a smart contract for a decentralized Auction Platform
  • Understand good practices for developing Ethereum Smart Contracts and see real-world examples to comprehend what is the advantage of Blockchain Technologies.
  • Design, develop and deploy a smart contract for a decentralized FundRaising Platform
  • Use geth and Set Up a Private Ethereum Blockchain
  • Understand what is InterPlanetary File System (IPFS) and how it works. Installing and Running an IPFS Node on Linux or Windows
English It's time to start implementing the first 3 functions which are mandatory for a functional ERC 20 token. So let's create the 1st function. I'll copy paste the function signature from here, from the interface, to my contract and inside the function I write return supply. That's all; the total supply function returns the total number of tokens which is hold in the supply variable ; then we implement the balance of function. I'll copy the function signature from the interface and paste it in my token contract. OK this is our function. The function must return the number of Kryptos tokens owned by this address. So return balances[tokenowner] For example if I have an Ethereum address I want to see how many tokens does that address have I'll call the balance off and as argument I'll pass that Ethereum address and the last function I implement at this moment is the transfer function. Copy paste the function in my contract. This function will transfer from my account or from the account that calls the function This number of tokens to this address and will return a boolean variable of true if the transfer was successful and false otherwise. This is a very important function. In fact this is what makes the token transferable. Here we need the requirement. We must verify that the account that calls these function and sends tokens has that number of tokens in its balance. So if I want to send 100 Kryptos I must have at least 100 Kryptos in my balance So here i write require And now the balance of the account that transfers tokens so balances, in this variable all balances are saved, [ msg.sender] msg.sender being the account, in fact the address of the account, that calls this function that wants to transfer Kryptos. So this value should be greater than or equal to tokens, the number of transferred tokens, and there is a good idea to verify that tokens greater than zero I don't allow the function to transffer zero tokens. Let's take a look here on the Etherium wiki page. And here it's a very good example. Let's assume that this token contract has only 2 token holders, this address with a balance of 100, and this address with a balance of 2 hundred; then the balance is off this address will be 100 and the balance is off this address will be 200 Nothing special til now; the balance of function will return 100 respectively 200. Let's see how the transfer function works. So if the 1st address wants to transfer 10 tokens to another address then the address that possesses the tokens will execute the transfer function and the first argument will be the recipient address, the address that receives the token and then the value that will be sent ;after calling this function balances of the 1st address will be 90 balances of the 2nd address will be 210. So in effect this function substracks this unit from the balance of the 1st address and adds this unit to the balance of the 2nd address. So it's simple; let's implement this functionality. So here I write balances [ to] so the recipient += tokens So I add to its balance the number of transferred tokens; if that address didn't have any tokens there is no problem because its balance is zero. So I add this value to zero. And finally that address will have only this number of tokens and balances [ msg.sender] (so the address of the account that possesses tokens and transfers them) -= tokens If the function reaches this point I return tru; this function must also emit an event - so the transfer event - So here I'll create that event. So event transfer- with uppercase T- (I remind you that an event is written with an upper case letter.) So better I'll copy paste this line from the interface into my contract. This is the event and here I emit the event. So transfer [ msg.sender] This is from to - this is the recipient and the number of tokens -this will be a log that will be recorded on the block chain when somebody transfers Kryptos tokens and here we have to write emit ; the transfer function is ready. We must also create or implement the contract constructor. In the constructor I initialize the founder with msg.sender so the founder or the owner of this contract will be the account that deploys the contract and the supply with number wanted by the founder. So here let's create the constructor; constructor public and now the supply can be an argument of the constructor or can be a hard coded value. Let's say in this example, because it's simpler, to be a hard coded value. So supply= let's say 1 million. Here we have 1 million Kryptos and founder= msg.sender OK, one question: who has Kryptos at the beginning so how could someone transfer Kryptos to another account if that person that transfers Kryptos doesn't have them. At the beginning the founder will have all tokens will have 1 million of Kryptos and he can transfer to other accounts. So here balances of founder equals supply and the constructor has been defined. We've developed a basic but functional ERC20 token. Our contract implements a real token we can transfer tokens to other Etherum address like any ERC 20 token. In the next lecture will test and deploy our ERC. 20 token on ringbe test network. See you there.