美文网首页区块链技术与金融区块链研习社白话区块链
实现ERC-20Token的增发、冻结、兑换等功能

实现ERC-20Token的增发、冻结、兑换等功能

作者: 唠嗑008 | 来源:发表于2018-06-13 10:58 被阅读18次

    前言

    在之前的文章教你如何创建自己的数字货币中讲解了,如何创建自己的ERC-20 代币。而这一节是在上一节的基础上,增加一些关于Token的高级功能,比如:Token的增发、冻结、兑换等。

    1、首先,我们来看一个基本的owned合约

        contract owned {
            address public owner;
    
            function owned() {
                owner = msg.sender;
            }
    
            modifier onlyOwner {
                require(msg.sender == owner);
                _;
            }
    
            //所有权转移
            function transferOwnership(address newOwner) onlyOwner {
                owner = newOwner;
            }
        }
    

    这个合约唯一的功能就是转移当前合约的所有权。这里用到了一个函数修改器onlyOwner,,关于函数修改器,可以参考之前的文章【Solidity智能合约系列】10--函数修改器

    2、让代币合约继承owned,以拥有onlyOwner修改器

    contract MyToken is owned {
        function MyToken(
            uint256 initialSupply,
            string tokenName,
            uint8 decimalUnits,
            string tokenSymbol,
            address centralMinter
            ) {
            if(centralMinter != 0 ) owner = centralMinter;
        }
    }
    

    代币增发

    实现代币增发,代币增发就如同央行印钞票一样,想必很多人都需要这样的功能。

    给合约添加以下的方法:

    function mintToken(address target, uint256 mintedAmount) onlyOwner {
            balanceOf[target] += mintedAmount;
            totalSupply += mintedAmount;
            Transfer(0, owner, mintedAmount);
            Transfer(owner, target, mintedAmount);
        }
    

    注意onlyOwner修改器添加在函数末尾,这表示只有ower才能调用这用函数。
    功能很简单,就是给指定的账户增加代币,同时增加总供应量。

    资产冻结

    有时为了监管的需要,需要实现冻结某些账户,冻结后,其资产仍在账户,但是不允许交易,之道解除冻结。
    给合约添加以下的变量和方法(可以添加到合约的任何地方,但是建议把mapping加到和其他mapping一起,event也是如此):

    mapping (address => bool) public frozenAccount;
    event FrozenFunds(address target, bool frozen);
    
    function freezeAccount(address target, bool freeze) onlyOwner {
        frozenAccount[target] = freeze;
        FrozenFunds(target, freeze);
    }
    

    单单以上的代码还无法冻结,需要把他加入到transfer函数中才能真正生效,因此修改transfer函数。这样在转账前,对发起交易的账号做一次检查,只有不是被冻结的账号才能转账。

    function transfer(address _to, uint256 _value) {
            require(!frozenAccount[msg.sender]);
            ...
    }
    

    代币买卖(兑换)

    可以自己的货币中实现代币与其他数字货币(ether 或其他tokens)的兑换机制。有了这个功能,我们的合约就可以在一买一卖中赚利润了。

    先来设置下买卖价格

    uint256 public sellPrice;
    uint256 public buyPrice;
    
    function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner {
        sellPrice = newSellPrice;
        buyPrice = newBuyPrice;
    }
    

    setPrices()添加了onlyOwner修改器,注意买卖的价格单位是wei(最小的货币单位: 1 eth = 10^18 wei)

    添加来添加买卖函数:

    function buy() payable returns (uint amount){
        amount = msg.value / buyPrice;                    // calculates the amount
        require(balanceOf[this] >= amount);               // checks if it has enough to sell
        balanceOf[msg.sender] += amount;                  // adds the amount to buyer's balance
        balanceOf[this] -= amount;                        // subtracts amount from seller's balance
        Transfer(this, msg.sender, amount);               // execute an event reflecting the change
        return amount;                                    // ends function and returns
    }
    
    function sell(uint amount) returns (uint revenue){
        require(balanceOf[msg.sender] >= amount);         // checks if the sender has enough to sell
        balanceOf[this] += amount;                        // adds the amount to owner's balance
        balanceOf[msg.sender] -= amount;                  // subtracts the amount from seller's balance
        revenue = amount * sellPrice;
        msg.sender.transfer(revenue);                     // sends ether to the seller: it's important to do this last to prevent recursion attacks
        Transfer(msg.sender, this, amount);               // executes an event reflecting on the change
        return revenue;                                   // ends function and returns
    }
    

    加入了买卖功能后,要求我们在创建合约时发送足够的以太币,以便合约有能力回购市面上的代币,否则合约将破产,用户没法先合约卖代币。

    实现Gas的自动补充

    以太坊中的交易时需要gas(支付给矿工的费用,费用以ether来支付)。而如果用户没有以太币,只有代币的情况(或者我们想向用户隐藏以太坊的细节),就需要自动补充gas的功能。这个功能将使我们代币更加好用。

    自动补充的逻辑是这样了,在执行交易之前,我们判断用户的余额(用来支付矿工的费用),如果用户的余额非常少(低于某个阈值时)可能影响到交易进行,合约自动售出一部分代币来补充余额,以帮助用户顺利完成交易。

    先来设定余额阈值:

    uint minBalanceForAccounts;
    
        function setMinBalance(uint minimumBalanceInFinney) onlyOwner {
             minBalanceForAccounts = minimumBalanceInFinney * 1 finney;
        }
    

    finney 是货币单位 1 finney = 0.001eth
    然后交易中加入对用户的余额的判断。

    function transfer(address _to, uint256 _value) {
        ...
        if(msg.sender.balance < minBalanceForAccounts)
            sell((minBalanceForAccounts - msg.sender.balance) / sellPrice);
        if(_to.balance<minBalanceForAccounts)   // 可选,让接受者也补充余额,以便接受者使用代币。
            _to.send(sell((minBalanceForAccounts - _to.balance) / sellPrice));
    }
    

    未完,待续。。。

    参考:
    https://ethereum.org/token

    相关文章

      网友评论

        本文标题:实现ERC-20Token的增发、冻结、兑换等功能

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