> ## Documentation Index
> Fetch the complete documentation index at: https://docs.geode.ag/llms.txt
> Use this file to discover all available pages before exploring further.

# ConstantProductCurveLib.sol

> 纯函数库，实现 Geocurve——永久的虚拟恒定乘积联合曲线。

## 概述

`ConstantProductCurveLib` 实现 Geocurve 联合曲线数学。它使用**虚拟恒定乘积**公式——与 Uniswap 相同的 `x × y = k`，但有锚定起始价格的虚拟储备。

此库处理所有曲线模式结算：从曲线计算出清价格、确定成交，以及计算分发或吸收多少代币。

## 曲线公式

```
(Vt - S) × (Ve + E) = K = Vt × Ve
```

| 变量     | 含义                        |
| ------ | ------------------------- |
| **Vt** | 虚拟代币储备（不可变，≥ totalSupply） |
| **Ve** | 虚拟 ETH 储备（不可变）            |
| **S**  | 累积分发的代币                   |
| **E**  | 累积的真实 ETH                 |
| **K**  | 不变量常数                     |

## 核心函数

### `computeLaunchSettlement()`

曲线模式批量结算的主入口。替代曲线池的 `ClearingPriceLib.computeClearingPrice()`。

```solidity theme={null}
function computeLaunchSettlement(
    GeodeIntent[] memory buys,
    GeodeIntent[] memory sells,
    uint256 maxBatchSize,
    LaunchState memory ls,
    uint256 settlementFeeBps,
    uint256 deployerRoyaltyBps
) internal pure returns (
    SettlementResult memory result,
    bool[] memory buyFills,
    bool[] memory sellFills
)
```

### `curvePrice()`

返回曲线上的当前边际价格：

```
price = (Ve + E) / (Vt - S)
```

### `ethCostToBuy()`

从当前状态购买 `deltaTokens` 代币的 ETH 成本：

```
ethIn = K / (Vt - S - Δs) - (Ve + E)
```

### `tokensForEth()`

花费 `ethIn` ETH 可获得多少代币：

```
Δs = (Vt - S) - K / (Ve + E + ethIn)
```

### `ethReturnOnSell()`

卖出 `deltaTokens` 代币可获得多少 ETH：

```
ethOut = (Ve + E) - K / (Vt - S + Δs)
```

## 价格行为

| 操作  | S    | E    | 价格   |
| --- | ---- | ---- | ---- |
| 买入  | ↑ 增加 | ↑ 增加 | ↑ 上升 |
| 卖出  | ↓ 减少 | ↓ 减少 | ↓ 下降 |
| 无操作 | 不变   | 不变   | 不变   |

曲线**完全可逆**——代币可以随时买卖。曲线是永久的；没有毕业或迁移。

<Warning>
  **渐近定价**：当 `Vt = totalSupply` 时，随着 100% 代币被分发，价格趋近无穷大。当 `Vt > totalSupply` 时，曲线在全部分发时有有限的最高价格。
</Warning>

## 源代码

<Card title="ConstantProductCurveLib.sol" icon="github" href="https://github.com/Geode-vAMM/geodex/blob/main/contracts/src/libraries/ConstantProductCurveLib.sol">
  在 GitHub 上查看完整源代码（约 269 行）。
</Card>
