技能标识:defi-position-tracker
>
跨协议监控LP仓位、质押奖励和收益耕作。计算无常损失,追踪成本基础,并将结构化数据提供给加密税务代理用于税务报告。
| 类别 | 协议 |
|---|---|
| DEX LP | Uniswap v2/v3, Curve, Balancer, Velodrome, PancakeSwap |
| 借贷 |
Ethereum, Arbitrum, Optimism, Base, Polygon, BSC, Avalanche, Fantom, Solana (通过 Birdeye/Helius)。
使用 DeBank Pro API(最全面)拉取钱包地址的所有活跃 DeFi 仓位:
bash
免费替代方案 — Zapper API:
bash
curl -s https://api.zapper.xyz/v2/balances?addresses[]=0xYOUR_WALLET&networks[]=ethereum&networks[]=arbitrum \
-H Authorization: Basic $(echo -n :YOURZAPPERKEY | base64)
公式:
IL% = 2 * sqrt(priceratio) / (1 + priceratio) - 1
其中 priceratio = currentprice / entry_price,针对波动资产与稳定资产。
Python 实现:
python
import math
def impermanentloss(entryprice: float, current_price: float) -> float:
计算 50/50 LP 仓位的无常损失百分比。
参数:
entry_price: 进入 LP 时波动资产的价格(以稳定资产计价)
current_price: 波动资产的当前价格
返回:
以小数表示的无常损失(负值 = 损失)。乘以 100 得到百分比。
示例:
entry_price = 2000 # 进入时 ETH 价格
current_price = 3000 # 当前 ETH 价格
il = impermanent_loss(2000, 3000)
# 返回 ~-0.0203 → -2.03% IL
priceratio = currentprice / entry_price
il = (2 * math.sqrt(priceratio)) / (1 + priceratio) - 1
return il
def lppositionpnl(
token0qty: float, token1qty: float,
token0entry: float, token1entry: float,
token0current: float, token1current: float,
feesearnedusd: float = 0.0
) -> dict:
LP 仓位的完整盈亏分析,包括无常损失和费用。
返回:
包含以下字段的字典: hodlvalue, lpvalue, ilusd, feesearned, net_pnl
hodlvalue = (token0qty token0current) + (token1qty token1_current)
lpvalue = calculatelpvalue(
token0qty, token1qty,
token0entry, token1entry,
token0current, token1current
)
ilusd = lpvalue - hodl_value
netpnl = ilusd + feesearnedusd
return {
hodlvalueusd: hodl_value,
lpvalueusd: lp_value,
ilusd: ilusd,
ilpct: ilusd / hodlvalue if hodlvalue else 0,
feesearnedusd: feesearnedusd,
netpnlusd: net_pnl,
netpnlpct: netpnl / hodlvalue if hodl_value else 0,
}
def calculatelpvalue(t0qty, t1qty, t0entry, t1entry, t0cur, t1_cur):
计算当前价格下恒定乘积 AMM LP 的价值。
k = t0qty * t1qty # 不变量
# 在当前价格下: t0new = sqrt(k * t1cur / t0_cur) — 等等,用进入时的比例重新计算
entryvalue = (t0qty t0entry) + (t1qty t1_entry)
priceratio = t0cur / t0_entry
# AMM 重新平衡: 每一边 = sqrt(initialproduct * priceratio)
t0new = math.sqrt(k * t0cur / t1_cur)
t1new = math.sqrt(k * t1cur / t0_cur)
return (t0new t0cur) + (t1new t1cur)
Uniswap v3 LP(集中流动性):
Uniswap v3 的无常损失与价格范围相关。使用官方 SDK 或 Revert Finance API:
bash
追踪入场价格和数量,以实现准确的盈亏和税务报告:
python
from dataclasses import dataclass
from datetime import datetime
from typing import List
@dataclass
class LPEntry:
单个 LP 入场事件(添加流动性)。
timestamp: datetime
protocol: str
chain: str
pool: str
token0_symbol: str
token0_qty: float
token0priceusd: float
token1_symbol: str
token1_qty: float
token1priceusd: float
tx_hash: str
gascostusd: float = 0.0
@property
def costbasisusd(self) -> float:
return (self.token0qty * self.token0price_usd +
self.token1qty * self.token1price_usd +
self.gascostusd)
@dataclass
class LPExit:
LP 退出事件(移除流动性)。
timestamp: datetime
protocol: str
chain: str
pool: str
token0qtyreturned: float
token0priceusd: float
token1qtyreturned: float
token1priceusd: float
fees_token0: float
fees_token1: float
tx_hash: str
gascostusd: float = 0.0
@property
def proceeds_usd(self) -> float:
return (self.token0qtyreturned * self.token0priceusd +
self.token1qtyreturned * self.token1priceusd +
self.feestoken0 * self.token0price_usd +
self.feestoken1 * self.token1price_usd -
self.gascostusd)
IRS 处理方式(当前指引):
bash
Aave 利息累积:
bash
以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:
帮我安装 SkillHub 和 defi-position-tracker-1776058683 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 defi-position-tracker-1776058683 技能
skillhub install defi-position-tracker-1776058683
文件大小: 6.25 KB | 发布时间: 2026-4-15 12:27