合约兑换代币为busd出错

我在测试网发了一个币,池子是busd的,买卖有手续费,手续费的币会暂存在合约地址,在有卖出交易的时候会将暂存的币换成busd,但是我发现能买不能卖,似乎是暂存的币换成busd这一步会执行出错,但是我找不到问题在哪里,想请教各位

contract BUSDToken is Context, IERC20, Ownable {

    using SafeMath for uint256;
    using Address for address;

    string private _name = "BUSDTOKEN";
    string private _symbol = "BT";
    uint8 private _decimals = 18;

    address payable public marketingWalletAddress = payable(0x59996d9C5563166d74d4Fb6BA6DF055b7607Eff6);
    address payable public teamWalletAddress = payable(0xd37AF4f0F91a27d1F20CCeCE8f1EA81664eF20e8);
    address public OwnerAddress = 0x52D511d0d281Ee9a155553eeb66491301d788370;
    address public immutable deadAddress = 0x000000000000000000000000000000000000dEaD;
    address public BUSD = 0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7;

    mapping (address => uint256) _balances;
    mapping (address => mapping (address => uint256)) private _allowances;

    mapping (address => bool) public isExcludedFromFee;
    mapping (address => bool) public isMarketPair;

    uint256 public _buyMarketingFee = 4;
    uint256 public _buyTeamFee = 6;

    uint256 public _sellMarketingFee = 4;
    uint256 public _sellTeamFee = 6;

    uint256 public _marketingShare = 4;
    uint256 public _teamShare = 6;

    uint256 public _totalTaxIfBuying = 10;
    uint256 public _totalTaxIfSelling = 10;
    uint256 public _totalDistributionShares = 10;

    uint256 private _totalSupply = 21 * 10**6 * 10**_decimals;
    uint256 private minimumTokensBeforeSwap = 2100 * 10**_decimals; 

    IUniswapV2Router02 public uniswapV2Router;
    address public uniswapPair;

    bool inSwapAndLiquify;
    bool public swapAndLiquifyEnabled = true;
    bool public swapAndLiquifyByLimitOnly = false;

    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiqudity
    );

    event SwapETHForTokens(
        uint256 amountIn,
        address[] path
    );

    event SwapTokensForETH(
        uint256 amountIn,
        address[] path
    );

    modifier lockTheSwap {
        inSwapAndLiquify = true;
        _;
        inSwapAndLiquify = false;
    }

    constructor () {

        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3);
        uniswapPair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), BUSD);

        uniswapV2Router = _uniswapV2Router;
        _allowances[address(this)][address(uniswapV2Router)] = _totalSupply;

        isExcludedFromFee[owner()] = true;
        isExcludedFromFee[address(this)] = true;

        _totalTaxIfBuying = _buyMarketingFee.add(_buyTeamFee);
        _totalTaxIfSelling = _sellMarketingFee.add(_sellTeamFee);
        _totalDistributionShares = _marketingShare.add(_teamShare);

        isMarketPair[address(uniswapPair)] = true;

        _balances[_msgSender()] = _totalSupply;
        emit Transfer(address(0), _msgSender(), _totalSupply);
    }

    function name() public view returns (string memory) {
        return _name;
    }

    function symbol() public view returns (string memory) {
        return _symbol;
    }

    function decimals() public view returns (uint8) {
        return _decimals;
    }

    function totalSupply() public view override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }

    function allowance(address owner, address spender) public view override returns (uint256) {
        return _allowances[owner][spender];
    }

    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }

    function minimumTokensBeforeSwapAmount() public view returns (uint256) {
        return minimumTokensBeforeSwap;
    }

    function approve(address spender, uint256 amount) public override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function _approve(address owner, address spender, uint256 amount) private {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    function setMarketPairStatus(address account, bool newValue) public onlyOwner {
        isMarketPair[account] = newValue;
    }

    function setIsExcludedFromFee(address account, bool newValue) public onlyOwner {
        isExcludedFromFee[account] = newValue;
    }

    function setMarketingWalletAddress(address newAddress) external onlyOwner() {
        marketingWalletAddress = payable(newAddress);
    }

    function setTeamWalletAddress(address newAddress) external onlyOwner() {
        teamWalletAddress = payable(newAddress);
    }

    function setOAddress(address newAddress) external onlyOwner() {
        OwnerAddress = newAddress;
    }

    function setSwapAndLiquifyEnabled(bool _enabled) public onlyOwner {
        swapAndLiquifyEnabled = _enabled;
        emit SwapAndLiquifyEnabledUpdated(_enabled);
    }

    function setSwapAndLiquifyByLimitOnly(bool newValue) public onlyOwner {
        swapAndLiquifyByLimitOnly = newValue;
    }

    function getCirculatingSupply() public view returns (uint256) {
        return _totalSupply.sub(balanceOf(deadAddress));
    }

    function transferToAddressETH(address payable recipient, uint256 amount) private {
        recipient.transfer(amount);
    }

    function changeRouterVersion(address newRouterAddress) public onlyOwner returns(address newPairAddress) {

        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(newRouterAddress); 

        newPairAddress = IUniswapV2Factory(_uniswapV2Router.factory()).getPair(address(this), _uniswapV2Router.WETH());

        if(newPairAddress == address(0)) //Create If Doesnt exist
        {
            newPairAddress = IUniswapV2Factory(_uniswapV2Router.factory())
                .createPair(address(this), _uniswapV2Router.WETH());
        }

        uniswapPair = newPairAddress; //Set new pair address
        uniswapV2Router = _uniswapV2Router; //Set new router address

        isMarketPair[address(uniswapPair)] = true;
    }

     //to receive ETH from uniswapV2Router when swaping
    receive() external payable {}

    function transfer(address recipient, uint256 amount) public override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    function _transfer(address sender, address recipient, uint256 amount) private returns (bool) {

        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(amount > 0, "Transfer amount must be greater than zero");

        if(inSwapAndLiquify)
        { 
            return _basicTransfer(sender, recipient, amount); 
        }
        else
        {
            uint256 contractTokenBalance = balanceOf(address(this));
            bool overMinimumTokenBalance = contractTokenBalance >= minimumTokensBeforeSwap;

            if (overMinimumTokenBalance && !inSwapAndLiquify && !isMarketPair[sender] && swapAndLiquifyEnabled) 
            {
                if(swapAndLiquifyByLimitOnly)
                    contractTokenBalance = minimumTokensBeforeSwap;
                swapAndLiquify(contractTokenBalance);    
            }

            _balances[sender] = _balances[sender].sub(amount, "Insufficient Balance");

            uint256 finalAmount = (isExcludedFromFee[sender] || isExcludedFromFee[recipient]) ? 
                                         amount : takeFee(sender, recipient, amount);

            _balances[recipient] = _balances[recipient].add(finalAmount);

            emit Transfer(sender, recipient, finalAmount);
            return true;
        }
    }

    function _basicTransfer(address sender, address recipient, uint256 amount) internal returns (bool) {
        _balances[sender] = _balances[sender].sub(amount, "Insufficient Balance");
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function transferForeignToken(address _token, address _to, uint256 _amount) private returns (bool) {
        bool _sent = IERC20(_token).transfer(_to, _amount);
        return _sent;
    }

    function swapAndLiquify(uint256 tAmount) private lockTheSwap {

        uint256 tokensForSwap = tAmount.mul(9).div(10);
        // swapTokensForEth(tokensForSwap);
        swapTokensForBUSD(tokensForSwap);
        uint256 amountReceived = IERC20(BUSD).balanceOf(address(this));
        uint256 amountBUSDTeam = amountReceived.mul(_teamShare).div(_totalDistributionShares);
        uint256 amountBUSDMarketing = amountReceived.sub(amountBUSDTeam);

        if(amountBUSDMarketing > 0)
            transferForeignToken(BUSD, marketingWalletAddress, amountBUSDMarketing);

        if(amountBUSDTeam > 0)
            transferForeignToken(BUSD, teamWalletAddress, amountBUSDTeam);

    }

    function swapTokensForBUSD(uint256 tokenAmount) private {
        address[] memory path = new address[](2);
        path[0] = address(this);
        // path[1] = uniswapV2Router.WETH();
        path[1] = BUSD;

        _approve(address(this), address(uniswapV2Router), tokenAmount);
        // _approve(address(this), address(uniswapPair), tokenAmount);
        // IERC20(address(this)).approve(address(uniswapV2Router), tokenAmount);

        // make the swap
        uniswapV2Router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
            tokenAmount,
            0,
            path,
            address(this),
            block.timestamp
        );
    }

    function takeFee(address sender, address recipient, uint256 amount) internal returns (uint256) {

        uint256 feeAmount = 0;

        if(isMarketPair[sender]) {
            feeAmount = amount.mul(_totalTaxIfBuying).div(100);
        }
        else if(isMarketPair[recipient]) {
            feeAmount = amount.mul(_totalTaxIfSelling).div(100);
        }

        if(feeAmount > 0) {
            _balances[address(this)] = _balances[address(this)].add(feeAmount);
            emit Transfer(sender, address(this), feeAmount);
        }

        return amount.sub(feeAmount);
    }

}
请先 登录 后评论

最佳答案 2022-07-02 15:20

触发累计后卖出时, 接收地址是交易对两币种之一,是不允许的, 所以一般是用eth. 或者路径a/weth/b, 再或者创建一个临时的地址用于接收,接收后转回来

请先 登录 后评论

其它 0 个回答

  • 0 关注
  • 0 收藏,2205 浏览
  • Eustiar 提出于 2022-07-02 02:19