Blockchain Revolution: Part Eight

Building the First Token

Abhishek Sengar
Technology at Nineleaps
5 min readJan 11, 2019

--

Blockchain Revolution Part 8

The seventh post of the series talked about smart contracts and ways to deploy them. In this last article we will create our own cryptocurrency. To do so I have reused the same contract we created last time MyToken as shown below and further added to it.

The function _transferFunds() sends an amount from one account to another.

All the DApps are decentralized however some may require a manager to perform certain operations such as adding more coins, restricting some users from using your currency. So, let’s create a manager.

Create another contract Owned

Add a state variable owner of type address. Then add a modifier onlyOwner() to tweak the functionality. It is much like checking a condition, before function is executed. Inside the modifier check if the sender address belongs to owner or not. If the address belongs to the owner then only the function in which the modifier is called will be executed.

Next step will be to make a function for transferring ownership to another address. Create a function transferOwnership() with the address of new owner and add the modifier to it so that only the owner can execute this operation.

Now let’s go back to the contract MyToken.sol where we will add more features for the token.

Inherit the contract Owned to MyToken using the is keyword.

This means that now all the functions inside MyToken can call variable owner and modifier onlyOwner(). This is how inheritance is performed in solidity.

Now, let’s assume you wish to restrict some people from using their tokens. For this we can add a parameter that authorizes contract owner to freeze and unfreeze some accounts. Freezing of accounts means that no transactions can happen from and to that account.

To enable this you need to create a mapping and add an event FrozenFunds().

Events are like signals that the contracts generate and anything connected to the blockchain listen to these events and act accordingly.

Next add a function freezeAccounts() where the target address to be frozen is specified.

Within the function, using a map, set the address of the target account to freeze value true. Only the owner of the contract should be authorized to freeze account and that’s why modifier onlyOwner is added. After setting the bool value of mapping to true, emit the event FrozenAccount().

Now in the _transferFunds() function, check if the account is frozen before making the transaction. It should look like this:

Both from and to addresses should be unfrozen for the transaction to occur. Now let’s consider you gave another address access to transfer funds on your behalf and you want to set a limitation on how much the user can send on your behalf. For this we need to create another method approve() and a mapping allowance as shown below:

We create a map within a map. Solidity provides a feature to specify the return type in the function. The return type specified here is boolean. _spender is the address which is authorized to execute transactions on our behalf and _value is the max transfer limit. We are setting the spending limit to _value for the _spender within the function and returning true.

Now let’s create another function transfer() which will check all the above conditions before making any transaction.

In this scenario, we will first check if the sender is authorized to make a transaction by checking the allowance of that sender. If the allowance limit is more than the amount being send, then transaction can be made. If the condition is satisfied, then function _transferFunds() is called where remaining conditions are checked. If everything is fine, then the function returns true and the transaction occurs.

Now we have to deploy it to the test network. For which we create a file 2_deployment_mytoken.js in the folder migrations. After creating the file, add the following lines to it. Here we are setting the constructor values for the MyToken contract we created before.

And that’s it. Deploy it to the test network and you have your own token.

For the complete code, checkout the github link here.

The promise of Blockchain in a digital world

With this we conclude our series ‘Blockchain Revolution’. We have listed all the articles at the end of this post, in case you missed any of them or like to revisit.

While this series may have come to an end, blockchain technology has only just begun. With blockchain evolving from merely supporting cryptocurrency to being used as business applications, we are already starting to witness its value. Although, right now what we are experiencing is just the tip of the iceberg. This distributed ledger technology holds staggering potential to impact business, economics and society. We just have to wait and see as it emerges to be the next big frontier.

--

--