> For the complete documentation index, see [llms.txt](https://document.sunprotocol.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://document.sunprotocol.io/zhi-neng-he-yue/oracle-he-yue.md).

# Oracle合约

Oracle 同时提供 SUN/USDT 池子的即时价格报价和 30 分钟 TWAP 报价，并被质押、借贷和涡轮等合约用于价格偏差保护。

### 报价接口

| 函数                                 | 用途                    |
| ---------------------------------- | --------------------- |
| `quoteSunToUSDTBySpot(sunAmount)`  | 按当前池子储备估算 USDT        |
| `quoteUSDTToSUNBySpot(usdtAmount)` | 按当前池子储备估算 SUN         |
| `quoteSunToUSDTByTwap(sunAmount)`  | 按最新有效 TWAP 估算 USDT    |
| `quoteUSDTToSUNByTwap(usdtAmount)` | 按最新有效 TWAP 估算 SUN     |
| `twapUpdatedAt()`                  | TWAP 最近更新时间           |
| `TWAP_PERIOD()`                    | TWAP 更新最小间隔，当前为 30 分钟 |
| `MAX_TWAP_STALENESS()`             | TWAP 最大有效时长，当前为 1 小时  |

TWAP 尚未初始化或已经过期时，TWAP 报价会回退。`update()` 是任何地址均可调用的状态更新函数，但需要交易和 Gas；若尚未达到更新间隔，可能返回已有值或在首次观测阶段回退。

`spotPriceX112()` 和 `twapPriceX112()` 使用 Q112 定点格式，普通界面更适合直接使用四个 `quote...` 函数。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://document.sunprotocol.io/zhi-neng-he-yue/oracle-he-yue.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
