💻 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 Kaia 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
- MetaMask
- Remix IDE
- Kaia Plugin on Remix
- Test KAIA from Faucet
Step 1 - Get the WitnetPriceRouter address
Go to Multi-chain addresses and select Kaia Price Feeds.
To get the price of the KAIA/USD pair on Kairos 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 Kaia 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: Kaia Kairos
* WitnetPriceRouter: 0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0
**/
constructor() {
witnetPriceRouter = IWitnetPriceRouter(0xeD074DA2A76FD2Ca90C1508930b4FB4420e413B0);
}
/// Returns the KAIA / 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 Kaia Kairos.
/// ID4: 0x6cc828d1
function getKaiaUsdPrice() 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 getKaiaUsdPrice
function, we have used the address of the WitnetPriceRouter and the ID4 mentioned above.
Step 3 - Compile and Deploy the contract
Select the Kaia icon in the sidebar to use the Kaia plugin. Choose Kairos environment and add your account by providing your MetaMask private key. Make sure you have enough test KAIA from Faucet.
Now you have your contract deployed.
Step 4 - Get the KAIA/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 getKaiaUsdPrice function.
Click on it to fetch the current price of KAIA per USD. At the time of writing, it is 192307
, which means 1 KAIA equals to $0.192307.
Conclusion
In this tutorial, we saw how to use Witnet oracle to get the Price Feed on kairos testnet. These same steps can be applied when trying to get the price Feed on Kaia Cypress(Mainnet). If you want more information, visit Kaia Docs and Witnet Oracle Docs. If you have any questions, visit Kaia Forum.
If you have any questions, please join our Discord server, or send us an email at developers@kaia.io