I want to apply a fee of the 10% every time my token is transferred (except the case the from is the owner).
When i use PancakeSwap I'm able to create a pool, however, when I try to swap the token from the just created pool, I receive the error 'Pancake: TRANSFER_FAILED'
This happen when the from filed is equal to the _uniswapV2Pair address.
Why this happens and how to fix this problem?
The relevant token code is below:
// to avoid cycles
modifier lockSwapTokenForETH {
_inSwapTokenForETH = true;
_;
_inSwapTokenForETH = false;
}
function _transfer(address from, address to, uint256 amount) private {
// by adding "from != _uniswapV2Pair" it works!
if (!_inSwapTokenForETH && from != _owner){
uint feeAmount = amount * 10 / 100;
_transfer(from, address(this), feeAmount);
_swapTokensForEth(feeAmount);
_transfer(from, to, amount - feeAmount);
emit Transfer(from, to, amount - feeAmount);
}
else{
_transfer(from, to, amount);
emit Transfer(from, to, amount);
}
}
function _swapTokensForEth(uint256 tokenAmount) private lockSwapTokenForETH {
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = _uniswapV2Router.WETH();
_approve(address(this), address(_uniswapV2Router), tokenAmount);
_uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(tokenAmount, 0, path, address(this), block.timestamp);
}
receive() external payable {}