# Constant-Product AMM

`D25-F01-A01` · 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-product-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 { constantProductQuote } from "fintech-algorithms/digital-assets-and-on-chain-finance/amm-pricing/constant-product-amm";
```

## Signature

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

The `x · y = k` curve behind Uniswap v2. Price is set by the reserve ratio, so every trade moves it — and the slippage a trade suffers is a deterministic function of its size relative to the pool.

## 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, taken from the input before the swap. · min: 0, max: 1 |

## Returns

`{ amountOut, priceImpact, effectivePrice, spotPriceAfter, … }`

Output amount with the price impact and the post-trade spot price.

## Errors

- When a reserve is zero, or feeRate falls outside 0…1 — throws

## 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
constantProductQuote(reserve0, reserve1, amount0In, feeRate)
```

### Returns

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

```json
{
  "model": "constant-product",
  "reserve0Before": 1000,
  "reserve1Before": 1000,
  "amount0In": 10,
  "effectiveAmount0In": 9.97,
  "feeAmount0": 0.03,
  "amount1Out": 9.871580343971,
  "reserve0After": 1010,
  "reserve1After": 990.128419656029,
  "invariantBefore": 1000000,
  "invariantAfter": 1000029.7038525896,
  "spotPriceBeforeToken1PerToken0": 1,
  "spotPriceAfterToken1PerToken0": 0.980325167976,
  "executionPriceToken1PerToken0": 0.987158034397
}
```

Showing 14 of 16 fields.

## Other exports

`constantSumQuote`, `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-product-amm/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/digital-assets-and-on-chain-finance/amm-pricing/constant-product-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
