-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathterminate_in_future.rs
163 lines (139 loc) · 5.84 KB
/
terminate_in_future.rs
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
mod setup;
use crate::setup::*;
#[test]
fn test_lockup_terminate_with_timestamp_in_future() {
let e = Env::init(None);
let users = Users::init(&e);
let amount = d(60000, TOKEN_DECIMALS);
e.set_time_sec(GENESIS_TIMESTAMP_SEC);
let lockups = e.get_account_lockups(&users.alice);
assert!(lockups.is_empty());
let res = e.add_to_deposit_whitelist(&e.owner, &users.eve.valid_account_id());
assert!(res.is_ok());
ft_storage_deposit(&e.owner, TOKEN_ID, &users.eve.account_id);
e.ft_transfer(&e.owner, amount, &users.eve);
let (lockup_schedule, vesting_schedule) = lockup_vesting_schedule(amount);
let lockup_create = LockupCreate {
account_id: users.alice.valid_account_id(),
schedule: lockup_schedule,
vesting_schedule: Some(VestingConditions::Schedule(vesting_schedule)),
};
let balance: WrappedBalance = e
.add_lockup(&users.eve, amount, &lockup_create)
.unwrap_json();
assert_eq!(balance.0, amount);
let lockups = e.get_account_lockups(&users.alice);
assert_eq!(lockups.len(), 1);
let lockup_index = lockups[0].0;
// before_cliff, 0 vested
e.set_time_sec(GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC - 1);
// try TERMINATE with past timestamp
let res = e.terminate_with_timestamp(
&e.owner,
lockup_index,
GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC - 1 - 1,
);
assert!(!res.is_ok(), "expected terminate in past to fail");
assert!(format!("{:?}", res.status()).contains("expected termination_timestamp >= now"));
// TERMINATE with future timestamp
let res: WrappedBalance = e
.terminate_with_timestamp(
&e.owner,
lockup_index,
GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC * 2,
)
.unwrap_json();
assert_eq!(res.0, amount / 2);
let terminator_balance = e.ft_balance_of(&users.eve);
assert_eq!(terminator_balance, amount / 2);
let lockups = e.get_account_lockups(&users.alice);
assert_eq!(lockups[0].1.total_balance, amount / 2);
assert_eq!(lockups[0].1.claimed_balance, 0);
assert_eq!(lockups[0].1.unclaimed_balance, 0);
// during release of remaining schedule
e.set_time_sec(GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC * 2 + ONE_YEAR_SEC * 2 / 3);
let lockups = e.get_account_lockups(&users.alice);
assert_eq!(lockups[0].1.unclaimed_balance, amount / 4);
// end of remaining schedule
e.set_time_sec(GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC * 3 + ONE_YEAR_SEC / 3);
ft_storage_deposit(&users.eve, TOKEN_ID, &users.alice.account_id);
let res: WrappedBalance = e.claim(&users.alice).unwrap_json();
assert_eq!(res.0, amount / 2);
let balance = e.ft_balance_of(&users.alice);
assert_eq!(balance, amount / 2);
// User's lockups should be empty, since fully claimed.
let lockups = e.get_account_lockups(&users.alice);
assert!(lockups.is_empty());
// Manually checking the lockup by index
let lockup = e.get_lockup(0);
assert_eq!(lockup.total_balance, amount / 2);
assert_eq!(lockup.claimed_balance, amount / 2);
assert_eq!(lockup.unclaimed_balance, 0);
}
#[test]
fn test_lockup_terminate_with_timestamp_in_future_no_storage_deposit() {
let e = Env::init(None);
let users = Users::init(&e);
let amount = d(60000, TOKEN_DECIMALS);
e.set_time_sec(GENESIS_TIMESTAMP_SEC);
let lockups = e.get_account_lockups(&users.alice);
assert!(lockups.is_empty());
// adding another owner
let res = e.add_to_deposit_whitelist(&e.owner, &users.eve.valid_account_id());
assert!(res.is_ok());
ft_storage_deposit(&e.owner, TOKEN_ID, &users.eve.account_id);
e.ft_transfer(&e.owner, amount, &users.eve);
let schedule = Schedule(vec![
Checkpoint {
timestamp: GENESIS_TIMESTAMP_SEC,
balance: 0,
},
Checkpoint {
timestamp: GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC,
balance: amount,
},
]);
let lockup_create = LockupCreate {
account_id: users.alice.valid_account_id(),
schedule: schedule.clone(),
vesting_schedule: Some(VestingConditions::Schedule(schedule.clone())),
};
// create lockup succeeds
let res = e.add_lockup(&users.eve, amount, &lockup_create);
let balance: WrappedBalance = res.unwrap_json();
assert_eq!(balance.0, amount);
let lockups = e.get_account_lockups(&users.alice);
assert_eq!(lockups.len(), 1);
let lockup_index = lockups[0].0;
storage_force_unregister(&users.eve, TOKEN_ID);
// terminate with no storage deposit creates unlocked lockup
let termination_call_timestamp = GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC * 1 / 3;
let termination_effective_timestamp = GENESIS_TIMESTAMP_SEC + ONE_YEAR_SEC * 2 / 3;
e.set_time_sec(termination_call_timestamp);
let res: WrappedBalance = e
.terminate_with_timestamp(&users.eve, lockup_index, termination_effective_timestamp)
.unwrap_json();
assert_eq!(res.0, 0);
let lockups = e.get_account_lockups(&users.eve);
assert_eq!(lockups.len(), 1);
let lockup = &lockups[0].1;
assert_eq!(lockup.unclaimed_balance, amount / 3);
assert_eq!(lockup.total_balance, amount / 3);
let balance = e.ft_balance_of(&users.alice);
assert_eq!(balance, 0);
// checking schedule, must be unlocked since the moment of termination
// starting checkpoint is preserved
assert_eq!(lockup.schedule.0[0].balance, 0);
assert_eq!(
lockup.schedule.0[0].timestamp,
termination_call_timestamp - 1,
"expected refund finish first timestamp one second before the termination"
);
// finish checkpoint is termination timestamp
assert_eq!(lockup.schedule.0[1].balance, amount / 3);
assert_eq!(
lockup.schedule.0[1].timestamp, // trimmed schedule
termination_call_timestamp,
"expected refund finish to be at termination timestamp"
);
}