前言
在之前的文章教你如何創(chuàng)建自己的數(shù)字貨幣中講解了,如何創(chuàng)建自己的ERC-20 代幣。而這一節(jié)是在上一節(jié)的基礎(chǔ)上,增加一些關(guān)于Token的高級(jí)功能,比如:Token的增發(fā)、凍結(jié)、兌換等。
1、首先,我們來(lái)看一個(gè)基本的owned合約
contract owned {
address public owner;
function owned() {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
//所有權(quán)轉(zhuǎn)移
function transferOwnership(address newOwner) onlyOwner {
owner = newOwner;
}
}
這個(gè)合約唯一的功能就是轉(zhuǎn)移當(dāng)前合約的所有權(quán)。這里用到了一個(gè)函數(shù)修改器onlyOwner,,關(guān)于函數(shù)修改器,可以參考之前的文章【Solidity智能合約系列】10--函數(shù)修改器
2、讓代幣合約繼承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ù)。這樣在轉(zhuǎn)賬前,對(duì)發(fā)起交易的賬號(hào)做一次檢查,只有不是被凍結(jié)的賬號(hào)才能轉(zhuǎn)賬。
function transfer(address _to, uint256 _value) {
require(!frozenAccount[msg.sender]);
...
}
代幣買(mǎi)賣(mài)(兌換)
可以自己的貨幣中實(shí)現(xiàn)代幣與其他數(shù)字貨幣(ether 或其他tokens)的兌換機(jī)制。有了這個(gè)功能,我們的合約就可以在一買(mǎi)一賣(mài)中賺利潤(rùn)了。
先來(lái)設(shè)置下買(mǎi)賣(mài)價(jià)格
uint256 public sellPrice;
uint256 public buyPrice;
function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
setPrices()添加了onlyOwner修改器,注意買(mǎi)賣(mài)的價(jià)格單位是wei(最小的貨幣單位: 1 eth = 10^18 wei)
添加來(lái)添加買(mǎi)賣(mài)函數(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
}
加入了買(mǎi)賣(mài)功能后,要求我們?cè)趧?chuàng)建合約時(shí)發(fā)送足夠的以太幣,以便合約有能力回購(gòu)市面上的代幣,否則合約將破產(chǎn),用戶沒(méi)法先合約賣(mài)代幣。
實(shí)現(xiàn)Gas的自動(dòng)補(bǔ)充
以太坊中的交易時(shí)需要gas(支付給礦工的費(fèi)用,費(fèi)用以ether來(lái)支付)。而如果用戶沒(méi)有以太幣,只有代幣的情況(或者我們想向用戶隱藏以太坊的細(xì)節(jié)),就需要自動(dòng)補(bǔ)充gas的功能。這個(gè)功能將使我們代幣更加好用。
自動(dòng)補(bǔ)充的邏輯是這樣了,在執(zhí)行交易之前,我們判斷用戶的余額(用來(lái)支付礦工的費(fèi)用),如果用戶的余額非常少(低于某個(gè)閾值時(shí))可能影響到交易進(jìn)行,合約自動(dòng)售出一部分代幣來(lái)補(bǔ)充余額,以幫助用戶順利完成交易。
先來(lái)設(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));
}
未完,待續(xù)。。。