fintech-algorithms

Concentrated-Liquidity Position

Install and import

bash
npm install fintech-algorithms
ts
import { concentratedLiquidityPosition } from "fintech-algorithms/digital-assets-and-on-chain-finance/amm-pricing/concentrated-liquidity-position";

Signature

concentratedLiquidityPosition(liquidity, priceLower, priceUpper, currentPrice)

Uniswap v3 positions: liquidity supplied only within a price range. Far more capital-efficient inside the range, and entirely one-sided outside it — a position whose price has left its range is holding only the losing asset.

Parameters

NameTypeNotes
liquiditynumberPosition liquidity L.
min: 0
priceLowernumberLower bound of the range.
min: 0
priceUppernumberUpper bound of the range.
min: 0
currentPricenumberCurrent pool price.
min: 0

Returns

{ amount0, amount1, inRange, … }

Token amounts held with an explicit in-range flag — out of range is the state that surprises position holders.

Errors

  • When priceLower is not below priceUpper — 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

liquidity
1000
priceLower
0.81
priceUpper
1.21
currentPrice
0.7

Call

concentratedLiquidityPosition(liquidity, priceLower, priceUpper, currentPrice)

Returns

object with 14 fields: model, liquidity, priceLowerToken1PerToken0, priceUpperToken1PerToken0, currentPriceToken1PerToken0, sqrtPriceLower, sqrtPriceUpper, sqrtPriceCurrent, …

{
  "model": "concentrated-liquidity-position",
  "liquidity": 1000,
  "priceLowerToken1PerToken0": 0.81,
  "priceUpperToken1PerToken0": 1.21,
  "currentPriceToken1PerToken0": 0.7,
  "sqrtPriceLower": 0.9,
  "sqrtPriceUpper": 1.1,
  "sqrtPriceCurrent": 0.836660026534,
  "amount0": 202.020202020202,
  "amount1": 0,
  "valueInToken1AtCurrentPrice": 141.414141414141,
  "state": "below-range",
  "active": false,
  "boundaryConvention": "price<=lower is below; lower<price<upper is active; price>=upper is above"
}

Other exports

This module also exports constantProductQuote, constantSumQuote, stableSwapInvariant, stableSwapQuote, weightedProductQuote. 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

Concentrated-Liquidity Position — system map

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
  • Uniswap concentrated-liquidity concept guide — Uniswap Labs
  • Source discipline