Skip to main content

💻 Tutorial

The Witnet multichain decentralized oracle enables smart contracts to retrieve secure and reliable data from the outside world in order to realize their true potential. These valuable data sets range from price feeds, to randomness and also information from web API et al.

In this tutorial, you will see how to get the latest price of any of the supported currency pairs on Klaytn through Price Feeds.

On Witnet, there are 2 ways to get the price feed: from the Price Feeds Router or from a Price Feed Contract. Using Price Feeds Router is the easiest way to get price feeds as it removes the need to know the actual contract addresses handling the price updates from the Witnet oracle.

Witnet price feeds are timely updated with fresh data as aggregated from multiple reliable data sources and attested by the Witnet decentralized oracle network, which offers high data integrity guarantees.

Prerequisites

Step 1 - Get the WitnetPriceRouter address

Go to Multi-chain addresses and select Klaytn Price Feeds.

To get the price of the KLAY/USD pair on Baobab network, we will need both the Price Router address and the ID(which represents the pair).

WitnetPriceRouter Address: 0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0 ID: 0x6cc828d1

Step 2 - Open Remix IDE

Make sure you have the Klaytn Plugin installed.

Click this link to open the code in Remix:

or use the code below

// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;

import "witnet-solidity-bridge/contracts/interfaces/IWitnetPriceRouter.sol";

contract PriceFeed {

IWitnetPriceRouter public immutable witnetPriceRouter;

/*
* Network: Klaytn Baobab
* WitnetPriceRouter: 0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0
**/
constructor() {
witnetPriceRouter = IWitnetPriceRouter(0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0);
}

/// Returns the KLAY / USD price (6 decimals), ultimately provided by the Witnet oracle, and
/// the timestamps at which the price was reported back from the Witnet oracle's sidechain
/// to Klaytn Baobab.
/// ID4: 0x6cc828d1
function getKlayUsdPrice() external view returns (int256 _lastPrice, uint256 _lastTimestamp) {
(_lastPrice, _lastTimestamp,) = witnetPriceRouter.valueFor(bytes4(0x6cc828d1));
}
}

If you look at the parameter in the constructor and in the getKLayUsdPrice function, we have used the address of the WitnetPriceRouter and the ID4 mentioned above.

Step 3 - Compile and Deploy the contract

Select the Klaytn icon in the sidebar to use the Klaytn plugin. Choose baobab environment and add your account by providing your MetaMask private key. Make sure you have enough test KLAY from Faucet.

Now you have your contract deployed.

Step 4 - Get the KLAY/USD price

Once your contract is deployed, you will be able to see the PriceFeed contract under the "Deployed Contracts" section. Click on it, and the available functions will show up. Here, we have the getKlayUsdPrice function.

Click on it to fetch the current price of KLAY per USD. At the time of writing, it is 192307, which means 1 KLAY equals to $0.192307.

Conclusion

In this tutorial, we saw how to use Witnet oracle to get the Price Feed on baobab testnet. These same steps can be applied when trying to get the price Feed on Klaytn Cypress(Mainnet). If you want more information, visit Klaytn Docs and Witnet Oracle Docs. If you have any questions, visit Klaytn Forum.

info

If you have any questions, please join our Discord server, or send us an email at developers@klaytn.foundation