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 {
}