name: tokenomics description: 代币经济学模拟与分析。支持供应建模、质押机制、流动性挖矿、治理动态、基于代理的模拟以及cadCAD集成。 allowed-tools: Read, Grep, Write, Bash, Edit, Glob, WebFetch
代币经济学建模技能
用于协议设计的专业代币经济学模拟与分析。
能力
- 供应建模: 代币供应与分配
- 质押模拟: 质押与奖励机制
- 流动性挖矿: LP激励计划
- 治理动态: 代币治理建模
- 基于代理的模拟: cadCAD经济模型
- 通胀分析: 通胀/通缩机制
- LP经济学: DEX流动性与无常损失
供应分配模型
归属时间表
# vesting_model.py
import numpy as np
import pandas as pd
class VestingSchedule:
def __init__(self, total_supply: int = 1_000_000_000):
self.total_supply = total_supply
# 分配百分比
self.allocation = {
'团队': 0.20,
'投资者': 0.15,
'社区': 0.30,
'金库': 0.20,
'流动性': 0.15
}
# 归属参数(月)
self.vesting = {
'团队': {'悬崖期': 12, '持续时间': 36},
'投资者': {'悬崖期': 6, '持续时间': 24},
'社区': {'悬崖期': 0, '持续时间': 48},
'金库': {'悬崖期': 0, '持续时间': 60},
'流动性': {'悬崖期': 0, '持续时间': 1} # TGE
}
def get_unlocked(self, month: int) -> dict:
unlocked = {}
for category, params in self.vesting.items():
allocation = self.total_supply * self.allocation[category]
if month < params['悬崖期']:
unlocked[category] = 0
elif month >= params['悬崖期'] + params['持续时间']:
unlocked[category] = allocation
else:
elapsed = month - params['悬崖期']
unlocked[category] = allocation * (elapsed / params['持续时间'])
return unlocked
def get_circulating_supply(self, month: int) -> int:
unlocked = self.get_unlocked(month)
return sum(unlocked.values())
释放时间表
# emission_model.py
class EmissionSchedule:
def __init__(
self,
initial_emission: float = 1000,
decay_rate: float = 0.9, # 每期衰减10%
period_length: int = 365 # 天数
):
self.initial_emission = initial_emission
self.decay_rate = decay_rate
self.period_length = period_length
def get_daily_emission(self, day: int) -> float:
period = day // self.period_length
return self.initial_emission * (self.decay_rate ** period)
def get_cumulative_emission(self, days: int) -> float:
total = 0
for day in range(days):
total += self.get_daily_emission(day)
return total
质押经济学
质押模型
# staking_model.py
class StakingPool:
def __init__(
self,
total_staked: float = 0,
reward_rate: float = 0.10, # 10% 年化收益率
lock_period: int = 30 # 天数
):
self.total_staked = total_staked
self.reward_rate = reward_rate
self.lock_period = lock_period
self.stakers = {}
def stake(self, address: str, amount: float):
if address not in self.stakers:
self.stakers[address] = {
'amount': 0,
'reward_debt': 0,
'lock_until': 0
}
self.stakers[address]['amount'] += amount
self.stakers[address]['lock_until'] = self.lock_period
self.total_staked += amount
def calculate_rewards(self, address: str, days: int) -> float:
if address not in self.stakers:
return 0
staker = self.stakers[address]
share = staker['amount'] / self.total_staked if self.total_staked > 0 else 0
daily_rate = self.reward_rate / 365
return staker['amount'] * daily_rate * days
def get_apy(self) -> float:
return self.reward_rate * 100
veToken模型(投票托管)
# ve_token_model.py
import math
class VeTokenModel:
def __init__(self, max_lock_time: int = 4 * 365): # 最长4年
self.max_lock_time = max_lock_time
self.locks = {}
def lock(self, address: str, amount: float, lock_days: int):
lock_days = min(lock_days, self.max_lock_time)
ve_balance = amount * (lock_days / self.max_lock_time)
self.locks[address] = {
'amount': amount,
'lock_days': lock_days,
've_balance': ve_balance,
'start_time': 0
}
return ve_balance
def get_voting_power(self, address: str, current_day: int) -> float:
if address not in self.locks:
return 0
lock = self.locks[address]
remaining = max(0, lock['lock_days'] - current_day)
return lock['amount'] * (remaining / self.max_lock_time)
流动性挖矿
LP奖励模型
# lp_rewards_model.py
class LPRewardsPool:
def __init__(
self,
reward_per_block: float = 10,
total_lp_tokens: float = 0
):
self.reward_per_block = reward_per_block
self.total_lp_tokens = total_lp_tokens
self.acc_reward_per_share = 0
self.last_reward_block = 0
self.users = {}
def deposit(self, user: str, amount: float, block: int):
self._update_pool(block)
if user in self.users:
pending = self._pending_rewards(user)
self.users[user]['pending'] += pending
if user not in self.users:
self.users[user] = {'amount': 0, 'reward_debt': 0, 'pending': 0}
self.users[user]['amount'] += amount
self.users[user]['reward_debt'] = \
self.users[user]['amount'] * self.acc_reward_per_share
self.total_lp_tokens += amount
def _update_pool(self, block: int):
if self.total_lp_tokens == 0:
self.last_reward_block = block
return
blocks = block - self.last_reward_block
rewards = blocks * self.reward_per_block
self.acc_reward_per_share += rewards / self.total_lp_tokens
self.last_reward_block = block
def _pending_rewards(self, user: str) -> float:
if user not in self.users:
return 0
return self.users[user]['amount'] * self.acc_reward_per_share \
- self.users[user]['reward_debt']
无常损失计算器
# impermanent_loss.py
def calculate_impermanent_loss(price_ratio: float) -> float:
"""
计算Uniswap V2风格AMM的无常损失。
price_ratio: 新价格 / 初始价格
"""
return 2 * math.sqrt(price_ratio) / (1 + price_ratio) - 1
def il_vs_holding(initial_value: float, price_ratio: float) -> dict:
il = calculate_impermanent_loss(price_ratio)
lp_value = initial_value * (1 + il)
hold_value = initial_value * (1 + price_ratio) / 2
return {
'lp_value': lp_value,
'hold_value': hold_value,
'il_percentage': il * 100,
'il_dollar': hold_value - lp_value
}
cadCAD模拟
基础cadCAD模型
# cadcad_model.py
from cadCAD.configuration import Configuration
from cadCAD.engine import ExecutionMode, ExecutionContext, Executor
# 状态变量
initial_state = {
'token_price': 1.0,
'total_supply': 100_000_000,
'circulating_supply': 10_000_000,
'staked_supply': 0,
'treasury': 20_000_000
}
# 参数
system_params = {
'staking_apr': [0.10, 0.15, 0.20],
'inflation_rate': [0.05],
'buy_pressure': [0.01, 0.02]
}
# 状态更新函数
def update_price(params, step, sL, s, _input):
buy_pressure = params['buy_pressure']
sell_pressure = s['circulating_supply'] * 0.001
price_change = (buy_pressure - sell_pressure) / s['circulating_supply']
new_price = max(0.01, s['token_price'] * (1 + price_change))
return ('token_price', new_price)
def update_staking(params, step, sL, s, _input):
staking_apr = params['staking_apr']
stake_incentive = staking_apr * s['token_price']
new_staked = s['staked_supply'] + s['circulating_supply'] * stake_incentive * 0.1
return ('staked_supply', new_staked)
# 策略
def staking_policy(params, step, sL, s):
return {'stake_action': 'stake' if s['token_price'] > 0.5 else 'unstake'}
# 配置
partial_state_update_blocks = [
{
'policies': {'staking': staking_policy},
'variables': {
'token_price': update_price,
'staked_supply': update_staking
}
}
]
治理模拟
# governance_model.py
class GovernanceSimulation:
def __init__(self, total_voting_power: float):
self.total_voting_power = total_voting_power
self.proposals = {}
self.quorum = 0.04 # 4%法定人数
self.pass_threshold = 0.5 # 50%通过门槛
def create_proposal(self, id: str, description: str):
self.proposals[id] = {
'description': description,
'for_votes': 0,
'against_votes': 0,
'abstain_votes': 0,
'status': 'active'
}
def vote(self, proposal_id: str, voting_power: float, support: int):
proposal = self.proposals[proposal_id]
if support == 1:
proposal['for_votes'] += voting_power
elif support == 0:
proposal['against_votes'] += voting_power
else:
proposal['abstain_votes'] += voting_power
def execute(self, proposal_id: str) -> bool:
proposal = self.proposals[proposal_id]
total_votes = proposal['for_votes'] + proposal['against_votes']
# 检查法定人数
if total_votes < self.total_voting_power * self.quorum:
proposal['status'] = 'defeated'
return False
# 检查门槛
if proposal['for_votes'] / total_votes >= self.pass_threshold:
proposal['status'] = 'executed'
return True
else:
proposal['status'] = 'defeated'
return False
流程集成
| 流程 | 目的 |
|---|---|
economic-simulation.js |
协议经济学 |
staking-contract.js |
质押设计 |
governance-system.js |
治理建模 |
yield-aggregator.js |
收益优化 |
最佳实践
- 建模多种场景
- 包含对抗性代理
- 测试边界情况(零流动性,100%质押)
- 根据真实协议数据验证
- 考虑MEV和套利
- 记录所有假设
另请参阅
skills/defi-protocols/SKILL.md- DeFi集成agents/defi-architect/AGENT.md- DeFi专家- cadCAD文档