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
333 lines (280 loc) · 9.18 KB
/
Copy pathtest.rs
File metadata and controls
333 lines (280 loc) · 9.18 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#![cfg(test)]
extern crate std;
use crucible::assert_reverts;
use crucible::prelude::*;
use crate::{ContractError, Vesting, VestingClient};
use soroban_sdk::testutils::Ledger;
// ---------------------------------------------------------------------------
// 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 {
/// Build the environment and deploy the contract *without* initializing it.
fn build() -> 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);
Ctx {
env,
id,
admin,
beneficiary,
token,
}
}
/// Full happy-path setup: build + initialize.
fn setup() -> Self {
let ctx = Self::build();
ctx.env.mock_all_auths();
VestingClient::new(ctx.env.inner(), &ctx.id).initialize(
&ctx.admin.address(),
&ctx.beneficiary.address(),
&ctx.token.address(),
&TOTAL,
&BASE_TIME,
&Duration::days(CLIFF_DAYS).as_seconds(),
&Duration::days(VEST_DAYS).as_seconds(),
);
ctx
}
fn client(&self) -> VestingClient<'_> {
VestingClient::new(self.env.inner(), &self.id)
}
}
// ---------------------------------------------------------------------------
// Existing 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);
}
#[test]
fn test_initialize_overflow_start_plus_cliff() {
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);
env.mock_all_auths();
// start + cliff overflows (u64::MAX + 1)
let start = u64::MAX;
let cliff = 1;
let duration = 100;
let res = VestingClient::new(env.inner(), &id).try_initialize(
&admin,
&beneficiary,
&token.address(),
&TOTAL,
&start,
&cliff,
&duration,
);
assert!(res.is_err());
let err = res.err().unwrap();
match err {
Ok(e) => {
assert_eq!(
e,
soroban_sdk::Error::from_contract_error(ContractError::Overflow as u32)
);
}
_ => panic!("Expected contract error, got {:?}", err),
}
}
#[test]
fn test_initialize_overflow_cliff_end_plus_duration() {
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);
env.mock_all_auths();
// cliff_end + duration overflows (u64::MAX - 100 + 50 + 51 = u64::MAX + 1)
let start = u64::MAX - 100;
let cliff = 50;
let duration = 51;
let res = VestingClient::new(env.inner(), &id).try_initialize(
&admin,
&beneficiary,
&token.address(),
&TOTAL,
&start,
&cliff,
&duration,
);
assert!(res.is_err());
let err = res.err().unwrap();
match err {
Ok(e) => {
assert_eq!(
e,
soroban_sdk::Error::from_contract_error(ContractError::Overflow as u32)
);
}
_ => panic!("Expected contract error, got {:?}", err),
}
}
#[test]
fn test_initialize_near_max_valid_schedule() {
let env = MockEnv::builder()
.at_timestamp(u64::MAX - 150)
.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);
env.mock_all_auths();
// Valid near-max (u64::MAX - 100 + 50 + 50 = u64::MAX, no overflow)
let start = u64::MAX - 100;
let cliff = 50;
let duration = 50;
let client = VestingClient::new(env.inner(), &id);
client.initialize(
&admin,
&beneficiary,
&token.address(),
&TOTAL,
&start,
&cliff,
&duration,
);
// Check vested amounts at boundaries
env.inner().ledger().set_timestamp(start + cliff - 1);
assert_eq!(client.vested(), 0);
env.inner().ledger().set_timestamp(start + cliff);
assert_eq!(client.vested(), 0);
env.inner()
.ledger()
.set_timestamp(start + cliff + duration / 2);
assert_eq!(client.vested(), TOTAL / 2);
env.inner().ledger().set_timestamp(start + cliff + duration);
assert_eq!(client.vested(), TOTAL);
}