-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathlib.rs
1963 lines (1741 loc) · 74.4 KB
/
lib.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! A smart contract that allows tokens to be locked up.
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::Base58PublicKey;
use near_sdk::{env, ext_contract, near_bindgen, AccountId};
pub use crate::foundation::*;
pub use crate::foundation_callbacks::*;
pub use crate::getters::*;
pub use crate::internal::*;
pub use crate::owner::*;
pub use crate::owner_callbacks::*;
pub use crate::types::*;
pub mod foundation;
pub mod foundation_callbacks;
pub mod gas;
pub mod owner_callbacks;
pub mod types;
pub mod getters;
pub mod internal;
pub mod owner;
#[global_allocator]
static ALLOC: near_sdk::wee_alloc::WeeAlloc = near_sdk::wee_alloc::WeeAlloc::INIT;
/// Indicates there are no deposit for a cross contract call for better readability.
const NO_DEPOSIT: u128 = 0;
/// The contract keeps at least 3.5 NEAR in the account to avoid being transferred out to cover
/// contract code storage and some internal state.
pub const MIN_BALANCE_FOR_STORAGE: u128 = 3_500_000_000_000_000_000_000_000;
#[ext_contract(ext_staking_pool)]
pub trait ExtStakingPool {
fn get_account_staked_balance(&self, account_id: AccountId) -> WrappedBalance;
fn get_account_unstaked_balance(&self, account_id: AccountId) -> WrappedBalance;
fn get_account_total_balance(&self, account_id: AccountId) -> WrappedBalance;
fn deposit(&mut self);
fn deposit_and_stake(&mut self);
fn withdraw(&mut self, amount: WrappedBalance);
fn stake(&mut self, amount: WrappedBalance);
fn unstake(&mut self, amount: WrappedBalance);
fn unstake_all(&mut self);
}
#[ext_contract(ext_whitelist)]
pub trait ExtStakingPoolWhitelist {
fn is_whitelisted(&self, staking_pool_account_id: AccountId) -> bool;
}
#[ext_contract(ext_transfer_poll)]
pub trait ExtTransferPoll {
fn get_result(&self) -> Option<PollResult>;
}
#[ext_contract(ext_self_owner)]
pub trait ExtLockupContractOwner {
fn on_whitelist_is_whitelisted(
&mut self,
#[callback] is_whitelisted: bool,
staking_pool_account_id: AccountId,
) -> bool;
fn on_staking_pool_deposit(&mut self, amount: WrappedBalance) -> bool;
fn on_staking_pool_deposit_and_stake(&mut self, amount: WrappedBalance) -> bool;
fn on_staking_pool_withdraw(&mut self, amount: WrappedBalance) -> bool;
fn on_staking_pool_stake(&mut self, amount: WrappedBalance) -> bool;
fn on_staking_pool_unstake(&mut self, amount: WrappedBalance) -> bool;
fn on_staking_pool_unstake_all(&mut self) -> bool;
fn on_get_result_from_transfer_poll(&mut self, #[callback] poll_result: PollResult) -> bool;
fn on_get_account_total_balance(&mut self, #[callback] total_balance: WrappedBalance);
fn on_get_account_unstaked_balance_to_withdraw_by_owner(
&mut self,
#[callback] unstaked_balance: WrappedBalance,
);
}
#[ext_contract(ext_self_foundation)]
pub trait ExtLockupContractFoundation {
fn on_withdraw_unvested_amount(
&mut self,
amount: WrappedBalance,
receiver_id: AccountId,
) -> bool;
fn on_get_account_staked_balance_to_unstake(
&mut self,
#[callback] staked_balance: WrappedBalance,
);
fn on_staking_pool_unstake_for_termination(&mut self, amount: WrappedBalance) -> bool;
fn on_get_account_unstaked_balance_to_withdraw(
&mut self,
#[callback] unstaked_balance: WrappedBalance,
);
fn on_staking_pool_withdraw_for_termination(&mut self, amount: WrappedBalance) -> bool;
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct LockupContract {
/// The account ID of the owner.
pub owner_account_id: AccountId,
/// Information about lockup schedule and the amount.
pub lockup_information: LockupInformation,
/// Information about vesting including schedule or termination status.
pub vesting_information: VestingInformation,
/// Account ID of the staking pool whitelist contract.
pub staking_pool_whitelist_account_id: AccountId,
/// Information about staking and delegation.
/// `Some` means the staking information is available and the staking pool contract is selected.
/// `None` means there is no staking pool selected.
pub staking_information: Option<StakingInformation>,
/// The account ID that the NEAR Foundation, that has the ability to terminate vesting.
pub foundation_account_id: Option<AccountId>,
}
impl Default for LockupContract {
fn default() -> Self {
env::panic(b"The contract is not initialized.");
}
}
#[near_bindgen]
impl LockupContract {
/// Requires 25 TGas (1 * BASE_GAS)
///
/// Initializes lockup contract.
/// - `owner_account_id` - the account ID of the owner. Only this account can call owner's
/// methods on this contract.
/// - `lockup_duration` [deprecated] - the duration in nanoseconds of the lockup period from
/// the moment the transfers are enabled. During this period tokens are locked and
/// the release doesn't start. Instead of this, use `lockup_timestamp` and `release_duration`
/// - `lockup_timestamp` - the optional absolute lockup timestamp in nanoseconds which locks
/// the tokens until this timestamp passes. Until this moment the tokens are locked and the
/// release doesn't start.
/// - `transfers_information` - the information about the transfers. Either transfers are
/// already enabled, then it contains the timestamp when they were enabled. Or the transfers
/// are currently disabled and it contains the account ID of the transfer poll contract.
/// - `vesting_schedule` - If provided, then it's either a base64 encoded hash of vesting
/// schedule with salt or an explicit vesting schedule.
/// Vesting schedule affects the amount of tokens the NEAR Foundation will get in case of
/// employment termination as well as the amount of tokens available for transfer by
/// the employee. If Hash provided, it's expected that vesting started before lockup and
/// it only needs to be revealed in case of termination.
/// - `release_duration` - is the duration when the full lockup amount will be available.
/// The tokens are linearly released from the moment tokens are unlocked.
/// The unlocking happens at the timestamp defined by:
/// `max(transfers_timestamp + lockup_duration, lockup_timestamp)`.
/// If it's used in addition to the vesting schedule, then the amount of tokens available to
/// transfer is subject to the minimum between vested tokens and released tokens.
/// - `staking_pool_whitelist_account_id` - the Account ID of the staking pool whitelist contract.
/// - `foundation_account_id` - the account ID of the NEAR Foundation, that has the ability to
/// terminate vesting schedule.
#[init]
pub fn new(
owner_account_id: AccountId,
lockup_duration: WrappedDuration,
lockup_timestamp: Option<WrappedTimestamp>,
transfers_information: TransfersInformation,
vesting_schedule: Option<VestingScheduleOrHash>,
release_duration: Option<WrappedDuration>,
staking_pool_whitelist_account_id: AccountId,
foundation_account_id: Option<AccountId>,
) -> Self {
assert!(
env::is_valid_account_id(owner_account_id.as_bytes()),
"The account ID of the owner is invalid"
);
assert!(
env::is_valid_account_id(staking_pool_whitelist_account_id.as_bytes()),
"The staking pool whitelist account ID is invalid"
);
if let TransfersInformation::TransfersDisabled {
transfer_poll_account_id,
} = &transfers_information
{
assert!(
env::is_valid_account_id(transfer_poll_account_id.as_bytes()),
"The transfer poll account ID is invalid"
);
}
let lockup_information = LockupInformation {
lockup_amount: env::account_balance(),
termination_withdrawn_tokens: 0,
lockup_duration: lockup_duration.0,
release_duration: release_duration.map(|d| d.0),
lockup_timestamp: lockup_timestamp.map(|d| d.0),
transfers_information,
};
let vesting_information = match vesting_schedule {
None => {
assert!(
foundation_account_id.is_none(),
"Foundation account can't be added without vesting schedule"
);
VestingInformation::None
}
Some(VestingScheduleOrHash::VestingHash(hash)) => VestingInformation::VestingHash(hash),
Some(VestingScheduleOrHash::VestingSchedule(vs)) => {
VestingInformation::VestingSchedule(vs)
}
};
assert!(
vesting_information == VestingInformation::None ||
env::is_valid_account_id(foundation_account_id.as_ref().unwrap().as_bytes()),
"Foundation account should be added for vesting schedule"
);
Self {
owner_account_id,
lockup_information,
vesting_information,
staking_information: None,
staking_pool_whitelist_account_id,
foundation_account_id,
}
}
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(test)]
mod tests {
use std::convert::TryInto;
use near_sdk::{testing_env, MockedBlockchain, PromiseResult, VMContext};
use test_utils::*;
use super::*;
mod test_utils;
pub type AccountId = String;
const SALT: [u8; 3] = [1, 2, 3];
fn basic_context() -> VMContext {
get_context(
system_account(),
to_yocto(LOCKUP_NEAR),
0,
to_ts(GENESIS_TIME_IN_DAYS),
false,
)
}
fn new_vesting_schedule(offset_in_days: u64) -> VestingSchedule {
VestingSchedule {
start_timestamp: to_ts(GENESIS_TIME_IN_DAYS - YEAR + offset_in_days).into(),
cliff_timestamp: to_ts(GENESIS_TIME_IN_DAYS + offset_in_days).into(),
end_timestamp: to_ts(GENESIS_TIME_IN_DAYS + YEAR * 3 + offset_in_days).into(),
}
}
fn no_vesting_schedule() -> VestingSchedule {
VestingSchedule {
start_timestamp: to_ts(0).into(),
cliff_timestamp: to_ts(0).into(),
end_timestamp: to_ts(0).into(),
}
}
fn new_contract_with_lockup_duration(
transfers_enabled: bool,
vesting_schedule: Option<VestingSchedule>,
release_duration: Option<WrappedDuration>,
foundation_account: bool,
lockup_duration: Duration,
) -> LockupContract {
let lockup_start_information = if transfers_enabled {
TransfersInformation::TransfersEnabled {
transfers_timestamp: to_ts(GENESIS_TIME_IN_DAYS).into(),
}
} else {
TransfersInformation::TransfersDisabled {
transfer_poll_account_id: AccountId::from("transfers"),
}
};
let foundation_account_id = if foundation_account {
Some(account_foundation())
} else {
None
};
let vesting_schedule = vesting_schedule.map(|vesting_schedule| {
VestingScheduleOrHash::VestingHash(
VestingScheduleWithSalt {
vesting_schedule,
salt: SALT.to_vec().into(),
}
.hash()
.into(),
)
});
LockupContract::new(
account_owner(),
lockup_duration.into(),
None,
lockup_start_information,
vesting_schedule,
release_duration,
AccountId::from("whitelist"),
foundation_account_id,
)
}
fn new_contract(
transfers_enabled: bool,
vesting_schedule: Option<VestingSchedule>,
release_duration: Option<WrappedDuration>,
foundation_account: bool,
) -> LockupContract {
new_contract_with_lockup_duration(
transfers_enabled,
vesting_schedule,
release_duration,
foundation_account,
to_nanos(YEAR),
)
}
fn lockup_only_setup() -> (VMContext, LockupContract) {
let context = basic_context();
testing_env!(context.clone());
let contract = new_contract(true, None, None, false);
(context, contract)
}
#[test]
fn test_lockup_only_basic() {
let (mut context, contract) = lockup_only_setup();
// Checking initial values at genesis time
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_owners_balance().0, 0);
assert_eq!(
contract.get_locked_vested_amount(no_vesting_schedule()).0,
to_yocto(LOCKUP_NEAR)
);
// Checking values in 1 day after genesis time
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + 1);
assert_eq!(contract.get_owners_balance().0, 0);
// Checking values next day after lockup timestamp
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR + 1);
testing_env!(context.clone());
assert_almost_eq(contract.get_owners_balance().0, to_yocto(LOCKUP_NEAR));
}
#[test]
fn test_add_full_access_key() {
let (mut context, mut contract) = lockup_only_setup();
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR);
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(1).try_into().unwrap();
testing_env!(context.clone());
contract.add_full_access_key(public_key(4));
}
#[test]
#[should_panic(expected = "Tokens are still locked/unvested")]
fn test_add_full_access_key_when_vesting_is_not_finished() {
let mut context = basic_context();
testing_env!(context.clone());
let vesting_schedule = new_vesting_schedule(YEAR);
let mut contract = new_contract(true, Some(vesting_schedule), None, true);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR - 10);
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(1).try_into().unwrap();
testing_env!(context.clone());
contract.add_full_access_key(public_key(4));
}
#[test]
#[should_panic(expected = "Tokens are still locked/unvested")]
fn test_add_full_access_key_when_lockup_is_not_finished() {
let mut context = basic_context();
testing_env!(context.clone());
let mut contract = new_contract(true, None, Some(to_nanos(YEAR).into()), false);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR - 10);
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(1).try_into().unwrap();
testing_env!(context.clone());
contract.add_full_access_key(public_key(4));
}
#[test]
#[should_panic(expected = "Can only be called by the owner")]
fn test_call_by_non_owner() {
let (mut context, mut contract) = lockup_only_setup();
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR);
context.predecessor_account_id = non_owner();
context.signer_account_id = non_owner();
testing_env!(context.clone());
contract.select_staking_pool(AccountId::from("staking_pool"));
}
#[test]
#[should_panic(expected = "Presented vesting schedule and salt don't match the hash")]
fn test_vesting_doesnt_match() {
let mut context = basic_context();
testing_env!(context.clone());
let vesting_schedule = new_vesting_schedule(5);
let mut contract = new_contract(true, Some(vesting_schedule), None, true);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR);
context.predecessor_account_id = account_foundation();
context.signer_account_id = non_owner();
testing_env!(context.clone());
let not_real_vesting = new_vesting_schedule(100);
contract.terminate_vesting(Some(VestingScheduleWithSalt {
vesting_schedule: not_real_vesting,
salt: SALT.to_vec().into(),
}));
}
#[test]
#[should_panic(expected = "Expected vesting schedule and salt, but it was not provided")]
fn test_vesting_schedule_and_salt_not_provided() {
let mut context = basic_context();
testing_env!(context.clone());
let vesting_schedule = new_vesting_schedule(5);
let mut contract = new_contract(true, Some(vesting_schedule), None, true);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR);
context.predecessor_account_id = account_foundation();
context.signer_account_id = non_owner();
testing_env!(context.clone());
contract.terminate_vesting(None);
}
#[test]
#[should_panic(expected = "Explicit vesting schedule exists")]
fn test_explicit_vesting() {
let mut context = basic_context();
testing_env!(context.clone());
let vesting_schedule = new_vesting_schedule(5);
let mut contract = LockupContract::new(
account_owner(),
to_nanos(YEAR).into(),
None,
TransfersInformation::TransfersEnabled {
transfers_timestamp: to_ts(GENESIS_TIME_IN_DAYS).into(),
},
Some(VestingScheduleOrHash::VestingSchedule(
vesting_schedule.clone(),
)),
None,
AccountId::from("whitelist"),
Some(account_foundation()),
);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR);
context.predecessor_account_id = account_foundation();
context.signer_account_id = non_owner();
testing_env!(context.clone());
contract.terminate_vesting(Some(VestingScheduleWithSalt {
vesting_schedule,
salt: SALT.to_vec().into(),
}));
}
#[test]
#[should_panic(expected = "Foundation account can't be added without vesting schedule")]
fn test_init_foundation_key_no_vesting() {
let context = basic_context();
testing_env!(context.clone());
new_contract(true, None, None, true);
}
#[test]
#[should_panic(expected = "Foundation account can't be added without vesting schedule")]
fn test_init_foundation_key_no_vesting_with_release() {
let context = basic_context();
testing_env!(context.clone());
new_contract(true, None, Some(to_nanos(YEAR).into()), true);
}
#[test]
#[should_panic(expected = "Can only be called by NEAR Foundation")]
fn test_call_by_non_foundation() {
let mut context = basic_context();
testing_env!(context.clone());
let vesting_schedule = new_vesting_schedule(0);
let mut contract = new_contract(true, Some(vesting_schedule.clone()), None, true);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR);
context.predecessor_account_id = non_owner();
context.signer_account_id = non_owner();
testing_env!(context.clone());
contract.terminate_vesting(None);
}
#[test]
#[should_panic(expected = "Transfers are disabled")]
fn test_transfers_not_enabled() {
let mut context = basic_context();
testing_env!(context.clone());
let mut contract = new_contract(false, None, None, false);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR + 1);
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(1).try_into().unwrap();
context.is_view = false;
testing_env!(context.clone());
contract.transfer(to_yocto(100).into(), non_owner());
}
#[test]
fn test_enable_transfers() {
let mut context = basic_context();
testing_env!(context.clone());
let mut contract = new_contract(false, None, None, false);
context.is_view = true;
testing_env!(context.clone());
assert!(!contract.are_transfers_enabled());
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR + 1);
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(1).try_into().unwrap();
context.is_view = false;
testing_env!(context.clone());
contract.check_transfers_vote();
let poll_result = Some(to_ts(GENESIS_TIME_IN_DAYS + 10).into());
context.predecessor_account_id = lockup_account();
// NOTE: Unit tests don't need to read the content of the promise result. So here we don't
// have to pass serialized result from the transfer poll.
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
assert!(contract.on_get_result_from_transfer_poll(poll_result));
context.is_view = true;
testing_env!(context.clone());
// Not unlocked yet
assert_eq!(contract.get_owners_balance().0, 0);
assert!(contract.are_transfers_enabled());
assert_eq!(contract.get_vesting_information(), VestingInformation::None);
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR + 10);
testing_env!(context.clone());
// Unlocked yet
assert_eq!(
contract.get_owners_balance().0,
to_yocto(LOCKUP_NEAR).into()
);
context.is_view = false;
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.transfer(to_yocto(100).into(), non_owner());
}
#[test]
fn test_check_transfers_vote_false() {
let mut context = basic_context();
testing_env!(context.clone());
let mut contract = new_contract(false, None, None, false);
context.is_view = true;
testing_env!(context.clone());
assert!(!contract.are_transfers_enabled());
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR + 1);
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(1).try_into().unwrap();
context.is_view = false;
testing_env!(context.clone());
contract.check_transfers_vote();
let poll_result = None;
// NOTE: Unit tests don't need to read the content of the promise result. So here we don't
// have to pass serialized result from the transfer poll.
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
assert!(!contract.on_get_result_from_transfer_poll(poll_result));
context.is_view = true;
testing_env!(context.clone());
// Not enabled
assert!(!contract.are_transfers_enabled());
}
#[test]
fn test_lockup_only_transfer_call_by_owner() {
let (mut context, mut contract) = lockup_only_setup();
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR + 1);
context.is_view = true;
testing_env!(context.clone());
assert_almost_eq(contract.get_owners_balance().0, to_yocto(LOCKUP_NEAR));
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(1).try_into().unwrap();
context.is_view = false;
testing_env!(context.clone());
assert_eq!(env::account_balance(), to_yocto(LOCKUP_NEAR));
contract.transfer(to_yocto(100).into(), non_owner());
assert_almost_eq(env::account_balance(), to_yocto(LOCKUP_NEAR - 100));
}
#[test]
#[should_panic(expected = "Staking pool is not selected")]
fn test_staking_pool_is_not_selected() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
let amount = to_yocto(LOCKUP_NEAR - 100);
testing_env!(context.clone());
contract.deposit_to_staking_pool(amount.into());
}
#[test]
fn test_staking_pool_success() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
// Selecting staking pool
let staking_pool = "staking_pool".to_string();
testing_env!(context.clone());
contract.select_staking_pool(staking_pool.clone());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(
context.clone(),
PromiseResult::Successful(b"true".to_vec()),
);
contract.on_whitelist_is_whitelisted(true, staking_pool.clone());
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_staking_pool_account_id(), Some(staking_pool));
assert_eq!(contract.get_known_deposited_balance().0, 0);
context.is_view = false;
// Deposit to the staking_pool
let amount = to_yocto(LOCKUP_NEAR - 100);
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.deposit_to_staking_pool(amount.into());
context.account_balance = env::account_balance();
assert_eq!(context.account_balance, to_yocto(LOCKUP_NEAR) - amount);
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_deposit(amount.into());
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_known_deposited_balance().0, amount);
context.is_view = false;
// Staking on the staking pool
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.stake(amount.into());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_stake(amount.into());
// Assuming there are 20 NEAR tokens in rewards. Unstaking.
let unstake_amount = amount + to_yocto(20);
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.unstake(unstake_amount.into());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_unstake(unstake_amount.into());
// Withdrawing
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.withdraw_from_staking_pool(unstake_amount.into());
context.account_balance += unstake_amount;
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_withdraw(unstake_amount.into());
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_known_deposited_balance().0, 0);
context.is_view = false;
// Unselecting staking pool
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.unselect_staking_pool();
assert_eq!(contract.get_staking_pool_account_id(), None);
}
#[test]
fn test_staking_pool_refresh_balance() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
// Selecting staking pool
let staking_pool = "staking_pool".to_string();
testing_env!(context.clone());
contract.select_staking_pool(staking_pool.clone());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(
context.clone(),
PromiseResult::Successful(b"true".to_vec()),
);
contract.on_whitelist_is_whitelisted(true, staking_pool.clone());
// Deposit to the staking_pool
let amount = to_yocto(LOCKUP_NEAR - 100);
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.deposit_to_staking_pool(amount.into());
context.account_balance = env::account_balance();
assert_eq!(context.account_balance, to_yocto(LOCKUP_NEAR) - amount);
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_deposit(amount.into());
// Staking on the staking pool
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.stake(amount.into());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_stake(amount.into());
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_owners_balance().0, 0);
assert_eq!(contract.get_liquid_owners_balance().0, 0);
assert_eq!(contract.get_known_deposited_balance().0, amount);
context.is_view = false;
// Assuming there are 20 NEAR tokens in rewards. Refreshing balance.
let total_balance = amount + to_yocto(20);
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.refresh_staking_pool_balance();
// In unit tests, the following call ignores the promise value, because it's passed directly.
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_get_account_total_balance(total_balance.into());
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_known_deposited_balance().0, total_balance);
assert_eq!(contract.get_owners_balance().0, to_yocto(20));
assert_eq!(contract.get_liquid_owners_balance().0, to_yocto(20));
context.is_view = false;
// Withdrawing these tokens
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
let transfer_amount = to_yocto(15);
contract.transfer(transfer_amount.into(), non_owner());
context.account_balance = env::account_balance();
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_known_deposited_balance().0, total_balance);
assert_eq!(contract.get_owners_balance().0, to_yocto(5));
assert_eq!(contract.get_liquid_owners_balance().0, to_yocto(5));
context.is_view = false;
}
#[test]
#[should_panic(expected = "Staking pool is already selected")]
fn test_staking_pool_selected_again() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
// Selecting staking pool
let staking_pool = "staking_pool".to_string();
testing_env!(context.clone());
contract.select_staking_pool(staking_pool.clone());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(
context.clone(),
PromiseResult::Successful(b"true".to_vec()),
);
contract.on_whitelist_is_whitelisted(true, staking_pool.clone());
// Selecting another staking pool
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.select_staking_pool("staking_pool_2".to_string());
}
#[test]
#[should_panic(expected = "The given staking pool account ID is not whitelisted")]
fn test_staking_pool_not_whitelisted() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
// Selecting staking pool
let staking_pool = "staking_pool".to_string();
testing_env!(context.clone());
contract.select_staking_pool(staking_pool.clone());
context.predecessor_account_id = lockup_account();
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(
context.clone(),
PromiseResult::Successful(b"false".to_vec()),
);
contract.on_whitelist_is_whitelisted(false, staking_pool.clone());
}
#[test]
#[should_panic(expected = "Staking pool is not selected")]
fn test_staking_pool_unselecting_non_selected() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
// Unselecting staking pool
testing_env!(context.clone());
contract.unselect_staking_pool();
}
#[test]
#[should_panic(expected = "There is still a deposit on the staking pool")]
fn test_staking_pool_unselecting_with_deposit() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
// Selecting staking pool
let staking_pool = "staking_pool".to_string();
testing_env!(context.clone());
contract.select_staking_pool(staking_pool.clone());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(
context.clone(),
PromiseResult::Successful(b"true".to_vec()),
);
contract.on_whitelist_is_whitelisted(true, staking_pool.clone());
// Deposit to the staking_pool
let amount = to_yocto(LOCKUP_NEAR - 100);
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.deposit_to_staking_pool(amount.into());
context.account_balance = env::account_balance();
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_deposit(amount.into());
// Unselecting staking pool
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.unselect_staking_pool();
}
#[test]
fn test_staking_pool_owner_balance() {
let (mut context, mut contract) = lockup_only_setup();
context.predecessor_account_id = account_owner();
context.signer_account_id = account_owner();
context.signer_account_pk = public_key(2).try_into().unwrap();
context.block_timestamp = to_ts(GENESIS_TIME_IN_DAYS + YEAR + 1);
let lockup_amount = to_yocto(LOCKUP_NEAR);
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_owners_balance().0, lockup_amount);
context.is_view = false;
// Selecting staking pool
let staking_pool = "staking_pool".to_string();
testing_env!(context.clone());
contract.select_staking_pool(staking_pool.clone());
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(
context.clone(),
PromiseResult::Successful(b"true".to_vec()),
);
contract.on_whitelist_is_whitelisted(true, staking_pool.clone());
// Deposit to the staking_pool
let mut total_amount = 0;
let amount = to_yocto(100);
for _ in 1..=5 {
total_amount += amount;
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.deposit_to_staking_pool(amount.into());
context.account_balance = env::account_balance();
assert_eq!(context.account_balance, lockup_amount - total_amount);
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_deposit(amount.into());
context.is_view = true;
testing_env!(context.clone());
assert_eq!(contract.get_known_deposited_balance().0, total_amount);
assert_eq!(contract.get_owners_balance().0, lockup_amount);
assert_eq!(
contract.get_liquid_owners_balance().0,
lockup_amount - total_amount - MIN_BALANCE_FOR_STORAGE
);
context.is_view = false;
}
// Withdrawing from the staking_pool. Plus one extra time as a reward
let mut total_withdrawn_amount = 0;
for _ in 1..=6 {
total_withdrawn_amount += amount;
context.predecessor_account_id = account_owner();
testing_env!(context.clone());
contract.withdraw_from_staking_pool(amount.into());
context.account_balance += amount;
assert_eq!(
context.account_balance,
lockup_amount - total_amount + total_withdrawn_amount
);
context.predecessor_account_id = lockup_account();
testing_env_with_promise_results(context.clone(), PromiseResult::Successful(vec![]));
contract.on_staking_pool_withdraw(amount.into());
context.is_view = true;
testing_env!(context.clone());
assert_eq!(
contract.get_known_deposited_balance().0,
total_amount.saturating_sub(total_withdrawn_amount)
);
assert_eq!(
contract.get_owners_balance().0,
lockup_amount + total_withdrawn_amount.saturating_sub(total_amount)
);
assert_eq!(
contract.get_liquid_owners_balance().0,
lockup_amount - total_amount + total_withdrawn_amount - MIN_BALANCE_FOR_STORAGE
);
context.is_view = false;
}
}