Marketplace has launched, further enabling blockchain developers! Learn more

How to create your own Oracle with an Ethereum smart contract

September 23, 2022


Ethereum smart contracts are the most valuable features of the Ethereum blockchain network; The development of applications driven by smart contracts on the Ethereum blockchain has skyrocketed recently. These smart-contracts are sandboxed and can’t access the data available outside of the Ethereum blockchain. Ethereum smart contracts can’t make HTTP calls or access the hardware layers; they are merely code that gets executed only once a transaction is sent. To provide data access to smart contracts and essentially connect them to the outside world, beyond the Ethereum blockchain network -  an Oracle is required. In this article, we will understand/learn how to set up our own Oracle inside a smart contract.

What is an Oracle in Ethereum?

The Ethereum blockchain network is a cluster of nodes that execute smart contracts. To obtain the same consistent outcome, all nodes must have access to the same set of inputs, and this is called determinism. To validate the smart contract's output, Ethereum relies on this determinism property, meaning that the validating nodes must all produce the identical output while running the code. 

Maintaining this property is a rather tricky task. Since Ethereum is a general-purpose platform, the smart-contracts that it runs require data from different external sources like the internet. The usefulness of smart-contracts can be significantly restricted without access to these external data sources. On the other hand, if nodes are allowed to collect information from these external sources, even a tiny time difference may result in data discrepancy with different nodes obtaining different inputs, thus producing different outputs and violating the determinism property, which will result in a contract failure.

To avoid such issues, smart-contracts are restricted from interacting with external data sources or call any internet URL, and this is where the Oracles come to the rescue. An Oracle is a way for smart-contracts to access real-world data. Oracles grab this real-world data and push it onto the blockchain for other smart contracts to properly access it. Oracles themselves are a type of a smart contract.

Here are some examples of off-chain data storage:

Trimmed blocks: Ethereum nodes may contain a large set of data, but for optimization purposes, a node can be created with just a portion of the distributed ledger. Such nodes are known as trimmed/pruned nodes and are saved in a centralized location.

State variables: The data stored on the Ethereum blockchain cannot be changed; it is immutable. However, the contents of state variables may fluctuate with changes in account balances. The solution is to store such data off the chain.

Oracles: Discussed in this article.

Digital assets: Digitized assets often require huge data sets to define them. Given the limited size of blocks, it is not feasible to store them on the chain.

There are two main types of Oracles:

Hardware Oracles: This is when data is coming from hardware devices like barcode scanners. This type of information is useful for things like registering event occurrences.

Software Oracles: This refers to the information readily available online, such as weather forecasts, fuel rates, exchange rates, stock prices, etc. Software Oracles provide smart contracts with a wide range of up-to-date data.

Creating our Oracle

For our demonstration of working with Oracles, we will use an Oracle service called Provable. Provable is the leading Oracle service for smart contracts and blockchain applications, serving thousands of requests every day on platforms like Ethereum, Rootstock, R3 Corda, Hyperledger Fabric and EOS

For this demo, we will make a smart-contract which will obtain the current diesel fuel price in the USA.

To get started, we’ll need some test Ether. We will use Kovan which you can obtain from pasting your wallet address into the Kovan faucet gitter. A chatbot will provide you with test Ethereum currency. Make sure to install the Metamask browser extension and to create an ETH wallet.

Head over to the Ethereum Remix IDE and make a new Solidity file, for example - dieselprice.sol

Paste the following code into your new Solidity script:

pragma solidity >= 0.5.0 < 0.6.0;

import "";

contract DieselPrice is usingProvable {

    uint public dieselPriceUSD;

    event LogNewDieselPrice(string price);
    event LogNewProvableQuery(string description);

        update(); // First check at contract creation...

    function __callback(
        bytes32 _myid,
        string memory _result
        require(msg.sender == provable_cbAddress());
        emit LogNewDieselPrice(_result);
        dieselPriceUSD = parseInt(_result, 2); // Let's save it as cents...
        // Now do something with the USD Diesel price...

    function update()
        emit LogNewProvableQuery("Provable query was sent, standing by for the answer...");
        provable_query("URL", "xml(");

Explanation of the code above: 

Line 1: Declaring the solidity version

Line 3: Importing the latest version of the Provable API, 

Line 5: Starting our contract named DieselPrice, usingProvable refers to the API 

Lines 7-16: Creating a variable dieselPriceUSD which will store the price, instantiating our event for the price and Provable query event, making a constructor

Lines: 18-28: making a callback function which calls the smart-contract after the output is received, giving the result from callback function to our variable and converting the price from dollars to cents

Lines 30-37: Passing an output string and an API string to fetch diesel price to our constructor function, the API fetches info in XML format, fuelPrices.diesel fetches the specific tag

Compile the smart-contract and deploy it using injected Web3 (make sure to select Kovan testnet on Metamask before compiling the contract). Ignore the warning about unused variables in the code. Approve the transaction from metamask. 

Now you will see your contract under the “Deployed Contracts” section in Remix. Open the deployed contract and click on dieselPriceUSD - it will display the output under the button, similar to what’s on the image below. Sometimes you may need to wait a minute before real data is displayed. So if you’re seeing a 0 value, wait and try again.


You can refer to Provable’s official documentation for more examples and use cases.

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)

Related articles 18

Solidity vs Vyper
Originally Published On: Aug 18, 2021
Updated On: Sep 23, 2022

With the introduction to smart... When we think of blockchains,... Introduction IntroductionVyper is... Now, the million dollar question...

Continue reading
How to write an Ethereum smart contract using Solidity
Originally Published On: Apr 5, 2020
Updated On: Sep 23, 2022

This article is intended for... Before getting started with... The smart contract term was... As we saw that smart contracts... Now, let's write a simple smart... After writing a smart contract... So we have successfully created...

Continue reading
How to Setup Local Development Environment for Solidity
Originally Published On: Jul 10, 2021
Updated On: Sep 23, 2022

The success story of blockchain... According to the Now that you are geared up with... Installation Now that our contract is ready... Now, let's write some test... The last thing, we need to do is... For deploying using hardhat, you... Here we saw how to work with...

Continue reading
How to Deploy a Smart Contract on MATIC/Polygon
Originally Published On: Oct 27, 2021
Updated On: Sep 23, 2022

Ethereum is a very lovely... With the rapid adoption of... We'll deploy our contract on... After creating your QuickNode... We will need some test MATIC to... Time to write our smart... Now click on the Solidity logo... A big thank you and kudos if you...

Continue reading
How to Create a BEP20 Token
Originally Published On: Jul 3, 2021
Updated On: Sep 23, 2022

BEP20 is the Binance Smart Chain... The first thing you will want to... The Next thing we will need to... Because BSC uses the EVM, the... With the initial BEP20.sol... In order to view this in your... Congratulations! You made it to...

Continue reading
What is an ABI?
Originally Published On: Mar 20, 2021
Updated On: Sep 23, 2022

While interacting with a smart... ABI (Application Binary... The JSON format of a contract’s... One of the most common ways is... Now that you know what ABI is,...

Continue reading
How to call another smart contract from your solidity code
Originally Published On: Jan 30, 2021
Updated On: Sep 23, 2022

Smart contracts are the most... Smart contracts are computer... We’ll deploy our contracts on... Now to achieve our goal we'll... Now you will see two contracts... Now click on count button under... Now let's update the value of... Let's check if the INTERFACE... Here we successfully saw how we...

Continue reading
How to create and deploy an ERC-721 (NFT)
Originally Published On: Mar 6, 2021
Updated On: Sep 23, 2022

Digital collectibles compatible... Fungible means to be the same or... ERC stands for Ethereum Request... Digital art (or... Now that we know what ERC-721... Before writing our NFT contract,... For ease and security, we’ll use... Congratulations on creating your...

Continue reading
How to deploy a smart contract with Brownie
Originally Published On: Jul 23, 2021
Updated On: Sep 23, 2022

Python is one of the most... Smart contract development is... Brownie is built on top of... First of all, lets cd into the... We will deploy our contract on... We need to set up our QuickNode... As our contract will be deployed... Before deploying the contract,... So, today we learned brownies...

Continue reading
How to Write a Secure Smart Contract in Vyper
Originally Published On: Feb 2, 2022
Updated On: Sep 23, 2022

Hey readers! If you are familiar... In this project we will be... First things first, if we are... We can begin by writing our... Here we will write the code... Congrats on making it to the...

Continue reading
Como crear un Token BEP20
Originally Published On: Jan 10, 2022
Updated On: Sep 23, 2022

BEP20 es en Binance Smart Chain... La primera cosa que hay que... El próximo paso que deberás... Como BSC usa la EVM, el código... Con el contrato BEP20.sol... Para poder ver esto en la... ¡Felicidades! Acabas de terminar...

Continue reading
How to Create a Dutch Auction Smart Contract
Originally Published On: Feb 11, 2022
Updated On: Sep 23, 2022

Often to perform any transaction... Auctions are platforms for... A Dutch auction, also known as... We will deploy our contract on... Before moving further, it is... Now that our Dutch auction... If you made it here,...

Continue reading