Skip to content

sedaprotocol/seda-pyth-price-op

Repository files navigation

seda-protocol

Pyth Price Oracle Program

This Oracle Program fetches price data from the Pyth API and provides it to the SEDA network. It demonstrates how to integrate external price feeds with SEDA's decentralized oracle infrastructure.

What This Oracle Program Does

This Oracle Program is built from the SEDA starter kit and implements a complete Pyth Network price feed solution:

  1. Fetches Real-Time Price Data: Connects to Pyth Network's API via SEDA's data proxy (pyth.proxy.testnet.seda.xyz) to retrieve live price information for any supported asset
  2. Handles Price Formatting: Properly converts Pyth's raw price data by applying the exponent system - scales prices to 6 decimal precision for consistent formatting
  3. Ensures Data Integrity: Uses SEDA's consensus mechanism with multiple executors to prevent manipulation and ensure reliable data
  4. Calculates Median Prices: Aggregates results from multiple sources and calculates the median for maximum reliability and resistance to outliers
  5. Provides EVM Integration: Includes smart contracts for seamless integration with Ethereum-compatible blockchains

Technical Details

  • Precision: Converts Pyth's variable exponent format to fixed 6 decimal places (e.g., 45127616 with expo: -5 becomes 451.276160)
  • Data Proxy: Uses SEDA's secure proxy infrastructure for external API calls to prevent censorship and ensure reliability
  • Consensus: Supports multiple reveals with median calculation for robust price determination
  • Integer Arithmetic: Uses u128 for precise calculations to avoid floating-point precision issues

Deployed Oracle Program

Network: SEDA Testnet
Program ID: cf98370a6bdf3f9f7eaefe282ac3f5979dec8a284965a119edbd3c1495b3a32a
Explorer: View on SEDA Explorer

Quick Start

Note

This Oracle Program is built from the SEDA starter kit. For detailed setup instructions, requirements, and advanced usage, see the starter kit documentation.

Essential Commands

# Build the Oracle Program
bun run build

# Run tests
bun run test

# Deploy to SEDA network
bun run deploy

# Submit a data request
bun run post-dr

Oracle Program Components

This Pyth Network Oracle Program consists of the following key components:

  • src/main.rs: The entry point that coordinates both the execution and tally phases of the Data Request.

  • src/execution_phase.rs: Fetches price data from the Pyth Network API via SEDA's data proxy using a single price feed ID. Converts Pyth's raw price (with exponent) to a scaled 6-decimal precision value using integer arithmetic. Outputs the result as a u128 value in little-endian byte format. Each executor runs this phase independently.

  • src/tally_phase.rs: Aggregates revealed results from all executors (parsed as u128 little-endian values), validates each reveal, and calculates the median price. This deterministic phase ensures consensus by filtering invalid reveals and outputting the final median value as u128 little-endian bytes.

  • src/fetch_pyth.rs: Contains the Pyth Network API integration logic, including data structures for parsing Pyth's response format and the HTTP client for fetching price data.

Usage

Input Format

The Oracle Program expects a single Pyth Network price feed ID as input (e.g., TSLA/USD "16dad506d7db8da01c87581c87ca897a012a153557d4d578c3b9c9e1bc0632f1").

To find the price feed ID for a specific asset, you can query the Pyth Network API:

curl "https://hermes.pyth.network/v2/price_feeds?query=TSLA&asset_type=equity" | jq

This will return the available price feed IDs for the queried asset.

Output Format

The Oracle Program returns the actual price value as a string with 6 decimal precision (e.g., "451.276160").

Price Conversion Process

  1. Fetches Raw Data: Retrieves price data from Pyth Network via SEDA's data proxy
  2. Applies Exponent: Converts Pyth's raw price using the exponent (e.g., 45127616 with expo: -5 = 451.27616)
  3. Scales to 6 Decimals: Multiplies by 10^6 for consistent 6-decimal precision
  4. Consensus: Multiple executors run the same process, results are aggregated
  5. Median Calculation: Final price is the median of all executor results

Example Price Conversion

Raw Pyth Data: {"price": "45127616", "expo": -5}
Calculation: 45127616 * 10^(-5) = 451.27616
Scaled: 451.27616 * 10^6 = 451276160
Final Output: "451.276160"

Environment Setup

Create a .env file with the required variables:

# RPC for the SEDA network you want to interact with
SEDA_RPC_ENDPOINT=https://rpc.testnet.seda.xyz

# Your SEDA chain mnemonic
SEDA_MNEMONIC=your_mnemonic_here

# Oracle Program ID (use the deployed one above or upload your own)
ORACLE_PROGRAM_ID=cf98370a6bdf3f9f7eaefe282ac3f5979dec8a284965a119edbd3c1495b3a32a

License

Contents of this repository are open source under MIT License.

About

SEDA Oracle Program to fetch latest price from Pyth API

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published