Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins

It is easy to build a blockchain oracle network. Yes, it is. But what is the Oracle network? Let’s break it down to understand why oracle networks are important and how it adds superpowers to the blockchain networks.

In simple words, an oracle network is a basic web server that allows the transfer of information from the internet to the blockchain layer. But how? let’s take a look at this architecture.

  1. Oracle client is the initiator of the request. The client invokes a function on the smart contract which emits an event.
  2. Oracle server listens to the event and executes the request or action defined outside over the internet.
  3. Oracle server then updates the information by passing it to the oracle contract by invoking the contract update function.

Let’s look at the code for the Oracle contract:

  1. The client invokes the updateBTCCap() function that emits the event for the server to do an internet request to get the BTC Cap.
  2. Once the server completes the request and gets the BTC Cap, it invokes the setBTCCap function with the cap as the argument.
  3. Once this is complete the client can then call the getBTCCap function to get the latest BTC cap that is updated by the server.

The part of server code below:

  1. The oracle contract has two instances. One is the Websocket based connection that is oracleContractWss and the other is a simple instance with an HTTPS provider oracleInstance.
  2. The web socket instance can listen to the events and we get the events from the latest block. This means it will only account for the upcoming events. If you specify any old block the events starting from that block will be fetched.
  3. On data from the event, we request an API to fetch the BTC Cap. And invoke the setBTCCap function using the HTTPS instance of the contract.

The entire source code for the project is below in the Github repository.

GitHub – adityak74/blockchain-eth-simple-oracle: A simple ethereum based oracle network, contract, server in NodeJS

Follow me for more blockchain content. If this content helps you out 👏 👏


Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.

It is easy to build a blockchain oracle network. Yes, it is. But what is the Oracle network? Let’s break it down to understand why oracle networks are important and how it adds superpowers to the blockchain networks.

In simple words, an oracle network is a basic web server that allows the transfer of information from the internet to the blockchain layer. But how? let's take a look at this architecture.

  1. Oracle client is the initiator of the request. The client invokes a function on the smart contract which emits an event.
  2. Oracle server listens to the event and executes the request or action defined outside over the internet.
  3. Oracle server then updates the information by passing it to the oracle contract by invoking the contract update function.

Let’s look at the code for the Oracle contract:

  1. The client invokes the updateBTCCap() function that emits the event for the server to do an internet request to get the BTC Cap.
  2. Once the server completes the request and gets the BTC Cap, it invokes the setBTCCap function with the cap as the argument.
  3. Once this is complete the client can then call the getBTCCap function to get the latest BTC cap that is updated by the server.

The part of server code below:

  1. The oracle contract has two instances. One is the Websocket based connection that is oracleContractWss and the other is a simple instance with an HTTPS provider oracleInstance.
  2. The web socket instance can listen to the events and we get the events from the latest block. This means it will only account for the upcoming events. If you specify any old block the events starting from that block will be fetched.
  3. On data from the event, we request an API to fetch the BTC Cap. And invoke the setBTCCap function using the HTTPS instance of the contract.

The entire source code for the project is below in the Github repository.

GitHub – adityak74/blockchain-eth-simple-oracle: A simple ethereum based oracle network, contract, server in NodeJS

Follow me for more blockchain content. If this content helps you out 👏 👏


Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


Print Share Comment Cite Upload Translate
APA
Aditya Karnam | Sciencx (2024-03-28T09:50:40+00:00) » Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins. Retrieved from https://www.scien.cx/2022/01/17/writing-a-simple-blockchain-oracle-contract-oracle-server-and-client-in-under-5-mins/.
MLA
" » Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins." Aditya Karnam | Sciencx - Monday January 17, 2022, https://www.scien.cx/2022/01/17/writing-a-simple-blockchain-oracle-contract-oracle-server-and-client-in-under-5-mins/
HARVARD
Aditya Karnam | Sciencx Monday January 17, 2022 » Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins., viewed 2024-03-28T09:50:40+00:00,<https://www.scien.cx/2022/01/17/writing-a-simple-blockchain-oracle-contract-oracle-server-and-client-in-under-5-mins/>
VANCOUVER
Aditya Karnam | Sciencx - » Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins. [Internet]. [Accessed 2024-03-28T09:50:40+00:00]. Available from: https://www.scien.cx/2022/01/17/writing-a-simple-blockchain-oracle-contract-oracle-server-and-client-in-under-5-mins/
CHICAGO
" » Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins." Aditya Karnam | Sciencx - Accessed 2024-03-28T09:50:40+00:00. https://www.scien.cx/2022/01/17/writing-a-simple-blockchain-oracle-contract-oracle-server-and-client-in-under-5-mins/
IEEE
" » Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins." Aditya Karnam | Sciencx [Online]. Available: https://www.scien.cx/2022/01/17/writing-a-simple-blockchain-oracle-contract-oracle-server-and-client-in-under-5-mins/. [Accessed: 2024-03-28T09:50:40+00:00]
rf:citation
» Writing a simple blockchain oracle contract, oracle server, and client in under 5 mins | Aditya Karnam | Sciencx | https://www.scien.cx/2022/01/17/writing-a-simple-blockchain-oracle-contract-oracle-server-and-client-in-under-5-mins/ | 2024-03-28T09:50:40+00:00
https://github.com/addpipe/simple-recorderjs-demo