智能合约中,是如何做到自动加池子的?

今天比较流行的智能合约中,很多都是带转帐或者买卖收费的。有百分之几的币,会自动加底池,这又是如何实现的呢,现在我给大家介绍一下自动加底池的实现模式。有兴趣的,可以加我wx交流:54516204.
首先,定义一个合约中,沉淀多少个币才开始加底池。
_max=xxxx*10**18
然后在合约中,定义以下几个接口。
IUniswapV2Factory
IUniswapV2Pair
IUniswapV2Router01
IUniswapV2Router02
然后在主合约中
//Pancake Swap V2 address
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x6401cbB0DA6983C53138007AC8e756E22Df8ddC7);
// Create a uniswap pair for this new token
uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
.createPair(address(this), _uniswapV2Router.WETH());
// set the rest of the contract variables
uniswapV2Router = _uniswapV2Router;
定义一下路由信息。
以下就是加池子的主要程序了
function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
// 1/2 balance is sent to the marketing wallet, 1/2 is added to the liquidity pool
uint256 marketingTokenBalance = contractTokenBalance.div(2);
uint256 liquidityTokenBalance = contractTokenBalance.sub(marketingTokenBalance);

     // Split the token balance to be liquified into halves, this is so we can deposit the same amount
     // of BNB and Harold into the LP
     uint256 tokenBalanceToLiquifyAsBNB = liquidityTokenBalance.div(2);
     uint256 tokenBalanceToLiquify = liquidityTokenBalance.sub(tokenBalanceToLiquifyAsBNB);

     // capture the contract's current BNB balance.
     // this is so that we can capture exactly the amount of BNB that the
     // swap creates, and not make the liquidity event include any BNB that
     // has been manually sent to the contract
     uint256 initialBalance = address(this).balance;

     // 75% of the balance will be converted into BNB
     uint256 tokensToSwapToBNB = tokenBalanceToLiquifyAsBNB.add(marketingTokenBalance);

     // swap tokens for BNB
     swapTokensForEth(tokensToSwapToBNB);

     // Total BNB that has been swapped
     uint256 bnbSwapped = address(this).balance.sub(initialBalance);

     // BNB to liquify is 25% of the total token balance or 33% of the BNB that has already been liquified
     uint256 bnbToLiquify = bnbSwapped.div(3);

     // Add liquidity to pancake swap
     addLiquidity(tokenBalanceToLiquify, bnbToLiquify);

     emit SwapAndLiquify(tokenBalanceToLiquifyAsBNB, bnbToLiquify, tokenBalanceToLiquify);

     // The remaining BNB balance is to be donated to the marketing wallet
     uint256 marketingBNBToDonate = bnbSwapped.sub(bnbToLiquify);

     // Transfer the BNB to the marketing wallet
     _marketingAddress.transfer(marketingBNBToDonate);
     emit DonateToMarketing(marketingBNBToDonate);
 }

猜你喜欢

转载自blog.csdn.net/weixin_38532278/article/details/123715378