February 15, 2020 Filza Tariq

Deep Dive into Azure Blockchain for Enterprise Blockchain Solutions [Part 2]

Spread the word!

The essence of building Azure Blockchain-based solutions is a 3-Step simple approach for leveraging Blockchain for businesses with easy integration of components. Azure Blockchain facilitates to automate most of the part for developers, hence assisting the developers to directly focus on building their Blockchain application logic. Here is the 3-Step approach that is crucial to Blockchain development with Azure:

 

1. Choose the Underlying Network:

Building the foundation of your Blockchain network by choosing a consortium network. Azure Blockchain Service provides you with the flexibility to deploy pre-configured templates of consortium networks. Azure Blockchain provides support for almost every popular consortium network including Hyperledger Fabric, Corda, Ethereum PoA, Quorum etc.

2. Model Smart Contracts

Once you choose a network, you need to build your Blockchain application logic on the top of that. For that very purpose, Azure Blockchain provides you with Azure Blockchain Development Kit for Visual Studio Code. Azure Blockchain Development kit assists the testing of smart contracts and deployment of smart contracts on Azure Blockchain networks, public networks like Ethereum or even locally deploying the smart contracts on Ganache.

3. Extending Your Application

After setting your Blockchain network and modelling the smart contracts you need an additional layer to interact with these smart contracts and writing or reading data from the ledger. Azure Blockchain Workbench accelerates the development process by providing support for extending your Blockchain application. It also provides the flexibility to integrate your Blockchain application with the existing apps and databases that your business uses already.

In Part 1 of Azure Blockchain for Enterprise Blockchain solutions series, we had exercised Step 1 of this three-step approach and launched an Ethereum Proof-of-Authority (PoA) network on Azure. Now, in the second part of the series, we will further learn about writing and deploying a simple smart contract on the Ethereum PoA network that we had deployed in Part 1.

In this article we will look at the following two methods for deploying your smart contracts on Ethereum PoA network:

    1. Using Remix for deploying smart contracts to Azure Ethereum PoA
    2. Using Truffle and HD Wallet Provider

If you want to deploy the smart contract for learning and testing purposes, it’s preferred to go with the first method that is using Remix (a browser IDE for compiling, testing and deploying smart contracts). However, if you are developing a product with Azure Ethereum PoA, it is advisable to go with the second method as Truffle provides you with a fine structure for managing the different components of your Blockchain application. 

 

In this tutorial, we are going to explore both the methods with a very simple test smart contract deployment on Ethereum PoA. So let’s get started with the fun part.

Method 1 of 2: Using Remix for deploying smart contracts to Azure Ethereum PoA

There are no prerequisites for this method except the Metamask extension that you must already have installed if you have followed the first part of this Azure Blockchain series. 

Step 1: Writing Smart Contract

Navigate to https://remix.ethereum.org and click on the (+) button at the top left to add a new smart contract. Name the smart contract as “news.sol”. Now copy the following smart contract in your “news.sol” file:

 

pragma solidity >=0.4.0 <=0.6.0; 
     contract News{ 
    struct newsFeed{ 
    address publisher; 
     string newsDesc; 
} 
mapping(uint => newsFeed) public newsFeeds;
	uint public newsCount;

	function addNews(string memory _newsDesc) public {
		newsCount++;
		newsFeeds[newsCount].publisher = msg.sender;
		newsFeeds[newsCount].newsDesc = _newsdesc;

	}
}

 

This is a very simple contract I have written for testing purposes. The mapping in solidity is like an associative array that maps the integer index with newsfeed structure here. Every time you add a piece of new news that is a simple string, the newscount is incremented, that is used as an index to access and store data in the newsfeed structure. Msg.sender in solidity denotes the Ethereum address of the caller of the contract.

So now as you have written your smart contract, navigate to the solidity compiler in the left pane in Remix and compile your contract.

Compiling your smart contract

Compiling your smart contract

Your smart contract will hopefully compile without any errors if you’re not at bad luck today.

Step 2: Deploying smart contract on Ethereum PoA

As your smart contract has compiled successfully it is ready to get deployed on the network that in our case is Ethereum Proof-of-Authority Network on Azure. Now in part 1 of this series after deploying the Ethereum PoA we got the deployment outputs containing the field like Ethereum RPC, admin site etc. So now we are going to use the deployment outputs here to deploy our smart contract on the network.

In your Azure Portal navigate to ResourceGroup→ Deployments→ Microsoft-azure-blockchain…→ Outputs and copy the Ethereum RPC endpoint to your keyboard.

Azure Portal navigate to ResourceGroup

Azure Portal navigate to ResourceGroup

RPC in the Blockchain is a data exchange protocol that allows for communication between your browser and your Blockchain network or node.

Now let’s add this Ethereum RPC endpoint in Metamask so we can deploy the smart contract to Ethereum PoA network from Remix. Open Metamask extension in the browser and navigate to Settings→ Networks→ Add Network and paste the RPC endpoint you just copied into New RPC URL field. Adding into this field will connect your Metamask to Ethereum PoA network.

Once your Ethereum PoA network is connected, navigate to the “Deploy & run transactions” pane in Remix and click on Deploy.

Deploy and run transactions

Deploy and run transactions

This will successfully deploy your smart contract on your Ethereum PoA network. You can now view your contract in the Deployed Contracts section of remix and can interact with it by adding into the fields.

Deployed contracts section

Deployed contracts section

Method 2 of step 2: Using Truffle and HD Wallet Provider

In this method, you are going to use truffle for deploying your contracts. You need the following prerequisites for this method:

  1. Truffle Suite: Client-based Ethereum development environment
  2. Any code editor of your choice. However, if you are working with Azure Blockchain I would suggest you go with VS code as it allows you to interact with your network using Azure Blockchain Development that comes handy.

So now let’s deploy our smart contract on Ethereum PoA using Truffle step-by-step.

Step 1: Create a Truffle Project

Run your command prompt as an administrator, navigate to the folder in which you want to create the truffle project and type the following command:

Truffle init

This will set a project layout for you in the selected folder like below:

Folder selection

Folder selection

Now let’s add our smart contract in the contracts folder. You can use the same smart contract here that we have used in method 1.

Step 2: Adding Migrations

Navigate to the migration folder of your smart contract and create a new file named “2_deploy_contracts.js” for the “news.sol” contract. Paste the following code in this newly created file.

 

var news = artifacts.require("News");

module.exports = function(deployer) 
{
  deployer.deploy(news);
};


 

Step 3: Adding your network settings

Now we are going to do the real work of deploying our smart contract on Ethereum PoA on Azure using Truffle.

Truffle gives you a “truffle-config.js” file to add your network settings. We are going to modify this file to add our Ethereum PoA network settings. For that purpose paste the following code in your “truffle-config.js” file:

 

const HDWalletProvider = require("@truffle/hdwallet-provider");
const rpc_endpoint = "Your RPC endpoint of ethereum PoA";
const mnemonic = "Your unique seed phrase";

module.exports = {
  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    },
    poa: {
      provider: function() {
return new HDWalletProvider(mnemonic, rpc_endpoint)
},
      network_id: 10101010,
      gasPrice : 0
    }
  }
};

 

In const rpc_endpoint add your PoA network endpoint and in mnemonic add you unique Metamask account phrase. You can find that in Metamask → Security & Privacy → Reveal seed phrase.

Truffle HD wallet provider: For privacy reasons, truffle provides you with HD wallet provider that signs your transactions for you using your private key rather giving access of your private key to any third-party.

Since we are using truffle HD wallet provider in our project, you can install the HD wallet in your project with the following command:

npm install @truffle/hdwallet-provider

 

Step 4: Deploying your Smart Contract

Now in your command prompt add the following command:
truffle migrate –network poa
This will start your deployment to your Ethereum PoA network on Azure.

 

Deploying your smart contract

Deploying your smart contract

However, you must use truffle version 5.0.5 to deploy your smart contract on Ethereum PoA. The latest version of truffle points some gas-related errors with Azure deployments. You can downgrade your truffle version with the following command:

npm i -g [email protected]

 

Step 5: Sending transaction to your network

As now you have deployed your smart contract to the Ethereum PoA network lets send a transaction to our network. In your project folder create another file with the name “sendtransaction.js” and paste the following code in that file:

 

var news = artifacts.require("news");
module.exports = function(done) {
console.log("Getting the deployed version of the news smart contract")

news.deployed().then(function(instance) {

console.log("Calling add news function for contract ", instance.address);

return instance.addnews("Hello, Xord!");

}).then(function(result) {

console.log("Transaction hash: ", result.tx);
console.log("Request complete");

done();

}).catch(function(e) {

console.log(e);

done();

});

};

Now execute this script with the following command in the command prompt:

Deployment on the Ethereum Proof of Authority network on Azure

Deployment on the Ethereum Proof of Authority network on Azure

truffle exec sendtransaction.js –network poa
And Hurrah! Your smart contract is successfully deployed on the Ethereum Proof of Authority network on Azure!

That’s all for the part 2 of this Azure Blockchain for Enterprise Blockchain Solutions series. In this part, we have practised the second step of the Azure Blockchain 3 Step approach for building Blockchain applications. In the third and final part of the series, we will learn about building and modelling Blockchain applications with Azure Blockchain Workbench. Happy Chaining till then!

Find more articles on Blockchain on our blog https://xord.one/blog/ or contact Xord @https://xord.one/contact/ for Blockchain projects and free consultation.


Spread the word!
, , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 9 =