
Author: Ravikiran Betha
During the very early stages of trading by mankind, the parties involved in trade used to exchange goods and services through barter system. The system was based on estimated/perceived value between the goods being exchanged. There was lack of a measure to value the goods and services being exchanged independently. The trade was between two parties that exactly needed these goods in exchange.
Example: 100 kgs of Rice for 2 sheeps
The reasoning was simple, One party had rice and needed sheeps and like wise, other party needed rice. They essentially fulfilled the needs of each other. There was no independent valuation of the goods involved as there was no common unit of measure of value.
This lead the human kind to invent money, a clear measure of value. The outcome is the ability to define the value of rice and sheep independently.
So, with money in play,
1 kg of rice is 1 dollar and hence 100 kgs is 100 dollars.
2 sheeps may be priced differently based on weight, size and other attributes.
1st Sheep is 65 dollars and 2nd sheep is 52 dollars based on the attributes.
With money, the parties are no more tied, the owner of 100 kgs can sell rice to 100 different people at 1 dollar per kg. Like wise, the sheeps owner can sell his sheep to two different people. With money as measure of value, the parties involved in the trade got lot more flexibility to value and also transfer of value.
All this became possible because money guarantees to deliver value it represents. A 100 dollar bill guarantees to deliver 100 dollars worth and that is how the money has become the acceptable norm for transfer of value. As money was adopted as standard for transfer of value, each geographic boundary defined a unit of measure for its trading activity. The premises for circulation of money is based on value available for transfer in the country. The money issued by countries is referred as fiat currency. The issuing of money is monitored so that supply of money in circulation holds tightly to the value available for transfer.
ERC20
Money as Dollar or Japanese yen represents value in their respective economy. Like wise, in decentralised world, there was a need to represent value. The idea is to be able to measure the value of anything in the economy, which lead to definition of ERC20 standard. ERC20 defines the properties of token that has the ability to represent and maintain the value. They are fungible meaning each unit is same as other. This is similar to how each dollar delivers the exact same value.
As ERC20 needs to represent money in Defi, it has the below functionalities to render the representation.
Manage Money Supply
i) mint()
Function to add tokens into circulation. Similar to how central bank mints currency to support expansion of economic activity in a country
ii) burn()
Function to remove tokens from circulation. Similar to how a central bank reduces the available currency in circulation to control devaluation of currency.
Use of money in circulation
i) Balance()
Function to check balance for an account. All the tokens minted are accounted as balance of one of the accounts.
ii) transfer()
Function to move funds to an account
iii) Approve() & transferFrom()
Function pair allows the account owner to authorise another account to spend funds on behalf
Decimals
Solidity does not support fractions. Hence, it is the responsibility of the ERC20 decide on the fractions.
Example:
decimals = 18
means 1 token is represented as 1000000000000000000
and 1.256478456481 tokens is represented as 1256478456481000000
Issuing Authority
The fiat currency is issued by governing body of the country, referred as central bank. They measure the economic activity with in the country and ensure the fiat currency in circulation is able to deliver the promised value.
In Defi, the equivalent is issued by the protocol and is governed by the protocol to ensure it delivers value. We will understand how the value is brought into and maintained within the protocol to ensure value delivery in the coming days by looking at Defi primitives.
Understanding ERC20 role in Defi is critical. As the purpose of ERC20 is clear, we will dwell into the below primitives of Defi in the coming days one by one.
Defi Primitives
a) Escrow
b) Vault
c) Staking
d) AMM
e) DEX
f) Borrow and Lending
g) Stable coin
h) Options
Below is an example of ERC20 contract, refer openzeppelin for correct implementation.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.14;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
contract ERC20 is IERC20 {
using SafeMath for uint256;
string public name;
string public symbol;
uint8 public decimals;
uint256 public totalSupply_;
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
// Name, symbol and decimals for the token
constructor(string memory name_, string memory symbol_, uint8 decimals_) {
name = name_;
symbol = symbol_;
decimals = decimals_;
}
// Total number of tokens in circulation
function totalSupply() public override view returns (uint256) {
return totalSupply_;
}
// Balance of a given account
function balanceOf(address account) public override view returns (uint256) {
return balances[account];
}
// Transfer funds to a target account
function transfer(address receiver, uint256 numTokens) public override returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(numTokens);
balances[receiver] = balances[receiver].add(numTokens);
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
// Functions to delegate spending on behalf
function approve(address delegate, uint256 numTokens) public override returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
function allowance(address owner, address delegate) public override view returns (uint) {
return allowed[owner][delegate];
}
function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) {
require(numTokens <= balances[owner]);
require(numTokens <= allowed[owner][msg.sender]);
balances[owner] = balances[owner].sub(numTokens);
allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens);
balances[buyer] = balances[buyer].add(numTokens);
emit Transfer(owner, buyer, numTokens);
return true;
}
// Minter functions, should be access controlled
function mint(address account, uint256 amount) external {
balances[account] += amount;
totalSupply_ += amount;
}
// Minter functions, should be access controlled
function burn(address account, uint256 amount) external {
balances[account] -= amount;
totalSupply_ -= amount;
}
}
library SafeMath {
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
assert(c >= a);
return c;
}
}