# Weighted-Product AMM

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

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

## Signature

```ts
weightedProductQuote(balanceIn, balanceOut, weightIn, weightOut, amountIn, feeRate)
```

Balancer's generalisation of constant product to arbitrary weights, so a pool can hold 80/20 rather than 50/50. The weights set both the target allocation and the slippage profile.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `balanceIn` | `number` | yes | Balance of the input token. · min: 0 |
| `balanceOut` | `number` | yes | Balance of the output token. · min: 0 |
| `weightIn` | `number` | yes | Normalised weight of the input token. · min: 0, max: 1 |
| `weightOut` | `number` | yes | Normalised weight of the output token. · min: 0, max: 1 |
| `amountIn` | `number` | yes | Input amount. · min: 0 |
| `feeRate` | `number` | yes | Fee as a fraction. · min: 0, max: 1 |

## Returns

`{ amountOut, priceImpact, spotPrice, … }`

Output amount with the weighted spot price.

## Errors

- When a weight is zero, or a balance is not positive — 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

`balanceIn`:

```json
800
```

`balanceOut`:

```json
200
```

`weightIn`:

```json
0.8
```

`weightOut`:

```json
0.2
```

`amountIn`:

```json
40
```

`feeRate`:

```json
0.003
```

### Call

```ts
weightedProductQuote(balanceIn, balanceOut, weightIn, weightOut, amountIn, feeRate)
```

### Returns

object with 18 fields: model, balanceInBefore, balanceOutBefore, weightIn, weightOut, amountIn, effectiveAmountIn, feeAmountIn, …

```json
{
  "model": "weighted-product",
  "balanceInBefore": 800,
  "balanceOutBefore": 200,
  "weightIn": 0.8,
  "weightOut": 0.2,
  "amountIn": 40,
  "effectiveAmountIn": 39.88,
  "feeAmountIn": 0.12,
  "amountOut": 35.365448312358,
  "balanceInAfter": 840,
  "balanceOutAfter": 164.634551687642,
  "invariantBefore": 606.286626604159,
  "invariantAfterFeeRetained": 606.355925414232,
  "spotPriceBeforeOutputPerInput": 1
}
```

Showing 14 of 18 fields.

## Other exports

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