Recap

In a previous tutorial, we created and created a capped a timebound crowdsale. Now, we review how to create a “burnable token”.

What is token burning?

Token burning implies to reducing the total supply of tokens.

But wait, blockchains are immutable. So, how do we do this?

Making a burnable token is accomplished by sending tokens to an address whose private keys are not accessible by anyone and subtracting the number from the total supply in our contract.

We will continue to build our ExampleToken.sol, which we used in our previous tutorials.

Let’s dive into the code:

ExampleToken.sol

For this tutorial, we are removing thtoken minting feature from our contract and giving it a fixed total supply of 10000. OpenZeppelin library provides BurnableToken.sol . We will import this contract to implement burnable token functionality in our ExampleToken .

pragma solidity ^0.4.23; import "./BasicToken.sol"; contract BurnableToken is BasicToken { event Burn(address indexed burner, uint256 value); function burn(uint256 _value) public {

_burn(msg.sender, _value);

} function _burn(address _who, uint256 _value) internal {

require(_value <= balances[_who]); balances[_who] = balances[_who].sub(_value);

totalSupply_ = totalSupply_.sub(_value);

emit Burn(_who, _value);

emit Transfer(_who, address(0), _value);

}

}

BurnableToken exposes burn(uint256 _value) method which is calling an internal _burn(address _who, uint256 _value) method. This internal method is performing the following tasks.

Reducing the sender’s token balance — It will reduce the token balance of the account who is executing the transaction. msg.sender is a global variable which represents the account who is executing the transaction. Reducing the token’s total supply — Then the method is reducing the total supply of our token by changing the value of _totalSupply variable available in BasicToken.sol Emitting Transfer event — This point is noteworthy, as you can see there is no actual transfer to address(0) . It’s just emitting an event which shows token is transferred to address(0) . This achieves the same results as we are reducing the total supply. Also, if you check the transfer method in BasicToken.sol , you will see that we have a check that restricts anyone to send tokens to address(0) .

What is address(0)?

This represents 0x0000000000000000000000000000000000000000 on ethereum blockchain. It’s also the ethereum genesis address and no-one has its private keys. (You can check how much this address worth.)

Making ExampleToken burnable

Now, let's see how to implement this on our exampleToken contract.

contract ExampleToken is StandardToken, DetailedERC20, BurnableToken{ constructor(string _name, string _symbol, uint8 _decimals)

DetailedERC20(_name, _symbol, _decimals)

public {

totalSupply_ = 10000;

balances[msg.sender] = 10000;

} }

We just need to inherit BurnableToken.sol. That’s it, this will give us full functionality of to burn tokens.

As mentioned earlier, we are setting total supply to 10000 and giving it to the person who is deploying our ExampleToken contract.

Testing Burnable ExampleToken

OpenZeppelin library is well tested, so we will just add a basic test case to see that everything is working as expected.

it('Burn test' , async() => {

await this.token.burn(1000);

const totalSupply = await this.token.totalSupply();

console.log(totalSupply);

assert.equal(totalSupply , 9000);

});

As you can see, we are burning 1000 tokens and checking if our token supply is now reduced to 9000. You can write more test cases of you own if you’d like. If you need a reference, check my GitHub repository.

Conclusion

So we have learned how to create a burnable token using OpenZeppelin library.

Token burning is standard practice. It increases the token value as it decreases the Total supply. Coins like BNB (Binance coin) use token burning to increase the value of tokens.

In the next part, we will see how to smart contract to pay dividends to token holders. If you have any questions, let us know in comments.

You can check, the whole code in my GitHub repository.

Starting a new blockchain project, or looking for a Solidity developer?

Crowdbotics helps business build cool things with Solidity (among other things). If you have a blockchain project where you need additional developer resources, drop us a line. Crowbotics can help you estimate build time for given product and feature specs, and provide specialized Solidity developers as you need them. If you’re building with Solidity, check out Crowdbotics.