两个概念
- ERC
- EIP
常见的ERC标准
- ERC-20Token Standard(早期区块链项目都基于此标准)
- ERC-721 Non-Fungible Token Standard(非同质化标准)
- ERC-165 Standard Interface Detection
- ERC-777 Token Standard
其它标准见 eips.ethereum.org/etc
ERC-20标准详解
ERC-20为一套接口,实现了这套接口,即为实现这套标准
pragma solidity^0.6.1;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender)
external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value)
external returns (bool);
function transferFrom(address from, address to, uint256 value)
external returns (bool);
event Transfer(
address indexed from,
address indexed to,
uint256 value
);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
pragma solidity^0.6.1;
import "./IERC20.sol";
contract test_erc20 is IERC20{
string private _name;
uint256 private _totalSupply;
address private admin;
mapping(address=>uint256) private _balances;
mapping(address=>mapping(address=>uint256))private _allowance;
constructor(string memory name) public{
_name=name;
_totalSupply=0;
admin=msg.sender;
}
function name() external view returns(string memory){
return _name;
}
function mint(address to ,uint256 value) external{
require(admin==msg.sender,"only admin can do");
require(address(0)!=to,"to must is an address");
_balances[to]+=value;
_totalSupply+=value;
emit Transfer(address(0),to,value);
}
function totalSupply() external override view returns (uint256){
return _totalSupply;
}
function balanceOf(address who) external override view returns (uint256){
return _balances[who];
}
function allowance(address owner, address spender) override
external view returns (uint256){
return _allowance[owner][spender];
}
function transfer(address to, uint256 value) override external returns (bool){
require(value>0,"value must >0");
require(_balances[msg.sender]>=value,"balance must enough!");
require(address(0)!=to,"to must is an address");
_balances[msg.sender]-=value;
_balances[to]+=value;
emit Transfer(msg.sender,to,value);
}
function approve(address spender, uint256 value)override
external returns (bool){
require(_balances[msg.sender]>=value,"balance must enough!");
require(address(0)!=spender,"to must is an address");
_allowance[msg.sender][spender]=value;
emit Approval(msg.sender,spender,value);
}
function transferFrom(address from, address to, uint256 value)override
external returns (bool){
require(address(0)!=to,"to must is an address");
require(value>0,"value must >0");
require(_allowance[from][to]>=value,"allowance's value must enough");
_allowance[from][to]-=value;
_balances[from]-=value;
_balances[to]+=value;
emit Transfer(from,to,value);
}
}
函数测试
选择第一个账户为管理员,负责部署合约
1. 部署合约
2. 模拟挖矿,赋予参与方金额
注意:只有管理员拥有挖矿权限,即调用挖矿函数
3.查询余额以及总的发行量
这里是引用
4.进行转账
第三账户—>第二个账户 转账的三要素:谁转账,转给谁,转多少
5.进行授权
授权第三方进行转账
6.授权转账
注意:这一步是在上一步骤的基础上进行的,转账的三要素要匹配,否则会出现错误
|