本文首发于深刻浅出区块链社区 原文连接:实现一个可管理、增发、兑换、冻结等高级功能的代币原文已更新,请读者前往原文阅读app
本文主要介绍代币高级功能的实现: 代币管理、代币增发、代币兑换、资产冻结、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,函数修改器是一个合约属性,能够被继承,还能被重写。它用于在函数执行前检查某种前置条件。 关于函数修改器可进一步阅读Solidity 教程系列10 - 彻底理解函数修改器ui
若是熟悉Python的同窗,会发现函数修改器的做用和Python的装饰器很类似。this
而后让代币合约继承owned以拥有onlyOwner修改器,代码以下:code
contract MyToken is owned { function MyToken( uint256 initialSupply, string tokenName, uint8 decimalUnits, string tokenSymbol, address centralMinter ) { if(centralMinter != 0 ) owner = centralMinter; } }
实现代币增发,代币增发就如同央行印钞票同样,想必不少人都须要这样的功能。blog
给合约添加如下的方法:继承
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(支付给矿工的费用,费用以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的使用,这里就不一一截图演示了,请你们本身测试验证。
若是你在建立代币的过程当中遇到问题,欢迎到个人**知识星球**提问,做为星球成员福利,成员可加入区块链技术付费交流群。
☛ 深刻浅出区块链 - 系统学习区块链,打造最好的区块链技术博客。
☛ 个人知识星球为各位解答区块链技术问题,欢迎加入讨论。
☛ 关注公众号“深刻浅出区块链技术”第一时间获取区块链技术信息。