美文网首页区块链开发以太坊区块链研习社
以太坊智能合约开发指南3(ERC-20代码实现)

以太坊智能合约开发指南3(ERC-20代码实现)

作者: luohuayong | 来源:发表于2018-02-10 11:56 被阅读377次

    根据ERC-20标准定义接口

    pragma solidity ^0.4.0;
    
    contract ERC20Interface {
        event Transfer(address indexed _from, address indexed _to, uint256 _value);
        event Approval(address indexed _owner, address indexed _spender, uint256 _value);
    
        function name() public view returns (string name);
        function symbol() public view returns (string symbol);
        function decimals() public view returns (uint8 decimals);
        function totalSupply() public view returns (uint256 totalSupply);
        function balanceOf(address _owner) public view returns (uint256 balance);
        function transfer(address _to, uint256 _value) public returns (bool success);
        function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
        function approve(address _spender, uint256 _value) public returns (bool success);
        function allowance(address _owner, address _spender) public view returns (uint256 remaining);
    }
    
    

    代码实现一个ERC-20代币合约

    pragma solidity ^0.4.0;
    
    import "./ERC20Interface.sol";
    
    contract ERC20 is ERC20Interface{
        string private _name = "MyToken";
        string private _symbol = "MYT";
        uint8 private _decimals = 3;
        uint256 private _totalSupply = 10 * (10 ** uint256(_decimals));
        mapping(address=>uint256) private _balances;
        mapping(address=>mapping(address=>uint256)) private _allowances;
    
        event Transfer(address indexed _from, address indexed _to, uint256 _value);
        event Approval(address indexed _owner, address indexed _spender, uint256 _value);
        function ERC20(){
            _balances[msg.sender] = _totalSupply;
        }
    
        function name() public view returns (string name){
            name = _name;
        }
    
        function symbol() public view returns (string symbol){
            symbol = _symbol;
        }
    
        function decimals() public view returns (uint8 decimals){
            decimals = _decimals;
        }
    
        function totalSupply() public view returns (uint256 totalSupply){
            totalSupply = _totalSupply;
        }
    
        function balanceOf(address _owner) public view returns (uint256 balance){
            balance = _balances[_owner];
        }
    
        function transfer(address _to, uint256 _value) public returns (bool success){
            require(_balances[msg.sender] >= _value);
            _balances[msg.sender] -= _value;
            _balances[_to] += _value;
            Transfer(msg.sender, _to, _value);
            success = true;
        }
    
        function transferFrom(address _from, address _to, uint256 _value) public returns (bool success){
            require(_balances[_from] >= _value);
            require(_allowances[_from][msg.sender] >= _value);
            _balances[_from] -= _value;
            _allowances[_from][msg.sender] -= _value;
            _balances[_to] += _value;
            Transfer(_from, _to, _value);
            success = true;
        }
    
        function approve(address _spender, uint256 _value) public returns (bool success){
            _allowances[msg.sender][_spender] = _value;
            Approval(msg.sender, _spender, _value);
            success = true;
        }
    
        function allowance(address _owner, address _spender) public view returns (uint256 remaining){
            remaining = _allowances[_owner][_spender];
        }
    }
    

    代码说明

    • 代币名称为 “MyToken”
    • 代币标识符为 “MYT”
    • 小数位数为3
    • 代币初始总供给为 10 * 1000 = 10000
    • 账户余额映射
    • 账户配额映射,这里是两层映射,例如账户A授信给账户B额度为value的配额
    • 代币转移事件,事件内容为:将账户_from的代币转移到账户_to中,金额为_valueindexed关键字是对事件的字段做索引,具体用途后面会说。
    • 配额许可事件,事件内容为:拥有者_owner许可给_spender配额,额度为_value
    • 构造方法,将代币的初始总供给都分配给合约的部署账户。合约的构造方法只在合约部署时执行一次。
    • 公共方法(合约外部可访问),返回合约名称。view关键子表示函数只查询状态变量,而不写入。函数的view关键字与constant等价。
    • 公共方法,返回合约标识符。
    • 公共方法,返回合约小数位。
    • 公共方法,返回合约总供给额。
    • 公共方法,查询账户_owner的账户余额。
    • 公共方法,将合约调用者的代币转移到账户_to中,金额为_value,转移成功返回true。
    • 验证合约调用者(msg.sender)账户余额是否大于等于_value
    • 合约调用者账户余额减去_value
    • 接收账户_to账户余额额加上_value
    • 发送Transfer()事件
    • 返回转移成功(true)
    • 公共方法,将账户_from的代币转移到账户_to中,金额为_value,转移成功返回true
    • 验证账户_from的代币余额是否大于等于_value
    • 验证账户_from给合约调用者msg.sender的配额是否大于等于_value
    • 账户_from的账户余额减去_value
    • 账户_from给合约调用者(msg.sender)的配额减去_value
    • 接收账户_to账户余额额加上_value
    • 发送Transfer()事件
    • 返回转移成功true
    • 公共方法,合约调用者批准给账户_spender配额,额度为_value,批准成功返回true
    • 合约调用者msg.sender对账户_spender的配额等于_value
    • 发送Approval()事件
    • 返回批准成功true
    • 公共方法,查询账户_owner批准给账户_spender的配额
    • 返回账户_owner批准给账户_spender的配额

    参考文档
    https://github.com/ConsenSys/Tokens/blob/master/contracts/eip20/EIP20Interface.sol
    https://github.com/ConsenSys/Tokens/blob/master/contracts/eip20/EIP20.sol

    相关文章

      网友评论

        本文标题:以太坊智能合约开发指南3(ERC-20代码实现)

        本文链接:https://www.haomeiwen.com/subject/iphwzxtx.html