> 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/bond-zhi-ya-he-yue.md).

# Bond质押合约

该合约负责 Bond 购买、SUN 单币质押、订单结算、管理费以及本金和个人收益领取。普通用户查询订单时，推荐优先使用 BondInfo，因为 BondInfo 会把原始字段和实时可领取金额组合成一个结果。

### 查询用户订单编号

| 函数                            | 用途                      |
| ----------------------------- | ----------------------- |
| `userBondOrderCount(user)`    | 查询用户的 Bond 订单数量         |
| `userBondOrders(user,index)`  | 按从 0 开始的索引读取 Bond 订单 ID |
| `userStakeOrderCount(user)`   | 查询用户的单币质押订单数量           |
| `userStakeOrders(user,index)` | 按从 0 开始的索引读取质押订单 ID     |

Bond 与质押共用全局递增的 `orderId`。因此订单编号不保证在某个用户或某种订单中连续。

### 查询订单原始数据

* `orderBase(orderId)`：ID、创建用户、当前持有人、订单种类、状态、产品类型、开始和结束时间、管理费有效期。
* `orderAmounts(orderId)`：本金、已领取本金、复利基数、已结算收益、动态奖励相关数值及累计领取。
* `orderIndexes(orderId)`：订单上次结算使用的收益指数和 rebase 编号。

关键枚举：`kind=1` 表示 Bond，`kind=2` 表示 SUN 单币质押；`status=1` 表示正常，`status=2` 表示已抵押。只有当前 `holder` 才能领取，已抵押订单不能通过普通领取流程操作。

### 领取收益

```
claimReward(orderId, amount)
```

* `amount` 使用 SUN 最小单位，不能为 0。
* 传入精确数量可领取部分收益。
* 传入 `uint256` 最大值表示领取当前全部可领取收益。
* 该操作会先结算订单，然后把收益计入**涡轮合约的 `claimableReward`**；它不是直接把 SUN 转入用户钱包。

### 领取本金

```
claimPrincipal(orderId, amount)
```

该函数领取已经线性释放的本金，并把 SUN 直接转给当前订单持有人。`amount` 不能超过实时 `claimablePrincipal`；同样可用最大 `uint256` 表示领取全部当前可领取本金。

在写入前，先调用 `BondInfo.getOrder(orderId)` 查看 `holder`、`status`、`claimableReward` 和 `claimablePrincipal`。

### 其他用户操作

| 函数                                       | 说明                                     |
| ---------------------------------------- | -------------------------------------- |
| `buyBond(typeId,usdtAmount)`             | 使用 USDT 购买指定类型 Bond；需提前授权 USDT         |
| `stake(typeId,sunAmount)`                | 创建 SUN 单币质押；需提前授权 SUN，并授权用于初始管理费的 USDT |
| `payManagementFee(orderId,periods)`      | 为订单续付若干周期管理费                           |
| `settleOrder(orderId)`                   | 主动触发单个订单结算，不直接领取                       |
| `transferOrderHolder(orderId,newHolder)` | 转移订单持有人身份，需谨慎使用                        |

购买或质押前，应先用 BondInfo 的 `quoteBondPurchase` 或 `quoteInitialManagementFee` 获取预估值。

> ## 链上领取示例
>
> 先通过Bondinfo查询自身的订单
>
> <h3 align="center"></h3>

<figure><img src="https://554674054-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7CKKhHI2WonLxUL3XwJ6%2Fuploads%2FdpId1BEn7WqQ78YLQwM2%2F%E6%9F%A5%E8%AF%A2%E8%AE%A2%E5%8D%95.png?alt=media&amp;token=33476863-cd1e-46f7-bad5-90c41a629e66" alt=""><figcaption></figcaption></figure>

BOND质押合约领取4633订单的本金和收益

<figure><img src="https://554674054-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7CKKhHI2WonLxUL3XwJ6%2Fuploads%2FRMKa8JAmt38qLSyoLMCQ%2F%E9%A2%86%E5%8F%96%E6%9C%AC%E9%87%91%E5%92%8C%E5%88%A9%E6%B6%A6.png?alt=media&amp;token=f99c5eee-fb44-4942-bbff-a710dac29522" alt=""><figcaption></figcaption></figure>


---

# 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/bond-zhi-ya-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.
