ERC20은 Ethereum Request for Comments 20의 약자로써 네트워크 상에서 토큰들끼리 서로 상호 작용하기 위하여 모든 토큰이 따라야하는 표준 규칙 집합이다.
ERC20 토큰을 배포할때 여러가지 툴을 사용할 수 있다.
- truffle
- Klaytn IDE
- Remix
- etc...
ERC20을 배포할 때 대부분의 코드가 openzeppelin을 참조한다.
오늘은 openzeppelin에 쓰여져 있는 각 코드의 구조와 함수를 살펴보고자 한다.
openzeppelin에서 제공하는 공식 ERC20 코드는 Core, Extensions, Utilities로 크게 3가지로 나뉜다.
Core
- IERC20 : ERC20의 기본적인 인터페이스
- ERC20 : ERC20에서 가장 기본적인 기능이 구현된 클래스
- ERC20Detailed : 토큰 등록을 하기 위한 정보를 가지고 있는 클래스
Extensions
- ERC20Mintable
- ERC20의 확장자
- 코인 발행 관련 기능
- Minter만 작동시킬 수 있다.
- ERC20Burnable
- ERC20의 확장자
- 코인 소각 관련 기능
- ERC20Pausable
- ERC20의 확장자
- 일시정지 기능
- 일정 세일이 끝날 때까지 거래를 중단하거나 큰 버그가 발생할경우 토큰 동결을 위해 사용할 수 있음
- ERC20Capped
- ERC20Mintable의 확장자
- 발행량 한도를 추가하는 기능
Utilities
- SafeERC20
- ERC20 계약과 안전하게 상호 작용할 수 있도록 사용하는 유틸리티
- TokenTimelock
- 특정 시간 후에 토큰을 추출할 수 있는 토큰 보유자 컨트랙트
위 클래스들의 관계를 정리한 표를 발견해서 첨부해본다!

IERC20
함수
totalSupply()
- 존재하는 함수 개수를 리턴해주는 함수
function totalSupply() external view returns (uint256);
balanceOf(account)
- 현 계정이 가지고 있는 토큰 개수를 리턴해주는 함수
function balanceOf(address account) external view returns (uint256);
transfer(recipient, amount)
- 발신자에서 수신자에게 토큰을 이동한다.
- 작업의 성공 여부를 boolean값으로 반환한다.
- Transfer 이벤트를 방출한다.
function transfer(address to, uint256 amount) external returns (bool);
allowance(owner, spender)
- spender(지출자?)가 소유자 대신 소유자 계정에서 지출할 수 있는 남은 토큰 수를 반환한다.
- 기본적으로 반환 값은 0
- 이 값은 approve나 transferFrom 함수가 작동될때 변경된다.
function allowance(address owner, address spender) external view returns (uint256);
approve(spender, amount)
- 이 함수에 들어온 값을 호출자가 소유자 계정에서 지출할 수 있는 토큰의 개수로 설정하는 함수.
- 성공 여부를 boolean값으로 반환한다.
function approve(address spender, uint256 amount) external returns (bool);
transferFrom(sender, recipient, amount)
- allowance 함수를 사용해서 수신자 토큰을 발신자에게 이동한다.
- 성공 여부를 boolean값으로 반환한다.
- Transfer 이벤트를 방출한다.
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
이벤트
Transfer
- 토큰이 계정에서 계정으로 이동할때 방출되는 이벤트
- value값이 0일 수도 있음
event Transfer(address indexed from, address indexed to, uint256 value);
Approval
- approve 함수에 의해서 allowance가 재설정 될때 실행되는 이벤트
- value값은 새 allowance임
event Approval(address indexed owner, address indexed spender, uint256 value);
ERC20
함수
totalSupply()
balanceOf(account)
transfer(recipient, amount)
- recipient는 주소가 0이면 안됨
- caller는 최소 amount만큼의 밸런스가 있어야함
allowance(owner, spender)
approve(spender, amount)
- spender는 주소가 0이면 안됨
transferFrom(sender, recipient, amount)
- 업데이트된 allowance 값을 approval이벤트에 보냄
- 보낸 사람과 받는 사람의 주소는 0이면 안됨
- 발신인은 최소 금액의 잔액이 있어야 함
- 발신자는 최소 amount만큼의 토큰에 대해 allowance를 해줘야함
increaseAllowance(spender, addedValue)
- caller가 spender에게 부여한 allowance를 증가시킨다.
- 업데이트된 allowance를 나타내는 Approval 이벤트를 방출시킨다.
- spender주소가 0이면 안됨
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}
decreaseAllowance(spender, subtractedValue)
- caller가 spender에게 부여한 allowance를 감소시키는 함수
- 나머지 위와 같음
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
address owner = _msgSender();
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
return true;
}
_transfer(sender, recipient, amount)
- 토큰 금액을 보낸 사람에서 받는 사람으로 이동하는 함수.
- 내부 기능은 transfer함수와 동일
- 자동 토큰 수수료 책정, 슬래싱 메커니즘(?) 등을 구현할때 사용할 수 있다.
- sender와 recipient의 주소가 0이 되면 안됨
- sender의 잔고가 amount 이상이여야 함
function _transfer(
address from,
address to,
uint256 amount
) internal virtual {
require(from != address(0), "ERC20: transfer from the zero address");
require(to != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(from, to, amount);
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
unchecked {
_balances[from] = fromBalance - amount;
}
_balances[to] += amount;
emit Transfer(from, to, amount);
_afterTokenTransfer(from, to, amount);
}
_mint(account, amount)
- amount만큼의 토큰을 생성하고 account에 그 토큰을 할당하여 totalSupply를 늘린다.
- 0 주소로 설정된 transfer이벤트를 방출한다.
- 주소가 0이면 안됨
function _mint(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply += amount;
_balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(address(0), account, amount);
}
_burn(account, amount)
- account에서 amount만큼의 토큰을 파괴하여 totalSupply를 줄인다.
- 0주소로 설정하기 위해 transfer이벤트를 방출한다.
- account주소가 0이면 안됨
- account의 잔액이 amount이상이어야 함
function _burn(address account, uint256 amount) internal virtual {
require(account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(account, address(0), amount);
uint256 accountBalance = _balances[account];
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
unchecked {
_balances[account] = accountBalance - amount;
}
_totalSupply -= amount;
emit Transfer(account, address(0), amount);
_afterTokenTransfer(account, address(0), amount);
}
_approve(owner, spender, amount)
- amount를 owner의 토큰에 대한 allowance로 설정하는 함수.
- 내부 기능은 approve함수와 동일.
- 특정 하위 시스템에 대한 자동 allowance를 설정한다.
- spender와 owner의 주소가 0이 되면 안됨
function _approve(
address owner,
address spender,
uint256 amount
) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
spendAllowance(owner, spender, amount)
- 지출된 amount를 기준으로 sepnder에 대한 owner의 allowance를 업데이트 한다.
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
}
}
_beforeTokenTransfer(from, to, amount)
- 토큰 전송 전에 호출되는 훅함수.
- minting과 burning이 포함된다.
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
_afterTokenTransfer(from, to, amount)
- 토큰 전송 후에 호출되는 함수
- 위와 같음
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
ERC20-Mintable
ERC20, MinterRole을 참조한다
함수
mint (account, amount)
- 새 토큰을 생성할 수 있는 권한이 있는 함수
- caller가 MinterRole을 가지고 있어야한다.
function mint(address account, uint256 amount) public onlyMinter returns (bool) {
_mint(account, amount);
return true;
}
ERC20-Burnable
ERC20을 참조한다.
함수
burn(value)
- caller로부터 value 만큼의 토큰을 파괴한다.
function burn(uint256 value) public {
_burn(msg.sender, value);
}
burnFrom(from, value)
- ERC20 의 burnFrom 보면 됨
function burnFrom(address from, uint256 value) public {
_burnFrom(from, value);
}
ERC20-Pausable
ERC20과 Pausable을 참조한다.
함수
transfer(to, value)
transferFrom(from, to, value)
appender(spender, value)
increateAllowance(spender, addedValue)
decreaseAllowance(spender, subtractedValue)
'Solidity' 카테고리의 다른 글
[solidity] assert, revert, require란? (0) | 2022.02.28 |
---|---|
[solidity] Event란? (indexed) (0) | 2022.02.25 |
[solidity] abi.encodePacked (0) | 2022.02.23 |
[solidity] This란? (0) | 2022.02.23 |
[크립토 좀비] 솔리디티 openzeppelin (0) | 2021.11.30 |