How to add fee in ERC20 token?

Hello, I just try simple code and adding fee each time I transfer, however I can add liquidity on uniswap but I can't buy it when fee > 0. Anyone can help me why this code is failed ?


1 Like

Yeah I know the reason, I have to put slippage greater than fee to do the transaction

Most likely, the uniswap pool contract verifies that the full amount has been transferred into itself.

Regardless, I'd change this:

if (fee > 0) {
    uint256 _fee = _transferAmount * fee / 10000;
   ...
}

To this:

uint256 _fee = _transferAmount * fee / 10000;
if (_fee > 0) {
   ...
}
1 Like

Take in mind that Uniswap v3 doesn't support token with fees

1 Like

Well, that's pretty much what I've speculated above.

1 Like

you may try the swap function which support fee on transfer.
like swapExactTokensForTokensSupportingFeeOnTransferTokens(), swapExactETHForTokensSupportingFeeOnTransferTokens(), swapExactTokensForETHSupportingFeeOnTransferTokens.