Introduction to Blockchain|Smart Contract and uses|ERC20 token|Applications
Blockchain is the latest trend going on due to its varied number of advantages. It is basically used with digital currencies initially, to transfer money without any hassle but due its secureness, decentralised manner, integrity, removal of intermediates it can be used widely in different fields such as Enterprises, Health, Logistics, Finance and many more.
Blockchain it’s characteristics and components
- It is a list of blocks that stores the information of a particular node/person/system.
- There is an underlying distributed ledger system where each node has all the transaction history taking place in that blockchain network.
- A block has following properties like index — stores the index value of block, data — message present in block(transactions), Current Hash value, Previous block hash value, difficulty — level of mining difficulty, nonce — random value generated to modify the hash value, timestamp.Generally, the first block of a network is known as Genesis block.
- Cryptographic functions are the mathematical functions like sha256, keccak256 that are used to encrypt our data into fixed length size to protect our privacy and secure the transaction. These hash functions only create hash values. Digital Signatures are also used which uses encryption methods using public-private pair key to authenticate the user and verify the originality of message.
- When a transaction is executed it needs to be checked and verified by the other nodes or miners to add that block into blockchain network. This is done with the help of Proof of Work concept, where the network participants partcipate in finding the data hash value(generated taking into consideration of all properties of block) by changing the Nonce value such that final hash value must generate the target number of leading zeroes(can be called as difficulty). If a node is successful in finding data hash value he/she may get rewards in terms of bitcoins/any other digital currency. This process is known as Mining.
- Finally, after consensus of all the network peers for the newly generated block, it is finally added to blockchain network.
Here’s the demo of blockchain network:
It is one of the famous open-source public blockchain platform which is very similar to bitcoins. Many famous digital currencies are built on top of Etherium. It deals with Ethers (currency used to exchange on Etherium platform). One of the main components of Etherium is Smart Contracts. Just like Bitcoin is only used to transfer coins from one address to another, this smart contracts with the help of which etherium network runs have an added functionaliy where it can send/ receive money or execute some other functionality with the help of rules. It is like an if/else/while condition in case of programming language. The main advantage of smart contract is it’s automated, if the condition is met then the contract is automatically executed removing the intermediates.
In this section, we discuss about the working of Smart Contracts:
- Below is the Smart contract for sending ethers to the different address in fig 1.
3. Similar to any other programming language, it has datatypes, access modifiers,functions, constructors. Additionally, receive function is to receive ethers where payable is nothing but contract is ready to send/receive ethers.
4. The send Ether function has two check statements initially, to check the authentication of user, and to verify the amount transferrable is less than the total amount he have.
5. If we go through the structure of code, the starting line is version of solidity to use, followed by owner state variables, constructor and functions.
These Smart contracts has gained popularity in many fields like finance, banking, supply chain, taxes, health due to its automatically execution of contract when certain conditions are met.
One of the biggest implementation and breakthrough of Etherium based tokens is ERC20 Token.
Before diving into the ERC20, let us dicuss about the difference between tokens and Smart Contracts. A token is an asset that can be transferrable digitally between two people. It is one of the forms of Smart contracts also known as token contracts that maps the account addresses to the balance as shown in below figure fig 2. In the figure below, each balance has total number of tokens.
The unit of balance is known as token. As mentioned before smart contract can be used in many fields and for each field balance is different like it may represent some physical objects, monetary value etc.
Why ERC20 token:
Also known as “Etherium Request For Comments 20” has been a huge success in ICO(Initial Coin Offering), in lame terms ICO is similar to IPO(Initial Public Offering) where organizations give away digital assets to raise millions of dollars. Before ERC20, every ICO token have their own function and different arguments. If a developer wants to trade between one person(token A) to another person(token B), he has to go through the code entirely and check for the correct senders address and various other constraints for a clean and secure transaction which is very time-consuming. To avoid this, ERC20 has come into picture which is nothing but a template such that all tokens have same functions, function names and take in same arguments, thus allowing faster trading exchanges between the tokens which is the reason for its popularity.
Functions of ERC20:
ERC20 is a standard baseline which is followed by almost all the smart contracts. A token present on Etherium blockchain network can be sent, received, calculate total supply, balance amount for each individual’s address. If this standard is followed then the particular smart contract is known as tokens.
Note : All tokens are smart contracts but not all smart contracts are tokens.
The main functions of ERC20 are displayed below:
- totalSupply() function give us the total supply or the sum of balances in a token contract as shown in fig 1.
- balanceOf() function takes address of owner as input, and balance of users as output
- allowance() function takes contract owner address and spender address , calculates remaining token that can be spent by the given address which returns it as output.
- transfer() function simply sends the token to address from the total supply.
- approve() function is to approve someone to spend tokens form our acccount where senders address and number of tokens to be spent is taken as input.
- transferfrom() function transfers tokens from one address to another address.
- Transfer() and Approval() are two events that are triggered according to the above functions. The former tells about the movement of tokens along different addresses while the latter gets the details about approval of tokens.
Above are the optional elements, which are name, symbol of token and number of decimals tells us the what kind of token units are used. If the contract represents an indivisible entity then make decimal as zero, or if it represents a fixed size number then use this number as decimal or else use 18 as decimals value.
Lets get down to code execution of ERC20 token:
The above code is similar to the code shown in fig 1 for transferring amount to address given as input, but here we are using token contract. All the functions discussed above are used.
- In the constructor we initialized a token with the following values.
- balanceof() function gives balance amount of input address.
- transfer() function transfers amount from senders address to receivers with the following required statements given in the first two lines of code in the transfer function
Let’s use this above token code in code shown in fig 1 to transfer the tokens. An additional variable is introduced shown in fig 6 that is token address such that in order to transfer a token we need token address and above code is called using import function to access its functions. The rest of the code is same.
We shall discuss briefly about how smart contracts are used in various domains.
- Maintaining Health Records : The records of an individual are secured and maintained at one place only, that is in block. For example, suppose I have medical records in three different countries, now doctors in each of these countries have no access to their other medical records. But using smart contracts, we have full control on our data as all our data is stored at one place only, and we can show only the necessary data to the hospital organizations instead of whole medical history which is secure in a manner.
- Smart Contract also help in clinical trials where lots of data of an individual are collected and are automatically submitted for an individual’s advantage.
- Several blockchain companies creates marketplace for users to monetize their own data preventing them from digital marketing frauds. To do so, Basic Attention Token is used which is an ERC20 token that serves as a utility between adverstisers, publishers and users. For example Brave browser.
This comes to the end of this blog, feel free to contact me at firstname.lastname@example.org
ERC20 Token Standard: A Beginners Guide
While most of us are fascinated by technological applications such as ICOs and Smart Contracts, very few of us…
What is an ICO Token | Learn all about ICO tokens | Cointelegraph
It's an entity with a value specified by the eminent. If it's a fashion startup, one token can be equal to one dress or…