I have created an ERC20 token using OpenZepplin 5.0. We have reflection and burn features enabled in the token.
The ERC20.sol suggests:
-
Include any tax/ burn logic in _transfer.
We don't want to touch the ERC20.sol of OZ. However since the function cannot be overridden in our token contract, we only are left with option to add it in ERC20.sol -
_Update should be overridden for adding any extra logic. Following is our code. We observed that when burn is called in this function, the _update is called again. Due to this we cannot add nonreentrant keyword to the method as the process will fail.
-
We had to add following since the burn was going in a recursive call till the burn fee is zero by calling _update in a loop.
if (recipient == address(0))
{
// This is a burn operation, handle accordingly
super._transfer(sender, recipient, amount);
}
Is there any better way of doing this?
- Above code works perfectly fine as expected. However when we run the tests several tests fail unless the _update function has only super._update. Even a single require method before super call in _update will result in test case failures.
Pls help with above.
The code for the function is below...
function _update(address sender, address recipient, uint256 amount)
internal override(ERC20, ERC20Pausable)
notBlacklisted(sender) notBlacklisted(recipient) whenNotPaused {
require(amount > 0, "KSCxToken: Transfer amount must be greater than zero");
if (recipient == address(0))
{
// This is a burn operation, handle accordingly
super._transfer(sender, recipient, amount);
}
else
{
//AntiWhale
if (recipient != owner() && recipient != address(0))
{
uint256 maxTransferAmount = (totalSupply() * maxTransferAmountRate) / MAX_RATE;
require(amount <= maxTransferAmount, "KSCxToken: Transfer amount exceeds the maxTransferAmount");
uint256 recipientBalance = balanceOf(recipient);
uint256 maxWalletBalance = (totalSupply() * maxWalletBalanceRate) / MAX_RATE;
require(recipientBalance + amount <= maxWalletBalance, "KSCxToken: Exceeds maximum wallet token balance");
}
//Tax Fee & Burn
if (sender == owner() || recipient == owner() ||
isExcludedFromFeeAndBurn[sender] ||
isExcludedFromFeeAndBurn[recipient] ||
recipient == feeDestination)
{
super._transfer(sender, recipient, amount); // Transfer without fee or burn
}
else
{
// Calculate fee and burn amounts
uint256 feeAmount = (amount * transactionFee * 10**decimals()) / (100 * 10**decimals());
uint256 burnAmount = (amount * burnRate * 10**decimals()) / (100 * 10**decimals());
uint256 amountAfterFeeAndBurn = amount - feeAmount - burnAmount;
if(amountAfterFeeAndBurn > 0){
super._transfer(sender, recipient, amountAfterFeeAndBurn);// Transfer the net amount
}
if (feeAmount > 0) {
super._transfer(sender, feeDestination, feeAmount); // Ensure this uses _transfer not _update to avoid recursion
}
if (burnAmount > 0) {
_burn(sender, burnAmount); // Burn the specified amount
}
}
}
Environment
Remix, Solidity 0.8.20, OZ V5.0 libraries, bsc test chain, ERC20 Burnable Pausable