> 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/zh/mechanics/deep-dives/stableswap.md).

# StableSwap

双资产 StableSwap 曲线、动态费用、执行限制和脱锚时的 LP 价值

Hann StableSwap 池持有 USDHN 和 USDT。储备接近平衡时，放大系数可降低两者兑换的价格影响；交易消耗一侧储备时，价格影响增加。报价反映当前储备、放大系数、费用设置和代币精度。

## 储备与放大系数

应用双资产不变量前，池子将余额归一化到 18 位精度。USDT 为 6 位，USDHN 为 18 位，须先统一单位。

对正的归一化储备 `x`、`y`，令 `A` 为 `currentA()` 返回值，`D` 为不变量。忽略整数舍入，求解器满足：

$$
2A(x+y)+D=2AD+\frac{D^3}{4xy}
$$

等量储备时 `D = x + y`。兑换增加输入储备，再解出保持 `D` 的输出储备，最后应用费用和舍入。

放大系数越高，平衡附近曲线越平。可用输出仍由储备决定，脱锚后兑换率会随资产失衡变化。当前 `A` 也可按配置的渐变计划调整。

## 动态费用

基础费率是 `swapFee`，失衡倍率是 `offpegFeeMultiplier`，分母为 `10,000` 基点。`4` bps 等于 `0.04%`；实际应用读取当前池费率。

倍率超过 `10,000` 时，计算储备越偏离平衡，动态费率越高。基础费率 `f`、倍率 `m`、`B = 10,000`、归一化余额 `x`、`y` 的连续形式为：

$$
f\_{dynamic}=\frac{mf}{B+(m-B)\frac{4xy}{(x+y)^2}}
$$

`x = y` 时 `4xy/(x+y)^2` 为 1，费用回到 `f`。一侧变小时，该分数下降，费用趋于 `mf/B`。合约按交易余额用整数计算，报价包含此结果。`m ≤ B` 或基础费为零时使用基础费。

管理费占兑换费的一部分，其余计入池内流动性。失衡存款在计算 LP 数量前也收失衡费。

## 精确输入与精确输出

应用的 USDHN/USDT 路径只有一个池子，支持两类订单：

| 订单   | 固定数量 | 链上边界                    |
| ---- | ---- | ----------------------- |
| 精确输入 | 支付量  | 实际输出至少为 `amountOutMin`。 |
| 精确输出 | 到账量  | 实际输入不超过 `amountInMax`。  |

精确输入报价 `995` 枚，滑点 `0.5%`（`50` bps）：

$$
minOut\approx995\left(1-\frac{50}{10{,}000}\right)=990.025
$$

精确输出则在报价输入上增加容忍比例，计算最高支付量。两者均转为代币精度。报价已含费用，滑点设置允许报价到执行之间的变化。

路由验证工厂注册、代币顺序和允许名单，适用于其报价、兑换和存款。直接池调用遵循池合约自身规则。

## 期限与失败

期限是兑换或流动性交易最晚可执行时间。过期、未满足输入／输出边界、池暂停、流动性不足或不支持的转账扣费代币都会使对应交易回滚。

报价后其他交易仍可能改变余额。期限限定时间窗口，最低输出和最高输入限定允许结果。

回滚撤销该交易的池变化和代币转移。此前确认的授权保留，失败交易仍消耗 Gas。

## LP 代表的资产

LP 代表计入费用后的池余额份额。持有总供应比例 `s` 时，按比例提取约得到各可用储备的 `s` 倍，受代币舍入和输入最低量约束。

应用提取销毁 LP，返还 USDHN 和 USDT 两种资产。合约级单币提取另有曲线和费用影响。池暂停时仍可按比例移除。

虚拟价格从每 LP 的 `D` 推导，衡量不变量下的归一化池价值。脱锚时，美元价值还取决于各资产外部价格。

质押 LP 继续承担池内风险。RewardStaker 记录质押余额和奖励，领取奖励后 LP 对应的池资产份额保持原状。

## 脱锚如何改变池子

USDHN 市价低于 USDT 时，交易者可在外部买入廉价 USDHN，卖入池中换 USDT。USDHN 储备增加、USDT 减少，LP 持有更多较弱资产。

USDHN 高于 USDT 时，交易者用 USDT 从池子买入 USDHN，再外部卖出。USDHN 储备减少、USDT 增加，LP 持有更少升值资产。

### 两个余额示例

起点为 `1,000,000` USDHN 和 `1,000,000` USDT，各值 `$1`。`10%` LP 份额代表各 `100,000`，总值 `$200,000`。

以下以给定期末余额和固定 `10%` 份额计算，余额为示例输入，费用及其他 LP 存取排除在外。

| 数值          | USDHN 跌至 `$0.90`                            | USDHN 涨至 `$1.05`                            |
| ----------- | ------------------------------------------- | ------------------------------------------- |
| 期末池余额       | `1,800,000` USDHN + `200,000` USDT          | `200,000` USDHN + `1,800,000` USDT          |
| 你的 `10%` 份额 | `180,000` USDHN + `20,000` USDT             | `20,000` USDHN + `180,000` USDT             |
| 份额价值        | `180,000 × $0.90 + 20,000 × $1 = $182,000`  | `20,000 × $1.05 + 180,000 × $1 = $201,000`  |
| 持有原代币的价值    | `100,000 × $0.90 + 100,000 × $1 = $190,000` | `100,000 × $1.05 + 100,000 × $1 = $205,000` |
| 相对持有的差额     | `−$8,000`                                   | `−$4,000`                                   |

差额来自代币构成变化。提取最低量约束数量，市场价格决定其价值。评估时将池手续费、质押奖励与此风险一起考虑。

操作步骤见[兑换、跨链与流动性](/zh/protocol/stableswap-dex.md)，授权和 LP 组合操作见[Zapper](/zh/protocol/zapper.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/zh/mechanics/deep-dives/stableswap.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.
