Executing smart contracts on a public Blockchain network takes time due to the consensus mechanism. Hence it is important to implement a notification mechanism with smart contracts such that client applications are notified the moment blocks are mined. This blog post presents a quick demonstration of how this can be achieved for Ethereum.
This post was originally published on PubNub blog by Shyam Purkayastha
Thinking about digital transformation conjures images of what work technology once was, and how it’s transformed through all the innovation that’s taken place over the last couple of decades.
Our physical tools are going digital. They’re deemed valid even though they may not be tangible. Take the case of cryptocurrency. The promise is in a few years from now, you may not have to carry cash or even credit cards. All your cash will be tucked away in a secured digital wallet.
At PubNub, we take pride in driving digital transformation for our customers. From chat to IoT to eCommerce, PubNub powers underlying business processes for thousands of companies. In this tutorial, we’ll extend into a future economy driven by blockchain.
Blockchain: The Guardian of Digital Transactions
Blockchain provides a secure way of accessing and transacting assets. However, the biggest benefit comes from its ability to execute smart contracts across business boundaries. The best part is, a smart contract is not merely a digital copy of a paper document stored in 1s and 0s. It actually acts as a policy enforcer.
Example: Service Subscription
A smart contract is a computer program which executes a set of contractual rules coded in the source code of the program. If you think of the popular Ethereum Blockchain platform, then Solidity is the programming language for writing smart contracts.
Any business process can be transformed into a Blockchain-enabled process by codifying the protocols of business transactions in a smart contract. Consider a simple insurance service. A company offering a subscription to its insurance policy can codify the rules for the customers in a series of transactions. Moreover, these rules can work across multiple business entities, thanks to the Blockchain’s promise of a distributed ledger.
PubNub-powered Smart Contract
The idea of a smart contract-based subscription service can be envisaged as a web app which allows users who have signed up to subscribe to a service. The backend is driven by a private Ethereum blockchain with Solidity. As a demo app, Ganache can be used to simulate an Ethereum Blockchain and Truffle framework can be used via a REST API to invoke Solidity contracts.
And where does PubNub fit in?
PubNub can fit in almost anywhere, but for this specific case, its use for sending realtime notifications of contract execution.
Before you begin, make sure that you have access to the tutorial source code from Github.
You can choose to run the application by following the README file from the repo. In this tutorial, we’ll dive into the details. Here’s an outline of what we’ll cover:
- Step 1 – Install software
- Step 2 – Install packages and dependencies
- Step 3 – Compile smart contract
- Step 4 – Deploy the smart contract
- Step 5 – Deploy the web server
- Step 6 – Test application
- Step 7 – Perform user operation.
- Step 8 – Check stats
Step 1: Install Software
- Truffle provides an application suite to compile and build Solidity based smart contracts.
- Ganache acts as the Ethereum Blockchain node which logs all transactions on a local blockchain node.
Follow the Truffle installation guide to install the Truffle npm package and Ganache download page to download and install the Ganache executable. Ganache is mainly available for Windows, but includes options for other OSs.
With Truffle, you also get the solc-js Solidity compiler which will be used for compiling the smart contract code.
Step 2: Install Packages and Dependencies
It is time now to set up a demo project using the Truffle CLI tools. Truffle ships with a lot of pre-built app packages called Truffle Boxes.
We are going to use a third-party box that supports invocation of smart contracts through REST APIs. It’s called express-box.
Run this command to install express-box within a new project folder:
truffle unbox arvindkalra/express-box
You should now have the following directories installed inside the project folder:
- /contracts – Contains the .sol smart contract files
- /migrations- Contains the code for migration and deployment of contracts
- /public_static – Contains the web root directory for serving the application
- /test – Contains the test contract. But you can ignore it for this demo.
You will also get a few js files at the root level:
- js – the expressjs application server that defines the REST endpoints
- js – Config file for connecting with Ganache
By default, express-box contains a different application. We will replace these files from the GitHub repo:
Alternatively, you can ignore the express-box installation and clone the GitHub repo under the project folder to get the same directory structure.
At the end, you also need to install the PubNub NodeJS package.
npm install pubnub
Step 3: Compile Smart Contracts
This application depends on the Subscribe.sol contract. This defines three transactions which form the core business logic of the app.
The application also includes a default Migration.sol contract which is used for deployment.
From the project root location, compile the contracts as follows:
Once compiled you can see the JSON compiled contract file in build/contracts/ folder.
Writing Artifacts to .\build\contracts
Step 4: Deploy Smart Contracts
The fun begins now. You should now launch Ganache and make sure that its settings are per the truffle.jssettings.
With Ganache up and running, you can deploy the contracts by running these commands in sequence.
truffle migrate --reset
truffle migrate --network-development
If all goes well, you should see a “Network up to date” output on your screen.
Step 5: Deploy the web server
The smart contracts are up and running and it’s time now to hook them up with REST API so that they can be invoked from a client application.
npm start on the terminal and you should see the web server spring into action.
The web server is a simple expressJS program. Apart from hosting the webpage, it defines three APIs
- /getStatus – Fetches the subscription status of a given customer account
- /getCount – Fetches the count of already subscribed customers
- /subscribe – Initiates a transaction to subscribe a new customer
All the API routes are defined in server.js
Step 6: Test Application
Open your browser and access the app at http://localhost:3000.
You will be greeted with the homepage which prompts you to enter a customer account. The assumption here is that the customers already have an account and this app enables them to subscribe to the service using their account number.
The web application logic is contained in accounts.js. The main operations are triggered as part of button clicks.
The index.html file defines a set of handlebar templates which are displayed on the screen based on the state of the app and user account subscription.
Step 7: Perform User Operation
Let’s take a quick test drive and witness how users will use the app.
When the subscription transaction is completed, the server emits the transaction details which is then channeled back to the app via a PubNub channel. The name of the PubNub channel is the same as the customer account number.
This logic is defined in app.js
If an already subscribed user tries to subscribe, then the contract kicks in to indicate that the user is already subscribed.
Step 8: Check Stats
You can also query the smart contract for the number of subscribers. The /getCount API returns the current count of subscribed customers. It is hooked to the getCustomerCount( ) contract function to return this data.
And that’s it! Now you know how to hook up PubNub to get realtime updates on Ethereum transactions. For a real-world application, this can be a great boon, especially in cases where the transactions take a longer time to be committed due to the time lag in consensus and mining.