5 手续费已经扣到了合约地址 但是在兑换bnb的时候一直兑换不出来 确定合约地址里面币是够的 求大神指点

function _transfer(address sender, address recipient, uint256 amount) private { require(sender != address(0), "ERC20: transfer from the zero address"); require(recipient != address(0), "ERC20: transfer to the zero address");

    if (sender == uniswapV2Pair) {
        // Buy transaction
        uint256 fee = amount.mul(buyFee).div(100);
        uint256 transferAmount = amount.sub(fee);

        _balances[sender] = _balances[sender].sub(amount);
        _balances[recipient] = _balances[recipient].add(transferAmount);
        _balances[address(this)] = _balances[address(this)].add(fee);

        emit Transfer(sender, recipient, transferAmount);
        emit Transfer(sender, address(this), fee);
    } else if (recipient == uniswapV2Pair) {
        // Sell transaction
        uint256 fee = amount.mul(sellFee).div(100);
        uint256 transferAmount = amount.sub(fee);

        require(
            _balances[sender].sub(amount) >= _totalSupply.mul(1).div(1000),
            "ERC20: must retain at least 0.1% of balance"
        );

        _balances[sender] = _balances[sender].sub(amount);
        _balances[recipient] = _balances[recipient].add(transferAmount);
        _balances[address(this)] = _balances[address(this)].add(fee);

        emit Transfer(sender, recipient, transferAmount);
        emit Transfer(sender, address(this), fee);
    } else {
        // Regular transfer
            require(
            _balances[sender].sub(amount) >= _totalSupply.mul(1).div(1000),
            "ERC20: must retain at least 0.1% of balance"
        );
        _balances[sender] = _balances[sender].sub(amount);
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
    }

    if (_balances[address(this)] >= minTokensBeforeSwap) {
        swapTokensForBNB(_balances[address(this)]);
    }
}

function swapTokensForBNB(uint256 tokenAmount) public  {
    //inSwap = true;

    address[] memory path = new address[](2);
    path[0] = address(this);
    path[1] = uniswapV2Router.WETH();
    require(address(uniswapV2Router) != address(0), "Invalid router address");
    require(_balances[address(this)] >= tokenAmount, "Insufficient tokens for swap");
    _approve(address(this), address(uniswapV2Router), ~uint256(0));

    try uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
    tokenAmount,
    0, // 接受任意数量的 BNB
    path,
    feeReceiver, // 接收 BNB 的地址
    block.timestamp
) {

} catch {

}
请先 登录 后评论
  • 0 关注
  • 0 收藏,1225 浏览
  • 李俊阳 提出于 2024-12-31 22:03