Building a Simple Blockchain Oracle With Solidity and Node.js | by Ian Rowan | May, 2022

Learn the way blockchain oracles deliver net 2.0 knowledge to net 3.0 by constructing a easy climate oracle with solidity and node.js

Photograph by Tezos on Unsplash

Oracles on the blockchain are frameworks that permit for the blockchain world to interface with knowledge from the remainder of the online, we’ll name this the online 2.0 world. As purposes of sensible contracts proceed to develop, so will the number of knowledge wanted to deal with distinctive use circumstances.

The very fact of the matter is that net 2.0 and net 3.0 are two distinct networks and most helpful knowledge at present lives on net 2.0. By making a set of protocols to allow sensible contracts to entry this knowledge, a brand new era of the online, system design, and blockchain will emerge. I wrote about this broadly in my final article:

Present protocols have leaned into the idea of the oracle to construct hybrid techniques that depend on sensible contracts and off chain APIs to bridge net 2.0 knowledge in addition to different blockchains. Probably the most outstanding oracle is Chainlink, which offers pricing knowledge, connection to different blockchains, entry to most APIs, and quite a lot of different knowledge feeds.

Different main examples embrace token bridges which permit for motion of tokens and knowledge between chains with the assistance of off chain companies. As time goes on, many extra distinctive oracles are more likely to emerge.

At present, organizations like Singularity NET are engaged on constructing oracles that create marketplaces and quick access to APIs offering companies like AI inference on this particular instance.

On this article, I goal to offer a basic definition of the oracle idea by strolling via the overall structure of present use circumstances and constructing a easy on-chain climate oracle with solidity and node.js to additional visualize how this protocol capabilities.

Courtesy of

When approaching the issue of using an off-chain service to do some motion on behalf of a wise contract a very powerful factor to recollect is that there is no such thing as a formal messaging course of between the sensible contract and the service. With this assumption we all know that the sensible contract can not “push”, so the service should pay attention, watch, or pull.

There are solely two on-chain gadgets that the service can have the flexibility to look at, state variables and occasions. Watching state variables is cumbersome, because it requires many interactions with the contract. Occasions however require no direct interplay.

A wise contract occasion is emitted like this:

emit newEvent(block.timestamp)

An occasion may be seen as a log of the sensible contract’s actions outlined by the developer. Identical to other forms of logs, different companies can subscribe to this feed to look at for particular forms of occasions, collect knowledge from their arguments, and do something they’d like with them. These logs are seen to anybody with entry to the blockchain and are accessible through libraries like web3.js.

Given this distinctive communication system, sensible contracts can cheaply “notify” companies within the outdoors world of occasions, or within the oracle case jobs that must be accomplished. Understanding occasions and on-chain to off-chain messaging is a very powerful piece of the oracle design.

As soon as the service has discovered a brand new occasion, triggering its actions, it could possibly take the precious knowledge within the occasion and a singular job id and carry out the off-chain motion simply as every other program would.

As soon as the duty is full the service can use a web3 library to transact with the contract. A typical transaction might “add” the outcomes of the request/occasion with the job id, so the sensible contract can proceed to work on no matter it plans to do with this off-chain knowledge. Placing this all collectively it seems to be like this:

1. Oracle sensible contract emits an occasion with details about the job.

2. Off-chain oracle service listens for occasions and pulls information when one is triggered.

3. Off chain oracle interacts with any service or knowledge to obtain outcome.

4. Off-chain oracle transacts with oracle sensible contract to replace knowledge for the job.

5. Good contract ecosystems use knowledge for no matter it wants it for.

This, in fact, is an oversimplification of oracle designs like Chainlink which embrace many nodes and consensus protocols to make sure oracle knowledge is decentralized. Whereas attention-grabbing and necessary, a easy understanding would be the finest context for constructing on the idea.

With a purpose to visualize the idea of the oracle we are able to construct a easy single service, single node oracle to make the idea much less daunting.

I’ve chosen a service like climate, as a result of the enter knowledge is simple to transmit, and APIs are plentiful. Given our understanding of the event-driven oracle, our system design will look one thing like this

Climate Oracle system diagram

As you possibly can see, we have now one solidity contract and one node.js program, thus the necessities for this design are merely:

  1. Solidity sensible contracts
  2. Node.js
  3. Web3.js
  4. A climate API (utilizing OpenWeather)

First, we are able to leap into the sensible contract:

I’ve offered in-line notes with extra particulars, line by line, however to summarize our contract is utilizing a few mappings to carry standing of jobIds(true = full, false = incomplete) and the outcomes of the job.

The contract offers a perform to set off a job, getWeather, which takes location knowledge as arguments, creates a job id, and emits an occasion with the related location and job data.

An necessary function of this oracle is the operator modifier on the updateWeather perform. To solely permit the service to work together with this perform this modifier is required, in any other case, anybody might replace a jobs knowledge.

Now that we have now a blockchain oracle contract an off-chain knowledge supplier may be inbuilt node.js as follows:

Summarizing the node.js service past the main points within the feedback, a web3 supplier is about up to make use of whichever URL supplier our community is utilizing. Noting that the interacting pockets deal with should be an operator, we should be certain that our web3 supplier consists of that account.

Utilizing the web3 library we subscribe to take heed to the occasion. On a brand new occasion, the code makes use of the built-in API interface and occasion arguments to get the climate knowledge we requested.

On the results of that decision, the web3 library calls the updateWeather perform through our contracts abi to add the outcomes to the contract. Upon success, the preliminary design necessities have all been fulfilled and the sensible contract can entry the outcomes of this job for no matter it must do.

Whereas this was a easy instance it’d give a really feel for construct straightforward off-chain interfaces for distinctive use circumstances.

Altogether, blockchain oracles are set to open up the online 3.0 world to net 2.0 knowledge sources. This in flip permits for net 3.0 to start to understand extra benefits. The event-driven oracle design creates a framework for combining sensible contract capabilities with conventional software program engineering methods.

As this idea is expanded a complete ecosystem might emerge, permitting for the smoothest knowledge entry community, given how easy solidity makes interacting with different sensible contracts (APIs of the longer term).

There could also be nice alternatives in contributing to this ecosystem and beginning with an utility so simple as the climate is step one to diving in deeper and discovering one thing new

More Posts