@exponent-labs/market-three-math

中文文档
0.9.33发布于 2026/11/05AGPL-3.0TypeScriptCommonJS近 30 天下载 1.4万

Mathematical simulation package for Exponent CLMM (Concentrated Liquidity Market Maker) markets.

原始使用文档
文档目录15 节
  1. Overview
  2. Installation
  3. Usage
  4. Simulating a Swap
  5. Simulating Liquidity Deposit
  6. Working with Effective Prices
  7. Key Concepts
  8. Effective Price Model
  9. Tick-Based Liquidity
  10. Fee Structure
  11. API Reference
  12. Types
  13. Functions
  14. Relationship to On-Chain Code
  15. License

@exponent-labs/market-three-math

Mathematical simulation package for Exponent CLMM (Concentrated Liquidity Market Maker) markets.

Overview

This package provides TypeScript implementations of the CLMM mathematical functions from the Exponent protocol. It enables client-side simulation of:

  • Swap operations: Simulate PT ↔ SY trades with accurate pricing and fee calculations
  • Liquidity deposits: Calculate required token amounts for concentrated liquidity positions
  • Price calculations: Convert between effective prices, implied rates, and tick indices

The implementations are direct ports from the Rust on-chain program code in solana/programs/exponent_clmm.

Installation

yarn add @exponent-labs/market-three-math

Usage

Simulating a Swap

import { MarketThreeState, SwapDirection, simulateSwap } from "@exponent-labs/market-three-math"

// Prepare market state
const marketState: MarketThreeState = {
  financials: {
    expirationTs: Date.now() / 1000 + 86400, // 1 day from now
    ptBalance: 1000000,
    syBalance: 1000000,
    liquidityBalance: 1000000,
  },
  configurationOptions: {
    lnFeeRateRoot: 0.001,
    rateScalarRoot: 5.0,
    treasuryFeeBps: 2000, // 20%
    lastLnImpliedRate: 0.05,
    epsilonClamp: 0.000001,
    minLpTickAmount: 1000,
    maxLpSupply: 10000000000,
    tickSpace: 100,
    priceDecimals: 4,
  },
  ticks: {
    currentTick: 500,
    currentSpotPrice: 0.05,
    feeGrowthIndexGlobalPt: 0,
    feeGrowthIndexGlobalSy: 0,
    ticks: [
      // ... tick data
    ],
    market: "marketAddress",
  },
  currentSyExchangeRate: 1.0,
}

// Simulate buying PT with SY
const swapResult = simulateSwap(marketState, {
  direction: SwapDirection.SyToPt,
  amountIn: 100000, // 100k SY
  syExchangeRate: 1.0,
  isCurrentFlashSwap: false,
  priceSpotLimit: 0.06, // Maximum price willing to pay
})

console.log("PT received:", swapResult.amountOut)
console.log("Fees paid:", swapResult.lpFeeChargedOutToken + swapResult.protocolFeeChargedOutToken)
console.log("Final price:", swapResult.finalSpotPrice)

Simulating Liquidity Deposit

import { estimateBalancedDeposit, simulateAddLiquidity } from "@exponent-labs/market-three-math"

// Estimate balanced amounts for a target liquidity
const { ptNeeded, syNeeded } = estimateBalancedDeposit(
  marketState,
  100000, // Target liquidity
  4.5, // Lower tick APY (%)
  5.5, // Upper tick APY (%)
)

// Simulate the actual deposit
const depositResult = simulateAddLiquidity(marketState, {
  lowerTick: 45000, // 4.5% in basis points
  upperTick: 55000, // 5.5% in basis points
  maxSy: syNeeded,
  maxPt: ptNeeded,
  syExchangeRate: 1.0,
})

console.log("Liquidity added:", depositResult.deltaL)
console.log("PT spent:", depositResult.ptSpent)
console.log("SY spent:", depositResult.sySpent)

Working with Effective Prices

import { EffSnap, normalizedTimeRemaining } from "@exponent-labs/market-three-math"

const secondsRemaining = 86400 // 1 day
const syExchangeRate = 1.0

const snap = new EffSnap(normalizedTimeRemaining(secondsRemaining), syExchangeRate)

// Convert ln implied rate to effective price
const lnImpliedRate = 0.05 // 5%
const effectivePrice = snap.getEffectivePrice(lnImpliedRate)

// Convert back from effective price to implied rate
const recoveredRate = snap.impliedRateFromEffectivePrice(effectivePrice)

Key Concepts

Effective Price Model

The CLMM uses an effective price model where:

C = e^(-τ * u) / r

Where:

  • C = effective price (PT per SY)
  • τ = time factor (normalized time to expiry)
  • u = ln implied rate (spot price)
  • r = SY exchange rate

Tick-Based Liquidity

Liquidity is concentrated in tick ranges, similar to Uniswap v3:

  • Each tick represents a specific implied rate
  • Liquidity is active only when the current price is within the tick range
  • Ticks store principal PT and SY for accurate accounting

Fee Structure

Fees are calculated as:

  • LP fee: Retained by liquidity providers
  • Protocol fee: Sent to treasury (typically 20% of total fee)
  • Total fee rate increases as expiration approaches

API Reference

Types

  • MarketThreeState: Complete market state required for simulations
  • SwapArgs: Arguments for swap simulation
  • SwapOutcome: Results of a swap simulation
  • AddLiquidityArgs: Arguments for liquidity deposit simulation
  • AddLiquidityOutcome: Results of liquidity deposit simulation
  • Tick: Individual tick data structure
  • Ticks: Complete ticks array with metadata

Functions

Swap Functions

  • simulateSwap(marketState, args): Simulate a swap operation
  • getSwapQuote(marketState, amountIn, direction): Quick quote for a swap

Liquidity Functions

  • simulateAddLiquidity(marketState, args): Simulate liquidity deposit
  • estimateBalancedDeposit(marketState, targetLiquidity, lowerTickApy, upperTickApy): Estimate balanced PT/SY amounts
  • computeLiquidityTargetAndTokenNeeds(...): Low-level liquidity calculation

Utility Functions

  • EffSnap: Effective price snapshot class
  • calculateFeeRate(lnFeeRateRoot, secondsRemaining): Calculate current fee rate
  • normalizedTimeRemaining(secondsRemaining): Normalize time to [0,1]
  • getLnImpliedRate(tickKey): Convert tick key to implied rate
  • convertApyToApyBp(apyPercent) / convertApyBpToApy(apyBp): APY conversion utilities

Relationship to On-Chain Code

This package is a direct port of the Rust implementations from:

  • solana/programs/exponent_clmm/src/state/market_three/helpers/swap.rs
  • solana/programs/exponent_clmm/src/state/market_three/helpers/add_liquidity.rs
  • solana/programs/exponent_clmm/src/utils/math.rs

The TypeScript implementations maintain mathematical equivalence with the on-chain code to ensure accurate simulations.

License

AGPL-3.0

版本与发布记录

@exponent-labs/market-three-math 共发布 35 个版本,首次发布于 2026/01/22;过去 12 个月发布了 35 个版本。

版本发布日期距今
0.9.33latest2026/11/058 天前
0.9.322026/10/2816 天前
0.9.312026/10/2717 天前
0.9.302026/10/1530 天前
0.9.292026/09/231 个月前
0.9.282026/09/221 个月前

关于 @exponent-labs/market-three-math 的常见问题

@exponent-labs/market-three-math 是什么?

Mathematical simulation package for Exponent CLMM (Concentrated Liquidity Market Maker) markets.

如何安装 @exponent-labs/market-three-math?

在项目目录运行 npm install @exponent-labs/market-three-math,或使用 pnpm add / yarn add / bun add。

@exponent-labs/market-three-math 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@exponent-labs/market-three-math 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@exponent-labs/market-three-math 可以商用吗?

@exponent-labs/market-three-math 使用 AGPL-3.0 许可。最严格的 Copyleft:除分发外,通过网络向用户提供基于它的服务也需要开源对应源码。闭源 SaaS 使用前务必评估。以上为概括,不构成法律意见,请以许可原文为准。

@exponent-labs/market-three-math 的下载量有多少?

根据 npm 官方下载统计,@exponent-labs/market-three-math 在 2026/10/14 – 2026/11/12 这 30 天内被下载 13,967 次,日均约 466 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@exponent-labs/market-three-math 还在维护吗?

最新版本 0.9.33 发布于 2026/11/05(8 天前);至今共发布 35 个版本,其中过去 12 个月发布了 35 个;首次发布于 2026/01/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。