Weighted-Product AMM
Install and import#
npm install fintech-algorithmsimport { 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#
| Name | Type | Notes |
|---|---|---|
balanceIn | number | Balance of the input token. min: 0 |
balanceOut | number | Balance of the output token. min: 0 |
weightIn | number | Normalised weight of the input token. min: 0 · max: 1 |
weightOut | number | Normalised weight of the output token. min: 0 · max: 1 |
amountIn | number | Input amount. min: 0 |
feeRate | number | 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#
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#
8002000.80.2400.003Call#
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#
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.
References#
- Uniswap v2 Core whitepaper — Uniswap
- StableSwap whitepaper — Curve Finance / Michael Egorov
- Balancer whitepaper — Balancer Labs
- Uniswap v3 Core whitepaper — Uniswap
- Source discipline