DigiOracle Docs v1.0.1
  • 📔KlayOracle Documentation
  • Fundamentals
    • Introduction
    • Architecture
    • Aggregation Mechanism
    • Node & data provider communication
    • Installing KlayOracle
  • Smart Contract Developers
    • Price Feeds
      • Quick Start: Code Samples
      • How to Use KlayOracle Price Feeds
    • Random Number Generator
      • Quick Start: Code Samples
      • How to Use KlayOracle Random Number Generator
  • Data Providers
    • Introduction
    • Install & run data providers locally
    • How It Works
      • Configuring Data Feeds
      • Reducers
      • Data provider utilities
      • Configuration Files & Environmental Variables
      • OracleProvider Contract
      • Bootstrap Data Providers
    • Best Practices
  • Nodes
    • Introduction
    • Install & run node locally
  • Community
    • DigiOracle Champion Program
      • About the DigiOracle Champion Program
      • How to create content & become a DigiOracle Champion
      • Contribution Guidelines
      • FAQs
    • Bounties
Powered by GitBook
On this page
  • Retrieving the KLAY/USD pair price
  • Solidity Example
  • JavaScript example
  • Video Tutorial: Fetch KLAY/USD Price Feed using DigiOracle
  1. Smart Contract Developers
  2. Price Feeds

Quick Start: Code Samples

Retrieving the KLAY/USD pair price

Solidity Example

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.16;

import "https://github.com/KlayOracle/klayoracle-monorepo/blob/development/oracle-contract/contracts/KlayOracleInterface.sol";

contract OracleConsumerSample {
    address public immutable oracleAddress;

    uint256 public klayOutput;

    constructor(address _oracleAddress) {
        require(_oracleAddress != address(0));
        oracleAddress = _oracleAddress;
    }

    function swapUsdtoKlay() public returns (bool) {
        KlayOracleInterface oracle = KlayOracleInterface(oracleAddress);

        bool replied = oracle.newOracleRequest(
            this.swap.selector,
            address(this)
        );

        return replied;
    }

    function swap(uint256 _klayOutput) external {
        require(msg.sender == oracleAddress, "not allowed"); //ensure only Oracle contract can set price
        klayOutput = _klayOutput;
        //Swap usd to klay
    }
}

JavaScript example

const oracleAddress = "0xbc884088e406422a3ef39aedd1c546de7ac4be7c";

const abi = [
  {
      "inputs": [],
      "name": "latestRound",
      "outputs": [
        {
          "internalType": "bytes32",
          "name": "answer",
          "type": "bytes32"
        },
        {
          "internalType": "uint256",
          "name": "roundTime",
          "type": "uint256"
        },
        {
          "internalType": "uint256",
          "name": "timestamp",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function"
    }
]

const provider = new ethers.providers.JsonRpcProvider("https://api.baobab.klaytn.net:8651");

const oracleContract = new ethers.Contract(oracleAddress, abi, provider);

const [answer, roundTime, timestamp] = await oracleContract.latestRound()

Video Tutorial: Fetch KLAY/USD Price Feed using DigiOracle

PreviousPrice FeedsNextHow to Use KlayOracle Price Feeds

Last updated 1 year ago