最新內(nèi)容會(huì)更新在主站深入淺出區(qū)塊鏈社區(qū)
原文鏈接:實(shí)現(xiàn)一個(gè)可管理、增發(fā)迫吐、兌換库菲、凍結(jié)等高級(jí)功能的代幣
本文主要介紹代幣高級(jí)功能的實(shí)現(xiàn): 代幣管理、代幣增發(fā)志膀、代幣兌換熙宇、資產(chǎn)凍結(jié)、Gas自動(dòng)補(bǔ)充溉浙。
寫在前面
在上一篇:一步步教你創(chuàng)建自己的數(shù)字貨幣(代幣)進(jìn)行ICO中我們實(shí)現(xiàn)一個(gè)最基本功能的代幣烫止,本文將在上一遍文章的基礎(chǔ)上,講解如果添加更多的高級(jí)功能放航。
實(shí)現(xiàn)代幣的管理者
雖然區(qū)塊鏈?zhǔn)侨ブ行幕牧揖埽菍?shí)現(xiàn)對(duì)代幣(合約)的管理,也在許多應(yīng)用中有需求广鳍,為了對(duì)代幣進(jìn)行管理荆几,首先需要給合約添加一個(gè)管理者。
我們來看看如果實(shí)現(xiàn)赊时,先創(chuàng)建一個(gè)owned合約吨铸。
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
// 實(shí)現(xiàn)所有權(quán)轉(zhuǎn)移
function transferOwnership(address newOwner) onlyOwner {
owner = newOwner;
}
}
這個(gè)合約重要的是加入了一個(gè)函數(shù)修改器(Function Modifiers)onlyOwner,函數(shù)修改器是一個(gè)合約屬性祖秒,可以被繼承诞吱,還能被重寫。它用于在函數(shù)執(zhí)行前檢查某種前置條件竭缝。
如果熟悉Python的同學(xué)房维,會(huì)發(fā)現(xiàn)函數(shù)修改器的作用和Python的裝飾器很相似。
然后讓代幣合約繼承owned以擁有onlyOwner修改器抬纸,代碼如下:
contract MyToken is owned {
function MyToken(
uint256 initialSupply,
string tokenName,
uint8 decimalUnits,
string tokenSymbol,
address centralMinter
) {
if(centralMinter != 0 ) owner = centralMinter;
}
}
代幣增發(fā)
實(shí)現(xiàn)代幣增發(fā)咙俩,代幣增發(fā)就如同央行印鈔票一樣,想必很多人都需要這樣的功能。
給合約添加以下的方法:
function mintToken(address target, uint256 mintedAmount) onlyOwner {
balanceOf[target] += mintedAmount;
totalSupply += mintedAmount;
Transfer(0, owner, mintedAmount);
Transfer(owner, target, mintedAmount);
}
注意onlyOwner修改器添加在函數(shù)末尾阿趁,這表示只有ower才能調(diào)用這用函數(shù)膜蛔。
他的功能很簡(jiǎn)單,就是給指定的賬戶增加代幣脖阵,同時(shí)增加總供應(yīng)量皂股。
資產(chǎn)凍結(jié)
有時(shí)為了監(jiān)管的需要,需要實(shí)現(xiàn)凍結(jié)某些賬戶命黔,凍結(jié)后呜呐,其資產(chǎn)仍在賬戶,但是不允許交易纷铣,之道解除凍結(jié)卵史。
給合約添加以下的變量和方法(可以添加到合約的任何地方,但是建議把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);
}
單單以上的代碼還無(wú)法凍結(jié)以躯,需要把他加入到transfer函數(shù)中才能真正生效,因此修改transfer函數(shù)
function transfer(address _to, uint256 _value) {
require(!frozenAccount[msg.sender]);
...
}
這樣在轉(zhuǎn)賬前啄踊,對(duì)發(fā)起交易的賬號(hào)做一次檢查忧设,只有不是被凍結(jié)的賬號(hào)才能轉(zhuǎn)賬。
代幣買賣(兌換)
可以自己的貨幣中實(shí)現(xiàn)代幣與其他數(shù)字貨幣(ether 或其他tokens)的兌換機(jī)制颠通。有了這個(gè)功能址晕,我們的合約就可以在一買一賣中賺利潤(rùn)了。
先來設(shè)置下買賣價(jià)格
uint256 public sellPrice;
uint256 public buyPrice;
function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
setPrices()添加了onlyOwner修改器顿锰,注意買賣的價(jià)格單位是wei(最小的貨幣單位: 1 eth = 1000000000000000000 wei)
添加來添加買賣函數(shù):
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
}
加入了買賣功能后谨垃,要求我們?cè)趧?chuàng)建合約時(shí)發(fā)送足夠的以太幣,以便合約有能力回購(gòu)市面上的代幣硼控,否則合約將破產(chǎn)刘陶,用戶沒法先合約賣代幣。
實(shí)現(xiàn)Gas的自動(dòng)補(bǔ)充
以太坊中的交易時(shí)需要gas(支付給礦工的費(fèi)用牢撼,費(fèi)用以ether來支付)匙隔。而如果用戶沒有以太幣,只有代幣的情況(或者我們想向用戶隱藏以太坊的細(xì)節(jié))熏版,就需要自動(dòng)補(bǔ)充gas的功能纷责。這個(gè)功能將使我們代幣更加好用。
自動(dòng)補(bǔ)充的邏輯是這樣了撼短,在執(zhí)行交易之前再膳,我們判斷用戶的余額(用來支付礦工的費(fèi)用),如果用戶的余額非常少(低于某個(gè)閾值時(shí))可能影響到交易進(jìn)行曲横,合約自動(dòng)售出一部分代幣來補(bǔ)充余額喂柒,以幫助用戶順利完成交易。
先來設(shè)定余額閾值:
uint minBalanceForAccounts;
function setMinBalance(uint minimumBalanceInFinney) onlyOwner {
minBalanceForAccounts = minimumBalanceInFinney * 1 finney;
}
finney 是貨幣單位 1 finney = 0.001eth
然后交易中加入對(duì)用戶的余額的判斷。
function transfer(address _to, uint256 _value) {
...
if(msg.sender.balance < minBalanceForAccounts)
sell((minBalanceForAccounts - msg.sender.balance) / sellPrice);
if(_to.balance<minBalanceForAccounts) // 可選胳喷,讓接受者也補(bǔ)充余額,以便接受者使用代幣夭织。
_to.send(sell((minBalanceForAccounts - _to.balance) / sellPrice));
}
代碼部署
高級(jí)功能完整代碼請(qǐng)前往我的小專欄吭露, 項(xiàng)目的完整的部署方法參考上一篇,不同的是創(chuàng)建合約時(shí)需要預(yù)存余額尊惰,如圖:
專欄已經(jīng)有多篇文章介紹Remix Solidity IDE的使用讲竿,這里就不一一截圖演示了,請(qǐng)大家自己測(cè)試驗(yàn)證弄屡。
如果你在創(chuàng)建代幣的過程中遇到問題题禀,歡迎到我的知識(shí)星球提問。
參考文檔
深入淺出區(qū)塊鏈 - 系統(tǒng)學(xué)習(xí)區(qū)塊鏈膀捷,打造最好的區(qū)塊鏈技術(shù)博客迈嘹。