Appearance
我的持仓(链上查询)
说明
用户持仓数据直接从 Aptos 链上读取,不经过后端 HTTP API,通过 CLMM SDK 和链上 View 函数获取。
主要调用以下链上模块函数:
| 函数 | 说明 |
|---|---|
pool::get_pool_info | 获取池子整体状态(流动性、价格等) |
pool::get_user_positions_detail | 获取用户在指定池中的持仓列表 |
pool::get_pool_balances | 获取池子中两侧代币的余额 |
链上调用方式
typescript
// 通过 Aptos SDK client.view() 调用
const poolModule = `${deployer}::pool`
// 1. 获取池子信息
const poolInfo = await client.view({
function: `${poolModule}::get_pool_info`,
type_arguments: [],
arguments: [poolAddress],
})
// 2. 获取用户持仓
const userPositions = await client.view({
function: `${poolModule}::get_user_positions_detail`,
type_arguments: [],
arguments: [poolAddress, userAddress],
})
// 3. 获取池子余额
const poolBalances = await client.view({
function: `${poolModule}::get_pool_balances`,
type_arguments: [],
arguments: [poolAddress],
})持仓数据结构
get_pool_info 返回
typescript
interface PoolInfo {
liquidity: string // 总流动性
current_tick_index: {
bits: string // 当前 tick(有符号整数编码为 u64)
}
tick_spacing: string // tick 间距
current_sqrt_price: string // 当前价格的平方根(Q64.64 格式)
}get_user_positions_detail 返回
typescript
interface PositionDetail {
index: string // 持仓索引 ID
liquidity: string // 持仓流动性
tick_lower_index: {
bits: string // 下界 tick(有符号整数编码)
}
tick_upper_index: {
bits: string // 上界 tick(有符号整数编码)
}
pool: string // 池子地址
}注意:
tick_lower_index.bits和tick_upper_index.bits为u64编码的有符号整数,前端需用BigInt.asIntN(64, BigInt(bits))解码为实际 tick 值。
关键计算
根据持仓计算代币金额:
typescript
import { TickMath, ClmmPoolUtil } from '@aptos-cf-swapv3/clmm-sdk'
const lowerSqrtPrice = TickMath.tickIndexToSqrtPriceX64(tickLower)
const upperSqrtPrice = TickMath.tickIndexToSqrtPriceX64(tickUpper)
const { coinA, coinB } = ClmmPoolUtil.getCoinAmountFromLiquidity(
new BN(liquidity),
new BN(currentSqrtPrice),
lowerSqrtPrice,
upperSqrtPrice,
false,
)根据 tick 计算价格:
typescript
const price = TickMath.tickIndexToPrice(tickIndex, token0.decimals, token1.decimals)前端实现
持仓逻辑封装在:
- Hook:
apps/web/hooks/use-pool-position-info.ts - Store:
apps/web/store/remove-liquidity-store.ts
用户进入「移除流动性」页面时自动触发数据加载,结果存入 remove-liquidity-store。