Building the First Token
The of the series talked about smart contracts and ways to deploy them. In this last article we will create our own . To do so I have reused the same contract we created last time MyToken as shown below and further added to it.
pragma solidity ^0.4.20;
contract MyToken
function _transferFunds(address _from, address _to,
uint value)
}
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
contract Owned
modifier onlyOwner()
function transferOwnership(address newOwner) onlyOwner public
}
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.
modifier onlyOwner()
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.
function transferOwnership(address newOwner) onlyOwner public
Now let’s go back to the contract MyToken.sol where we will add more features for the .
Inherit the contract Owned to MyToken using the is keyword.
contract MyToken is Owned
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 . 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().
mapping (address => bool) public frozenAccount;
event FrozenFunds(address targetAccount, bool freeze);
Events are like signals that the contracts generate and anything connected to the listen to these events and act accordingly.
Next add a function freezeAccounts() where the target address to be frozen is specified.
function freezeAccount(address targetAccount, bool freeze)
onlyOwner public
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:
function _transferFunds(address _from, address _to, uint value) internal
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:
mapping (address => mapping (address => uint)) public allowance;
function approve(address _spender, uint _value) public
returns (bool success)
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.
function transfer(address _from, address _to, uint value) public returns (bool success)
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.
var mytoken = artifacts.require("./MyToken.sol");
module.exports = (deployer) =>
And that’s it. Deploy it to the test network and you have your own .
For the complete code, checkout the github link.
The promise of Blockchain in a digital world
With this we conclude our series ‘ 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, technology has only just begun. With evolving from merely supporting 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 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.
Published at Fri, 11 Jan 2019 06:48:57 +0000