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.
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:
-
Low-Latency Data: Updates twice per second, perfect for time-sensitive DeFi apps.
-
First-Party Sources: Trusted institutional data reduces manipulation risks.
-
Cross-Chain Support: Works on 100+ blockchains, including Conflux eSpace.
-
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.
Why Use Pyth on Conflux?
Conflux is a high-performance, layer-1 blockchain with:
-
Up to 3000+ TPS
-
Low gas fees
-
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.
Setting Up the Development Environment
Requirements:
-
Conflux Wallet extension
-
Node.js & Hardhat
-
Pyth SDK
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");
Usage
Fetching CFX Price
Run the interaction script to get real-time CFX/USD price:
npx hardhat run scripts/interact.js
The script will:
- Connect to Pyth Network’s Hermes price service
- Get price update data for CFX/USD
- Estimate gas for the transaction
- Execute the price update transaction
- 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
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)
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);
}
}
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
Acknowledgments
- Pyth Network for providing decentralized oracle infrastructure
- Conflux Network for the blockchain platform
- Hardhat for the development framework
Support
For questions or issues:
- GitHub Repo
- Check the Pyth Network documentation
- Review Conflux Network documentation
Note: This project is for educational and development purposes. Always test thoroughly on testnets before deploying to mainnet.