> For the complete documentation index, see [llms.txt](https://docs.chiliz.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chiliz.com/th/phatthana/sung-khuen/on-chain-raka-feed/withikan-thi-di.md).

# แนวทางปฏิบัติที่ดี

รูปแบบการตรวจสอบ Health status API การตรวจสอบความเก่า และ checklist การรวมระบบ สำหรับการใช้งาน Chiliz Chain price feeds อย่างปลอดภัย

## ตรวจสอบก่อนใช้ราคาเสมอ

`latestRoundData()` revert ด้วย `NoDataAvailable()` หากยังไม่มี round แต่ไม่บังคับตรวจสอบความเก่าหรือการ pause — คืนค่าที่เก็บไว้ตามที่เป็น ก่อนใช้ค่าราคาใดๆ ให้ตรวจสอบสามข้อเหล่านี้เสมอ:

1. **ตรวจสอบ pause**: เรียก `paused()` และปฏิเสธหาก feed ถูก paused
2. **ตรวจสอบ answer เป็นบวก**: กำหนดให้ `answer > 0`
3. **ตรวจสอบความเก่า**: ใช้ `getHealthStatus().isHealthy` เป็นเงื่อนไข

ตัวอย่าง `getSafePrice()` ใน [การอ่านราคา](https://docs.chiliz.com/th/phatthana/sung-khuen/on-chain-raka-feed/kan-an-raka.md) รองรับทั้งสามข้อ

## Health Status API

`PriceFeed` แต่ละรายการเปิดเผย view `getHealthStatus()` ที่รวบรวมข้อมูลความสดในการเรียกครั้งเดียว:

```solidity
function getHealthStatus()
    external view
    returns (
        uint256 lastUpdateTimestamp,
        uint256 totalRounds,
        uint256 timeSinceLastUpdate,
        bool isHealthy
    );
```

| ฟิลด์                 | คำอธิบาย                                                                        |
| --------------------- | ------------------------------------------------------------------------------- |
| `lastUpdateTimestamp` | `updatedAt` ของ round ล่าสุด                                                    |
| `totalRounds`         | จำนวน round ทั้งหมดที่เขียน                                                     |
| `timeSinceLastUpdate` | `block.timestamp - lastUpdateTimestamp` (หรือ `block.timestamp` หากไม่มีข้อมูล) |
| `isHealthy`           | `true` หาก `timeSinceLastUpdate <= updateInterval × healthFactor`               |

health factor ถูกกำหนดค่าเริ่มต้นเป็น `2` แต่สามารถกำหนดค่าต่อ feed โดย oracle admin ผ่าน `setHealthFactor()` อย่า hardcode `2`. อ่านโดยตรงจาก contract:

```solidity
uint80 factor = priceFeed.healthFactor(); // per-feed; do NOT assume the default of 2 (live feeds use 720)
```

health window คือ `updateInterval × healthFactor` ซึ่งกำหนดค่าต่อ feed — `updateInterval` คือระยะห่างขั้นต่ำ on-chain ขนาดเล็ก ไม่ใช่ heartbeat ซึ่งแตกต่างกันต่อ feed: CHZ/USDC ใช้ `5s × 720 = window 1 ชั่วโมง` PEPPER/CHZ ใช้ `10s × 720 = window 2 ชั่วโมง` ในขณะที่ off-chain pipeline ส่งข้อมูลตาม heartbeat \~1 ชั่วโมง อย่าใช้ `updateInterval` เพียงอย่างเดียวเป็นเกณฑ์ความเก่า — ใช้ `isHealthy` เป็นเงื่อนไข — ดู `getSafePrice()` ใน [การอ่านราคา](https://docs.chiliz.com/th/phatthana/sung-khuen/on-chain-raka-feed/kan-an-raka.md) สำหรับรูปแบบฉบับสมบูรณ์

ใช้ `getAllOracleHealthStatus()` บน `OracleFactoryReader` เพื่อตรวจสอบทุก feed ในระบบพร้อมกัน: มีประโยชน์สำหรับ dashboard และการแจ้งเตือน off-chain

## Cache `decimals()`

คู่แต่ละคู่มีความแม่นยำ decimal ของตัวเอง — เรียก `decimals()` เสมอเพื่อยืนยันก่อนตีความค่าราคา Decimals ถูกกำหนดในช่วง deployment และไม่เปลี่ยนแปลง ดังนั้นให้อ่านครั้งเดียวตอน contract initialization แทนการอ่านทุกครั้งที่อ่านราคา:

```solidity
uint8 public immutable feedDecimals;

constructor(address _feed) {
    priceFeed = IChilizPriceFeed(_feed);
    feedDecimals = priceFeed.decimals();
}
```

## จัดการสถานะ Paused อย่างสง่างาม

ในกรณีฉุกเฉิน oracle manager สามารถ pause feed ได้ จัดการ revert ที่เกิดขึ้นอย่างสะอาดแทนการใช้ค่าที่ไม่ดีโดยเงียบ:

```solidity
require(!priceFeed.paused(), "Feed paused");
```

## ใช้ Tracker ระหว่างการพัฒนา

[Price Oracle Tracker](https://price-oracle-tracker.vercel.app/?network=mainnet) ให้คุณเปรียบเทียบราคา on-chain กับ CoinMarketCap และ CoinGecko แบบเรียลไทม์ ทำให้ง่ายต่อการตรวจสอบความแม่นยำของ feed ระหว่างการรวมระบบ รวมถึงแสดงสถานะสุขภาพสด พารามิเตอร์ heartbeat และที่อยู่ contract แต่ละรายการสำหรับทุกคู่

## Checklist การรวมระบบ

* [ ] ได้รับที่อยู่ `PriceFeed` ผ่าน tracker, `pairToOracle()` หรือ `getActiveOracles()`
* [ ] ยืนยันคำอธิบายคู่และ decimals โดยใช้ `description()` และ `decimals()`
* [ ] Cache `decimals()` ตอน contract initialization
* [ ] อ่านราคาผ่าน `latestRoundData()` (revert ด้วย `NoDataAvailable()` หากยังไม่มี round)
* [ ] ตรวจสอบ `paused()` ก่อนใช้ราคา
* [ ] ตรวจสอบ `answer > 0`
* [ ] ใช้ `getHealthStatus().isHealthy` เป็นเงื่อนไขตรวจสอบความเก่า (อย่าสร้างเกณฑ์ของตัวเองจาก `updateInterval` เพียงอย่างเดียว)
* [ ] Normalize ราคาเป็นความแม่นยำภายในของคุณ (เช่น 18 decimals) หากจำเป็น
* [ ] ทดสอบบน Spicy Testnet ก่อน deploy บน Mainnet


---

# 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://docs.chiliz.com/th/phatthana/sung-khuen/on-chain-raka-feed/withikan-thi-di.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.
