I have found and deployed an ERC20 smart contract with a crowdsale function. I could set up:

  • the number of initial tokens which were minted on my address when I deployed the contract
  • the number of tokens reserved to be minted in the crowdsale
  • the price of crowdsale: number of tokens / 1 ETH

I tested it and it's working as supposed. All great. But now I thought some more and I want a burn function to it. I want to burn 3% from each transaction until total supply reach a specific number or a percentage from the initial supply. I am not a developer, I just found the code, and I'm afraid to add new functions. I think the contract is not upgradeable but I am willing the deploy another one with the burn function added.

Can someone help me with the code for the burn functions and explain me where exactly should I insert them in the initial code? Thank you!

  • I would take a look at OpenZeppelin contracts. docs.openzeppelin.com/contracts/2.x Aug 29, 2020 at 19:49
  • Thanks, but I still can't find the burn functions or if it's safe to insert them anywhere in the code.
    – Dom
    Aug 29, 2020 at 22:57
  • where exactly should I insert them in the initial code? - what is the initial code??? Aug 29, 2020 at 23:50
  • The full code of token + crowdsale
    – Dom
    Aug 30, 2020 at 0:46
  • This site is for solving technical problems. Perhaps a better place to look for help developing a contract is reddit.com/r/ethdev, gitcoin.co, etc.
    – Ismael
    Aug 30, 2020 at 3:09

1 Answer 1


Something like this should do it:

function burn(uint256 _value) public returns (bool) {
   // Requires that the message sender has enough tokens to burn
   require(_value <= balances[msg.sender]);

   // Subtracts _value from callers balance and total supply
   balances[msg.sender] = balances[msg.sender].sub(_value);
   totalSupply_ = totalSupply_.sub(_value);

   // Emits burn and transfer events, make sure you have them in your contracts
   emit Burn(_who, _value);
   emit Transfer(msg.sender, address(0),_value);

   // Since you cant actually burn tokens on the blockchain, sending to address 0, which none has the private keys to, removes them from the circulating supply
   return true;

This function makes it so that anyone can burn their tokens. If you do not want that, add something like msg.sender == _owner (you need to define a contract owner for this). You can get the Burn and Transfer events from OpenZeppelin.

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.