# StableSwap Invariant

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

Full page: https://docs.thefintechbuilder.com/digital-assets-and-on-chain-finance/amm-pricing/stableswap-invariant/
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 { stableSwapQuote } from "fintech-algorithms/digital-assets-and-on-chain-finance/amm-pricing/stableswap-invariant";
```

## Signature

```ts
stableSwapQuote(balances, amountIn, amplification, feeRate, inputIndex, outputIndex)
```

Curve's invariant: nearly constant-sum near the peg, constant-product away from it. The amplification coefficient sets where that transition happens, and it is what lets a stablecoin pool quote size with almost no slippage until it suddenly cannot.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `balances` | `number[]` | yes | Pool balances for every asset. |
| `amountIn` | `number` | yes | Input amount. · min: 0 |
| `amplification` | `number` | yes | Amplification coefficient. Higher keeps the curve flat further from the peg — and makes depegging more violent when it comes. · min: 0 |
| `feeRate` | `number` | yes | Fee as a fraction. · min: 0, max: 1 |
| `inputIndex` | `number` | yes | Index of the input asset in balances. · min: 0, integer: true |
| `outputIndex` | `number` | yes | Index of the output asset. · min: 0, integer: true |

## Returns

`{ amountOut, invariant, priceImpact, iterations, converged, … }`

Output with the invariant D and whether the Newton solve converged — an unconverged quote must not be traded on.

## Errors

- When the indices are equal or out of range, or the solve fails to converge — throws

## Complexity

Time `O(assets × iterations)`, space `O(assets)`.

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

`balances`:

```json
[1000, 1000]
```

`amountIn`:

```json
10
```

`amplification`:

```json
100
```

`feeRate`:

```json
0.0004
```

### Call

```ts
stableSwapQuote(balances, amountIn, amplification, feeRate, inputIndex, outputIndex)
```

### Returns

object with 18 fields: model, balancesBefore, balancesAfter, inputIndex, outputIndex, amountIn, grossAmountOut, feeAmountOut, …

```json
{
  "model": "stableswap",
  "balancesBefore": [1000, 1000],
  "balancesAfter": [1010, 990.004497337927],
  "inputIndex": 0,
  "outputIndex": 1,
  "amountIn": 10,
  "grossAmountOut": 9.999502463058,
  "feeAmountOut": 0.003999800985,
  "amountOut": 9.995502662073,
  "executionPriceOutputPerInput": 0.999550266207,
  "priceImpactFromPegFraction": 0.000049753694,
  "DBefore": 2000,
  "DAfterFeeRetained": 2000.004000000989,
  "DIterations": 1
}
```

Showing 14 of 18 fields.

## Other exports

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