In the smart contract, how to automatically add the pool?

Many of the more popular smart contracts today include transfers or transaction fees. A few percent of coins will automatically add to the pot. How is this realized? Now I will introduce to you the realization mode of automatically adding to the pot. If you are interested, you can add me to communicate with wx: 54516204.
First, define how many coins are deposited in a contract before starting to add to the pot.
_max=xxxx*10**18
Then in the contract, define the following interfaces.
IUniswapV2Factory
IUniswapV2Pair
IUniswapV2Router01
IUniswapV2Router02
Then in the main contract
//Pancake Swap V2 address
IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x6401cbB0DA6983C53138007AC8e756 E22Df8ddC7);
// 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;
define routing information.
The following is the main procedure of adding a pool
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);
 }

Guess you like

Origin blog.csdn.net/weixin_38532278/article/details/123715378