實現一個可管理、增發、兌換、凍結等高級功能的代幣

本文首發於深刻淺出區塊鏈社區
原文連接:實現一個可管理、增發、兌換、凍結等高級功能的代幣原文已更新,請讀者前往原文閱讀html

本文主要介紹代幣高級功能的實現: 代幣管理、代幣增發、代幣兌換、資產凍結、Gas自動補充。app

寫在前面

上一篇:一步步教你建立本身的數字貨幣(代幣)進行ICO中咱們實現一個最基本功能的代幣,本文將在上一遍文章的基礎上,講解若是添加更多的高級功能。函數

實現代幣的管理者

雖然區塊鏈是去中心化的,可是實現對代幣(合約)的管理,也在許多應用中有需求,爲了對代幣進行管理,首先須要給合約添加一個管理者。學習

咱們來看看若是實現,先建立一個owned合約。區塊鏈

contract owned {
        address public owner;

        function owned() {
            owner = msg.sender;
        }

        modifier onlyOwner {
            require(msg.sender == owner);
            _;
        }

        // 實現全部權轉移
        function transferOwnership(address newOwner) onlyOwner {
            owner = newOwner;
        }
    }

這個合約重要的是加入了一個函數修改器(Function Modifiers)onlyOwner,函數修改器是一個合約屬性,能夠被繼承,還能被重寫。它用於在函數執行前檢查某種前置條件。
關於函數修改器可進一步閱讀Solidity 教程系列10 - 徹底理解函數修改器測試

若是熟悉Python的同窗,會發現函數修改器的做用和Python的裝飾器很類似。

而後讓代幣合約繼承owned以擁有onlyOwner修改器,代碼以下:ui

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

代幣增發

實現代幣增發,代幣增發就如同央行印鈔票同樣,想必不少人都須要這樣的功能。this

給合約添加如下的方法:spa

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

注意onlyOwner修改器添加在函數末尾,這表示只有ower才能調用這用函數。
他的功能很簡單,就是給指定的帳戶增長代幣,同時增長總供應量。code

資產凍結

有時爲了監管的須要,須要實現凍結某些帳戶,凍結後,其資產仍在帳戶,可是不容許交易,之道解除凍結。
給合約添加如下的變量和方法(能夠添加到合約的任何地方,可是建議把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 = 1000000000000000000 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));
    }

代碼部署

高級功能完整代碼請前往個人小專欄, 項目的完整的部署方法參考上一篇,不一樣的是建立合約時須要預存餘額,如圖:

專欄已經有多篇文章介紹Remix Solidity IDE的使用,這裏就不一一截圖演示了,請你們本身測試驗證。

如何建立代幣發行代幣,如今也錄製了對應的視頻教程:經過代幣學以太坊智能合約開發
目前咱們也在招募體驗師,能夠點擊連接瞭解。

若是你在建立代幣的過程當中遇到問題,歡迎到個人知識星球提問,做爲星球成員福利,成員可加入區塊鏈技術付費交流羣。

參考文檔

深刻淺出區塊鏈 - 系統學習區塊鏈,打造最好的區塊鏈技術博客。

相關文章
相關標籤/搜索