1

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:

enter image description here

  1. 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.
  2. 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?

2 Answers 2

0

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

4
  • Hello thank you: I made the changes you suggested. DO lines 29 and 30 make sense? Also do I need to edit the transferFrom function to burn from sender and NOT msg.sender?
    – e n
    Nov 26, 2021 at 7:56
  • All balance checks are controlled by OpenZeppline ERC20 in _transfer function, and transferFrom also use _transfer function Nov 26, 2021 at 15:32
  • How do I enforce minimum supply?
    – e n
    Nov 29, 2021 at 4:33
  • after _burn check assert(_totalSupply >= _minimumSupply); Nov 29, 2021 at 8:52
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.