Calling Ethereum Smart Contracts in Azure Logic Apps
Microsoft released a preview Logic App Connector to deploy Solidity Smart Contracts, call Functions and execute Transactions.
An example usage scenario would be in case a document is added to the CosmosDB, a Logic App would be triggered to store additional data in the Blockchain.
This article aims to give a step-by-step overview on how to get this working.
This article describes the following steps in detail:
- Create an Account and Project on Infura.io
- Create an Account with Address using MetaMask
- Define a Smart Contract
- Deploy this Smart Contract to the Ropsten test network using Remix
- Create a Logic App in the Azure Portal
- Add the “Query SmartContract Function (preview)”
- Add the “Execute SmartContract Function(preview)”
- Test the Logic App
- Alternative: create and deploy your own Azure Function to interact with the Ethereum BlockChain
Create an Account and Project on Infura.io
With Infura.io, it’s very easy to interact (as a DApp) with the Ethereum BlockChain network.
First step is to create an account and then create a project.
One you have created the project, make sure to switch to the Ropsten endpoint and remember the complete URL, this will be used later.
Create an Account with Address using MetaMask
Download and install the MetaMask as a Chrome extension.
Create an account on the Ropsten Test Network using this guide. Once the account is correctly created, you can view it:
Make sure to view the details and export the private key, you need this later:
Note that initially, the balance will be 0 ETH. To get ETH to test SmartContracts, go to https://faucet.ropsten.be/ and request some test ETH:
Go to Etherscan.io to view details on your Account Address:
Define a Smart Contract
I’m using a very simple Solidity Smart Contract for this article.
Deploy this Smart Contract to the Ropsten test network using Remix
Go to http://remix.ethereum.org.
The first time you connect, MetaMask is detected and asks for permission to connect to Remix.
Copy the SmartContract to a new file:
And deploy the Smart Contract.
Note that it shows information about the contract deployment via the Metamask plugin)
Once it’s deployed, you can see the status in MetaMask:
Make sure to copy the ABI (Application Binary Interface) from the SmartContract, you will need this later.
Also remember the Contract Address, you will also need this later:
Create a Logic App in the Azure Portal
Now it’s time to go to the Azure Portal and create a new Logic App in the designer which includes the following steps:
- When a HTTP request is received : This is the start point
- Initialize variable steps: To define the ABI and the Address
- Query smart contract: To call a function from the Smart Contract
- Execute smart contract: To execute a function from the Smart Contract
- Return response: return (raw) response
Add “Query SmartContract Function (preview)” step
When adding this step, you first need to create a connection:
The private key is the key you copied from MetaMask.
Once the connection is correctly defined, you can fill in the details for the Query Smart Contract Function.
In the above example, the ABI and the Smart Contract Address are variables which are defined the the variables step. But it’s also possible to copy-paste the data directly for these field. The function which is called is named getString (without any arguments).
The response is:
You can also call a function with multiple parameters, see example below.
The response is:
Add the “Execute SmartContract Function(preview)”
Add this step and fill in the details for the Execute Smart Contract Function
Test the Logic App
If all is setup correctly, you should be able to run the Logic App in Azure Portal.
Create and deploy your own Azure Function to interact with the Ethereum Blockchain
Instead of using the Query and Execute SmartContract Connectors, it’s also possible to write your own Azure Function. This will be described in a new story.