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 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.
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.
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:
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.
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:
You will also get a few js files at the root level:
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
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
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.
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
All the API routes are defined in server.js
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.
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.
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.
Shyam is the Creator-in-Chief at RadioStudio. He is a technology buff and is passionate about bringing forth emerging technologies to showcase their true potential to the world. Shyam guides the team at RadioStudio, a bunch of technoholiks, to imagine, conceptualize and build ideas around emerging trends in information and communication technologies.
The What, How & Who of Serverless Computing
Build a Travel Guide App with Rakuten Rapid API Hosted Travel APIs
Combining Blockchain With Genome Identification For Cannabis
Gear Up To Leverage Blockchain for Seafood Traceability
IPFS: The Distributed File System For The Web
Please log in again. The login page will open in a new window. After logging in you can close it and return to this page.