一、说明
ERC314(BSC314,ARB314,BASE314)协议中为了被动提升代币的价格,增加了燃烧底池的功能。通过设置底池燃烧的比率,冷却时间来单边燃烧底池中的本币,从而实现相对WETH(ETH, BNB)的价格比例升高,从而实现被动提升代币价格的功能。
资金池的单边燃烧同时要配合正确的价格计算方式,才能在代币买卖过程中正确兑换出获取的代币或者WETH数量。其实,ERC314协议计算代币价格的原理和常规的DEX(uniswap,pancakeswap,sushiswap)是类似的,都是通过恒定常量计算公式a * b = k计算得到,即任意时刻两种代币折合成usdt的价值永远是等值的。
二、ERC314, BSC314协议核心代码实现
1.通过如下方式实现标准转账,类似于ERC20协议标准中的super._transfer
function _transfer(address from, address to, uint256 value) internal virtual {
if (to != address(0)) {
require(lastTransaction[msg.sender] != block.number, "You can't make two transactions in the same block");
lastTransaction[msg.sender] = uint32(block.number);
require(block.timestamp >= _lastTxTime[msg.sender] + 60, 'Sender must wait for cooldown');
_lastTxTime[msg.sender] = block.timestamp;
}
require(_balances[from] >= value, 'ERC20: transfer amount exceeds balance');
unchecked {
_balances[from] = _balances[from] - value;
}
if (to == address(0)) {
unchecked {
_totalSupply -= value;
}
} else {
unchecked {
_balances[to] += value;
}
}
emit Transfer(from, to, value);
}
该方法负责代币的常规转账,不附加税费,税费通常是在buy,sell方法中加入。同时在买卖方法中增加需要支付的bnb手续费。
2. 通过如下方式计算线上交易时的代币价值
function getAmountOut(uint256 value, bool _buy) public view returns (uint256) {
(uint256 reserveETH, uint256 reserveToken) = getReserves();
if (_buy) {
return (value * reserveToken) / (reserveETH + value);
} else {
return (value * reserveETH) / (reserveToken + value);
}
}
通过预先买卖的代币或者WETH计入到资金池中,然后再核算可以从资金池中置换出的另外一个代币的数量,这样可以避免dex的滑点设置功能。
3. 通过buy方法实现代币的买入功能
function buy() internal {
require(tradingEnable, 'Trading not enable');
uint256 msgValue = msg.value;
uint256 feeValue = msgValue * 450 / 10000;
uint256 swapValue = msgValue - feeValue;
feeReceiver.transfer(feeValue);
uint256 token_amount = (swapValue * _balances[address(this)]) / (address(this).balance);
if (maxWalletEnable) {
require(token_amount + _balances[msg.sender] <= _maxWallet, 'Max wallet exceeded');
}
uint256 user_amount = (token_amount / 10000) * 9950;
uint256 burn_amount = token_amount - user_amount;
_transfer(address(this), msg.sender, user_amount);
_transfer(address(this), address(0), burn_amount);
emit Swap(msg.sender, swapValue, 0, 0, user_amount);
}
在buy()方法中收取相应数量的WETH作为交易gas费用,同时设置交易的税费,增加税费的分发渠道(常规渠道:燃烧,回流底池,分红、回流营销钱包等)
4. 通过sell()实现代币的卖出功能
function sell(uint256 sell_amount) internal {
require(tradingEnable, 'Trading not enable');
uint256 swap_amount = (sell_amount / 10000) * 9950;
uint256 burn_amount = sell_amount - swap_amount;
uint256 ethAmount = (swap_amount * address(this).balance) / (_balances[address(this)] + swap_amount);
require(ethAmount > 0, 'Sell amount too low');
require(address(this).balance >= ethAmount, 'Insufficient ETH in reserves');
_transfer(msg.sender, address(this), swap_amount);
_transfer(msg.sender, address(0), burn_amount);
uint256 feeValue = ethAmount * 450 / 10000;
payable(feeReceiver).transfer(feeValue);
payable(msg.sender).transfer(ethAmount - feeValue);
if (
lpBurnEnabled &&
block.timestamp >= lastLpBurnTime + lpBurnFrequency
) {
autoBurnLiquidityPairTokens();
}
emit Swap(msg.sender, 0, sell_amount, ethAmount - feeValue, 0);
}
在卖单中触发底池的单边燃烧功能,同时卖单中设置交易的gas费用和代币线上买卖的税费。
5. 单边燃烧资金池核心代码实现
function autoBurnLiquidityPairTokens() internal returns (bool) {
lastLpBurnTime = block.timestamp;
// get balance of liquidity pair
uint256 liquidityPairBalance = balanceOf(address(this));
// calculate amount to burn
uint256 amountToBurn = liquidityPairBalance * (percentForLPBurn) / (
10000
);
address from = address(this);
address to = address(0xdead);
// pull tokens from pancakePair liquidity and move to dead address permanently`
if (amountToBurn > 0) {
_balances[from] -= amountToBurn;
_balances[to] += amountToBurn;
emit Transfer(from, to, amountToBurn);
}
emit AutoNukeLP(
liquidityPairBalance,
amountToBurn,
block.timestamp
);
return true;
}
区别于dex(uniswap,pancakeswap)单边燃烧资金池的情况,ERC314(bsc314,arb314,base314)单边燃烧资金池不需要提前获得资金池资源锁对象_lock() , 同时不存在_swap和_lock函数的资源锁争用死锁问题。因此可以在ERC314(bsc314,arb314,base314)中直接单边燃烧资金池或者修改资金池余额以实现从资金池燃烧通缩,分红,跨池兑换等功能。
至此,完成ERC314/BSC314协议实时燃烧资金池同步计算买卖价格的核心代码实现所有操作流程。
声明:本网站所有相关资料如有侵权请联系站长删除,资料仅供用户学习及研究之用,不构成任何投资建议!