# Oracle Use: Pyth and SEDA

SEDA provides the aggregation logic that combines Pyth prices with other sources (like Hyperliquid mark prices) to create a composite rate. Pyth provides the relayer infrastructure that connects to SEDA FAST, and serves as the oracle updater. Importantly, Pyth will be operating the relayers in a decentralized manner.

## Architecture & Actors

The following table depicts the different roles played by various actors in delivering price data to Nunchi:

<table data-header-hidden><thead><tr><th width="167.75"></th><th width="130"></th><th></th></tr></thead><tbody><tr><td><strong>Component</strong></td><td><strong>Role</strong></td><td><strong>Responsibility</strong></td></tr><tr><td><strong>Pyth</strong></td><td>Data Source</td><td>Provides high-quality, real-time price feeds</td></tr><tr><td><strong>HIP-3 Pusher (Pyth)</strong></td><td>Relayer</td><td>Listens to SEDA and updates HIP-3 oracles</td></tr><tr><td><strong>Oracle Updater (Pyth)</strong></td><td>Executor</td><td>Calls ‘SetOracle’ with SEDA composite rate</td></tr><tr><td><strong>SEDA</strong></td><td>Aggregator</td><td>Combines Pyth + other sources into composite rate</td></tr></tbody></table>

## Step-by-Step Flow

The flow of this architecture is depicted below:

<figure><img src="https://788773440-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAZF2BfymkD55xR0Nid8e%2Fuploads%2F9OoYpEydYOXrx6XS3en1%2Fimage.png?alt=media&#x26;token=c95ad481-3328-454e-9f31-338479c4d9a9" alt=""><figcaption></figcaption></figure>

Additional details on these steps are provided below:

{% hint style="info" %}
Note: All code below is in Python.
{% endhint %}

### **Step 1: Pyth Provides Price Data**

```python
# Pyth network continuously updates on-chain PythFeeds contract
# Price Feed ID: ae2603642690e2eab388e4c91edbf4eb248012b878a177d0b2c9ec8c7d891487
# Price: 35.34 USDC per VXX
# Confidence: ±0.01
# Publish Time: 1234567890
```

### Step 2: Hyperliquid Provides Mark Price

```python
# Hyperliquid DEX provides mark price from orderbook
# DEX: "nunchi"
# Asset: "VXX"
# Mark Price: 35.33 USDC per VXX
# Source: median(best_bid, best_ask, last_trade) 
```

### Step 3: SEDA Aggregates Prices

```python
# SEDA program executes:
# 1. Fetches Pyth price from PythFeeds contract
# 2. Fetches Hyperliquid mark price from API
# 3. Applies weighting logic based on market conditions
# 4. Returns composite rate

seda_result = seda_client.execute_nunchi_program(
    exec_inputs="ae2603642690e2eab388e4c91edbf4eb248012b878a177d0b2c9ec8c7d891487,nunchi,2"
)

# Result:
{
    "composite_rate": "35.33656",  # Blended price
    "pyth_price": "35.34",         # From Pyth
    "hyperliquid_price": "35.33",  # From Hyperliquid
    "weights": {
        "session": 0.95,           # 95% weight to Hyperliquid (during market hours)
        "reference": 0.05           # 5% weight to Pyth (reference)
    },
    "active_session": "NORMAL",    # Market is open
    "sources_used": [...]
}

```

### Step 4: Relayer (Pyth) Updates Oracle

```python
# Relayer uses SEDA composite rate
# Calls SetOracle on Hyperliquid HIP-3

exchange.perp_deploy_set_oracle(
    dex="nunchi",
    oracle_pxs={"nunchi:VXX": "35.33656"},  # From SEDA
    mark_pxs=[{"nunchi:VXX": "35.33656"}],
    external_perp_pxs={"nunchi:VXX": "35.33656"}
```

## Weighted Blending

SEDA uses weighted blending to combine Pyth and Hyperliquid prices:

```python
composite_rate = (
    hyperliquid_price * session_weight +
    pyth_price * reference_weight
)

```
