fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Weighted-Product AMM

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#

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#

NameTypeNotes
balanceInnumberBalance of the input token.
min: 0
balanceOutnumberBalance of the output token.
min: 0
weightInnumberNormalised weight of the input token.
min: 0 · max: 1
weightOutnumberNormalised weight of the output token.
min: 0 · max: 1
amountInnumberInput amount.
min: 0
feeRatenumberFee 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#

executed 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
800
balanceOut
200
weightIn
0.8
weightOut
0.2
amountIn
40
feeRate
0.003

Call#

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

Returns#

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

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

This module also 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.

Diagrams#

Weighted-Product AMM — system map

Calculation flow#

Diagram
flowchart LR
    A["Normalize balances, amount, and token order"] --> B["Select Weighted-Product AMM contract"]
    B --> C["Apply declared fee convention"]
    C --> D["Solve invariant or piecewise position"]
    D --> E{"Valid state?"}
    E -- "No" --> F["Reject: exhaustion, bounds, or convergence"]
    E -- "Yes" --> G["Update actual balances"]
    G --> H["Compute spot, execution, and diagnostics"]
    H --> I["Report exclusions and state label"]

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

  • Uniswap v2 Core whitepaper — Uniswap
  • StableSwap whitepaper — Curve Finance / Michael Egorov
  • Balancer whitepaper — Balancer Labs
  • Uniswap v3 Core whitepaper — Uniswap
  • Source discipline

The rest of the AMM Pricing family#