Create Your Own Cryptocurrency In Private Consortium Network Ethereum Azure Blockchain

Introduction

 
The words Blockchain and Cryptocurrency have been successful in disrupting the market for such a long time now. The effect they have made in the industry is massive. It looks like it has its own future, so I thought to write something about it. In this article, we will create our own cryptocurrency and our own private consortium network in Azure. We will be using Ethereum proof-of-work consortium solution from Azure. Sounds interesting? If yes, let’s just skip the introduction and jump in. I hope you will like this article.

You can always read this article on my blog here.

Background

We will be splitting this article into three parts.

  1. Introduction to Blockchain and Cryptocurrency
  2. Create your Own Private Consortium Network in Azure
  3. Create your Own Private Cryptocurrency

What are Blockchain and Cryptocurrency

 
Blockchain

We can summarize blockchain in the following points.

  1. It is a growing chain/list of blocks with records in it.
  2. These blocks are linked or chained with Cryptography.
  3. Each block will have its own unique cryptography hash.
  4. Each block has a “Previous Hash” property which is the hash of the previous block.
  5. The first block of the chain is known as Genesis block; only this block will not have the property “Previous Hash”.
  6. Hash, Previous Hash, Data, Timestamp are the minimum contents of a block.
  7. Here, the Data can be anything. For example, it can be the Transaction details like “Amount Transferred”, “Sender”, “Receiver” etc.
Why is Blockchain resistant to data modification?

By design, a Blockchain is resistant to modification of the data. Each block in Blockchain has its own unique cryptographic hash and previous hash property. Let’s say the Hash will be regenerated each time when there is a modification of data, timestamp. So, if you do any modification in any blocks, the hash property is changed to a different one, and the previous hash property of the next block becomes invalid, and so on. So, the entire network will have to undergo the update process, which is technically impossible.

Whenever there is an update, a new block is generated and the same will be chained to the network by taking the hash property of the last block, as its previous hash property.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
BlockChain

Real-time scenarios where Blockchain can be used?

The feasibility of Blockchain is almost everywhere. You can think of a lot of scenarios where the same can be applied. Let us see a few places where it can be used.

  1. What if all of our Government documents are considered as a block? The forgery in documents will become an impossible task. Won't it?
  2. With the help of Smart Contracts, we can use Blockchain even for some business scenarios, where the real-time validations are required; here, each stage can be treated as a block.
Cryptocurrency

Cryptocurrency is a digital currency that uses strong cryptographic security, unlike the physical currencies like notes, coins. It doesn’t have any physical form. All the transactions of cryptocurrency are verified through the network nodes and everything is recorded in a Blockchain.

The most famous cryptocurrency in the world is Bitcoin, which was invented by an unknown person or a bunch of people, known by a pseudo name "Satoshi Nakamoto". This is the same person who invented Blockchain.

We all send and receive money to our accounts, but use someone as a mediator, that in most cases is a bank. Now, what if we remove that mediator and replace it with a Blockchain? That’s where the cryptocurrency stands. With the help of Cryptocurrency and Blockchain, we don’t need to worry about the security, transaction costs which we are charged high from the banks. I personally feel that there will be a time where there is no physical currency anymore.
 

Create Your Own Cryptocurrency

 
We are one step away from creating our own cryptocurrency. Before we do that, we should create a private Consortium Network where we can add the Blockchain. There are two things you must consider in your mind.
  1. Coin
  2. Token

A Coin is a cryptocurrency where it uses its own Blockchain, whereas a Token is a cryptocurrency which uses the existing Blockchain. For example, a bitcoin uses its own Blockchain. We are going to use an existing Blockchain, which is nothing but Ethereum.

Let’s create an Ethereum account.
 

Creating an Ethereum Account


To work with Ethereum Blockchain, we need an Ethereum account. In this step, we will see how we can create one. There are many ways we can do this. One of them is using an existing tool called MetaMask. You can consider it a Wallet/Bank where you can create new accounts and make the transactions.

You can install MetaMask via a Google Chrome extension, where you can create the Ethereum account and get registered in the network. To install the same, go to the Chrom Web Store and search for MetaMask, and click on "Add to Chrome".

Once you have installed the extension, you will be redirected to a welcome page. You can also go to the page manually by clicking on the new extension icon. Continue the process until the page “Secret Backup Phrase” shows up. This is where you will get your backup phrase, which can be used to restore your accounts in case you forget the password. So, please make a copy of the same and store in a safe place. Once you have completed all the steps, an account will be generated for you in the Main Network.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
MetaMask Account

As you can see it has already generated a public cryptography id for your account, now consider this as your account id. You can always change the network you wish, as there are many other networks available, but we will be adding our own private network later using the custom option. For now, please remember to change it to any test network.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Ethereum Networks

Create Smart Contract

Luckily, to develop our own Token, we don’t need to download any IDE, as Ethereum provides its own online IDE, which is the remix. Go to the IDE, that’s where we are going to develop our own Cryptocurrency. The language used to program here is Solidity, it is a high-level object-oriented programming language used for creating smart contracts.

To create the token, we need to write some codes in Solidity, let’s do that now. Copy the codes below and paste it in the Remix editor, here I have named my Token as SibiCoin. You can use anything you wish.

  1. pragma solidity >=0.4.22 <0.6.0;  
  2.   
  3. contract owned {  
  4.     address public owner;  
  5.   
  6.     constructor() public {  
  7.         owner = msg.sender;  
  8.     }  
  9.   
  10.     modifier onlyOwner {  
  11.         require(msg.sender == owner);  
  12.         _;  
  13.     }  
  14.   
  15.     function transferOwnership(address newOwner) onlyOwner public {  
  16.         owner = newOwner;  
  17.     }  
  18. }  
  19.   
  20. interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes calldata _extraData) external; }  
  21.   
  22. contract TokenERC20 {  
  23.     // Public variables of the token  
  24.     string public name;  
  25.     string public symbol;  
  26.     uint8 public decimals = 18;  
  27.     // 18 decimals is the strongly suggested default, avoid changing it  
  28.     uint256 public totalSupply;  
  29.   
  30.     // This creates an array with all balances  
  31.     mapping (address => uint256) public balanceOf;  
  32.     mapping (address => mapping (address => uint256)) public allowance;  
  33.   
  34.     // This generates a public event on the blockchain that will notify clients  
  35.     event Transfer(address indexed from, address indexed to, uint256 value);  
  36.       
  37.     // This generates a public event on the blockchain that will notify clients  
  38.     event Approval(address indexed _owner, address indexed _spender, uint256 _value);  
  39.   
  40.     // This notifies clients about the amount burnt  
  41.     event Burn(address indexed from, uint256 value);  
  42.   
  43.     /** 
  44.      * Constrctor function 
  45.      * 
  46.      * Initializes contract with initial supply tokens to the creator of the contract 
  47.      */  
  48.     constructor(  
  49.         uint256 initialSupply,  
  50.         string memory tokenName,  
  51.         string memory tokenSymbol  
  52.     ) public {  
  53.         totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount  
  54.         balanceOf[msg.sender] = totalSupply;                    // Give the creator all initial tokens  
  55.         name = tokenName;                                       // Set the name for display purposes  
  56.         symbol = tokenSymbol;                                   // Set the symbol for display purposes  
  57.     }  
  58.   
  59.     /** 
  60.      * Internal transfer, only can be called by this contract 
  61.      */  
  62.     function _transfer(address _from, address _to, uint _value) internal {  
  63.         // Prevent transfer to 0x0 address. Use burn() instead  
  64.         require(_to != address(0x0));  
  65.         // Check if the sender has enough  
  66.         require(balanceOf[_from] >= _value);  
  67.         // Check for overflows  
  68.         require(balanceOf[_to] + _value > balanceOf[_to]);  
  69.         // Save this for an assertion in the future  
  70.         uint previousBalances = balanceOf[_from] + balanceOf[_to];  
  71.         // Subtract from the sender  
  72.         balanceOf[_from] -= _value;  
  73.         // Add the same to the recipient  
  74.         balanceOf[_to] += _value;  
  75.         emit Transfer(_from, _to, _value);  
  76.         // Asserts are used to use static analysis to find bugs in your code. They should never fail  
  77.         assert(balanceOf[_from] + balanceOf[_to] == previousBalances);  
  78.     }  
  79.   
  80.     /** 
  81.      * Transfer tokens 
  82.      * 
  83.      * Send `_value` tokens to `_to` from your account 
  84.      * 
  85.      * @param _to The address of the recipient 
  86.      * @param _value the amount to send 
  87.      */  
  88.     function transfer(address _to, uint256 _value) public returns (bool success) {  
  89.         _transfer(msg.sender, _to, _value);  
  90.         return true;  
  91.     }  
  92.   
  93.     /** 
  94.      * Transfer tokens from other address 
  95.      * 
  96.      * Send `_value` tokens to `_to` in behalf of `_from` 
  97.      * 
  98.      * @param _from The address of the sender 
  99.      * @param _to The address of the recipient 
  100.      * @param _value the amount to send 
  101.      */  
  102.     function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {  
  103.         require(_value <= allowance[_from][msg.sender]);     // Check allowance  
  104.         allowance[_from][msg.sender] -= _value;  
  105.         _transfer(_from, _to, _value);  
  106.         return true;  
  107.     }  
  108.   
  109.     /** 
  110.      * Set allowance for other address 
  111.      * 
  112.      * Allows `_spender` to spend no more than `_value` tokens in your behalf 
  113.      * 
  114.      * @param _spender The address authorized to spend 
  115.      * @param _value the max amount they can spend 
  116.      */  
  117.     function approve(address _spender, uint256 _value) public  
  118.         returns (bool success) {  
  119.         allowance[msg.sender][_spender] = _value;  
  120.         emit Approval(msg.sender, _spender, _value);  
  121.         return true;  
  122.     }  
  123.   
  124.     /** 
  125.      * Set allowance for other address and notify 
  126.      * 
  127.      * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it 
  128.      * 
  129.      * @param _spender The address authorized to spend 
  130.      * @param _value the max amount they can spend 
  131.      * @param _extraData some extra information to send to the approved contract 
  132.      */  
  133.     function approveAndCall(address _spender, uint256 _value, bytes memory _extraData)  
  134.         public  
  135.         returns (bool success) {  
  136.         tokenRecipient spender = tokenRecipient(_spender);  
  137.         if (approve(_spender, _value)) {  
  138.             spender.receiveApproval(msg.sender, _value, address(this), _extraData);  
  139.             return true;  
  140.         }  
  141.     }  
  142.   
  143.     /** 
  144.      * Destroy tokens 
  145.      * 
  146.      * Remove `_value` tokens from the system irreversibly 
  147.      * 
  148.      * @param _value the amount of money to burn 
  149.      */  
  150.     function burn(uint256 _value) public returns (bool success) {  
  151.         require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough  
  152.         balanceOf[msg.sender] -= _value;            // Subtract from the sender  
  153.         totalSupply -= _value;                      // Updates totalSupply  
  154.         emit Burn(msg.sender, _value);  
  155.         return true;  
  156.     }  
  157.   
  158.     /** 
  159.      * Destroy tokens from other account 
  160.      * 
  161.      * Remove `_value` tokens from the system irreversibly on behalf of `_from`. 
  162.      * 
  163.      * @param _from the address of the sender 
  164.      * @param _value the amount of money to burn 
  165.      */  
  166.     function burnFrom(address _from, uint256 _value) public returns (bool success) {  
  167.         require(balanceOf[_from] >= _value);                // Check if the targeted balance is enough  
  168.         require(_value <= allowance[_from][msg.sender]);    // Check allowance  
  169.         balanceOf[_from] -= _value;                         // Subtract from the targeted balance  
  170.         allowance[_from][msg.sender] -= _value;             // Subtract from the sender's allowance  
  171.         totalSupply -= _value;                              // Update totalSupply  
  172.         emit Burn(_from, _value);  
  173.         return true;  
  174.     }  
  175. }  
  176.   
  177. /******************************************/  
  178. /*       ADVANCED TOKEN STARTS HERE       */  
  179. /******************************************/  
  180.   
  181. contract SibiCoin is owned, TokenERC20 {  
  182.   
  183.     uint256 public sellPrice;  
  184.     uint256 public buyPrice;  
  185.   
  186.     mapping (address => boolpublic frozenAccount;  
  187.   
  188.     /* This generates a public event on the blockchain that will notify clients */  
  189.     event FrozenFunds(address target, bool frozen);  
  190.   
  191.     /* Initializes contract with initial supply tokens to the creator of the contract */  
  192.     constructor(  
  193.         uint256 initialSupply,  
  194.         string memory tokenName,  
  195.         string memory tokenSymbol  
  196.     ) TokenERC20(initialSupply, tokenName, tokenSymbol) public {}  
  197.   
  198.     /* Internal transfer, only can be called by this contract */  
  199.     function _transfer(address _from, address _to, uint _value) internal {  
  200.         require (_to != address(0x0));                          // Prevent transfer to 0x0 address. Use burn() instead  
  201.         require (balanceOf[_from] >= _value);                   // Check if the sender has enough  
  202.         require (balanceOf[_to] + _value >= balanceOf[_to]);    // Check for overflows  
  203.         require(!frozenAccount[_from]);                         // Check if sender is frozen  
  204.         require(!frozenAccount[_to]);                           // Check if recipient is frozen  
  205.         balanceOf[_from] -= _value;                             // Subtract from the sender  
  206.         balanceOf[_to] += _value;                               // Add the same to the recipient  
  207.         emit Transfer(_from, _to, _value);  
  208.     }  
  209.   
  210.     /// @notice Create `mintedAmount` tokens and send it to `target`  
  211.     /// @param target Address to receive the tokens  
  212.     /// @param mintedAmount the amount of tokens it will receive  
  213.     function mintToken(address target, uint256 mintedAmount) onlyOwner public {  
  214.         balanceOf[target] += mintedAmount;  
  215.         totalSupply += mintedAmount;  
  216.         emit Transfer(address(0), address(this), mintedAmount);  
  217.         emit Transfer(address(this), target, mintedAmount);  
  218.     }  
  219.   
  220.     /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens  
  221.     /// @param target Address to be frozen  
  222.     /// @param freeze either to freeze it or not  
  223.     function freezeAccount(address target, bool freeze) onlyOwner public {  
  224.         frozenAccount[target] = freeze;  
  225.         emit FrozenFunds(target, freeze);  
  226.     }  
  227.   
  228.     /// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth  
  229.     /// @param newSellPrice Price the users can sell to the contract  
  230.     /// @param newBuyPrice Price users can buy from the contract  
  231.     function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {  
  232.         sellPrice = newSellPrice;  
  233.         buyPrice = newBuyPrice;  
  234.     }  
  235.   
  236.     /// @notice Buy tokens from contract by sending ether  
  237.     function buy() payable public {  
  238.         uint amount = msg.value / buyPrice;                 // calculates the amount  
  239.         _transfer(address(this), msg.sender, amount);       // makes the transfers  
  240.     }  
  241.   
  242.     /// @notice Sell `amount` tokens to contract  
  243.     /// @param amount amount of tokens to be sold  
  244.     function sell(uint256 amount) public {  
  245.         address myAddress = address(this);  
  246.         require(myAddress.balance >= amount * sellPrice);   // checks if the contract has enough ether to buy  
  247.         _transfer(msg.sender, address(this), amount);       // makes the transfers  
  248.         msg.sender.transfer(amount * sellPrice);            // sends ether to the seller. It's important to do this last to avoid recursion attacks  
  249.     }  
  250. }  

The first step of the solidity program will start with defining the compiler.

pragma solidity >=0.4.22 <0.6.0;

Once you have edited the token code, you can navigate to the compile tab and compile your code. You can also check the Auto Compile option which will enable the auto-compilation of the code whenever you make any changes.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Ethereum Remix Solidity Compiler

Once the compilation is successful, you can go to the Run tab. As you can see, the account you had created in MetaMask has been already populated for you.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Ethereum Remix Solidity Run

Now, it is time to deploy our coin in the network, but when you do that you will get an error as “ALERT: Insufficient funds”. This is because you have only 0 ETH in your account. Well, no worries, we will find a way to make you rich. Let’s create our own private Consortium Network now so, that we can send money to your account. Are you ready to become rich?

Create Your Own Private Consortium Network

 
Creating the Network

Log into your Azure portal, and search for “Ethereum Proof-of-Work Consortium”. This is a super handy solution created by Microsoft for the Blockchain enthusiastic people. It deploys an Ethereum network, consisting of a set of transaction nodes and a set of mining nodes to record transactions. It may take between 30 minutes to do all the provision. I would create a separate resource group for this.

Once everything is created, you should be able to go to the resource group and see all the resources created.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Azure Resources

Now, click on the resource with the type Public IP address, and copy the DNS name and open in the browser. You should be able to see a page as below.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Ethereum Node Status Azure DNS Name

Here, the nodes start with “tx-” are the transaction nodes and the nodes start with “mn-” are the mining nodes. Mining is the process of validating and approving the transactions happening and registering them in the ledger or Blockchain.
 

Send Ethereum to Accounts


As our Private network is ready we can potentially change the MetaMask account network from the Test network to the network we have created. Click on the Network and then Custom RPC, in the new Network text box, you can give the DNS name we have generated earlier. Please remember that the default port is 8545.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
New Network in MetaMask Ethereum

As we have already connected the MetaMask to our own private network, now we can easily send the Ethereum to this account from our Network. Go to the network and paste the MetaMask account public cryptography key and click submit.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Send Ethereum to Accounts

If everything goes well, you should get a message as “Ether sent!”. You should also see that the new blocks are getting generated in both Transaction nodes and Mining nodes. Now We can check our MetaMask account. I can’t wait to see that I have become rich.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
MetaMask Private Account

Wow!. Now I feel like I have a currency printer. Now we can go and create the smart contract.

Deploy the Smart Contract to Private Network

Now let’s just go to the Remix IDE and compile our smart contract and deploy the Token.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Deploy Custom Cryptocurrency

Here, you can see that our account is showing as 2000 ether. Before you deploy the Token/Coin, you should give the value for initialSupply, which is the maximum supply available for this contract, and the tokenName, which is the name of the coin, in this case, it is SibiCoin, and tokenSymbol, which is the symbol of your token. Once you have filled everything, you can click on the transact button.

A MetaMask pop-up will be generated, click on the Confirm button. If you don’t see the pop-up, please click on the MetaMask Chrome extension button.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Initial Deployment of Smart Contract

If everything goes well, you should be able to see an alert as the transaction is confirmed.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
MetaMask Transaction Confirmed

You should also see a deployed contract under the Deployed Contracts section, copy the token and add it to your existing MetaMask.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Smart Contract Token Generated

Add Custom Token to MetaMask Account

To add, go to the MetaMask account details and click on Add Token, and provide the token address.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Add Custom Token to MetaMask

If you want to change the Decimal Precision, you should edit the same in your custom Solidity code. Once you have added the same you should be able to see 50000 sc (SibiCoin) in your account.

Sending Custom Coin from One to Another Account


To send our SibiCoins from our account “SibiAccount”, we should create a new account, for now, I will call it “SibiAccount 2”. Now let’s just copy the address of the second account and send some coins, yes, of course for free. Please make sure that you have added the Smart Contract Token in the second account as well.

Create Your Own Cryptocurrency in Private Consortium Network Ethereum Azure Blockchain
Send Custom Cryptocurrency MetaMask

Once the transaction is approved, you will get a notification and the coins should be reflected in your second account.

Conclusion


Wow! Now we have learned,

  • What is Blockchain
  • What is Cryptocurrency
  • What are Ethereum and MetaMask
  • How to create our own Cryptocurrency
  • How to use Azure Ethereum proof of work Blockchain solution
  • What is Smart Contract
  • How to use Remix tool and deploy custom Tokens
  • How to transfer custom cryptocurrency within accounts

You can always use the monitoring tools available in Azure, I will leave that to you. Please consider reading my Azure articles here.

Your turn. What do you think?

Thanks a lot for reading. Did I miss anything that you may think is needed in this article? Did you find this post useful? Kindly do not forget to share your feedback with me.


Similar Articles