# Constant-Sum AMM

`D25-F01-A02` · Digital Assets and On-Chain Finance → AMM Pricing · archetype `record-transform` · difficulty 2/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/digital-assets-and-on-chain-finance/amm-pricing/constant-sum-amm/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { constantSumQuote } from "fintech-algorithms/digital-assets-and-on-chain-finance/amm-pricing/constant-sum-amm";
```

## Signature

```ts
constantSumQuote(reserve0, reserve1, amount0In, feeRate)
```

The `x + y = k` curve: zero slippage, and the pool can be fully drained of one asset. Never used alone for that reason — it is the component that makes stableswap flat near the peg.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `reserve0` | `number` | yes | Reserve of the input token. · min: 0 |
| `reserve1` | `number` | yes | Reserve of the output token. · min: 0 |
| `amount0In` | `number` | yes | Input amount. · min: 0 |
| `feeRate` | `number` | yes | Fee as a fraction. · min: 0, max: 1 |

## Returns

`{ amountOut, depleted, effectivePrice, … }`

Output amount with an explicit depletion flag for the case the curve permits and reality does not.

## Errors

- When the output reserve cannot cover the trade — reported as depleted rather than thrown

## Complexity

Time `O(1)`, space `O(1)`.

## Worked example

Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

### Input

`reserve0`:

```json
1000
```

`reserve1`:

```json
1000
```

`amount0In`:

```json
10
```

`feeRate`:

```json
0.003
```

### Call

```ts
constantSumQuote(reserve0, reserve1, amount0In, feeRate)
```

### Returns

object with 16 fields: model, reserve0Before, reserve1Before, amount0In, effectiveAmount0In, feeAmount0, amount1Out, reserve0After, …

```json
{
  "model": "constant-sum",
  "reserve0Before": 1000,
  "reserve1Before": 1000,
  "amount0In": 10,
  "effectiveAmount0In": 9.97,
  "feeAmount0": 0.03,
  "amount1Out": 9.97,
  "reserve0After": 1010,
  "reserve1After": 990.03,
  "invariantBefore": 2000,
  "invariantAfter": 2000.03,
  "spotPriceBeforeToken1PerToken0": 1,
  "executionPriceToken1PerToken0": 0.997,
  "remainingExactFillCapacityToken0": 993.009027081244
}
```

Showing 14 of 16 fields.

## Other exports

`constantProductQuote`, `stableSwapInvariant`, `stableSwapQuote`, `weightedProductQuote`, `concentratedLiquidityPosition`. Every module additionally exports `run` as an alias of its primary
function, and a `meta` object carrying its catalog id, domain, family, shape and article URL.

## Verification and provenance

Tier: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.1.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/digital-assets-and-on-chain-finance/amm-pricing/constant-sum-amm/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/digital-assets-and-on-chain-finance/amm-pricing/constant-sum-amm/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/digital-assets-and-on-chain-finance/llms.txt
