# 多源价格聚合

## 算法

```
sources = [Chainlink, Pyth, UniswapV3 TWAP]
prices = [p1, p2, p3]

# 1. Staleness check
for (price, source) in zip(prices, sources):
    if now - source.last_update > stalenessThreshold:
        reject source

# 2. 离群点剔除 (median absolute deviation)
median = median(prices)
mad = median(|p - median| for p in prices)
filtered = [p for p in prices if |p - median| <= 3 * mad]

# 3. 加权平均
weights = [chainlinkWeight, pythWeight, uniWeight]
final = sum(p * w for p, w in zip(filtered, weights)) / sum(weights)

# 4. Deviation circuit breaker
if |final - lastFinal| / lastFinal > deviationThreshold:
    pause + emit DeviationAlert
```

## 当前实现

* OracleHub.sol：单 operator push 模式（PoC）
* 主网前 swap 多 source aggregator

## 关键参数

```solidity
stalenessThreshold = 60s
deviationThreshold = 500 bps (5%)
minSourceCount = 3
```

详见 [Oracle 系统](/protocol-mechanics/oracle-system.md)。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yellowpaper.axblade.io/algorithms/multi-source-price-aggregation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
