I have tried this:

function burn(uint256 tokens) public returns(bool){
    require(balances[msg.sender] >= tokens);
    balances[msg.sender] = balances[msg.sender].sub(tokens);
    _totalSupply = _totalSupply.sub(tokens);
    Burn(msg.sender, tokens);

but this code will only burn the tokens by the address owner. I want the contract owner to be able to burn all the tokens and making totalSupply to be 0.


As can be seen in the previous answers, if your token is distributed to tens of thousand of holders, it becomes impractical and almost impossible to set all their balances to 0.

You can achieve the same thing by:

  1. set the _totalSupply to 0
  2. create a boolean variable called allBurned and set it to true in the burnA() function
  3. modify all the functions that manage funds and put require(!allBurned) (or more complex code that does what you want)

This isn't possible with a typical erc20 implementation. If you really wanted to burn everything, you'd have to keep an array of all addresses that have a non-zero balance, and loop through that array in burn to set them all to zero.

  • Yes I agree that's not feasible to implement it in erc20, but that was just a use case I was trying ti implement to check the concept of how to implement it, thanks for your time and help. – Prateek Singhania Feb 16 '18 at 5:13

You need to empty the balances array and set _totalSupply to 0. To empty the array have a look at: https://ethereum.stackexchange.com/a/3377/31933

Another story is what kind of event you should trigger. I guess you should fire:

Burn(msg.sender, _totalSupply);

(although you can't use the value of _totalSupply as it's already zeroed, so store the value temporary in some other variable before zeroing)

  • Balances isn't an array, it's a map. – natewelch_ Feb 16 '18 at 8:47

To burn all of the tokens at once you would need to iterate over all elements in balances, which is not possible without modifications to the code, but even with those modifications, a transaction like that would very quickly be larger than the block gas limit when the token is used by a couple thousand addresses.

Here is a way to do it, I would not recommend doing that though:

mapping (address => uint) accountBalances;
mapping (uint => address) accountIndex;

uint accountCount;
function burnAllTokens()
    for(uint i=0;i<accountCount;i++)
        accountBalances[accountIndex[i]] = 0;
        _totalSupply = 0;

You obviously need to store all of the accounts in accountIndex whenever someone send to them.

  • @Grunzwanling, that worked, thanks for the help mate! – Prateek Singhania Feb 16 '18 at 5:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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