Using an oracle contract on Klaytn

Introduction

Oracles have become an integral part of blockchain ecosystems, because they allow integration to pull off-chain data into your smart contracts. Without access to external data, smart contracts are isolated from external systems, which vastly limits the potential scope of the smart contract.

In this article, we will give a brief overview of oracles along with a demonstration of how they can be used for dApp development.

Decentralized Oracles

There are decentralized and centralized oracles. Decentralized oracles can prevent single points of failure, which is the greatest weakness of centralized oracles. It means that the smart contract using the data will not rely on a single source. A decentralized oracle service comprises multiple participants in a peer-to-peer network that form consensus on the off-chain data before sending it to a smart contract.

How Oracles work

Let’s look at how oracles work. A user would send a request to the oracle contract for some data, and the contract would emit a log event including all the information about the request. Nodes that are subscribed to the log would then retrieve the data and send it back to the contract.

Below, we’ll show you how you can integrate an oracle contract by using Chainlink Oracles on Klaytn. We will get price data via Chainlink’s Price Feeds.

Using Chainlink Price Feed on Klaytn

Chainlink Price Feeds use multiple high quality data inputs, and combine them with a network of Chainlink oracles feeding price data into reference contracts, where the results are then aggregated.

Let’s look at how to integrate Chainlink price feeds in our smart contract.

Prerequisites:

Currently, only the testnet (Baobab) is available. We will use Chainlink Price Feeds on the Klaytn Baobab network to pull the latest price of the LINK/USD pair.

Steps

1. Get contract address

Go to Price Feed Addresses and click on the Klaytn logo.

chainlinkPF.png

We will be pulling the data for LINK/KLAY pair, so let’s copy the address below:

0xf49f81b3d2F2a79b706621FA2D5934136352140c

2. Open Remix IDE

Clicking this link “Price Consumer contract” will redirect you to Remix IDE, where you can test using the contract. Make sure you have the Klaytn Plugin installed.

3. Compile and Deploy the Contract

Paste the Klaytn Price Feed reference contract address in constructor(). Within the contract, you will find the function as shown below. Since this is a simple demonstration, we all the other variables have been annotated.

function getLatestPrice() public view returns (int) {
        (
            /*uint80 roundID*/,
            int price,
            /*uint startedAt*/,
            /*uint timeStamp*/,
            /*uint80 answeredInRound*/
        ) = priceFeed.latestRoundData();
        return price
    }

If you look at the price pair details, the decimal is 18. Therefore, we will tweak the return value `price` a little bit to make it more readable.

return price / 1e18;

 Now click on “Compile”.

Select the Klaytn icon in the sidebar to use the Klaytn module. Choose “Baobab” for ENVIRONMENT and add your account by providing your private key or keystore. Make sure you have received test KLAY from Faucet

Now, let’s deploy the contract.

To confirm that the contract has successfully landed on the Klaytn Baobab network, you can go to Klaytnfinder to check.

4. Call the getLatestPrice function

Under “Deployed Contracts”, you will be able to see the Price Consumer contract we just deployed. Click on it, and the available functions will show up. Here, we have the getLatestPrice function.

Click on the blue button.

You should now be able to see the price of LINK/KLAY in the output. At the time of writing, it is 41. It means you need 41 KLAY for 1 LINK.

Conclusion

Oracles are an essential feature for smart contracts that require outside data. In this tutorial, we demonstrated how to use an oracle address by calling a simple function that retrieves price data for LINK/KLAY. 

If you want more information, visit Klaytn Docs and Chainlink Developer Docs. If you have any questions, visit Klaytn Forum. In our next article, we will look at how to use VRF, so stay tuned!

MORE FROM Technology

Announcements

Klaytn Governance Council Votes to Pause KIR Grants

25/11/2022

Ecosystem

Klaytn Square Beta is now available

23/11/2022

Announcements

Announcing the winners of Klaymakers22, Klaytn’s Flagship Web3 Hackathon

21/11/2022

CATEGORIES

© Klaytn Foundation 2022. All rights reserved.
© Klaytn Foundation 2022. All rights reserved.