-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathlib.rs
902 lines (796 loc) · 32.5 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
use std::convert::TryInto;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::UnorderedMap;
use near_sdk::json_types::{Base58PublicKey, U128};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{
env, ext_contract, near_bindgen, AccountId, Balance, EpochHeight, Promise, PromiseResult,
PublicKey,
};
use uint::construct_uint;
mod internal;
/// The amount of gas given to complete `vote` call.
const VOTE_GAS: u64 = 100_000_000_000_000;
/// The amount of gas given to complete internal `on_stake_action` call.
const ON_STAKE_ACTION_GAS: u64 = 20_000_000_000_000;
/// The amount of yocto NEAR the contract dedicates to guarantee that the "share" price never
/// decreases. It's used during rounding errors for share -> amount conversions.
const STAKE_SHARE_PRICE_GUARANTEE_FUND: Balance = 1_000_000_000_000;
/// There is no deposit balance attached.
const NO_DEPOSIT: Balance = 0;
/// A type to distinguish between a balance and "stake" shares for better readability.
pub type NumStakeShares = Balance;
construct_uint! {
/// 256-bit unsigned integer.
pub struct U256(4);
}
#[cfg(test)]
mod test_utils;
#[global_allocator]
static ALLOC: near_sdk::wee_alloc::WeeAlloc = near_sdk::wee_alloc::WeeAlloc::INIT;
/// Inner account data of a delegate.
#[derive(BorshDeserialize, BorshSerialize, Debug, PartialEq)]
pub struct Account {
/// The unstaked balance. It represents the amount the account has on this contract that
/// can either be staked or withdrawn.
pub unstaked: Balance,
/// The amount of "stake" shares. Every stake share corresponds to the amount of staked balance.
/// NOTE: The number of shares should always be less or equal than the amount of staked balance.
/// This means the price of stake share should always be at least `1`.
/// The price of stake share can be computed as `total_staked_balance` / `total_stake_shares`.
pub stake_shares: NumStakeShares,
/// The minimum epoch height when the withdrawn is allowed.
/// This changes after unstaking action, because the amount is still locked for 3 epochs.
pub unstaked_available_epoch_height: EpochHeight,
}
/// Represents an account structure readable by humans.
#[derive(Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct HumanReadableAccount {
pub account_id: AccountId,
/// The unstaked balance that can be withdrawn or staked.
pub unstaked_balance: U128,
/// The amount balance staked at the current "stake" share price.
pub staked_balance: U128,
/// Whether the unstaked balance is available for withdrawal now.
pub can_withdraw: bool,
}
impl Default for Account {
fn default() -> Self {
Self {
unstaked: 0,
stake_shares: 0,
unstaked_available_epoch_height: 0,
}
}
}
/// The number of epochs required for the locked balance to become unlocked.
/// NOTE: The actual number of epochs when the funds are unlocked is 3. But there is a corner case
/// when the unstaking promise can arrive at the next epoch, while the inner state is already
/// updated in the previous epoch. It will not unlock the funds for 4 epochs.
const NUM_EPOCHS_TO_UNLOCK: EpochHeight = 4;
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct StakingContract {
/// The account ID of the owner who's running the staking validator node.
/// NOTE: This is different from the current account ID which is used as a validator account.
/// The owner of the staking pool can change staking public key and adjust reward fees.
pub owner_id: AccountId,
/// The public key which is used for staking action. It's the public key of the validator node
/// that validates on behalf of the pool.
pub stake_public_key: PublicKey,
/// The last epoch height when `ping` was called.
pub last_epoch_height: EpochHeight,
/// The last total balance of the account (consists of staked and unstaked balances).
pub last_total_balance: Balance,
/// The total amount of shares. It should be equal to the total amount of shares across all
/// accounts.
pub total_stake_shares: NumStakeShares,
/// The total staked balance.
pub total_staked_balance: Balance,
/// The fraction of the reward that goes to the owner of the staking pool for running the
/// validator node.
pub reward_fee_fraction: RewardFeeFraction,
/// Persistent map from an account ID to the corresponding account.
pub accounts: UnorderedMap<AccountId, Account>,
/// Whether the staking is paused.
/// When paused, the account unstakes everything (stakes 0) and doesn't restake.
/// It doesn't affect the staking shares or reward distribution.
/// Pausing is useful for node maintenance. Only the owner can pause and resume staking.
/// The contract is not paused by default.
pub paused: bool,
}
impl Default for StakingContract {
fn default() -> Self {
env::panic(b"Staking contract should be initialized before usage")
}
}
#[derive(BorshDeserialize, BorshSerialize, Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct RewardFeeFraction {
pub numerator: u32,
pub denominator: u32,
}
impl RewardFeeFraction {
pub fn assert_valid(&self) {
assert_ne!(self.denominator, 0, "Denominator must be a positive number");
assert!(
self.numerator <= self.denominator,
"The reward fee must be less or equal to 1"
);
}
pub fn multiply(&self, value: Balance) -> Balance {
(U256::from(self.numerator) * U256::from(value) / U256::from(self.denominator)).as_u128()
}
}
/// Interface for a voting contract.
#[ext_contract(ext_voting)]
pub trait VoteContract {
/// Method for validators to vote or withdraw the vote.
/// Votes for if `is_vote` is true, or withdraws the vote if `is_vote` is false.
fn vote(&mut self, is_vote: bool);
}
/// Interface for the contract itself.
#[ext_contract(ext_self)]
pub trait SelfContract {
/// A callback to check the result of the staking action.
/// In case the stake amount is less than the minimum staking threshold, the staking action
/// fails, and the stake amount is not changed. This might lead to inconsistent state and the
/// follow withdraw calls might fail. To mitigate this, the contract will issue a new unstaking
/// action in case of the failure of the first staking action.
fn on_stake_action(&mut self);
}
#[near_bindgen]
impl StakingContract {
/// Initializes the contract with the given owner_id, initial staking public key (with ED25519
/// curve) and initial reward fee fraction that owner charges for the validation work.
///
/// The entire current balance of this contract will be used to stake. This allows contract to
/// always maintain staking shares that can't be unstaked or withdrawn.
/// It prevents inflating the price of the share too much.
#[init]
pub fn new(
owner_id: AccountId,
stake_public_key: Base58PublicKey,
reward_fee_fraction: RewardFeeFraction,
) -> Self {
assert!(!env::state_exists(), "Already initialized");
reward_fee_fraction.assert_valid();
assert!(
env::is_valid_account_id(owner_id.as_bytes()),
"The owner account ID is invalid"
);
let account_balance = env::account_balance();
let total_staked_balance = account_balance - STAKE_SHARE_PRICE_GUARANTEE_FUND;
assert_eq!(
env::account_locked_balance(),
0,
"The staking pool shouldn't be staking at the initialization"
);
let mut this = Self {
owner_id,
stake_public_key: stake_public_key.into(),
last_epoch_height: env::epoch_height(),
last_total_balance: account_balance,
total_staked_balance,
total_stake_shares: NumStakeShares::from(total_staked_balance),
reward_fee_fraction,
accounts: UnorderedMap::new(b"u".to_vec()),
paused: false,
};
// Staking with the current pool to make sure the staking key is valid.
this.internal_restake();
this
}
/// Distributes rewards and restakes if needed.
pub fn ping(&mut self) {
if self.internal_ping() {
self.internal_restake();
}
}
/// Deposits the attached amount into the inner account of the predecessor.
#[payable]
pub fn deposit(&mut self) {
let need_to_restake = self.internal_ping();
self.internal_deposit();
if need_to_restake {
self.internal_restake();
}
}
/// Deposits the attached amount into the inner account of the predecessor and stakes it.
#[payable]
pub fn deposit_and_stake(&mut self) {
self.internal_ping();
let amount = self.internal_deposit();
self.internal_stake(amount);
self.internal_restake();
}
/// Withdraws the entire unstaked balance from the predecessor account.
/// It's only allowed if the `unstake` action was not performed in the four most recent epochs.
pub fn withdraw_all(&mut self) {
let need_to_restake = self.internal_ping();
let account_id = env::predecessor_account_id();
let account = self.internal_get_account(&account_id);
self.internal_withdraw(account.unstaked);
if need_to_restake {
self.internal_restake();
}
}
/// Withdraws the non staked balance for given account.
/// It's only allowed if the `unstake` action was not performed in the four most recent epochs.
pub fn withdraw(&mut self, amount: U128) {
let need_to_restake = self.internal_ping();
let amount: Balance = amount.into();
self.internal_withdraw(amount);
if need_to_restake {
self.internal_restake();
}
}
/// Stakes all available unstaked balance from the inner account of the predecessor.
pub fn stake_all(&mut self) {
// Stake action always restakes
self.internal_ping();
let account_id = env::predecessor_account_id();
let account = self.internal_get_account(&account_id);
self.internal_stake(account.unstaked);
self.internal_restake();
}
/// Stakes the given amount from the inner account of the predecessor.
/// The inner account should have enough unstaked balance.
pub fn stake(&mut self, amount: U128) {
// Stake action always restakes
self.internal_ping();
let amount: Balance = amount.into();
self.internal_stake(amount);
self.internal_restake();
}
/// Unstakes all staked balance from the inner account of the predecessor.
/// The new total unstaked balance will be available for withdrawal in four epochs.
pub fn unstake_all(&mut self) {
// Unstake action always restakes
self.internal_ping();
let account_id = env::predecessor_account_id();
let account = self.internal_get_account(&account_id);
let amount = self.staked_amount_from_num_shares_rounded_down(account.stake_shares);
self.inner_unstake(amount);
self.internal_restake();
}
/// Unstakes the given amount from the inner account of the predecessor.
/// The inner account should have enough staked balance.
/// The new total unstaked balance will be available for withdrawal in four epochs.
pub fn unstake(&mut self, amount: U128) {
// Unstake action always restakes
self.internal_ping();
let amount: Balance = amount.into();
self.inner_unstake(amount);
self.internal_restake();
}
/****************/
/* View methods */
/****************/
/// Returns the unstaked balance of the given account.
pub fn get_account_unstaked_balance(&self, account_id: AccountId) -> U128 {
self.get_account(account_id).unstaked_balance
}
/// Returns the staked balance of the given account.
/// NOTE: This is computed from the amount of "stake" shares the given account has and the
/// current amount of total staked balance and total stake shares on the account.
pub fn get_account_staked_balance(&self, account_id: AccountId) -> U128 {
self.get_account(account_id).staked_balance
}
/// Returns the total balance of the given account (including staked and unstaked balances).
pub fn get_account_total_balance(&self, account_id: AccountId) -> U128 {
let account = self.get_account(account_id);
(account.unstaked_balance.0 + account.staked_balance.0).into()
}
/// Returns `true` if the given account can withdraw tokens in the current epoch.
pub fn is_account_unstaked_balance_available(&self, account_id: AccountId) -> bool {
self.get_account(account_id).can_withdraw
}
/// Returns the total staking balance.
pub fn get_total_staked_balance(&self) -> U128 {
self.total_staked_balance.into()
}
/// Returns account ID of the staking pool owner.
pub fn get_owner_id(&self) -> AccountId {
self.owner_id.clone()
}
/// Returns the current reward fee as a fraction.
pub fn get_reward_fee_fraction(&self) -> RewardFeeFraction {
self.reward_fee_fraction.clone()
}
/// Returns the staking public key
pub fn get_staking_key(&self) -> Base58PublicKey {
self.stake_public_key.clone().try_into().unwrap()
}
/// Returns true if the staking is paused
pub fn is_staking_paused(&self) -> bool {
self.paused
}
/// Returns human readable representation of the account for the given account ID.
pub fn get_account(&self, account_id: AccountId) -> HumanReadableAccount {
let account = self.internal_get_account(&account_id);
HumanReadableAccount {
account_id,
unstaked_balance: account.unstaked.into(),
staked_balance: self
.staked_amount_from_num_shares_rounded_down(account.stake_shares)
.into(),
can_withdraw: account.unstaked_available_epoch_height <= env::epoch_height(),
}
}
/// Returns the number of accounts that have positive balance on this staking pool.
pub fn get_number_of_accounts(&self) -> u64 {
self.accounts.len()
}
/// Returns the list of accounts
pub fn get_accounts(&self, from_index: u64, limit: u64) -> Vec<HumanReadableAccount> {
let keys = self.accounts.keys_as_vector();
(from_index..std::cmp::min(from_index + limit, keys.len()))
.map(|index| self.get_account(keys.get(index).unwrap()))
.collect()
}
/*************/
/* Callbacks */
/*************/
pub fn on_stake_action(&mut self) {
assert_eq!(
env::current_account_id(),
env::predecessor_account_id(),
"Can be called only as a callback"
);
assert_eq!(
env::promise_results_count(),
1,
"Contract expected a result on the callback"
);
let stake_action_succeeded = match env::promise_result(0) {
PromiseResult::Successful(_) => true,
_ => false,
};
// If the stake action failed and the current locked amount is positive, then the contract
// has to unstake.
if !stake_action_succeeded && env::account_locked_balance() > 0 {
Promise::new(env::current_account_id()).stake(0, self.stake_public_key.clone());
}
}
/*******************/
/* Owner's methods */
/*******************/
/// Owner's method.
/// Updates current public key to the new given public key.
pub fn update_staking_key(&mut self, stake_public_key: Base58PublicKey) {
self.assert_owner();
// When updating the staking key, the contract has to restake.
let _need_to_restake = self.internal_ping();
self.stake_public_key = stake_public_key.into();
self.internal_restake();
}
/// Owner's method.
/// Updates current reward fee fraction to the new given fraction.
pub fn update_reward_fee_fraction(&mut self, reward_fee_fraction: RewardFeeFraction) {
self.assert_owner();
reward_fee_fraction.assert_valid();
let need_to_restake = self.internal_ping();
self.reward_fee_fraction = reward_fee_fraction;
if need_to_restake {
self.internal_restake();
}
}
/// Owner's method.
/// Calls `vote(is_vote)` on the given voting contract account ID on behalf of the pool.
pub fn vote(&mut self, voting_account_id: AccountId, is_vote: bool) -> Promise {
self.assert_owner();
assert!(
env::is_valid_account_id(voting_account_id.as_bytes()),
"Invalid voting account ID"
);
ext_voting::vote(is_vote, &voting_account_id, NO_DEPOSIT, VOTE_GAS)
}
/// Owner's method.
/// Pauses pool staking.
pub fn pause_staking(&mut self) {
self.assert_owner();
assert!(!self.paused, "The staking is already paused");
self.internal_ping();
self.paused = true;
Promise::new(env::current_account_id()).stake(0, self.stake_public_key.clone());
}
/// Owner's method.
/// Resumes pool staking.
pub fn resume_staking(&mut self) {
self.assert_owner();
assert!(self.paused, "The staking is not paused");
self.internal_ping();
self.paused = false;
self.internal_restake();
}
}
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use near_sdk::{serde_json, testing_env, MockedBlockchain, VMContext};
use crate::test_utils::*;
use super::*;
struct Emulator {
pub contract: StakingContract,
pub epoch_height: EpochHeight,
pub amount: Balance,
pub locked_amount: Balance,
last_total_staked_balance: Balance,
last_total_stake_shares: Balance,
context: VMContext,
}
fn zero_fee() -> RewardFeeFraction {
RewardFeeFraction {
numerator: 0,
denominator: 1,
}
}
impl Emulator {
pub fn new(
owner: String,
stake_public_key: String,
reward_fee_fraction: RewardFeeFraction,
) -> Self {
let context = VMContextBuilder::new()
.current_account_id(owner.clone())
.account_balance(ntoy(30))
.finish();
testing_env!(context.clone());
let contract = StakingContract::new(
owner,
Base58PublicKey::try_from(stake_public_key).unwrap(),
reward_fee_fraction,
);
let last_total_staked_balance = contract.total_staked_balance;
let last_total_stake_shares = contract.total_stake_shares;
Emulator {
contract,
epoch_height: 0,
amount: ntoy(30),
locked_amount: 0,
last_total_staked_balance,
last_total_stake_shares,
context,
}
}
fn verify_stake_price_increase_guarantee(&mut self) {
let total_staked_balance = self.contract.total_staked_balance;
let total_stake_shares = self.contract.total_stake_shares;
assert!(
U256::from(total_staked_balance) * U256::from(self.last_total_stake_shares)
>= U256::from(self.last_total_staked_balance) * U256::from(total_stake_shares),
"Price increase guarantee was violated."
);
self.last_total_staked_balance = total_staked_balance;
self.last_total_stake_shares = total_stake_shares;
}
pub fn update_context(&mut self, predecessor_account_id: String, deposit: Balance) {
self.verify_stake_price_increase_guarantee();
self.context = VMContextBuilder::new()
.current_account_id(staking())
.predecessor_account_id(predecessor_account_id.clone())
.signer_account_id(predecessor_account_id)
.attached_deposit(deposit)
.account_balance(self.amount)
.account_locked_balance(self.locked_amount)
.epoch_height(self.epoch_height)
.finish();
testing_env!(self.context.clone());
println!(
"Epoch: {}, Deposit: {}, amount: {}, locked_amount: {}",
self.epoch_height, deposit, self.amount, self.locked_amount
);
}
pub fn simulate_stake_call(&mut self) {
let total_stake = self.contract.total_staked_balance;
// Stake action
self.amount = self.amount + self.locked_amount - total_stake;
self.locked_amount = total_stake;
// Second function call action
self.update_context(staking(), 0);
}
pub fn skip_epochs(&mut self, num: EpochHeight) {
self.epoch_height += num;
self.locked_amount = (self.locked_amount * (100 + u128::from(num))) / 100;
}
}
#[test]
fn test_restake_fail() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
zero_fee(),
);
emulator.update_context(bob(), 0);
emulator.contract.internal_restake();
let receipts = env::created_receipts();
assert_eq!(receipts.len(), 2);
// Mocked Receipt fields are private, so can't check directly.
assert!(serde_json::to_string(&receipts[0])
.unwrap()
.contains("\"actions\":[{\"Stake\":{\"stake\":29999999999999000000000000,"));
assert!(serde_json::to_string(&receipts[1])
.unwrap()
.contains("\"method_name\":\"on_stake_action\""));
emulator.simulate_stake_call();
emulator.update_context(staking(), 0);
testing_env_with_promise_results(emulator.context.clone(), PromiseResult::Failed);
emulator.contract.on_stake_action();
let receipts = env::created_receipts();
assert_eq!(receipts.len(), 1);
assert!(serde_json::to_string(&receipts[0])
.unwrap()
.contains("\"actions\":[{\"Stake\":{\"stake\":0,"));
}
#[test]
fn test_deposit_withdraw() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
zero_fee(),
);
let deposit_amount = ntoy(1_000_000);
emulator.update_context(bob(), deposit_amount);
emulator.contract.deposit();
emulator.amount += deposit_amount;
emulator.update_context(bob(), 0);
assert_eq!(
emulator.contract.get_account_unstaked_balance(bob()).0,
deposit_amount
);
emulator.contract.withdraw(deposit_amount.into());
assert_eq!(
emulator.contract.get_account_unstaked_balance(bob()).0,
0u128
);
}
#[test]
fn test_stake_with_fee() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
RewardFeeFraction {
numerator: 10,
denominator: 100,
},
);
let deposit_amount = ntoy(1_000_000);
emulator.update_context(bob(), deposit_amount);
emulator.contract.deposit();
emulator.amount += deposit_amount;
emulator.update_context(bob(), 0);
emulator.contract.stake(deposit_amount.into());
emulator.simulate_stake_call();
assert_eq!(
emulator.contract.get_account_staked_balance(bob()).0,
deposit_amount
);
let locked_amount = emulator.locked_amount;
let n_locked_amount = yton(locked_amount);
emulator.skip_epochs(10);
// Overriding rewards (+ 100K reward)
emulator.locked_amount = locked_amount + ntoy(100_000);
emulator.update_context(bob(), 0);
emulator.contract.ping();
let expected_amount = deposit_amount
+ ntoy((yton(deposit_amount) * 90_000 + n_locked_amount / 2) / n_locked_amount);
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(bob()).0,
expected_amount
);
// Owner got 10% of the rewards
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(owner()).0,
ntoy(10_000)
);
let locked_amount = emulator.locked_amount;
let n_locked_amount = yton(locked_amount);
emulator.skip_epochs(10);
// Overriding rewards (another 100K reward)
emulator.locked_amount = locked_amount + ntoy(100_000);
emulator.update_context(bob(), 0);
emulator.contract.ping();
// previous balance plus (1_090_000 / 1_100_030)% of the 90_000 reward (rounding to nearest).
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(bob()).0,
expected_amount
+ ntoy((yton(expected_amount) * 90_000 + n_locked_amount / 2) / n_locked_amount)
);
// owner earns 10% with the fee and also small percentage from restaking.
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(owner()).0,
ntoy(10_000)
+ ntoy(10_000)
+ ntoy((10_000u128 * 90_000 + n_locked_amount / 2) / n_locked_amount)
);
assert_eq!(emulator.contract.get_number_of_accounts(), 2);
}
#[test]
fn test_stake_unstake() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
zero_fee(),
);
let deposit_amount = ntoy(1_000_000);
emulator.update_context(bob(), deposit_amount);
emulator.contract.deposit();
emulator.amount += deposit_amount;
emulator.update_context(bob(), 0);
emulator.contract.stake(deposit_amount.into());
emulator.simulate_stake_call();
assert_eq!(
emulator.contract.get_account_staked_balance(bob()).0,
deposit_amount
);
let locked_amount = emulator.locked_amount;
// 10 epochs later, unstake half of the money.
emulator.skip_epochs(10);
// Overriding rewards
emulator.locked_amount = locked_amount + ntoy(10);
emulator.update_context(bob(), 0);
emulator.contract.ping();
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(bob()).0,
deposit_amount + ntoy(10)
);
emulator.contract.unstake((deposit_amount / 2).into());
emulator.simulate_stake_call();
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(bob()).0,
deposit_amount / 2 + ntoy(10)
);
assert_eq_in_near!(
emulator.contract.get_account_unstaked_balance(bob()).0,
deposit_amount / 2
);
let acc = emulator.contract.get_account(bob());
assert_eq!(acc.account_id, bob());
assert_eq_in_near!(acc.unstaked_balance.0, deposit_amount / 2);
assert_eq_in_near!(acc.staked_balance.0, deposit_amount / 2 + ntoy(10));
assert!(!acc.can_withdraw);
assert!(!emulator
.contract
.is_account_unstaked_balance_available(bob()),);
emulator.skip_epochs(4);
emulator.update_context(bob(), 0);
assert!(emulator
.contract
.is_account_unstaked_balance_available(bob()),);
}
#[test]
fn test_stake_all_unstake_all() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
zero_fee(),
);
let deposit_amount = ntoy(1_000_000);
emulator.update_context(bob(), deposit_amount);
emulator.contract.deposit_and_stake();
emulator.amount += deposit_amount;
emulator.simulate_stake_call();
assert_eq!(
emulator.contract.get_account_staked_balance(bob()).0,
deposit_amount
);
assert_eq_in_near!(emulator.contract.get_account_unstaked_balance(bob()).0, 0);
let locked_amount = emulator.locked_amount;
// 10 epochs later, unstake all.
emulator.skip_epochs(10);
// Overriding rewards
emulator.locked_amount = locked_amount + ntoy(10);
emulator.update_context(bob(), 0);
emulator.contract.ping();
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(bob()).0,
deposit_amount + ntoy(10)
);
emulator.contract.unstake_all();
emulator.simulate_stake_call();
assert_eq_in_near!(emulator.contract.get_account_staked_balance(bob()).0, 0);
assert_eq_in_near!(
emulator.contract.get_account_unstaked_balance(bob()).0,
deposit_amount + ntoy(10)
);
}
/// Test that two can delegate and then undelegate their funds and rewards at different time.
#[test]
fn test_two_delegates() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
zero_fee(),
);
emulator.update_context(alice(), ntoy(1_000_000));
emulator.contract.deposit();
emulator.amount += ntoy(1_000_000);
emulator.update_context(alice(), 0);
emulator.contract.stake(ntoy(1_000_000).into());
emulator.simulate_stake_call();
emulator.skip_epochs(3);
emulator.update_context(bob(), ntoy(1_000_000));
emulator.contract.deposit();
emulator.amount += ntoy(1_000_000);
emulator.update_context(bob(), 0);
emulator.contract.stake(ntoy(1_000_000).into());
emulator.simulate_stake_call();
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(bob()).0,
ntoy(1_000_000)
);
emulator.skip_epochs(3);
emulator.update_context(alice(), 0);
emulator.contract.ping();
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(alice()).0,
ntoy(1_060_900) - 1
);
assert_eq_in_near!(
emulator.contract.get_account_staked_balance(bob()).0,
ntoy(1_030_000)
);
// Checking accounts view methods
// Should be 2, because the pool has 0 fee.
assert_eq!(emulator.contract.get_number_of_accounts(), 2);
let accounts = emulator.contract.get_accounts(0, 10);
assert_eq!(accounts.len(), 2);
assert_eq!(accounts[0].account_id, alice());
assert_eq!(accounts[1].account_id, bob());
let accounts = emulator.contract.get_accounts(1, 10);
assert_eq!(accounts.len(), 1);
assert_eq!(accounts[0].account_id, bob());
let accounts = emulator.contract.get_accounts(0, 1);
assert_eq!(accounts.len(), 1);
assert_eq!(accounts[0].account_id, alice());
let accounts = emulator.contract.get_accounts(2, 10);
assert_eq!(accounts.len(), 0);
}
#[test]
fn test_low_balances() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
zero_fee(),
);
let initial_balance = 100;
emulator.update_context(alice(), initial_balance);
emulator.contract.deposit();
emulator.amount += initial_balance;
let mut remaining = initial_balance;
let mut amount = 1;
while remaining >= 4 {
emulator.update_context(alice(), 0);
amount = 2 + (amount - 1) % 3;
emulator.contract.stake(amount.into());
emulator.simulate_stake_call();
remaining -= amount;
}
}
#[test]
fn test_rewards() {
let mut emulator = Emulator::new(
owner(),
"KuTCtARNzxZQ3YvXDeLjx83FDqxv2SdQTSbiq876zR7".to_string(),
zero_fee(),
);
let initial_balance = ntoy(100);
emulator.update_context(alice(), initial_balance);
emulator.contract.deposit();
emulator.amount += initial_balance;
let mut remaining = 100;
let mut amount = 1;
while remaining >= 4 {
emulator.skip_epochs(3);
emulator.update_context(alice(), 0);
emulator.contract.ping();
emulator.update_context(alice(), 0);
amount = 2 + (amount - 1) % 3;
emulator.contract.stake(ntoy(amount).into());
emulator.simulate_stake_call();
remaining -= amount;
}
}
}