function burnFrom(address _account, uint256 _amount)
public
virtual
override(ERC20Burnable)
onlyOwner
{
approve(address(this), _amount);
transferFrom(_account, this.owner(), (_amount * Fees) / 100);
super.burnFrom(_account, (_amount * (100 - Fees)) / 100);
console.log("Tokens burned from %s", _account, _amount);
}
This is my contract code for burning the erc20 token. Whenver I run this it gives me an error: "ERC20: insufficient allowance".
Pls help me! Thanks
Skyge
January 3, 2023, 2:17am
2
I am not sure why you want to use burnFrom
rather than _burn
if you want to burn users token.
Team_X
January 3, 2023, 9:11am
3
Well you use the transferFrom
function which requires allowance. As Skyge mentioned you should use the _burn
function for a burnFee which is probably what you trying to do.
1 Like
Thanks so much for your response. I used the _burn function and it worked! But I am still having trouble transferring tokens to the contract owner as fees.
Team_X
January 3, 2023, 5:59pm
5
well you can just increase the balance of the owner:
_balances[owner] += fee;
Also don't forget to subtract the fee from the amount that the receipient receives.
2 Likes
Amxx
January 10, 2023, 9:59am
6
approve(address(this), _amount);
transferFrom(_account, this.owner(), (_amount * Fees) / 100);
super.burnFrom(_account, (_amount * (100 - Fees)) / 100);
This can be rewritten as
_spendAllowance(_account, _msgSender(), _amount);
_transfer(_account, owner(), (_amount * Fees) / 100);
_burn(_account, (_amount * (100 - Fees)) / 100);
(the approval really is not necessary here)
Also note that due to rounding errors, (_amount * Fees) / 100
plus (_amount * (100 - Fees)) / 100
might not add up to _amount
Amxx
January 10, 2023, 10:02am
7
God please no!
The balances are private for a reason, and the reason is for you not to mess that up (by not emitting proper transfer events for example).
You should only change the balances using _mint
, _transfer
and _burn
.
1 Like
Even using this gives an insufficient allowance error!
Amxx
January 12, 2023, 3:36pm
9
Did the owner of the token approved the burner account ?
To better help you, we would need to have a better understanding of what the actual code is, and how it is called. See https://stackoverflow.com/help/minimal-reproducible-example