如果有在 etherscan 上面看過 ERC20 token 的資訊的話,應該會知道有些 token 有小數點、跟名稱。而這些資訊應該要在 contract 裡面怎麼設定呢?今天就讓我們來聊一聊這邊的資訊吧!
本日合約:
pragma solidity ^0.4.25;
import "browser/SafeMath.sol";
import "browser/IERC20.sol";
contract ERC20 is IERC20 {
using SafeMath for uint256;
string public constant name = "haha token";
uint8 public constant decimals = 18;
string public constant symbol = "HHT";
uint256 private _totalSupply;
mapping(address => uint256) _balances;
mapping(address => mapping(address => uint256)) _approve;
// 所有存在的 Token 數量
function totalSupply() external view returns (uint256) {
return _totalSupply;
}
// 讀取 tokenOwner 這個 address 所持有的 Token 數量
// address => uint256
function balanceOf(address tokenOwner) external view returns (uint256 balance) {
return _balances[tokenOwner];
}
// 從 msg.sender 轉 tokens 個 Token 給 to 這個 address
// msg.sender ---tokens---> to
function transfer(address to, uint256 tokens) external returns (bool success) {
return _transfer(msg.sender, to, tokens);
}
// 得到 tokenOwner 授權給 spender 使用的 Token 剩餘數量
function allowance(address tokenOwner, address spender) external view returns (uint256 remaining) {
return _approve[tokenOwner][spender];
}
// tokenOwner -> spender -> tokens
// address => address => uint256
// msg.sender 授權給 spender 可使用自己的 tokens 個 Token
function approve(address spender, uint256 tokens) external returns (bool success) {
_approve[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
// 將 tokens 個 Token 從 from 轉到 to
function transferFrom(address from, address to, uint256 tokens) external returns (bool success) {
_approve[from][msg.sender] = _approve[from][msg.sender].sub(tokens);
return _transfer(from, to, tokens);
}
function _transfer(address from, address to, uint256 tokens) internal returns (bool success) {
_balances[from] = _balances[from].sub(tokens);
_balances[to] = _balances[to].add(tokens);
emit Transfer(from, to, tokens);
return true;
}
}
本日影片:
https://youtu.be/i_1qIY8shrw
Smart Contract 實戰教學播放清單:
https://www.youtube.com/playlist?list=PLHmOMPRfmOxSJcrlwyandWYiuP9ZAMYoF