1. ERC-20이란
- ERC-20은 Ethereum Request for Comment 20의 약자이며 20은 20번째라는 뜻이다.
- 이더리움블록체인 네트워크에서 정한 표즌 토큰 스팩
- ERC-20이라는 표준을 사용하는 이유는 토큰끼리의 호환을 위해서 이다.
2. 코드
2.1. ERC20interface
- 자식 컨트랙트를 위한 틀이며, 추상 함수로만 구성되어야 한다.
- 해당 내용을 구현하지는 않고 이를 상속하는 쪽에서 구현하며 다른 컨트랙트들에게 이 컨트랙트는 이런 함수들을 포함하니 안심하고 호출하라는 정보를 주는 역할을 한다.
interface ERC20Interface {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function transferFrom(address spender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 amount);
event Transfer(address indexed spender, address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 oldAmount, uint256 amount);
}
2.2. Contract
2.2.1. totalSupply
function totalSupply() external view virtual override returns (uint256) {
return _totalSupply;
}
2.2.2. balanceOf
function balanceOf(address account) external view virtual override returns (uint256) {
return _balances[account];
}
2.2.3. transfer
- transfer은 내부 함수인 _transfer를 호출한다.
- contract 보다 상위인 interface를 override 해서 transfer을 사용 하고 기능은 _transfer에서 구현
function transfer(address recipient, uint amount) public virtual override returns (bool) {
_transfer(msg.sender, recipient, amount);
emit Transfer(msg.sender, recipient, amount);
return true;
}
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
require(isTokenLock(sender, recipient) == false, "TokenLock: invalid token transfer");
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] = senderBalance.sub(amount);
_balances[recipient] = _balances[recipient].add(amount);
}
}
2.2.4. approve
function approve(address spender, uint amount) external virtual override returns (bool) {
uint256 currentAllowance = _allowances[msg.sender][spender];
require(_balances[msg.sender] >= amount,"ERC20: The amount to be transferred exceeds the amount of tokens held by the owner.");
_approve(msg.sender, spender, currentAllowance, amount);
return true;
}
function _approve(address owner, address spender, uint256 currentAmount, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
require(currentAmount == _allowances[owner][spender], "ERC20: invalid currentAmount");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, currentAmount, amount);
}
2.2.5. allowance
function allowance(address owner, address spender) external view override returns (uint256) {
return _allowances[owner][spender];
}
2.2.6. transferFrom
- transferFrom은 양도를 수행하는 거래 대행자(msg-sender)가 sender가 허락해준 값만큼 상대방(recipient)에게 토큰을 이동합니다.
- 이동을 위해 _transfer 함수를 실행하고 완료되면 _approve함수를 실행합니다.
function transferFrom(address sender, address recipient, uint256 amount) external virtual override returns (bool) {
_transfer(sender, recipient, amount);
emit Transfer(msg.sender, sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][msg.sender];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, msg.sender, currentAllowance, currentAllowance - amount);
return true;
}
2.3. SafeMath
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a / b;
return c;
}
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;
}
}
2.4. OwnerHelper
abstract contract OwnerHelper {
address private _owner;
event OwnershipTransferred(address indexed preOwner, address indexed nextOwner);
modifier onlyOwner {
require(msg.sender == _owner, "OwnerHelper: caller is not owner");
_;
}
constructor() {
_owner = msg.sender;
}
function owner() public view virtual returns (address) {
return _owner;
}
function transferOwnership(address newOwner) onlyOwner public {
require(newOwner != _owner);
require(newOwner != address(0x0));
address preOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(preOwner, newOwner);
}
}
2.5. TokenLock
2.5.1. isTokenLock
bool public _tokenLock;
mapping (address => bool) public _personalTokenLock;
constructor(string memory getName, string memory getSymbol) {
_tokenLock = true;
}
function isTokenLock(address from, address to) public view returns (bool lock) {
lock = false;
if(_tokenLock == true)
{
lock = true;
}
if(_personalTokenLock[from] == true || _personalTokenLock[to] == true) {
lock = true;
}
}
2.6. 전체코드
pragma solidity 0.8.10;
interface ERC20Interface {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function transferFrom(address spender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 amount);
event Transfer(address indexed spender, address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 oldAmount, uint256 amount);
}
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a / b;
return c;
}
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;
}
}
abstract contract OwnerHelper {
address private _owner;
event OwnershipTransferred(address indexed preOwner, address indexed nextOwner);
modifier onlyOwner {
require(msg.sender == _owner, "OwnerHelper: caller is not owner");
_;
}
constructor() {
_owner = msg.sender;
}
function owner() public view virtual returns (address) {
return _owner;
}
function transferOwnership(address newOwner) onlyOwner public {
require(newOwner != _owner);
require(newOwner != address(0x0));
address preOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(preOwner, newOwner);
}
}
contract SimpleToken is ERC20Interface, OwnerHelper {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) public _allowances;
uint256 public _totalSupply;
string public _name;
string public _symbol;
uint8 public _decimals;
bool public _tokenLock;
mapping (address => bool) public _personalTokenLock;
constructor(string memory getName, string memory getSymbol) {
_name = getName;
_symbol = getSymbol;
_decimals = 18;
_totalSupply = 100000000e18;
_balances[msg.sender] = _totalSupply;
_tokenLock = true;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() external view virtual override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) external view virtual override returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint amount) public virtual override returns (bool) {
_transfer(msg.sender, recipient, amount);
emit Transfer(msg.sender, recipient, amount);
return true;
}
function allowance(address owner, address spender) external view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint amount) external virtual override returns (bool) {
uint256 currentAllowance = _allowances[msg.sender][spender];
require(_balances[msg.sender] >= amount,"ERC20: The amount to be transferred exceeds the amount of tokens held by the owner.");
_approve(msg.sender, spender, currentAllowance, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) external virtual override returns (bool) {
_transfer(sender, recipient, amount);
emit Transfer(msg.sender, sender, recipient, amount);
uint256 currentAllowance = _allowances[sender][msg.sender];
require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
_approve(sender, msg.sender, currentAllowance, currentAllowance - amount);
return true;
}
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
require(isTokenLock(sender, recipient) == false, "TokenLock: invalid token transfer");
uint256 senderBalance = _balances[sender];
require(senderBalance >= amount, "ERC20: transfer amount exceeds balance");
_balances[sender] = senderBalance.sub(amount);
_balances[recipient] = _balances[recipient].add(amount);
}
function isTokenLock(address from, address to) public view returns (bool lock) {
lock = false;
if(_tokenLock == true)
{
lock = true;
}
if(_personalTokenLock[from] == true || _personalTokenLock[to] == true) {
lock = true;
}
}
function removeTokenLock() onlyOwner public {
require(_tokenLock == true);
_tokenLock = false;
}
function removePersonalTokenLock(address _who) onlyOwner public {
require(_personalTokenLock[_who] == true, "err");
_personalTokenLock[_who] = false;
}
function _approve(address owner, address spender, uint256 currentAmount, uint256 amount) internal virtual {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
require(currentAmount == _allowances[owner][spender], "ERC20: invalid currentAmount");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, currentAmount, amount);
}
}