帖子
分享您的知识。
Bekky45
Jul 17, 2025
专家问答
在 Sui 中实施归属计划的最佳实践
我正在Sui Move中设计代币归属系统(例如,用于团队分配或投资者锁定),需要确保:
安全 — 不提早提款
灵活性 — 支持悬崖,线性/渐进归属
气体效率 — 最低的存储成本
透明度 — 轻松验证归属状态
当前的挑战:
-基本的时锁可以使用,但缺乏精细控制 -在多收件人管理方面苦苦挣扎 -不确定如何处理提前解雇案件
问题:
授予合约最安全的架构是什么?
如何实现不同的归属曲线(线性、分级、悬崖)?
处理可撤销和不可撤销的时间表的最佳方法是什么?
需要避免的常见漏洞?
- Sui
- Move
0
1
分享
评论
答案
1Jul 17 2025, 13:24
####1. 核心归属架构
####最低限度的可行实现
module vesting::linear {
use sui::coin;
use sui::clock;
use sui::balance;
use sui::tx_context;
struct VestingSchedule has key {
id: UID,
recipient: address,
total_amount: u64,
start_time: u64, // Clock timestamp
duration_ms: u64, // Total vesting period
claimed: u64
}
/// Initialize vesting
public entry fn create(
recipient: address,
total_amount: u64,
duration_ms: u64,
clock: &Clock,
ctx: &mut TxContext
) {
let schedule = VestingSchedule {
id: object::new(ctx),
recipient,
total_amount,
start_time: clock.timestamp_ms,
duration_ms,
claimed: 0
};
transfer::transfer(schedule, recipient);
}
}
关键组件:
-Clock
基于时间戳(不是区块号)
-自保管的日程对象
-默认情况下为线性归属
####2. 高级归属模式
####悬崖 + 线性背心
public fun claimable_amount(
schedule: &VestingSchedule,
clock: &Clock
): u64 {
let elapsed = clock.timestamp_ms.saturating_sub(schedule.start_time);
// Cliff period (e.g., 1 year)
if (elapsed < CLIFF_MS) return 0;
// Linear vesting after cliff
let vested = (schedule.total_amount * elapsed) / schedule.duration_ms;
vested - schedule.claimed
}
####分级解锁(多个里程碑)
struct GradedVesting has key {
id: UID,
milestones: vector<Milestone>, // [{time: u64, percent: u8}]
claimed: u64
}
public fun graded_claimable(schedule: &GradedVesting, clock: &Clock): u64 {
let total_vested = 0;
let i = 0;
while (i < vector::length(&schedule.milestones)) {
let milestone = vector::borrow(&schedule.milestones, i);
if (clock.timestamp_ms >= milestone.time) {
total_vested = total_vested +
(schedule.total_amount * milestone.percent as u64) / 100;
};
i = i + 1;
};
total_vested - schedule.claimed
}
####3. 安全增强功能
####可撤销的归属(管理员控制)
struct AdminCap has key { id: UID }
public entry fn revoke(
schedule: VestingSchedule,
admin_cap: &AdminCap,
ctx: &mut TxContext
) {
assert!(address_of(signer) == admin_cap.admin, EUnauthorized);
let remaining = schedule.total_amount - schedule.claimed;
coin::transfer(remaining, admin_cap.admin, ctx);
object::delete(schedule);
}
####反领跑
public entry fn claim(
schedule: &mut VestingSchedule,
clock: &Clock,
ctx: &mut TxContext
) {
let amount = claimable_amount(schedule, clock);
assert!(amount > 0, ENothingToClaim);
assert!(tx_context::epoch(ctx) == clock.epoch(), EStaleClock);
schedule.claimed = schedule.claimed + amount;
transfer::public_transfer(coin::mint(amount, ctx), schedule.recipient);
}
####4. 气体优化技术
####批量归属时间表
struct BatchVesting has key {
id: UID,
schedules: Table<address, VestingSchedule>
}
// Claim all eligible in one TXN
public entry fn batch_claim(batch: &mut BatchVesting, clock: &Clock) {
let iter = table::iter(&mut batch.schedules);
while (table::has_next(&iter)) {
let (addr, schedule) = table::next(&iter);
let amount = claimable_amount(schedule, clock);
if (amount > 0) transfer_coins(addr, amount);
}
}
#####存储折扣
// Use small types where possible
struct CompactVesting {
start_time: u64,
duration_ms: u32, // Sufficient for 49-day periods
claimed: u64
}
####5. 常见漏洞
❌Time Oracle 攻击时间
// UNSAFE: Trusting user-provided timestamps
fun unsafe_claim(user_time: u64) { ... }
❌四舍五入误差
// May leave "dust" unclaimed
let vested = (amount * elapsed) / duration; // Use checked_math
❌缺少撤销逻辑
// Irrevocable vesting may violate securities laws
struct DangerousVesting { ... }
####6. 测试策略
####模拟时钟测试
#[test]
fun test_cliff_period() {
let clock = mock_clock(0);
let schedule = create_vesting(CLIFF_MS * 2);
// Before cliff
test_clock::advance(&mut clock, CLIFF_MS - 1);
assert!(claimable(&schedule, &clock) == 0, ETestFail);
// After cliff
test_clock::advance(&mut clock, 1);
assert!(claimable(...) > 0, ETestFail);
}
####模糊测试
#[test]
fun test_overflow_scenarios() {
let schedule = VestingSchedule {
start_time: u64::MAX - 1000,
duration_ms: 2000
};
// Should handle wrap-around correctly
claimable_amount(&schedule, &clock);
}
0
评论
你知道答案吗?
请登录并分享。
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
431帖子636答案