> For the complete documentation index, see [llms.txt](https://docs.hann.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hann.finance/th/developers/smart-contracts.md).

# สัญญาอัจฉริยะ

เครือข่าย ที่อยู่สัญญา ฟังก์ชันสาธารณะ event และสถานะบัญชี

สัญญา Hann Finance ครอบคลุมกู้ USDHN, Lending, สเตก และสวอป ใช้ที่อยู่กับ ABI ตามเครือข่ายและหลักประกันที่เชื่อมต่อ

## เครือข่ายและที่อยู่

| เครือข่าย      | Chain ID |
| -------------- | -------- |
| Kaia mainnet   | `8217`   |
| Kairos testnet | `1001`   |

### Kaia mainnet

สัญญา Hann Finance ยังไม่ deploy บน Kaia mainnet chain ID `8217`

### ที่อยู่ Kairos testnet

เครือข่าย **Kairos** chain ID `1001`

| สัญญา                 | ที่อยู่ | Explorer |
| --------------------- | ------- | -------- |
| โทเคน USDHN           | —       | —        |
| CollateralRegistry    | —       | —        |
| โทเคนและตะกร้า HNKAIA | —       | —        |
| Lending Pool          | —       | —        |
| KAIA Gateway          | —       | —        |
| vault bKAIA           | —       | —        |
| โทเคน bKAIA           | —       | —        |
| StableSwap Router     | —       | —        |
| พูล USDHN–USDT        | —       | —        |

`—` คือช่องที่ยังไม่ลงข้อมูล บันทึกที่อยู่พร้อมลิงก์สัญญานั้นใน explorer Kairos

ใช้ ABI จากรายการสัญญาที่ยืนยันใน explorer หากเป็น proxy เรียกที่อยู่ proxy ด้วย ABI implementation ข้อมูล reserve Lending ให้ underlying, aToken และโทเคนหนี้สำหรับฝาก ถอน กู้ และชำระ

## แกน CDP

แต่ละสาขามี BorrowerOperations, TroveManager, TroveNFT, พูล และแหล่งราคา เลือก KAIA, HNKAIA หรือ earnUSDT ผ่าน token กับ TroveManager reference ใน CollateralRegistry

| สัญญา                          | อินเทอร์เฟซและสถานะ                                                                                                                                             |
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| โทเคน USDHN                    | ยอด allowance โอน permit: `balanceOf`, `allowance`, `approve`, `transfer`, `permit`                                                                             |
| CollateralRegistry             | `collateralConfig`, `getToken`, `getTroveManager`, `isCollateralActive`, getter ค่าธรรมเนียม, `redeemCollateral`                                                |
| BorrowerOperations             | `openTrove`, `openTroveAndJoinInterestBatchManager`, `addColl`, `withdrawColl`, `withdrawUSDHN`, `repayUSDHN`, `adjustTrove`, `adjustZombieTrove`, `closeTrove` |
| TroveManager                   | `getLatestTroveData`, `getTroveStatus`, `getCurrentICR`, `getLatestBatchData`, `batchLiquidateTroves`, `urgentRedemption`                                       |
| TroveNFT                       | ERC-721 `ownerOf`, โอนเจ้าของ และ `Transfer`                                                                                                                    |
| SortedTroves                   | ลำดับดอกเบี้ยและ hints แทรก                                                                                                                                     |
| AddressesRegistry              | สัญญาอ้างอิงและพารามิเตอร์                                                                                                                                      |
| PriceFeed                      | ดึงราคา ราคาดีล่าสุด และสถานะออราเคิล                                                                                                                           |
| ActivePool / DefaultPool       | หลักประกันและหนี้ active กับที่กระจาย                                                                                                                           |
| CollSurplusPool / GasPool      | หลักประกันส่วนเกินและค่าชดเชยแก๊ส                                                                                                                               |
| HintHelpers / MultiTroveGetter | ค่าธรรมเนียม hints และอ่านโพซิชันชุด                                                                                                                            |
| RedemptionHelper               | `simulateRedemption`, `truncateRedemption`, ไถ่ถอนพร้อมขั้นต่ำแต่ละสาขา                                                                                         |

### พารามิเตอร์กู้ ดอกเบี้ย และไถ่ถอน

`BorrowerOperations.openTrove` รับ owner, owner index, หลักประกัน, USDHN, hints บน/ล่าง, อัตราต่อปี, ค่าล่วงหน้าสูงสุด, add manager, remove manager และ receiver ส่วน `openTroveAndJoinInterestBatchManager` ใช้ batch manager แทนอัตรารายบุคคลใน struct

ควบคุมดอกเบี้ยผ่าน `adjustTroveInterestRate`, `setInterestIndividualDelegate`, `setInterestBatchManager`, `removeFromBatch`, `switchBatchManager` โดย `addManagerOf`, `removeManagerReceiverOf` แสดงการมอบสิทธิ์กับผู้รับ อ่าน `MCR`, `CCR`, `SCR`, `BCR` จาก BorrowerOperations สาขา

ไถ่ถอนข้ามสาขาปกติเข้า `CollateralRegistry.redeemCollateral(usdhnAmount, maxIterationsPerCollateral, maxFeePercentage)` registry เลือกสาขาที่เปิดและไถ่ถอนได้ เผาจำนวนจริง ค่า iteration `0` คือไม่จำกัดตามที่แอปใช้ การจำกัดอาจได้ผลบางส่วน `redeemCollateral` ห้าอาร์กิวเมนต์ของ TroveManager เรียกโดย registry ส่วนหลังปิดใช้ `urgentRedemption`

`RedemptionHelper.redeemCollateral(usdhnAmount, maxIterationsPerCollateral, maxFeePercentage, minCollRedeemed)` เพิ่มขั้นต่ำรับแต่ละสาขา helper ดึง USDHN ด้วย `transferFrom` จึงต้องมี allowance อาร์เรย์ขั้นต่ำตามลำดับสาขา หากไม่ผ่านจะย้อนกลับและคืน USDHN ที่เหลือ `simulateRedemption`, `truncateRedemption` เป็น non-view เพราะอ่านราคาอาจอัปเดต cache ใช้ RPC simulation ดูผล

### สถานะ Trove และหนี้ล่าสุด

| ค่า | สถานะ                 | ความหมาย                                    |
| --- | --------------------- | ------------------------------------------- |
| `0` | `nonExistent`         | ไม่มีโพซิชันของ ID นี้                      |
| `1` | `active`              | เปิดและทำรายการปกติ                         |
| `2` | `closedByOwner`       | เจ้าของปิด                                  |
| `3` | `closedByLiquidation` | ปิดด้วยชำระบัญชี                            |
| `4` | `zombie`              | ยังเปิดแต่นำออกจากลำดับปกติ ใช้ทางปรับเฉพาะ |

`getLatestTroveData` คืน `entireDebt`, `entireColl`, ยอดกระจาย ดอกเบี้ย หนี้บันทึก อัตราต่อปี หนี้ถ่วงน้ำหนัก ค่าจัดการ และเวลาปรับล่าสุด ใช้ `entireDebt`, `entireColl` คำนวณความเสี่ยงปัจจุบัน

ทำดัชนี `TroveUpdated`, `TroveOperation`, `BatchedTroveUpdated`, `BatchUpdated`, `Liquidation`, `Redemption`, `RedemptionFeePaidToTrove` ร่วม `Transfer` ของ NFT เพื่อเจ้าของและประวัติ

## Earn: พูลเสถียรภาพ

StabilityPool แต่ละสาขารับ USDHN และแจกหลักประกันชำระบัญชีกับผลตอบแทน USDHN

| รายการ                    | ฟังก์ชัน                                                                            |
| ------------------------- | ----------------------------------------------------------------------------------- |
| ฝาก                       | `provideToSP(uint256 amount, bool doClaim)`                                         |
| ถอน                       | `withdrawFromSP(uint256 amount, bool doClaim)`                                      |
| รับหลักประกันสะสม         | `claimAllCollGains()`                                                               |
| อ่านยอดฝากเหลือ           | `getCompoundedUSDHNDeposit(address depositor)`                                      |
| อ่านหลักประกันและผลตอบแทน | `getDepositorCollGain`, `getDepositorYieldGain`, `getDepositorYieldGainWithPending` |
| อ่านบัญชีพูล              | `getTotalUSDHNDeposits`, `P`, `currentScale`, `scaleToS`, `scaleToB`                |

`deposits()` คือค่าเริ่มต้นที่เก็บ getter compounded deposit รวมขาดทุนชำระบัญชี `DepositUpdated` บันทึกยอดใหม่และ snapshot ตัวสะสม `DepositOperation` บันทึกรายการ ขาดทุน เปลี่ยนยอด ผลตอบแทน และหลักประกัน `P_Updated`, `S_Updated`, `B_Updated`, `ScaleUpdated` แสดงตัวสะสม ส่วน `offset`, `triggerUSDHNRewards` ควบคุมโดยโปรโตคอล

## Lending

Pool จัดการ reserve หลักประกัน และหนี้บัญชี configuration ให้ที่อยู่ underlying, aToken และหนี้ลอยตัว

| รายการ               | ฟังก์ชัน Pool                                                                                                        |
| -------------------- | -------------------------------------------------------------------------------------------------------------------- |
| ฝาก                  | `supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode)`                                     |
| ถอน                  | `withdraw(address asset, uint256 amount, address to)`                                                                |
| กู้                  | `borrow(address asset, uint256 amount, uint256 interestRateMode, uint16 referralCode, address onBehalfOf)`           |
| ชำระ                 | `repay(address asset, uint256 amount, uint256 interestRateMode, address onBehalfOf)`                                 |
| เปิดปิดหลักประกัน    | `setUserUseReserveAsCollateral(address asset, bool useAsCollateral)`                                                 |
| เลือก eMode          | `setUserEMode(uint8 categoryId)`                                                                                     |
| อ่านบัญชีและ reserve | `getUserAccountData`, `getUserConfiguration`, `getReserveData`, `getConfiguration`, `getReservesList`                |
| ชำระบัญชี            | `liquidationCall(address collateralAsset, address debtAsset, address user, uint256 debtToCover, bool receiveAToken)` |

แอปกู้และชำระด้วยโหมดลอยตัว `2` KAIA native ใช้ `WKaiaGateway.depositKAIA`, `withdrawKAIA`, `borrowKAIA`, `repayKAIA` ถอนใช้ allowance aWKAIA กู้ใช้การมอบสิทธิ์โทเคนหนี้ให้ gateway

ดัชนี `Supply`, `Withdraw`, `Borrow`, `Repay`, `LiquidationCall`, `ReserveDataUpdated`, `ReserveUsedAsCollateralEnabled`, `ReserveUsedAsCollateralDisabled`, `UserEModeSet` ส่วน `PoolConfigurator`, `ACLManager` ให้ผู้ดูแลตั้ง reserve และบทบาท

## โทเคนสเตกและทางเข้า vault

ฝาก ถอน รับ และอ่านใช้ **ที่อยู่ vault bKAIA** กับ ABI เผยแพร่

| รายการ bKAIA           | ทางเข้า                                                                        |
| ---------------------- | ------------------------------------------------------------------------------ |
| ฝาก KAIA native        | Payable `deposit(address receiver, uint256 minShares)`                         |
| ดูส่วนแบ่งและสินทรัพย์ | `previewDeposit`, `convertToShares`, `convertToAssets`                         |
| ขอถอน                  | `requestUnstakeByShares(uint256 shares)` หรือ `requestUnstake(uint256 assets)` |
| รับตั๋ว                | `claim(uint256 ticketId)`, `claimMany(uint256[] ticketIds)`                    |
| จัดการยกเลิก           | `settleCanceled(uint256 ticketId)`                                             |
| อ่านตั๋ว               | `requests(uint256 ticketId)` และ view helper                                   |

`claimMany` คืน `processed`, `totalPaid`, `totalFee` ส่วน `Requested`, `RequestedBatch`, `TicketStatusUpdated`, `ClaimPayout`, `ClaimManySummary`, `ClaimRetryQueued` บันทึกความคืบหน้าและจ่าย ชุดที่สำเร็จอาจเหลือตั๋วรอ

HNKAIA มี `supportedAssets`, `assets`, `previewDeposit`, `deposit`, `previewRedeemProRata`, `redeemProRata` และอ่านแลกคืนรายสินทรัพย์ ส่วนแบ่งกับสินทรัพย์อ้างอิงใช้หน่วยต่างกัน ใช้ทศนิยมและ preview ที่ตรง

## StableSwap และ Zapper

พูล USDHN–USDT ถือทุนสำรองและบันทึก LP ค่าธรรมเนียม และ amplification อ่าน `getReserves`, `getDy`, `getDx`, `quoteAddLiquidity`, `quoteRemoveLiquidity`, `getVirtualPrice` ส่วน `Swap`, `Mint`, `Burn` บันทึกซื้อขายกับสภาพคล่อง

router มี `getAmountsOut`, `getAmountsIn`, `swapExactTokensForTokens`, `swapTokensForExactTokens`, `addLiquidity`, `removeLiquidity` และแบบ permit input แน่นอนตั้งขั้นต่ำรับ output แน่นอนตั้งสูงสุดจ่าย struct รวมเส้นทาง ผู้รับ และ deadline ส่วน Lens ให้ราคาเสนอละเอียด RewardStaker บันทึก LP และรางวัล

Zapper สาขาแรปและทำ Trove เลเวอเรจมี `openLeveragedTrove`, `leverUpTrove`, `leverDownTrove`, `closeTroveByCollateral` สามตัวแรกใช้ `maxDebt` close struct มี `troveId`, `profitReceiver` ไม่มีขั้นต่ำรับของผู้ใช้ callback FlashSwapper รับพูลที่กำหนดและรายการ active ชำระ flash แล้วคืนส่วนเกิน

ดู[คู่มือเชื่อมต่อ](/th/developers/integrator-kit.md) สำหรับวอลเล็ต สิทธิ์ และ event [ภาพรวมเทคนิค](/th/developers/technical-overview.md) อธิบายสัญญากับสินทรัพย์


---

# 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.hann.finance/th/developers/smart-contracts.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.
