Loading...

实现一个可管理、增发、兑换、冻结等高级功能的代币

实现一个可管理、增发、兑换、冻结等高级功能的Tokens

电报联系方式

实现代币的管理者

尽管区块链技术强调去中心化,但在许多应用中,有时仍需要有效地管理代币或智能Contracts。为了实现这种代币管理,首要步骤通常是向智能合约添加一个管理员角色或实体。

我们来看看如果实现,先创建一个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,函数修改器是一个合约属性,可以被继承,还能被重写。它用于在函数执行前检查某种前置条件。

然后让代币合约继承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) external 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) external onlyOwner {
frozenAccount[target] = freeze;
FrozenFunds(target, freeze);
}

单单以上的代码还无法冻结,需要把他加入到transfer函数中才能真正生效,因此修改transfer函数

function transfer(address _to, uint256 _value) public {
require(!frozenAccount[msg.sender]);

}

这样在转账前,对发起交易的账号做一次检查,只有不是被冻结的账号才能转账。

代币买卖(兑换)

为了实现代币与其他数字货币(如以太币或其他代币)的兑换机制,首先需要建立买卖价格。在合约中,您可以采取以下方式来设置买卖价格,以便进行利润赚取

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)支付。然而,有些用户可能只拥有代币而没有足够的以太币,或者他们可能希望隐藏以太坊底层细节,因此需要一种自动补充矿工费用的功能,以提高代币的便利性。

自动补充矿工费用的逻辑如下:在执行交易之前,智能合约会检查用户的以太币余额。如果用户的以太币余额非常低,可能会妨碍交易的进行,那么合约将自动出售一部分代币来补充用户的以太币余额,以确保用户能够顺利完成交易。这个功能旨在提高用户体验,使代币的使用更加便捷。

开发联系:DEXDAO

 

 

 

© 版权声明

Related posts

No comments

No comments...