forked from benelabs/crucible
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
185 lines (158 loc) · 5.42 KB
/
Copy pathtest.rs
File metadata and controls
185 lines (158 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#![cfg(test)]
extern crate std;
use crucible::assert_reverts;
use crucible::prelude::*;
use crate::{Vesting, VestingClient};
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
const TOTAL: i128 = 10_000_000; // 10 tokens (7 decimals)
const BASE_TIME: u64 = 1_000_000;
const CLIFF_DAYS: u64 = 30;
const VEST_DAYS: u64 = 180;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
struct Ctx {
pub env: MockEnv,
pub id: soroban_sdk::Address,
pub admin: AccountHandle,
pub beneficiary: AccountHandle,
pub token: MockToken,
}
impl Ctx {
fn setup() -> Self {
let env = MockEnv::builder()
.at_timestamp(BASE_TIME)
.with_contract::<Vesting>()
.with_account("admin", Stroops::xlm(100))
.with_account("beneficiary", Stroops::xlm(10))
.build();
let id = env.contract_id::<Vesting>();
let admin = env.account("admin");
let beneficiary = env.account("beneficiary");
let token = MockToken::new(&env, "VEST", 7);
token.mint(&admin, TOTAL);
// Initialize the vesting schedule. mock_all_auths() is needed for:
// - admin.require_auth() inside initialize()
// - token.transfer(admin, contract, total) inside initialize()
env.mock_all_auths();
VestingClient::new(env.inner(), &id).initialize(
&admin,
&beneficiary,
&token.address(),
&TOTAL,
&BASE_TIME,
&Duration::days(CLIFF_DAYS).as_seconds(),
&Duration::days(VEST_DAYS).as_seconds(),
);
Ctx {
env,
id,
admin,
beneficiary,
token,
}
}
fn client(&self) -> VestingClient<'_> {
VestingClient::new(self.env.inner(), &self.id)
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[test]
fn test_nothing_claimable_before_cliff() {
let ctx = Ctx::setup();
// Advance to just before the cliff ends.
ctx.env.advance_time(Duration::days(CLIFF_DAYS - 1));
assert_eq!(ctx.client().claimable(), 0);
}
#[test]
fn test_nothing_claimable_at_cliff_start() {
let ctx = Ctx::setup();
// At exactly the cliff boundary, 0 of the vesting window has elapsed.
ctx.env.advance_time(Duration::days(CLIFF_DAYS));
assert_eq!(ctx.client().claimable(), 0);
}
#[test]
fn test_partial_vesting_halfway_through() {
let ctx = Ctx::setup();
// Advance to cliff + half the vesting window → 50 % vested.
ctx.env
.advance_time(Duration::days(CLIFF_DAYS + VEST_DAYS / 2));
let claimable = ctx.client().claimable();
assert_eq!(claimable, TOTAL / 2);
}
#[test]
fn test_full_vesting_after_duration() {
let ctx = Ctx::setup();
// Advance past cliff + full vesting window → 100 % vested.
ctx.env.advance_time(Duration::days(CLIFF_DAYS + VEST_DAYS));
assert_eq!(ctx.client().claimable(), TOTAL);
}
#[test]
fn test_claim_transfers_tokens_to_beneficiary() {
let ctx = Ctx::setup();
ctx.env.advance_time(Duration::days(CLIFF_DAYS + VEST_DAYS));
ctx.env.mock_all_auths();
ctx.client().claim();
assert_eq!(ctx.token.balance(&ctx.beneficiary), TOTAL);
assert_eq!(ctx.client().claimable(), 0); // nothing left
}
#[test]
fn test_claim_before_cliff_reverts() {
let ctx = Ctx::setup();
// Nothing to claim before the cliff.
ctx.env.mock_all_auths();
assert_reverts!(ctx.client().claim(), "nothing to claim");
}
#[test]
fn test_partial_claim_then_more() {
let ctx = Ctx::setup();
// Claim at 50 %.
ctx.env
.advance_time(Duration::days(CLIFF_DAYS + VEST_DAYS / 2));
ctx.env.mock_all_auths();
ctx.client().claim();
assert_eq!(ctx.token.balance(&ctx.beneficiary), TOTAL / 2);
// Advance to 100 % and claim the rest.
ctx.env.advance_time(Duration::days(VEST_DAYS / 2));
ctx.client().claim();
assert_eq!(ctx.token.balance(&ctx.beneficiary), TOTAL);
}
#[test]
fn test_revoke_returns_unvested_tokens_to_admin() {
let ctx = Ctx::setup();
// Revoke at the 50 % mark — admin should receive the unvested half.
ctx.env
.advance_time(Duration::days(CLIFF_DAYS + VEST_DAYS / 2));
let vested_so_far = ctx.client().vested();
ctx.env.mock_all_auths();
ctx.client().revoke();
let unvested = TOTAL - vested_so_far;
assert_eq!(ctx.token.balance(&ctx.admin), unvested);
}
#[test]
fn test_claim_after_revoke_reverts() {
let ctx = Ctx::setup();
ctx.env.advance_time(Duration::days(CLIFF_DAYS + VEST_DAYS));
ctx.env.mock_all_auths();
ctx.client().revoke();
assert_reverts!(ctx.client().claim(), "revoked");
}
#[test]
fn test_vested_increases_monotonically_with_time() {
let ctx = Ctx::setup();
let v0 = ctx.client().vested();
ctx.env.advance_time(Duration::days(CLIFF_DAYS));
let v1 = ctx.client().vested();
ctx.env.advance_time(Duration::days(VEST_DAYS / 2));
let v2 = ctx.client().vested();
ctx.env.advance_time(Duration::days(VEST_DAYS));
let v3 = ctx.client().vested();
assert_eq!(v0, 0);
assert_eq!(v1, 0); // cliff boundary
assert!(v2 > v1);
assert_eq!(v3, TOTAL);
}