> 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/integrator-kit.md).

# คู่มือเชื่อมต่อระบบ

เลือกสัญญา เตรียมธุรกรรมวอลเล็ต และจัดทำดัชนีสำหรับ Hann Finance

เลือกเครือข่าย ใช้ที่อยู่สัญญาและ ABI ที่ยืนยัน เตรียมคำสั่งวอลเล็ต แล้วจัดทำดัชนี event [เอกสารสัญญา](/th/developers/smart-contracts.md) มีที่อยู่และทางเข้า

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

| เครือข่าย | Chain ID | การใช้                                   |
| --------- | -------- | ---------------------------------------- |
| Kaia      | `8217`   | Mainnet สัญญา Hann Finance ยังไม่ deploy |
| Kairos    | `1001`   | Testnet สำหรับทดสอบการเชื่อมต่อ          |

เลือกที่อยู่จาก[ตารางสัญญา](/th/developers/smart-contracts.md) ใช้ ABI จาก source ที่ยืนยันใน explorer หากเป็น proxy ให้เรียกที่อยู่ proxy ด้วย ABI implementation

เก็บ chain ID ที่อยู่ และ ABI ด้วยกัน อ่านที่อยู่หลักประกัน CDP และ underlying, aToken, โทเคนหนี้ของ reserve ผ่าน getter สาธารณะ

ก่อนขอลายเซ็น ตรวจ RPC และวอลเล็ตให้ใช้ chain ID ตรงกับที่อยู่สัญญา

### อ่านยอด USDHN

ส่งที่อยู่ USDHN, chain ID, RPC URL และวอลเล็ต ฟังก์ชันตรวจเครือข่ายแล้วคืนยอดในหน่วยพื้นฐาน

```ts
import { createPublicClient, http, type Address } from "viem";

const balanceOfAbi = [
  {
    type: "function",
    name: "balanceOf",
    stateMutability: "view",
    inputs: [{ name: "account", type: "address" }],
    outputs: [{ name: "", type: "uint256" }],
  },
] as const;

export async function getUsdhnBalance({
  contractAddress,
  chainId,
  rpcUrl,
  walletAddress,
}: {
  contractAddress: Address;
  chainId: number;
  rpcUrl: string;
  walletAddress: Address;
}): Promise<bigint> {
  const client = createPublicClient({ transport: http(rpcUrl) });
  const rpcChainId = await client.getChainId();
  if (rpcChainId !== chainId) {
    throw new Error("RPC chain ID does not match the selected network");
  }
  return client.readContract({
    address: contractAddress,
    abi: balanceOfAbi,
    functionName: "balanceOf",
    args: [walletAddress],
  });
}
```

USDHN ใช้ 18 ทศนิยม เก็บจำนวนเป็น `bigint` แล้วแสดงด้วย `formatUnits(balance, 18)` สินทรัพย์อื่นใช้ทศนิยมของตน หน่วยพื้นฐาน KAIA native คือ 10⁻¹⁸ KAIA

## 2. กำหนดรายการและผู้ใช้จ่าย

| รายการ                  | เป้าหมายธุรกรรม                       | สิทธิ์หรือลายเซ็น                                                  |
| ----------------------- | ------------------------------------- | ------------------------------------------------------------------ |
| กู้ CDP                 | `BorrowerOperations` หรือ Zapper สาขา | allowance หลักประกัน ทางเข้า Zapper กำหนด Permit2                  |
| ไถ่ถอน CDP ตรง          | `CollateralRegistry.redeemCollateral` | registry เผา USDHN ผู้เรียกโดยตรง ไม่ใช้ allowance                 |
| ฝาก Earn                | `StabilityPool.provideToSP` สาขา      | พูลลงทะเบียนย้าย USDHN ผ่าน `sendToPool` ไม่ต้อง allowance ERC-20  |
| ฝากหรือชำระ Lending     | `Pool`                                | allowance underlying ให้ Pool                                      |
| ฝากหรือชำระ KAIA native | `WKaiaGateway`                        | KAIA ใน `msg.value`                                                |
| ถอน KAIA native         | `WKaiaGateway.withdrawKAIA`           | allowance aWKAIA ให้ gateway                                       |
| กู้ KAIA native         | `WKaiaGateway.borrowKAIA`             | `approveDelegation` ของโทเคนหนี้ลอยตัวให้ gateway                  |
| ฝากหรือขอถอน bKAIA      | `LSTVault`                            | KAIA native สำหรับฝาก ส่วนแบ่งของตนสำหรับ `requestUnstakeByShares` |
| สวอปหรือ LP             | StableSwap Router หรือ Zapper         | allowance โทเคนเข้า/LP และ Permit2 ตามทางเข้า                      |

`approve` ตั้ง allowance ERC-20 ส่วน `permit` ใช้ลายเซ็นที่มี domain, nonce, spender, จำนวน และ deadline Permit2 มีข้อกำหนดสิทธิ์และลายเซ็นของทางเข้า `approveDelegation` ให้สิทธิ์กู้

อ่าน allowance หรือ delegation เดิมแล้วขอจำนวนที่จำเป็น ทำต่อหลังอนุมัติยืนยันแล้ว Zapper รวมรายการกับเส้นทาง สิทธิ์ที่สำเร็จก่อนยังอยู่หากเส้นทางย้อนกลับ

การเรียก `BorrowerOperations` ตรงกำหนดพารามิเตอร์ Trove และผู้จัดการ Zapper เพิ่มแรป native, Permit2 และเส้นทาง โพซิชันที่เปิดตรงต้องส่งมอบผู้จัดการถูกต้องก่อนปิดผ่านเส้นทางเลเวอเรจ

## 3. เตรียมธุรกรรมจากสถานะปัจจุบัน

1. อ่านโพซิชัน เจ้าของ พารามิเตอร์สาขา/reserve ทศนิยม และยอด
2. สร้างรายการตาม ABI Trove ต้องมี hints ลำดับและขีดจำกัดค่าล่วงหน้า Lending ลอยตัวใช้โหมด `2`
3. แนบขีดจำกัด: StableSwap ขั้นต่ำรับหรือสูงสุดจ่ายกับ deadline, Zapper `maxDebt` และค่าล่วงหน้าสูงสุด
4. เรียก `simulateContract` ด้วย sender, arguments และ value จริง แสดงหลักประกัน หนี้ ยอดรับ ค่าธรรมเนียม และความเสี่ยงโดยประมาณก่อนขอลายเซ็น การจำลองใช้บล็อกที่จำลอง การทำจริงใช้บล็อกที่บรรจุธุรกรรม
5. ส่งผ่านวอลเล็ตแล้วตรวจ receipt หากย้อนกลับสถานะโปรโตคอลคงเดิมและยังเสียแก๊ส การรับ bKAIA แบบชุดที่สำเร็จอาจมีบางตั๋วล้มเหลวหรือลองใหม่
6. รีเฟรชสัญญาที่กระทบ ติดตาม receipt กับ indexer แยกจนยอดและโพซิชันใหม่พร้อม

CDP ใช้ `getLatestTroveData(troveId).entireDebt` และ `entireColl` ร่วมราคาและขีดจำกัดสาขา Lending ใช้ `getUserAccountData(walletAddress)` กับ reserve รวมหลักประกันและ eMode แสดงเงื่อนไขชำระบัญชีของผลิตภัณฑ์พร้อมค่าโพซิชัน

### เส้นทาง flash

Zapper และ FlashSwapper ที่มีสิทธิ์ควบคุม add manager, remove manager และ receiver ระหว่างทำ callback ชำระสวอป ปรับ Trove จ่ายพูล DEX และคืนสิทธิ์ ราคาเสนอต้องใช้พูลกับโทเคนที่ตั้งไว้ สัญญาตรวจค่า flash และหนี้สุดท้ายแล้วคืนส่วนเกิน

`closeTroveByCollateral` รับ tuple ที่มี `troveId` และ `profitReceiver` ขายหลักประกันชำระหนี้เต็มล่าสุดแล้วคืนส่วนเกิน ABI ไม่มี `minOut` ที่ผู้ใช้กำหนด แสดงผลขายหลักประกันและสินทรัพย์ที่จะคืนก่อนปิด

## 4. จัดทำดัชนีโพซิชันและผล

| เรื่อง             | Event และการอ่าน                                                                                                                 |
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------- |
| เจ้าของ Trove      | `TroveNFT.Transfer` สาขา; `ownerOf(troveId)`                                                                                     |
| บัญชี Trove        | `TroveUpdated`, `TroveOperation`, `BatchedTroveUpdated`, `BatchUpdated`; `getLatestTroveData`, `getTroveStatus`                  |
| ชำระบัญชีและไถ่ถอน | TroveManager `Liquidation`, `Redemption`, `RedemptionFeePaidToTrove`                                                             |
| Earn               | `DepositUpdated`, `DepositOperation`; getter ยอดฝากปัจจุบัน หลักประกัน และผลตอบแทน USDHN                                         |
| Lending            | `Supply`, `Withdraw`, `Borrow`, `Repay`, `LiquidationCall`, `ReserveDataUpdated`; อ่าน reserve กับบัญชี                          |
| StableSwap         | พูล `Swap`, `Mint`, `Burn`; router `LiquidityAdded`, `LiquidityRemoved` และ event คืนเงิน                                        |
| bKAIA              | `Deposit`, `RequestedBatch`, `Requested`, `UnstakeShortfall`, `TicketStatusUpdated`, `ClaimPayout`, `ClaimManySummary` และ retry |

ระบุ log ด้วย chain ID, block hash, transaction hash และ log index เก็บสัญญาที่ส่งกับสาขาคู่ Trove ID ติดตาม NFT และหนี้เพื่ออัปเดตเจ้าของ จัดการบล็อกที่ถูกลบหรือแทนก่อนใช้ projection ใหม่

Event บันทึก ณ บล็อกนั้น ดอกเบี้ยกับราคาเปลี่ยนต่อได้ รวมประวัติกับการอ่านปัจจุบันเพื่อแสดงยอดและความเสี่ยง bKAIA ใช้สถานะตั๋วและ event จ่าย `claimMany` คืน `processed`, `totalPaid`, `totalFee` และต้องตรวจผลจ่ายรายตั๋วด้วย

## โอนข้ามเชน

ใช้ที่อยู่เผยแพร่ ABI ยืนยัน และราคาเสนอผู้ให้บริการ ตรวจเชนต้นทาง/ปลายทาง โทเคน ผู้รับ จำนวน ค่าใช้จ่าย และ spender ก่อนลงนาม ส่งคำสั่งต้นทางพร้อมสิทธิ์ที่ต้องใช้ แล้วติดตามการยืนยันกับส่งมอบแยกกัน

USDHN ข้ามเชนใช้ `USDHNOFTAdapter` หรือ `USDHNOFT` ปลายทางกับ ABI, peer และค่าธรรมเนียมที่ตรง OFT `send` ใช้ผ่านอินเทอร์เฟซข้ามเชน ส่วน `USDHNToken` ให้ ERC-20


---

# 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/integrator-kit.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.
