> 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/kan-an-raka.md).

# การอ่านราคา

## ขั้นตอนที่ 1: ค้นหา Oracle Address

สามวิธีในการค้นหาที่อยู่ `PriceFeed` contract สำหรับคู่ที่กำหนด:

* **Price Oracle Tracker** (ง่ายที่สุดในระหว่างการพัฒนา): [price-oracle-tracker.vercel.app](https://price-oracle-tracker.vercel.app/?network=mainnet) — แสดงคู่ที่ deploy แล้วทั้งหมด ราคาสด decimals heartbeat และที่อยู่
* **On-chain ผ่าน OracleFactory**: เรียก `pairToOracle(string pair)` สัญลักษณ์คู่ต้องส่งเป็นตัวพิมพ์ใหญ่ (เช่น `"PSG/USDT"`) — factory ไม่ normalize input ในการอ่าน
* **Bulk on-chain ผ่าน OracleFactoryReader**: เรียก `getActiveOracles()` เพื่อดึงคู่ที่ใช้งานได้ทั้งหมดและที่อยู่ในการเรียกครั้งเดียว

```solidity
interface IOracleFactory {
    function pairToOracle(string memory pair) external view returns (address);
    function getOracleCount() external view returns (uint256);
}

function resolveFeed(address factory, string memory pair)
    external view returns (address)
{
    // Pair symbol must be passed in uppercase (e.g. "PSG/USDT").
    return IOracleFactory(factory).pairToOracle(pair);
}
```

## ขั้นตอนที่ 2: เชื่อมต่อโดยใช้ Interface

`PriceFeed` contract รองรับ Chainlink `IAggregatorV3Interface` มาตรฐาน รวมถึงส่วนขยายเฉพาะของ Chiliz สำหรับสถานะสุขภาพและสถานะหยุดชั่วคราว

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

interface IAggregatorV3Interface {
    function decimals() external view returns (uint8);
    function description() external view returns (string memory);
    function version() external view returns (uint256);

    function getRoundData(uint80 _roundId)
        external view
        returns (
            uint80 roundId,
            int256 answer,
            uint256 startedAt,
            uint256 updatedAt,
            uint80 answeredInRound
        );

    function latestRoundData()
        external view
        returns (
            uint80 roundId,
            int256 answer,
            uint256 startedAt,
            uint256 updatedAt,
            uint80 answeredInRound
        );
}

interface IChilizPriceFeed is IAggregatorV3Interface {
    function paused() external view returns (bool);
    function updateInterval() external view returns (uint256);
    function healthFactor() external view returns (uint80);
    function getHealthStatus()
        external view
        returns (
            uint256 lastUpdateTimestamp,
            uint256 totalRounds,
            uint256 timeSinceLastUpdate,
            bool isHealthy
        );
}
```

## ขั้นตอนที่ 3: อ่านราคา

{% hint style="warning" %}
**feed ไม่ revert เมื่อสถานะ stale หรือ paused — นี่เป็นการออกแบบโดยตั้งใจ** `latestRoundData()` revert ด้วย `NoDataAvailable()` หากยังไม่มี round แต่ไม่ตรวจสอบว่า feed ถูก paused หรือ stale ซึ่งตรงกับการออกแบบ Chainlink AggregatorV3Interface: ผู้ใช้มีหน้าที่ตรวจสอบ `paused()` และ `getHealthStatus()` ก่อนใช้ราคา หมายเหตุ: scalar helpers (`latestAnswer()`, `latestTimestamp()`, `getAnswer()`, `getTimestamp()`) คืนค่า `0` แทนการ revert เมื่อไม่มีข้อมูล ดู [แนวทางปฏิบัติที่ดี](/th/phatthana/sung-khuen/on-chain-raka-feed/withikan-thi-di.md) สำหรับรูปแบบการตรวจสอบฉบับสมบูรณ์
{% endhint %}

ฟังก์ชันหลักคือ `latestRoundData()` ซึ่งคืนค่าราคาปัจจุบันพร้อมข้อมูล round และ timestamp

```solidity
contract LendingOracleConsumer {
    IChilizPriceFeed public priceFeed;

    constructor(address _feed) {
        priceFeed = IChilizPriceFeed(_feed);
    }

    function getSafePrice() public view returns (uint256 price18) {
        // 1. Check feed is not paused
        require(!priceFeed.paused(), "Feed paused");

        // 2. Read latest data (reverts with NoDataAvailable() if no rounds exist)
        (
            uint80 roundId,
            int256 answer,
            uint256 startedAt,
            uint256 updatedAt,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();

        // 3. Basic sanity checks
        require(answer > 0, "Invalid price");

        // 4. Optional: use explicit health check for staleness
        (, , , bool isHealthy) = priceFeed.getHealthStatus();
        require(isHealthy, "Stale price");

        // 5. Normalize to 18 decimals for internal math
        uint8 decimals_ = priceFeed.decimals();
        uint256 scale = 10 ** uint256(decimals_);
        price18 = uint256(answer) * 1e18 / scale; // This is the actual price of an asset
    }
}
```

หากโปรโตคอลของคุณใช้ decimals native ของ feed แทนการ normalize เป็น 18:

```solidity
function getRawPriceWithMeta()
    external view
    returns (int256 price, uint8 decimals_, uint256 updatedAt)
{
    require(!priceFeed.paused(), "Feed paused");

    (, int256 answer, , uint256 _updatedAt, ) =
        priceFeed.latestRoundData();
    require(answer > 0, "Invalid price");

    (, , , bool isHealthy) = priceFeed.getHealthStatus();
    require(isHealthy, "Stale price");

    decimals_ = priceFeed.decimals();
    price = answer;
    updatedAt = _updatedAt;
}
```

## ข้อมูลย้อนหลัง

oracle เก็บข้อมูล round ย้อนหลังทั้งหมดไว้ใน on-chain การอัปเดตราคาแต่ละครั้งจะสร้าง round ใหม่พร้อม `roundId` ที่เพิ่มขึ้น

```solidity
(uint80 roundId, int256 answer, , uint256 updatedAt, ) =
    priceFeed.getRoundData(targetRoundId);
```

view helper เพิ่มเติมที่ Chiliz feed เปิดเผย:

| ฟังก์ชัน                           | คืนค่า                                   |
| ---------------------------------- | ---------------------------------------- |
| `latestAnswer()`                   | ราคาล่าสุด หรือ `0` หากไม่มี             |
| `latestTimestamp()`                | `updatedAt` ล่าสุด หรือ `0` หากไม่มี     |
| `latestRound()`                    | round ID ปัจจุบัน                        |
| `getAnswer(uint80 roundId)`        | ราคาสำหรับ round ที่กำหนด หรือ `0`       |
| `getTimestamp(uint80 roundId)`     | timestamp สำหรับ round ที่กำหนด หรือ `0` |
| `getRoundsData(uint80[] roundIds)` | array `RoundData` แบบ bulk               |
| `roundExists(uint80 roundId)`      | `true` หาก `updatedAt > 0`               |

## Events

`PriceFeed` contract แต่ละรายการปล่อย:

* **`AnswerUpdated(int256 indexed current, uint80 indexed roundId, uint256 updatedAt)`**: ปล่อยในทุกการอัปเดตราคา กรองตามราคาหรือ round
* **`NewRound(uint80 indexed roundId, address indexed startedBy, uint256 startedAt)`**: ปล่อยเมื่อ round ใหม่เริ่มต้น กรองตาม round หรือ address ที่เริ่ม

## ฟังก์ชันค้นหา (OracleFactoryReader)

| ฟังก์ชัน                                | คำอธิบาย                                                    |
| --------------------------------------- | ----------------------------------------------------------- |
| `getActiveOracles()`                    | คืนค่าคู่ที่ใช้งานได้ทั้งหมดพร้อม oracle address            |
| `getAllOracles()`                       | คืนค่า oracle ทั้งหมด รวมถึงที่ไม่ได้ใช้งาน                 |
| `getOraclesByPairs(string[] pairs)`     | แก้ไขชื่อคู่หลายคู่เป็น oracle address ในการเรียกครั้งเดียว |
| `getOracleHealthStatus(address oracle)` | สถานะสุขภาพสำหรับ oracle เดียว                              |
| `getAllOracleHealthStatus()`            | สถานะสุขภาพสำหรับทุก oracle ในระบบ                          |


---

# 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 dynamically by asking a question.

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

```
GET https://docs.chiliz.com/th/phatthana/sung-khuen/on-chain-raka-feed/kan-an-raka.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
