EVG101 Part 7: Smart Contracts
Welcome to Part 7 of Everest’s crypto and blockchain guide! In this article, we will look at one of the key elements of Ethereum: smart contracts. We will then dive deeper and look into the most well-known technical standard for Ethereum smart contracts, the ERC-20. Finally, we conclude with an overview of some real-life use-cases of Ethereum decentralized apps.
To go back to our home page: link
To go back to the article on intro to Ethereum: link
To go to the next article on scaling solutions: link
Table of Contents
- What is a smart contract? What does it look like?
- What does ERC mean? What is ERC20 and how does it work?
- What are other prominent ERC standards?
- What are some prominent decentralized applications (dApps) powered by Ethereum?
TLDR;
1. What is a smart contract? What does it look like?
Smart contracts are computer protocols which digitally enforce the negotiation or performance of a contract. A smart contract’s behavior is dictated by a piece of code embedded in that account that automatically executes when certain specific conditions are met, or If-This-Then-That (IFTTT) logic.
In Ethereum, a smart contract is an alternative type of Ethereum account to a standard user account which also has an address and a balance. Smart contracts are automatically executed according to its code by the computers in the Ethereum network, and the resulting changes are recorded on the Ethereum blockchain. They are run on the (decentralized) blockchain, so there is no censorship, downtime, fraud, or third-party interference.
Normal Ethereum addresses require transactions to be authorized by the private key associated with that address. However, the actions a smart contract takes are completely pre-determined by the code programmed into it.
This means that the smart contract code anticipates every possible input it can receive and responds in a consistent way for each of those inputs.
Let’s use the visual above as an example. That smart contract only distributes ether to individuals whose first name is “Adam”, last name is “Smith”, and id is “adamsmith123”. Anyone who provides an input that does not fulfill those requirements, such as ‘Anne’, ‘Sasha’, or even a random string like ‘123fa’, will not receive any ether.
In general there are 3 types of Ethereum transactions:
- Transfer of value
- Creating a smart contract
- Calling a smart contract
An Ethereum transaction has the following fields:
- From (Sender address)
- To (Recipient address)
- Amount of ether
- Data
To create a smart contract, one leaves the “To” field empty and copies the smart contract code compiled to byte-code into the “Data” field. One can also give the smart contract ether by specifying the amount in “Amount” field.
To call a smart contract, the “To” field is the address of the smart contract, whereas the “Data” field contains the name of the specific function to be called from the smart contract and the parameters for that function. The amount of ether sent is 0 if the service is free, or whatever price is set by the creators of the smart contract.
Complementary smart contracts can be bundled together to create a dApp, a decentralized application. The smart contracts work together to provide a service for the users of the dApp.
If a smart contract is not securely written, it is vulnerable to exploitation. This was illustrated by the infamous DAO attack where a hacker was able to draw USD$50M worth of ether due to a loophole in the smart contract. Furthermore, the code programmed into a smart contract cannot be changed, so extra precautions need to be taken when making a smart contract. To guard against this, companies like OpenZeppelin offer users a battle-tested framework of reusable smart contracts which have been proven to be secure.
Contrary to popular belief, smart contracts exist in Bitcoin. The Lightning Network which is used to scale Bitcoin transaction throughput is a type of Bitcoin smart contract. Bitcoin smart contracts are more narrow in scope as the scripting language of Bitcoin is not Turing-complete; it is not capable of loops for example.
2. What does ERC mean? What is ERC-20 and how does it work?
ERC stands for Ethereum Request for Comment. Developers first submit an EIP (Ethereum Improvement Proposal) to the Ethereum community, and once it is approved by the Ethereum Committee, it becomes a new ERC, which then serves as a standard for other developers to build around.
Each ERC defines a set of rules required to implement tokens for the Ethereum ecosystem. Adherence to these rules make it easier for developers to predict the behavior of tokens in the Ethereum network and create interoperable applications and tokens for Ethereum. ERCs can dictate anything from protocol specifications to smart contract descriptions or structures.
The ERC-20 is the most famous technical standard (or template) for smart contracts on the Ethereum blockchain for implementing tokens. The majority of the tokens issued on the Ethereum blockchain are ERC-20 compliant. This means that the smart contract structure of these tokens follow the same format as the ERC-20 technical standard.
ERC-20 defines a specific set of rules that Ethereum tokens should implement so that they can function smoothly within the larger Ethereum ecosystem. It implements a standard API (application programming interface) which allows tokens to communicate with each other, solving the problem of incompatibility between different Ethereum tokens.
With reference to the above visual, the structure of an ERC-20 compliant smart contract would be as follows:
6 mandatory and executable functions
- totalSupply(): used to fix the total number of ERC-20 tokens to be created in the smart contract. Once this number is reached, the smart contract will refuse to create additional tokens
- balanceOf(address): tells you the number of tokens associated with a given address
- transfer(user_address, number_of_tokens): used to transfer tokens from smart contract into user account
- transferFrom(from_address, to_address, number_of_tokens): used to transfer tokens from one user account to another user account
- approve(spender_address, value): allows the spending address to remove tokens from your account multiple times, up to the value amount
- allowance(spender_address, owner_address): returns the amount of tokens which the spending address is still allowed to remove from the owner address
2 types of events (which are triggered when functions are run)
- transfer: This event is triggered when some tokens are transferred (so when either the transfer or transferFrom function is called), and logs the transferring address, the receiving address, and the amount of tokens transferred.
- approval: This event is triggered when the approve function is called and logs the address of the spending account, the address of the owning account, and the amount of tokens transferred.
3 optional functions related to token information
- token name
- symbol
- decimal: denotes the order of the smallest token value, or how many digits it can take after decimal point. The maximum value of decimal is 18.
3. What are other prominent ERC standards?
Other prominent ERC standards include:
- ERC-223: Rejects incompatible token transfers between different ERC tokens and returns them to sender as opposed to tokens being lost or burnt up. You can read more about the ERC-223 standard here: link.
- ERC-721: Non-fungible token standard used by CryptoKitties. You can read more about the ERC-721 standard here: link.
- ERC-1400: Securities token protocol which includes functions to account for whitelisted investors, issuance and redemption of security tokens etc. You can learn more about the ERC-1400 standard here: link.
4. What are some prominent decentralized applications (dApps) powered by Ethereum?
Augur
Augur is a decentralized market prediction platform which allows users to create prediction markets to crowdsource opinions on the outcome of any future events, such as the result of an election.
Basic Attention Token
Basic Attention Token helps advertising firms and publishers to value and make money off user attention on websites by securely monitoring user behavior on their web browser while protecting user privacy.
Cryptokitties
Cryptokitties is game based on collectible digital cats. It uses the ERC-721 standard to create unique digital assets which cannot be replicated.
Dai
Dai is a stablecoin project by the MakerDao team. Each Dai is pegged at USD$1, but unlike other stablecoins, it is collateralized with cryptocurrencies such as Ethereum as opposed to fiat.
Steemit
Steemit is a decentralized social network where good content is rewarded: users can reward other users for good content/blog posts with tips and receive rewards for their own content as well. Other user behavior such as comments or upvotes are rewarded with their native cryptocurrency, Steem, as well.
Concluding Remarks
In this article, we went into detail on Ethereum smart contracts as well as the most popular smart contract standard, the ERC-20. While the ERC-20 works well as a technical standard, Ethereum applications are held back by the inherent limitations of the Ethereum network, which can only process 15 transactions per second. This greatly limits the utility of the Ethereum network and was brutally exposed with the exploding popularity of the Cryptokitties app, which clogged the entire network. In the next article, we will talk about scaling solutions which will attempt to help Bitcoin and Ethereum increase transaction throughput so that these networks can be more amenable to real-world use-cases.
We see this guide as a continuous work-in-progress! Please leave any questions or remarks in the comments section and we will try our best to include them in updated versions of our guide. And if you found our guide useful, please leave some claps!
For more information on Everest, please visit our website.