📊 How to Fetch Price Feeds Using Pyth Oracle on Conflux

:blue_book: Introduction

In the world of decentralized finance (DeFi), smart contracts need accurate, real-time price data to function effectively. Whether you’re building a decentralized exchange, a lending platform, or a derivatives protocol, reliable price feeds are essential.

The Pyth Network is a leading decentralized oracle that provides high-fidelity, low-latency market data to smart contracts across over 100 blockchains, including Conflux. This guide offers a comprehensive walkthrough on integrating Pyth Oracle with Conflux, focusing on fetching the CFX/USD price feed.


:mag: Understanding Pyth Oracle

Pyth Oracle is a first-party oracle network delivering real-time financial data directly from institutional sources such as Binance, OKX, and Jane Street.

Key Features:

  • :stopwatch: Low-Latency Data: Updates twice per second, perfect for time-sensitive DeFi apps.
  • :bank: First-Party Sources: Trusted institutional data reduces manipulation risks.
  • :link: Cross-Chain Support: Works on 100+ blockchains, including Conflux eSpace.
  • :chart_with_upwards_trend: Extensive Feeds: 1300+ feeds — crypto, equities, FX, and commodities.

Pyth uses a pull-based model, so you only pay for the data you use, improving cost and scalability.


:rocket: Why Use Pyth on Conflux?

Conflux is a high-performance, layer-1 blockchain with:

  • :zap: Up to 3000+ TPS
  • :moneybag: Low gas fees
  • :globe_with_meridians: Compatibility with EVM (eSpace)

By integrating Pyth with Conflux, developers can:

  • Access reliable and fresh market data.
  • Enhance smart contracts with automated logic.
  • Ensure data integrity through trusted sources.

Example: A DEX on Conflux can use CFX/USD price from Pyth to ensure fair and up-to-date trading prices.


:brick: Setting Up the Development Environment

:wrench: Requirements:

  • :white_check_mark: Conflux Wallet extension
  • :white_check_mark: Node.js & Hardhat
  • :white_check_mark: Pyth SDK

:hammer_and_wrench: Install Hardhat:

npm install -g hardhat

The deployment script will:

  • Deploy the CFXPrice contract
  • Pass the Pyth contract address as constructor parameter
  • Output the deployed contract address

3. Update Contract Address

After deployment, update the contract address in scripts/interact.js:

const cfxPrice = await CFXPrice.attach("YOUR_DEPLOYED_CONTRACT_ADDRESS");

:computer: Usage

Fetching CFX Price

Run the interaction script to get real-time CFX/USD price:

npx hardhat run scripts/interact.js

The script will:

  1. Connect to Pyth Network’s Hermes price service
  2. Get price update data for CFX/USD
  3. Estimate gas for the transaction
  4. Execute the price update transaction
  5. Display the current price and confidence interval

Example Output

Estimated gas: 150000
Transaction sent: 0x1234...
Transaction confirmed in block: 12345
CFX/USD Price: 0.12345678
Confidence: 0.00012345

:wrench: Configuration

Hardhat Configuration

The project is configured to work with Conflux Network:

  • Testnet: https://evmtestnet.confluxrpc.com
  • Chain ID: 71 (Conflux Testnet)
  • Solidity Version: 0.8.24

Pyth Network Integration

  • Price Feed ID: 0x8879170230c9603342f3837cf9a8e76c61791198fb1271bb2552c9af7b33c933 (CFX/USD)
  • Pyth Contract: 0xDd24F84d36BF92C65F92307595335bdFab5Bbd21 (Conflux Testnet)

:receipt: Writing the Smart Contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;


import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";


contract CFXPrice {
    IPyth pyth;
    bytes32 constant CFX_USD_PRICE_ID = 0x8879170230c9603342f3837cf9a8e76c61791198fb1271bb2552c9af7b33c933;

    constructor(address pythContract) {
        pyth = IPyth(pythContract);
    }

    function getCFXPrice(bytes[] calldata priceUpdateData) public payable returns (int64, uint) {
        uint fee = pyth.getUpdateFee(priceUpdateData);
        pyth.updatePriceFeeds{value: fee}(priceUpdateData);

        PythStructs.Price memory price = pyth.getPriceNoOlderThan(CFX_USD_PRICE_ID,60);
        return (price.price, price.conf);
    }
}

:mag: Explanation:

Imports: IPyth and PythStructs for interacting with Pyth Oracle.

CFXUSD_PRICE_ID: Replace this with the real ID from Pyth Feed IDs.

getCFXUSDPrice: Updates the feed and retrieves current price data, ensuring it’s not stale.

CFXPrice Contract

The main contract (price.sol) provides:

  • Constructor: Takes Pyth contract address as parameter
  • getCFXPrice(): Public payable function that:
    • Calculates update fee
    • Updates price feeds with the fee
    • Returns current price and confidence

:pray: Acknowledgments

:telephone_receiver: Support

For questions or issues:


Note: This project is for educational and development purposes. Always test thoroughly on testnets before deploying to mainnet.