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: 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. 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. 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 Our 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: 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. 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. 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:Choose the Underlying Network:
Model Smart Contracts
Extending Your Application
Method 1 of 2: Using Remix for deploying smart contracts to Azure Ethereum PoA
Step 1: Writing Smart Contract
</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>pragma solidity >=0.4.0 <=0.6.0;<br>contract News{<br>struct newsFeed{<br>address publisher;<br>string newsDesc;<br>}<br>mapping(uint => newsFeed) public newsFeeds;<br>uint public newsCount;</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>function addNews(string memory _newsDesc) public {<br>newsCount++;<br>newsFeeds[newsCount].publisher = msg.sender;<br>newsFeeds[newsCount].newsDesc = _newsdesc;</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>}<br>}<br>
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.
Your smart contract will hopefully compile without any errors if you’re not at bad luck today.
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.
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.
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.
In this method, you are going to use truffle for deploying your contracts. You need the following prerequisites for this method:
So now let's deploy our smart contract on Ethereum PoA using Truffle step-by-step.
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:
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.
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.
<br>var news = artifacts.require("News");</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>module.exports = function(deployer)<br>{<br>deployer.deploy(news);<br>};</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>
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:
<br>const HDWalletProvider = require("@truffle/hdwallet-provider");<br>const rpc_endpoint = "Your RPC endpoint of ethereum PoA";<br>const mnemonic = "Your unique seed phrase";</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>module.exports = {<br>networks: {<br>development: {<br>host: "localhost",<br>port: 8545,<br>network_id: "*" // Match any network id<br>},<br>poa: {<br>provider: function() {<br>return new HDWalletProvider(mnemonic, rpc_endpoint)<br>},<br>network_id: 10101010,<br>gasPrice : 0<br>}<br>}<br>};</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>
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
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.
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]
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:
<br>var news = artifacts.require("news");<br>module.exports = function(done) {<br>console.log("Getting the deployed version of the news smart contract")</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>news.deployed().then(function(instance) {</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>console.log("Calling add news function for contract ", instance.address);</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>return instance.addnews("Hello, Xord!");</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>}).then(function(result) {</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>console.log("Transaction hash: ", result.tx);<br>console.log("Request complete");</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>done();</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>}).catch(function(e) {</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>console.log(e);</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>done();</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>});</p> <!-- /wp:paragraph --> <!-- wp:paragraph --> <p>};<br>
Now execute this script with the following command in the command prompt:
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 Our Workbench. Happy Chaining till then!
Find more articles on Blockchain on our blog https://xord.solutions/publications/ or contact Xord @https://https://xord.solutions/contact/ for Blockchain projects and free consultation.