最新內容會更新在主站深入淺出區塊鏈社區
原文鏈接:實現一個可管理、增發、兌換、凍結等高級功能的代幣
本文主要介紹代幣高級功能的實現: 代幣管理、代幣增發、代幣兌換、資產凍結、Gas自動補充。
寫在前面
在上一篇:一步步教你創建自己的數字貨幣(代幣)進行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,函數修改器是一個合約屬性,可以被繼承,還能被重寫。它用於在函數執行前檢查某種前置條件。
如果熟悉Python的同學,會發現函數修改器的作用和Python的裝飾器很相似。
然后讓代幣合約繼承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 = 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的使用,這里就不一一截圖演示了,請大家自己測試驗證。
如果你在創建代幣的過程中遇到問題,歡迎到我的知識星球提問。
參考文檔
如何創建代幣發行代幣,現在也錄制了對應的視頻教程:通過代幣學以太坊智能合約開發
目前我們也在招募體驗師,可以點擊鏈接了解。
深入淺出區塊鏈 - 系統學習區塊鏈,打造最好的區塊鏈技術博客。