Need help implementing Burn Logic in an ERC-20 token
Need help implementing Burn Logic in an ERC-20 token
Loading saved threads...
e n · External communityPost link
External question — Ethereum Stack Exchange
Author: e n
Original post: https://ethereum.stackexchange.com/questions/114269
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am learning solidity and have decided to program my own ERC-20 token. I have inherited the ERC-20 Open Zeppelin standard, but would like to add some functionality that calculates 5% of every transfer, and then burns that amount from the total supply. Here is what I have so far:
I have created two functions: calculateBurnAmount, and burn. These functions calculate 5% of the amount being transferred, and then call the OpenZeppelin burn function while passing it that amount.
In order for the smart contract to "know" to burn tokens on transfer, they have to be called in the transfer function itself, correct? Did I correctly implement this by listing the transfer function and then calling the two burn functions in the logic body?
Quote
Report
Saman H. Pasha · External communityPost link
External answer — Ethereum Stack Exchange
Author: Saman H. Pasha
Original post: https://ethereum.stackexchange.com/a/114270
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
In
transfer
function first calculate
amountToBurn
then subtract it from
amount
to get
amountToTransfer
.
uint256 amountToBurn = calculateBurnAmount(amount);
uint256 amountToTransfer = amount - amountToBurn;
_transfer(_msgSender(), recipient, amountToTransfer);
_burn(_msgSender(), amountToBurn);
remove
burn
function
Quote
Report
Alex Xela · External communityPost link
External answer — Ethereum Stack Exchange
Author: Alex Xela
Original post: https://ethereum.stackexchange.com/a/152938
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Well, I guess there's a mistake in your
calculateBurnAmount
function, at
line 29
:
require(_balances[msg.sender] >= _balances[msg.sender] + burnAmount);
Imagine a user has a balance of
40
tokens. He wants to transfer
20
of them, so the burn amount required for a successfull transfer will be 20 * 5% (20 / 20) =
1
.
Back to your code logic:
require( 40 >= 40 + 1)
As you probably see, your function will not be executed, because the required condition will never be met due to basic math axioms.
So, the correct requirement at
line 29
should be:
require(_balances[msg.sender] >= _amount + burnAmount);
Quote
Report
Post Reply
Checking account access…