> ## 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

> Pure library implementing the Geocurve — a permanent virtual constant-product bonding curve.

## Overview

`ConstantProductCurveLib` implements the Geocurve bonding curve math. It uses a **virtual constant-product** formula — the same `x × y = k` as Uniswap, but with virtual reserves that anchor the starting price.

This library handles all curve-mode settlement: computing the clearing price from the curve, determining fills, and calculating how many tokens to dispense or absorb.

## Curve Formula

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

| Variable | Meaning                                          |
| -------- | ------------------------------------------------ |
| **Vt**   | Virtual token reserve (immutable, ≥ totalSupply) |
| **Ve**   | Virtual ETH reserve (immutable)                  |
| **S**    | Cumulative tokens distributed                    |
| **E**    | Cumulative real ETH raised                       |
| **K**    | Invariant constant                               |

## Core Functions

### `computeLaunchSettlement()`

Main entry point for curve-mode batch settlement. Replaces `ClearingPriceLib.computeClearingPrice()` for curve pools.

```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
)
```

**Behavior**:

1. Computes the current curve price as the clearing price
2. Determines which intents fill at that price
3. Deducts settlement fees from inputs
4. Computes internal match amounts
5. Calculates `curveDispensed` (net buy residual → tokens from hook) and `curveAbsorbed` (net sell residual → tokens back to hook)
6. Computes deployer royalty

### `curvePrice()`

Returns the current marginal price on the curve:

```solidity theme={null}
function curvePrice(
    uint256 Vt, uint256 Ve,
    uint256 cumulativeDistributed, uint256 ethReserve
) internal pure returns (uint256 priceX18)
```

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

### `ethCostToBuy()`

How much ETH to buy `deltaTokens` from the current state:

```solidity theme={null}
function ethCostToBuy(
    uint256 Vt, uint256 Ve,
    uint256 cumulativeDistributed, uint256 ethReserve,
    uint256 deltaTokens
) internal pure returns (uint256 ethIn)
```

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

### `tokensForEth()`

How many tokens for spending `ethIn` ETH:

```solidity theme={null}
function tokensForEth(
    uint256 Vt, uint256 Ve,
    uint256 cumulativeDistributed, uint256 ethReserve,
    uint256 ethIn
) internal pure returns (uint256 deltaTokens)
```

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

### `ethReturnOnSell()`

How much ETH returned for selling `deltaTokens`:

```solidity theme={null}
function ethReturnOnSell(
    uint256 Vt, uint256 Ve,
    uint256 cumulativeDistributed, uint256 ethReserve,
    uint256 deltaTokens
) internal pure returns (uint256 ethOut)
```

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

## Price Behavior

| Action  | S           | E           | Price     |
| ------- | ----------- | ----------- | --------- |
| Buy     | ↑ increases | ↑ increases | ↑ rises   |
| Sell    | ↓ decreases | ↓ decreases | ↓ falls   |
| Nothing | unchanged   | unchanged   | unchanged |

The curve is **fully reversible** — tokens can be bought and sold at any time. The curve is permanent; there is no graduation or migration.

<Warning>
  **Asymptotic pricing**: When `Vt = totalSupply`, the price approaches infinity as 100% of tokens are distributed. When `Vt > totalSupply`, the curve has a finite maximum price at full distribution.
</Warning>

## Source

<Card title="ConstantProductCurveLib.sol" icon="github" href="https://github.com/Geode-vAMM/geodex/blob/main/contracts/src/libraries/ConstantProductCurveLib.sol">
  View the full source code on GitHub (\~269 lines).
</Card>
