Wednesday, 15 May 2019

What are the basics of Ethereum ERC20 Standard: Token Creation?


      Ether is the cryptocurrency and Ehtereum is the blockchain implementation it is built on. It is decentralized and distributed that is similar to the way the bitcoin works. It also uses an open ledger and mine for more coins. A great thing is a smart contracts which are essentially automated with no intermediary. Blocks of transactions can be created faster in ethereum. It allows the computer applications that are not just the currency to run on the network. For example, cloud storage like Dropbox is storing your files without the central authority. They will operate very well even if it financially goes down due to files are stored on several different computers on a distributed and decentralized network. It has over 5 million wallets capable of storing Ethereum blocks of transactions that can be created faster than Bitcoin. It uses the Turing Complete Programming language. It means more flexible and giving enough computing power to solve anything that makes easier to mine and use for different applications.

Ethereum Mining: It is similar to bitcoin mining. In each block of the transactions in the ethereum blockchain, miners use computers to repeatedly and very quickly get answers to the puzzle. This is a simplification of what happens, the mathematical problems that ensure the validity of the transactions occurring on the Ethereum blockchain. ETHASH is the specific algorithm that measures the particular miner done work of verifying transactions. It requires more memory to make it harder to mine using expensive A6 which are specialized mining chips. So, the miners rewarded an ether as an incentive.

Introduction to ERC20 Token:         
       ERC20 is basically an interface of a smart contract that defines how a token should look like. Here are the events and functions that we need to have in our token contract. In order for it to be accepted as ERC20 tokens, we need the function of our total supply that is public and return uint that should the total number of tokens in existence. The balanceof function should use the balance of an address that we give it. Allowance in ERC20 tokens we can give permissions to someone else to spend a certain amount of money from your wallet. It is tightly coupled with approve function. Approve function that allows certain wallet address to spend x amount of tokens from your wallet. The allowance will give the remaining tokens that the token owner has allowed spend by the spender. The function transfer allows you to transfer a certain amount of tokens to address. Now, when you use to transferFrom function, you can transfer tokens from the account of another person that has allowed you to use a certain amount of tokens to another person.
                 The transfer event happens at when we transfer some tokens from one account to another and approval event will be triggered when someone has approved someone to spend money on their behalf. And this is the interface of the standard tokens, If you have those functions inside your token it will be accepted as an ERC20 token.
Improving our Basic token to be ERC20 Standard: In the basic token, we only have the function transfer, So let's add rest of the ERC20 token functions, the public variables like name, symbol, decimal, totalSupply and mapping that tracks the allowances and also the events that are needed. Now, let's create the public transfer function code that receives the following arguments of address to and uint256 value that is public and returns a boolean code success. The transfer function will receive msg.sender and the variable to, value. Below the function, let's return true that the transfer was successful.
      The function transferFrom that receives the argument of address_from, address_to, and uint256 value. The function will also be public and will return boolean of success. Then require the value less than or equal to allowance. Afterwords to make sure there is enough money that we are allowed to spend will deduct the money from the allowance. Then, type the following allowance from msg.sender minus equal value. After that, let us transfer the money to the desired accounts that transfer from, to and value. At the end of the function return true to mark the function as successful.
      In the Approve function approve the addresses to spend a certain amount of money on our behalf. This function will receive the arguments of address sender, uint256 value. The function will be public and will return a boolean success, Imagine, you have the company account and want some employees to be able to spend money on behalf of the company. This is where the allowance comes in. So, type inside the bracket of the allowance of msg.sender of spender will be equal to the value. This will give a right to the allowance who have been authorized and what amount of money they're authorized to spend. Value is the maximum amount that the spender can use. Below this, let's emit the approval event. So, type emits approval of msg.sender, spender, value and at the end lets return true that the function was successfully executed. You can get the ERC20 tokens from Github.

Making an Ownable Token: We can add the ability of our token to be ownable. So we can use the modifier for the owner to restrict access to the function that can add tokens or burn them.  Now, we need to create a new contract called owned. Inside the brackets, we will have the public address code owner and also the constructor that has owner equals msg.sender. Then create modifier called onlyowner. Inside the modifier, type requires msg.sender to be equal to the owner. This is the modifier that we will use in the future to restrict access to certain functions. It is important to add the underscore or placeholder at the end of the modifier.
   Now, we also want to be able to transfer the ownership of the token to someone else. So, create a
function called transfer ownership that to receive address newowner and let's add the modifier on the owner to the function. We want only the owner to be able to transfer the ownership. So, inside the function type, the owner equals to newowner. Finally, add the "is owned" keyword at the top where we define the token contract.

Minting or New Token Creation: Mint token that receives address target and uint256 of mintedAmount. This function will be able to create new tokens and give them any account the owner
desires. It is important to give this function the modifier only owner. Inside, the balanceof target plus equal to mintedAmount. The totalSupply plus equal to mintedAmount. We need to emit our transfer event. So, Type emit transfer of zero, owner and mintedAmount. Below it, there is one more transfer event. Type emit transfer of owner to target and mintedAmount.  This is how you create a function that can mint tokens whenever the owner of the token decides.

Deploy our DApp to the Web: It consists of 2 parts. We would want to deploy our contracts into test network within Infura and we want to deploy our Front-End with Heroku which is the service that deploys our applications to the cloud. First, you need to set up and create the account. In order for an application to work, we need to deploy our contracts to the test network. Infura is a hosted ethereum node cluster. We have to set up an own cluster that syncs up the entire ethereum network cluster. So, after you created the account in Infura, login to the dashboard and you need to create a new project for our app.
            Now, we are ready to prepare our truffle project to deploy our contracts to the Rinkeby test network. In order to connect Infura, we need to install the truffle -hdwallet provider and deploy the contract in the test network. Let's register and install Heroku. Heroku is a cloud platform as a service. It supports several languages that you can deploy projects written in many kinds of languages. It supports node.js, ruby, java, php, python, and other languages. It has a free plan to create your account. In the dashboard, create your new application and install Heroku CLI. If you have registered and installed Heroku on your PC, then you are ready to run our truffle project in Heroku.
       The last thing, you need to do is to create a git repository for our project and deploy the Heroku application. In the terminal, login to heroku and create a git repository. Now type Git Init which will initiate the git repository and it is ready to add heroku as your remote repository. Then, we need to add all the files to Git by the command "Git add ." Let's create the commit and publish it. Finally, we need to push our project to Heroku. Once, everything is published, you can open your DApp by typing "Heroku Open". Now you have the working published DApp online and you can test out in the Rinkeby network where we deployed our contracts. It is the way to deploy our decentralized application on the web.

No comments:

Post a Comment