LP分红、加池分红USDT、杀机器人、防巨鲸的智能合约代币开发教程
LP分红也叫加池子分红,就是说用户在去中心化交易所(博饼、Uni)添加流动性之后,可以获得一些分红。这个分红,可以是USDT,业额可以是Doge等等。而且这种分红,并不是简单的流动性分红,而是将分别的代币直接转到LP的地址。
从经济学模型的角度来说,加池分红的模式,可以鼓励更多地用户添加流动性,从而保持币价的稳定。但是这种智能合约应该如何实现?具体用到了哪些技术呢?今天就给大家简单介绍一下这种“LP”分红的合约开发教程。
1、合约机制
先确定一下合约的功能机制:买卖14%滑点,3%给加LP池子分红,4%分配10级推荐(分本币),1%营销钱包,1%备用拉盘,5%进入NFT盲盒。其中推荐分红的部分,1级0.48%,2级0.44%,3级0.42%,4-10级各0.38%。下面把方法定义写出来:
address private fundAddress;//营销钱包地址
address private marketAddress;//市场拉盘钱包地址
address private nftAddress;//NFT 质押挖矿地址uint256 private startTradeBlock;//开启交易的区块
mapping(address => bool) private _feeWhiteList;//交易税白名单
mapping(address => bool) private _blackList;//黑名单mapping(address => address) private _invitor;//邀请者,即上级
mapping(address => bool) private _swapPairList;//交易对地址列表
uint256 private numTokensSellToFund;//合约卖币换U条件阀值
uint256 private limitAmount;//限购数量
address usdtPair = swapFactory.createPair(address(this), usdt); _usdtPair = IERC20(usdtPair);//交易对地址
2、交易代码
交易扣税,from == _swapPairList 表示买入,to == _swapPairList 表示卖出
if (_swapPairList[from] || _swapPairList[to]) {
交易未开启,只允许手续费白名单加池子(合约部署者默认是白名单),加完池子就开启交易
if (0 == startTradeBlock) {
require(_feeWhiteList[from] || _feeWhiteList[to], “!Trading”);
startTradeBlock = block.number;
}
交易手续费先以本币的形式存到合约地址,达到触发条件,再换成USDT分配出去。
uint256 contractTokenBalance = balanceOf(address(this));
if (
contractTokenBalance >= numTokensSellToFund &&
!inSwap &&
_swapPairList[to]
) {
swapTokenForFund(numTokensSellToFund);
}
}
将本币兑换成USDT,存到合约,等待分红
function swapTokenForFund(uint256 tokenAmount) private lockTheSwap {
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = usdt;
_swapRouter.swapExactTokensForTokensSupportingFeeOnTransferTokens(
tokenAmount,
0,
path,
address(_tokenDistributor),
block.timestamp
);
Lp分红实现的方式
if (
from != address(this)
&& startTradeBlock > 0) {
processLP(500000);
}
}function _tokenTransfer(
address sender,
address recipient,
uint256 tAmount,
uint256 fee
) private {
_balances[sender] = _balances[sender] – tAmount;
uint256 feeAmount = tAmount * fee / 100;
uint256 taxAmount = feeAmount;
交易执行LP分红,使用 gas(500000) 单位 gasLimit 去执行LP分红,间隔 10 分钟分红一次
function processLP(uint256 gas) private {
if (progressLPBlock + 200 > block.number) {
return;
}
uint totalPair = _usdtPair.totalSupply();
if (0 == totalPair) {
return;
}IERC20 USDT = IERC20(usdt);
uint256 usdtBalance = USDT.balanceOf(address(this));
按照比例进行分配
IERC20 USDT = IERC20(usdt);
uint256 usdtBalance = USDT.balanceOf(address(_tokenDistributor));
USDT.transferFrom(address(_tokenDistributor), nftAddress, usdtBalance / 2);
USDT.transferFrom(address(_tokenDistributor), fundAddress, usdtBalance / 10);
USDT.transferFrom(address(_tokenDistributor), marketAddress, usdtBalance / 10);
USDT.transferFrom(address(_tokenDistributor), address(this), usdtBalance * 3 / 10);
}
3、十代分红
可以设置10个代数的上下级关系,用户向一个新的钱包地址进行转账,即可实现关系绑定。下级交易的手续费,按照比例分配给不同阶段的上级。
if (fee > 0) {
address current;
if (_swapPairList[sender]) {
current = recipient;
} else {
current = sender;
}
uint256 inviterAmount;
uint256 perInviteAmount = feeAmount * 4 / 14000;
for (uint256 i; i < 10; ++i) {
address inviter = _invitor[current];if (address(0) == inviter) {
break;
}
if (0 == i) {
inviterAmount = perInviteAmount * 120;
} else if (1 == i) {
inviterAmount = perInviteAmount * 110;
} else if (2 == i) {
inviterAmount = perInviteAmount * 105;
} else {
inviterAmount = perInviteAmount * 95;
}
feeAmount -= inviterAmount;
_takeTransfer(sender, inviter, inviterAmount);
current = inviter;
}
4、防巨鲸
为了防止巨鲸大批量买入和卖出,代码里也设置了防巨鲸机制,单次买卖限额和钱包最大持有量。
1)单钱包限制持有
if (!_swapPairList[to] && !_feeWhiteList[to]) {
require(limitAmount >= balanceOf(to), “exceed LimitAmount”);
}
2)最大限购总量的百分之一
limitAmount = _tTotal / 100;
}function symbol() external view override returns (string memory) {
return _symbol;
}function name() external view override returns (string memory) {
return _name;
}function decimals() external view override returns (uint8) {
return _decimals;
}function totalSupply() external view override returns (uint256) {
return _tTotal;
}function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}function transfer(address recipient, uint256 amount) public override returns (bool) {
_transfer(msg.sender, recipient, amount);
return true;
}function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}function approve(address spender, uint256 amount) public override returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
_transfer(sender, recipient, amount);
5、防机器人
为了防止机器人在开盘的瞬间抢跑买入,造成用户资产的损失,本代码也加入了防止机器人的功能,具体实现如下,杀0、1、2、3区块的机器人。机器人买入将直接加入黑名单:
if (block.number <= startTradeBlock + 3) {
if (!_swapPairList[to]) {
_blackList[to] = true;
}
}
综上,该套代码实现了LP分USDT、只允许白名单地址首次添加流动性、黑名单、杀机器人、十代邀请分红、买卖限额与钱包最大持有量等多种功能机制,是一套完整的多功能代币模型。如您想了解,或者开发类似功能的合约,欢迎联系:电报@btc6540,微信:btc6540