@ exponent-labs/ market- 三马特
精准CLMM(集中流动性市场制造者)市场数学模拟包.
概览
这个软件包提供了从Exponent协议中实现 CLMM 数学函数的 TypeScript 执行. 它使客户方能够模拟:
- 交换业务:用准确的定价和费用计算模拟PT QQ SY交易
- 流动存款: 计算集中流动性头寸所需代号金额
- 价格计算: 在有效价格、隐含费率和勾选指数之间转换
实施是 Rust 链上程序代码中的直接端口 solana/programs/exponent_clmm。 。 。 .
安装
yarn add @exponent-labs/market-three-math
使用量
正在模拟交换
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)
模拟流动性存款
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)
与有效价格合作
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)
关键概念
有效的价格模式
在以下情况下,CMM采用有效的价格模式:
C = e^(-τ * u) / r
地点:
C= 有效价格(每SYPT)τ= 时间因素(正常至到期时间)u= 以隐含价格计算(现货价格)r=SY汇率
基于滴答的流动性
流动性集中在勾选范围,类似于Uniswap v3:
- 每个勾选符代表一个特定的暗示率
- 只有在当前价格在勾选范围内时才有流动性
- 为准确核算而存储 PT 和 SY 主机
收费结构
费用计算如下:
- 液化石油气费:由流动性提供者留存
- 礼宾费:送往国库(通常占总费用的20%)
- 到期后费率增加总额
API 参考
类型
MarketThreeState: 模拟所需的完整市场状态SwapArgs: 交换模拟参数SwapOutcome: 互换模拟的结果AddLiquidityArgs: 模拟流动性存款的论据AddLiquidityOutcome: 流动性存款模拟结果Tick: 单个勾选数据结构Ticks: 完成带有元数据的勾选数组
职能
交换函数
simulateSwap(marketState, args): 模拟互换操作getSwapQuote(marketState, amountIn, direction): 快速引用以交换
流动职能
simulateAddLiquidity(marketState, args): 模拟流动性存款estimateBalancedDeposit(marketState, targetLiquidity, lowerTickApy, upperTickApy): PT/SY 余额估计数computeLiquidityTargetAndTokenNeeds(...): 低水平流动性计算
工具函数
EffSnap: 有效的价格快照类calculateFeeRate(lnFeeRateRoot, secondsRemaining): 计算当前费率normalizedTimeRemaining(secondsRemaining): 将时间正常化为 [0, 1]getLnImpliedRate(tickKey): 将勾选键转换为暗示速率convertApyToApyBp(apyPercent)编号 :convertApyBpToApy(apyBp): APY 转换公用事业
与 On-Chain 代码的关系
此套件是 Rust 执行程序的一个直接端口:
solana/programs/exponent_clmm/src/state/market_three/helpers/swap.rssolana/programs/exponent_clmm/src/state/market_three/helpers/add_liquidity.rssolana/programs/exponent_clmm/src/utils/math.rs
TypeScript执行与链上代码保持了数学等同,以确保准确的模拟.
许可证
AGPL-3.0 导弹