-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathflows.cairo
More file actions
7065 lines (6247 loc) · 291 KB
/
flows.cairo
File metadata and controls
7065 lines (6247 loc) · 291 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
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
use ReleaseVersion::{V0, V1, V2};
use core::num::traits::Zero;
use core::num::traits::ops::pow::Pow;
use snforge_std::{TokenImpl, get_class_hash, start_cheat_block_number_global};
use staking::attestation::attestation::Attestation::MIN_ATTESTATION_WINDOW;
use staking::constants::{ALPHA, ALPHA_DENOMINATOR, STRK_IN_FRIS};
use staking::errors::{GenericError, InternalError};
use staking::flow_test::utils::MainnetClassHashes::{
MAINNET_POOL_CLASS_HASH_V1, MAINNET_STAKING_CLASS_HASH_V0, MAINNET_STAKING_CLASS_HASH_V1,
MAINNET_STAKING_CLASS_HASH_V2,
};
use staking::flow_test::utils::{
AttestationTrait, Delegator, FlowTrait, MultiVersionFlowTrait, ReleaseVersion,
RewardSupplierTrait, Staker, StakingTrait, SystemDelegatorTrait, SystemPoolTrait,
SystemStakerTrait, SystemState, SystemTrait, TokenHelperTrait,
};
use staking::pool::errors::Error as PoolError;
use staking::pool::interface_v0::{
PoolMemberInfo, PoolMemberInfoIntoInternalPoolMemberInfoV1Trait, PoolMemberInfoTrait,
};
use staking::pool::pool::Pool::STRK_CONFIG;
use staking::pool::utils::compute_rewards_rounded_down;
use staking::staking::errors::Error as StakingError;
use staking::staking::interface::{
IStakingConsensusDispatcherTrait, IStakingConsensusSafeDispatcherTrait, IStakingDispatcherTrait,
IStakingMigrationSafeDispatcherTrait, IStakingSafeDispatcherTrait, PoolInfo, StakerInfoV1,
StakerInfoV1Trait, StakerPoolInfoV2,
};
use staking::staking::objects::{EpochInfoTrait, NormalizedAmountTrait, StakerVersion};
use staking::staking::staking::Staking::V3_PREV_CONTRACT_VERSION;
use staking::staking::utils::STRK_WEIGHT_FACTOR;
use staking::test_utils::constants::{
BTC_18D_CONFIG, BTC_8D_CONFIG, BTC_DECIMALS_18, BTC_DECIMALS_8, EPOCH_DURATION, PUBLIC_KEY,
STRK_BASE_VALUE, TEST_BTC_DECIMALS, TEST_MIN_BTC_FOR_REWARDS, TEST_ONE_BTC,
};
use staking::test_utils::{
calculate_pool_member_rewards, calculate_staker_btc_pool_rewards_v2,
calculate_staker_strk_rewards_v2, calculate_strk_pool_rewards_v2,
calculate_strk_pool_rewards_with_pool_balance_v2, compute_rewards_per_unit,
declare_pool_contract, declare_pool_eic_contract, declare_staking_contract,
load_from_simple_map, load_one_felt, strk_pool_update_rewards_v0, to_amount_18_decimals,
upgrade_implementation,
};
use staking::types::{Amount, Commission};
use starknet::{ClassHash, ContractAddress, Store};
use starkware_utils::components::replaceability::interface::{EICData, ImplementationData};
use starkware_utils::errors::{Describable, ErrorDisplay};
use starkware_utils::math::abs::wide_abs_diff;
use starkware_utils::math::utils::mul_wide_and_div;
use starkware_utils::time::time::{Time, TimeDelta};
use starkware_utils_testing::test_utils::{assert_panic_with_error, cheat_caller_address_once};
/// Flow - Basic Stake:
/// Staker - Stake with pool - cover if pool_enabled=true
/// Staker increase_stake - cover if pool amount = 0 in calc_rew
/// Delegator delegate (and create) to Staker
/// Staker increase_stake - cover pool amount > 0 in calc_rew
/// Delegator increase_delegate
/// Exit and check
#[derive(Drop, Copy)]
pub(crate) struct BasicStakeFlow {}
pub(crate) impl BasicStakeFlowImpl of FlowTrait<BasicStakeFlow> {
fn test(self: BasicStakeFlow, ref system: SystemState) {
let min_stake = system.staking.get_min_stake();
let stake_amount = min_stake * 2;
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let staker = system.new_staker(amount: stake_amount * 2);
system.stake(:staker, amount: stake_amount, pool_enabled: true, commission: 200);
system.advance_k_epochs_and_attest(:staker);
system.increase_stake(:staker, amount: stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
let pool = system.staking.get_pool(:staker);
let delegator = system.new_delegator(amount: stake_amount);
system.delegate(:delegator, :pool, amount: stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
system.increase_stake(:staker, amount: stake_amount / 4);
system.advance_k_epochs_and_attest(:staker);
system.increase_delegate(:delegator, :pool, amount: stake_amount / 4);
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: stake_amount * 3 / 4);
system.advance_k_epochs_and_attest(:staker);
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.delegator_exit_action(:delegator, :pool);
system.delegator_claim_rewards(:delegator, :pool);
system.staker_exit_action(:staker);
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(system.token.balance_of(account: pool) < 100);
assert!(system.token.balance_of(account: staker.staker.address) == stake_amount * 2);
assert!(system.token.balance_of(account: delegator.delegator.address) == stake_amount);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: pool),
);
}
}
/// Pool upgrade regression flow.
/// Staker stake with pool
/// Upgrade
/// BasicStakeFlow
/// Staker2 stake with pool
/// Delegator switch
/// Test switch
#[derive(Drop, Copy)]
pub(crate) struct PoolUpgradeBasicFlow {
pub(crate) staker: Option<Staker>,
pub(crate) stake_amount: Option<Amount>,
pub(crate) initial_reward_supplier_balance: Option<Amount>,
}
pub(crate) impl PoolUpgradeBasicFlowImpl of MultiVersionFlowTrait<PoolUpgradeBasicFlow> {
fn versions(self: PoolUpgradeBasicFlow) -> Span<ReleaseVersion> {
[V1, V2].span()
}
fn setup(ref self: PoolUpgradeBasicFlow, ref system: SystemState) {
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let amount = system.staking.get_min_stake();
let staker = system.new_staker(amount: amount * 2);
let commission = 200;
system.stake(:staker, :amount, pool_enabled: true, :commission);
system.set_staker_for_migration(staker_address: staker.staker.address);
self.staker = Option::Some(staker);
self.stake_amount = Option::Some(amount);
self.initial_reward_supplier_balance = Option::Some(initial_reward_supplier_balance);
}
fn test(self: PoolUpgradeBasicFlow, ref system: SystemState, version: ReleaseVersion) {
let staker = self.staker.unwrap();
let stake_amount = self.stake_amount.unwrap();
let initial_reward_supplier_balance = self.initial_reward_supplier_balance.unwrap();
// BasicStakeFlow
system.advance_k_epochs_and_attest(:staker);
system.increase_stake(:staker, amount: stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
let pool = system.staking.get_pool(:staker);
let delegator = system.new_delegator(amount: stake_amount);
system.delegate(:delegator, :pool, amount: stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
system.increase_stake(:staker, amount: stake_amount / 4);
system.advance_k_epochs_and_attest(:staker);
system.increase_delegate(:delegator, :pool, amount: stake_amount / 4);
system.advance_k_epochs_and_attest(:staker);
let switch_amount = stake_amount * 3 / 4;
system.delegator_exit_intent(:delegator, :pool, amount: switch_amount);
system.advance_k_epochs_and_attest(:staker);
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.staker_exit_action(:staker);
system.delegator_claim_rewards(:delegator, :pool);
// Staker2 stake with pool
let staker2 = system.new_staker(amount: stake_amount);
system.stake(staker: staker2, amount: stake_amount, pool_enabled: true, commission: 100);
let pool2 = system.staking.get_pool(staker: staker2);
// Delegator switch
system
.switch_delegation_pool(
:delegator,
from_pool: pool,
to_staker: staker2.staker.address,
to_pool: pool2,
amount: switch_amount,
);
// Test balances
assert!(
system.token.balance_of(account: system.staking.address) == switch_amount
+ stake_amount,
);
assert!(system.token.balance_of(account: pool) < 100);
assert!(system.token.balance_of(account: staker.staker.address) == stake_amount * 2);
assert!(
system.token.balance_of(account: delegator.delegator.address) == stake_amount
- switch_amount,
);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: pool),
);
}
}
/// Flow - Basic Stake with BTC:
/// Staker stake
/// Staker open BTC pool
/// Staker increase_stake - cover if pool amount = 0 in calc_rew
/// Delegator delegate (and create) to Staker
/// Staker increase_stake - cover pool amount > 0 in calc_rew
/// Delegator increase_delegate
/// Delegator exit intent
/// Staker exit intent and action
/// Staker2 stake
/// Staker2 open BTC pool
/// Delegator switch
/// Test balances
#[derive(Drop, Copy)]
pub(crate) struct BasicStakeBTCFlow {}
pub(crate) impl BasicStakeBTCFlowImpl of FlowTrait<BasicStakeBTCFlow> {
fn test(self: BasicStakeBTCFlow, ref system: SystemState) {
let min_stake = system.staking.get_min_stake();
let stake_amount = min_stake * 2;
let delegate_amount = TEST_MIN_BTC_FOR_REWARDS * 16;
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let staker = system.new_staker(amount: stake_amount * 2);
let commission = 200;
system.stake(:staker, amount: stake_amount, pool_enabled: false, :commission);
// Staker open BTC pool
system.set_commission(:staker, :commission);
let token = system.btc_token;
let token_address = token.contract_address();
let pool = system.set_open_for_delegation(:staker, :token_address);
system.advance_k_epochs_and_attest(:staker);
system.increase_stake(:staker, amount: stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
let delegator = system.new_btc_delegator(amount: delegate_amount, :token);
system.delegate_btc(:delegator, :pool, amount: delegate_amount / 2, :token);
system.advance_k_epochs_and_attest(:staker);
system.increase_stake(:staker, amount: stake_amount / 4);
system.advance_k_epochs_and_attest(:staker);
system.increase_delegate_btc(:delegator, :pool, amount: delegate_amount / 4, :token);
system.advance_k_epochs_and_attest(:staker);
let switch_amount = delegate_amount * 3 / 4;
system.delegator_exit_intent(:delegator, :pool, amount: switch_amount);
system.advance_k_epochs_and_attest(:staker);
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.delegator_claim_rewards(:delegator, :pool);
system.staker_exit_action(:staker);
let staker2 = system.new_staker(amount: stake_amount);
system
.stake(
staker: staker2,
amount: stake_amount,
pool_enabled: false,
commission: commission / 2,
);
system.set_commission(staker: staker2, commission: commission / 2);
let pool2 = system.set_open_for_delegation(staker: staker2, :token_address);
system
.switch_delegation_pool(
:delegator,
from_pool: pool,
to_staker: staker2.staker.address,
to_pool: pool2,
amount: switch_amount,
);
assert!(system.token.balance_of(account: system.staking.address) == stake_amount);
assert!(system.btc_token.balance_of(account: system.staking.address) == switch_amount);
assert!(system.token.balance_of(account: pool) < 100);
assert!(system.token.balance_of(account: staker.staker.address) == stake_amount * 2);
assert!(
system.btc_token.balance_of(account: delegator.delegator.address) == delegate_amount
- switch_amount,
);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: pool),
);
}
}
/// Flow:
/// Staker Stake
/// Delegator delegate
/// Staker exit_intent
/// Staker exit_action
/// Delegator partially exit_intent
/// Delegator exit_action
/// Delegator exit_intent
/// Delegator exit_action
#[derive(Drop, Copy)]
pub(crate) struct DelegatorIntentAfterStakerActionFlow {}
pub(crate) impl DelegatorIntentAfterStakerActionFlowImpl of FlowTrait<
DelegatorIntentAfterStakerActionFlow,
> {
fn test(self: DelegatorIntentAfterStakerActionFlow, ref system: SystemState) {
let min_stake = system.staking.get_min_stake();
let stake_amount = min_stake * 2;
let staker = system.new_staker(amount: stake_amount * 2);
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let commission = 200;
system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
system.advance_k_epochs_and_attest(:staker);
let pool = system.staking.get_pool(:staker);
let delegator = system.new_delegator(amount: stake_amount);
system.delegate(:delegator, :pool, amount: stake_amount);
system.advance_k_epochs_and_attest(:staker);
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.staker_exit_action(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: stake_amount / 2);
system.delegator_exit_action(:delegator, :pool);
system.delegator_exit_intent(:delegator, :pool, amount: stake_amount / 2);
system.delegator_exit_action(:delegator, :pool);
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(
system.token.balance_of(account: pool) > 100,
); // TODO: Change this after implement calculate_rewards.
assert!(system.token.balance_of(account: staker.staker.address) == stake_amount * 2);
assert!(system.token.balance_of(account: delegator.delegator.address) == stake_amount);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(
system.token.balance_of(account: delegator.reward.address).is_zero(),
); // TODO: Change this after implement calculate_rewards.
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: pool),
);
}
}
/// Flow:
/// Staker - Stake without pool - cover if pool_enabled=false
/// Staker increase_stake - cover if pool amount=none in update_rewards
/// Staker claim_rewards
/// Staker set_open_for_delegation
/// Delegator delegate - cover delegating after opening an initially closed pool
/// Exit and check
#[derive(Drop, Copy)]
pub(crate) struct SetOpenForDelegationFlow {}
pub(crate) impl SetOpenForDelegationFlowImpl of FlowTrait<SetOpenForDelegationFlow> {
fn test(self: SetOpenForDelegationFlow, ref system: SystemState) {
let min_stake = system.staking.get_min_stake();
let initial_stake_amount = min_stake * 2;
let staker = system.new_staker(amount: initial_stake_amount * 2);
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let commission = 200;
system.stake(:staker, amount: initial_stake_amount, pool_enabled: false, :commission);
system.advance_k_epochs_and_attest(:staker);
system.increase_stake(:staker, amount: initial_stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
assert!(system.token.balance_of(account: staker.reward.address).is_zero());
system.staker_claim_rewards(:staker);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
let pool = system.set_open_for_strk_delegation(:staker, :commission);
system.advance_k_epochs_and_attest(:staker);
let delegator = system.new_delegator(amount: initial_stake_amount);
system.delegate(:delegator, :pool, amount: initial_stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.delegator_exit_intent(:delegator, :pool, amount: initial_stake_amount / 2);
system.delegator_exit_action(:delegator, :pool);
system.staker_exit_action(:staker);
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(
system.token.balance_of(account: pool) > 100,
); // TODO: Change this after implement calculate_rewards.
assert!(
system.token.balance_of(account: staker.staker.address) == initial_stake_amount * 2,
);
assert!(
system.token.balance_of(account: delegator.delegator.address) == initial_stake_amount,
);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(
system.token.balance_of(account: delegator.reward.address).is_zero(),
); // TODO: Change this after implement calculate_rewards.
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: pool),
);
}
}
/// Flow:
/// Staker1 stake with pool
/// Staker2 stake with pool
/// Staker1 add btc pool
/// Staker2 add btc pool
/// Strk delegator delegate to staker1
/// Btc delegator delegate to staker1
/// Delegators exit intent
/// Delegators change intent
/// Test delegators exit actions before exit window
/// Delegators exit actions after exit window
/// Delegators switch delegation pool
/// Test pools
#[derive(Drop, Copy)]
pub(crate) struct MultiplePoolsDelegatorIntentActionSwitchFlow {}
pub(crate) impl MultiplePoolsDelegatorIntentActionSwitchFlowImpl of FlowTrait<
MultiplePoolsDelegatorIntentActionSwitchFlow,
> {
fn test(self: MultiplePoolsDelegatorIntentActionSwitchFlow, ref system: SystemState) {
let amount = system.staking.get_min_stake();
let first_staker = system.new_staker(:amount);
let second_staker = system.new_staker(:amount);
let commission = 200;
system.stake(staker: first_staker, :amount, pool_enabled: true, :commission);
system.stake(staker: second_staker, :amount, pool_enabled: true, :commission);
// Set up pools.
let first_strk_pool = system.staking.get_pool(staker: first_staker);
let first_btc_pool = system
.set_open_for_delegation(
staker: first_staker, token_address: system.btc_token.contract_address(),
);
let second_strk_pool = system.staking.get_pool(staker: second_staker);
let second_btc_pool = system
.set_open_for_delegation(
staker: second_staker, token_address: system.btc_token.contract_address(),
);
// Delegators delegate.
let strk_delegator = system.new_delegator(:amount);
let btc_delegator = system.new_btc_delegator(:amount, token: system.btc_token);
system.delegate(delegator: strk_delegator, pool: first_strk_pool, :amount);
system
.delegate_btc(
delegator: btc_delegator, pool: first_btc_pool, :amount, token: system.btc_token,
);
// Delegators exit intents.
system.delegator_exit_intent(delegator: strk_delegator, pool: first_strk_pool, :amount);
system.delegator_exit_intent(delegator: btc_delegator, pool: first_btc_pool, :amount);
// Delegators change intent.
let amount = amount / 2;
system.delegator_exit_intent(delegator: strk_delegator, pool: first_strk_pool, :amount);
system.delegator_exit_intent(delegator: btc_delegator, pool: first_btc_pool, :amount);
// Delegators exit actions before exit window.
cheat_caller_address_once(
contract_address: first_strk_pool, caller_address: strk_delegator.delegator.address,
);
let res = system
.safe_delegator_exit_action(delegator: strk_delegator, pool: first_strk_pool);
assert_panic_with_error(res, GenericError::INTENT_WINDOW_NOT_FINISHED.describe());
cheat_caller_address_once(
contract_address: first_btc_pool, caller_address: btc_delegator.delegator.address,
);
let res = system.safe_delegator_exit_action(delegator: btc_delegator, pool: first_btc_pool);
assert_panic_with_error(res, GenericError::INTENT_WINDOW_NOT_FINISHED.describe());
// Delegators exit actions after exit window.
system.advance_time(time: system.staking.get_exit_wait_window());
system.delegator_exit_action(delegator: strk_delegator, pool: first_strk_pool);
system.delegator_exit_action(delegator: btc_delegator, pool: first_btc_pool);
// Delegators switch delegation pool.
system.delegator_exit_intent(delegator: strk_delegator, pool: first_strk_pool, :amount);
system.delegator_exit_intent(delegator: btc_delegator, pool: first_btc_pool, :amount);
system
.switch_delegation_pool(
delegator: strk_delegator,
from_pool: first_strk_pool,
to_staker: second_staker.staker.address,
to_pool: second_strk_pool,
amount: amount,
);
system
.switch_delegation_pool(
delegator: btc_delegator,
from_pool: first_btc_pool,
to_staker: second_staker.staker.address,
to_pool: second_btc_pool,
amount: amount,
);
// Test pools.
let first_staker_expected_pool_info = StakerPoolInfoV2 {
commission: Option::Some(commission),
pools: array![
PoolInfo {
pool_contract: first_strk_pool,
amount: Zero::zero(),
token_address: system.staking.get_token_address(),
},
PoolInfo {
pool_contract: first_btc_pool,
amount: Zero::zero(),
token_address: system.btc_token.contract_address(),
},
]
.span(),
};
let first_staker_pool_info = system.staker_pool_info(staker: first_staker);
assert!(first_staker_pool_info == first_staker_expected_pool_info);
let second_staker_expected_pool_info = StakerPoolInfoV2 {
commission: Option::Some(commission),
pools: array![
PoolInfo {
pool_contract: second_strk_pool,
amount,
token_address: system.staking.get_token_address(),
},
PoolInfo {
pool_contract: second_btc_pool,
amount,
token_address: system.btc_token.contract_address(),
},
]
.span(),
};
let second_staker_pool_info = system.staker_pool_info(staker: second_staker);
assert!(second_staker_pool_info == second_staker_expected_pool_info);
}
}
/// Flow:
/// Staker Stake
/// Delegator delegate
/// Delegator exit_intent partial amount
/// Delegator exit_intent with lower amount - cover lowering partial undelegate
/// Delegator exit_intent with zero amount - cover clearing an intent
/// Delegator exit_intent all amount
/// Delegator exit_action
#[derive(Drop, Copy)]
pub(crate) struct DelegatorIntentFlow {}
pub(crate) impl DelegatorIntentFlowImpl of FlowTrait<DelegatorIntentFlow> {
fn test(self: DelegatorIntentFlow, ref system: SystemState) {
let min_stake = system.staking.get_min_stake();
let stake_amount = min_stake * 2;
let staker = system.new_staker(amount: stake_amount);
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let commission = 200;
system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
system.advance_k_epochs_and_attest(:staker);
let pool = system.staking.get_pool(:staker);
let delegated_amount = stake_amount;
let delegator = system.new_delegator(amount: delegated_amount);
system.delegate(:delegator, :pool, amount: delegated_amount);
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: delegated_amount / 2);
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: delegated_amount / 4);
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: delegated_amount / 2);
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: Zero::zero());
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: delegated_amount);
system.advance_time(time: system.staking.get_exit_wait_window());
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_action(:delegator, :pool);
system.delegator_claim_rewards(:delegator, :pool);
system.advance_k_epochs_and_attest(:staker);
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.staker_exit_action(:staker);
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(system.token.balance_of(account: pool) < 100);
assert!(system.token.balance_of(account: staker.staker.address) == stake_amount);
assert!(system.token.balance_of(account: delegator.delegator.address) == delegated_amount);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: pool),
);
}
}
// Flow 8:
// Staker1 stake
// Staker2 stake
// Delegator delegate to staker1's pool
// Staker1 exit_intent
// Delegator exit_intent - get current block_timestamp as exit time
// Staker1 exit_action - cover staker action with while having a delegator in intent
// Delegator switch part of intent to staker2's pool - cover switching from a dead staker (should
// not matter he is back alive)
// Delegator exit_action in staker1's original pool - cover delegator exit action with dead staker
// Delegator claim rewards in staker2's pool - cover delegator claim rewards with dead staker
// Delegator exit_intent for remaining amount in staker1's original pool (the staker is dead there)
// Delegator exit_action in staker1's original pool - cover full delegator exit with dead staker
// Staker1 exit_intent
// Staker2 exit_intent
// Staker1 exit_action
// Staker2 exit_action
// Delegator exit_intent for full amount in staker2's pool
// Delegator exit_action for full amount in staker2's pool
#[derive(Drop, Copy)]
pub(crate) struct OperationsAfterDeadStakerFlow {}
pub(crate) impl OperationsAfterDeadStakerFlowImpl of FlowTrait<OperationsAfterDeadStakerFlow> {
fn test(self: OperationsAfterDeadStakerFlow, ref system: SystemState) {
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let min_stake = system.staking.get_min_stake();
let stake_amount = min_stake * 2;
let delegated_amount = stake_amount;
let staker1 = system.new_staker(amount: stake_amount);
let staker2 = system.new_staker(amount: stake_amount);
let delegator = system.new_delegator(amount: delegated_amount);
let commission = 200;
system.stake(staker: staker1, amount: stake_amount, pool_enabled: true, :commission);
system.advance_k_epochs_and_attest(staker: staker1);
system.stake(staker: staker2, amount: stake_amount, pool_enabled: true, :commission);
system.advance_k_epochs_and_attest(staker: staker1);
system.advance_k_epochs_and_attest(staker: staker2);
let staker1_pool = system.staking.get_pool(staker: staker1);
system.delegate(:delegator, pool: staker1_pool, amount: delegated_amount);
system.advance_k_epochs_and_attest(staker: staker1);
system.staker_exit_intent(staker: staker1);
system.advance_time(time: system.staking.get_exit_wait_window());
system.advance_k_epochs_and_attest(staker: staker2);
// After the following, delegator has 1/2 in staker1, and 1/2 in intent.
system.delegator_exit_intent(:delegator, pool: staker1_pool, amount: delegated_amount / 2);
system.advance_k_epochs_and_attest(staker: staker2);
system.staker_exit_action(staker: staker1);
// After the following, delegator has delegated_amount / 2 in staker1, delegated_amount
// / 4 in intent, and delegated_amount / 4 in staker2.
let staker2_pool = system.staking.get_pool(staker: staker2);
system
.switch_delegation_pool(
:delegator,
from_pool: staker1_pool,
to_staker: staker2.staker.address,
to_pool: staker2_pool,
amount: delegated_amount / 4,
);
system.advance_k_epochs_and_attest(staker: staker2);
// After the following, delegator has delegated_amount / 2 in staker1, and
// delegated_amount / 4 in staker2.
system.delegator_exit_action(:delegator, pool: staker1_pool);
system.advance_k_epochs_and_attest(staker: staker2);
// Claim rewards from second pool and see that the rewards are increasing.
assert!(system.token.balance_of(account: delegator.reward.address).is_zero());
system.delegator_claim_rewards(:delegator, pool: staker2_pool);
let delegator_reward_before_advance_epoch = system
.token
.balance_of(account: delegator.reward.address);
assert!(delegator_reward_before_advance_epoch.is_non_zero());
// Advance epoch and claim rewards again, and see that the rewards are increasing.
system.advance_epoch();
system.delegator_claim_rewards(:delegator, pool: staker2_pool);
let delegator_reward_after_advance_epoch = system
.token
.balance_of(account: delegator.reward.address);
assert!(delegator_reward_after_advance_epoch > delegator_reward_before_advance_epoch);
// Advance epoch and attest.
system.advance_k_epochs_and_attest(staker: staker2);
system.advance_epoch();
// After the following, delegator has delegated_amount / 4 in staker2.
system.delegator_exit_intent(:delegator, pool: staker1_pool, amount: delegated_amount / 2);
system.delegator_exit_action(:delegator, pool: staker1_pool);
system.delegator_claim_rewards(:delegator, pool: staker1_pool);
// Clean up and make all parties exit.
system.staker_exit_intent(staker: staker2);
system.advance_time(time: system.staking.get_exit_wait_window());
system.staker_exit_action(staker: staker2);
system.delegator_exit_intent(:delegator, pool: staker2_pool, amount: delegated_amount / 4);
system.delegator_exit_action(:delegator, pool: staker2_pool);
system.delegator_claim_rewards(:delegator, pool: staker2_pool);
// ------------- Flow complete, now asserts -------------
// Assert pools' balances are low.
assert!(system.token.balance_of(account: staker1_pool) < 100);
assert!(system.token.balance_of(account: staker2_pool) < 100);
// Assert all staked amounts were transferred back.
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(system.token.balance_of(account: staker1.staker.address) == stake_amount);
assert!(system.token.balance_of(account: staker2.staker.address) == stake_amount);
assert!(system.token.balance_of(account: delegator.delegator.address) == delegated_amount);
// Asserts reward addresses are not empty.
assert!(system.token.balance_of(account: staker1.reward.address).is_non_zero());
assert!(system.token.balance_of(account: staker2.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
// Assert all funds that moved from rewards supplier, were moved to correct addresses.
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker1.reward.address)
+ system.token.balance_of(account: staker2.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: staker1_pool)
+ system.token.balance_of(account: staker2_pool),
);
}
}
// Flow:
// Staker stake with commission 100%
// Delegator delegate
// Staker update_commission to 0%
// Delegator exit_intent
// Delegator exit_action, should get rewards
// Staker exit_intent
// Staker exit_action
#[derive(Drop, Copy)]
pub(crate) struct DelegatorDidntUpdateAfterStakerUpdateCommissionFlow {}
pub(crate) impl DelegatorDidntUpdateAfterStakerUpdateCommissionFlowImpl of FlowTrait<
DelegatorDidntUpdateAfterStakerUpdateCommissionFlow,
> {
fn test(self: DelegatorDidntUpdateAfterStakerUpdateCommissionFlow, ref system: SystemState) {
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let min_stake = system.staking.get_min_stake();
let stake_amount = min_stake * 2;
let delegated_amount = stake_amount;
let staker = system.new_staker(amount: stake_amount);
let delegator = system.new_delegator(amount: delegated_amount);
let commission = 10000;
// Stake with commission 100%
system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
system.advance_k_epochs_and_attest(:staker);
let pool = system.staking.get_pool(:staker);
system.delegate(:delegator, :pool, amount: delegated_amount);
// Update commission to 0%
system.set_commission(:staker, commission: Zero::zero());
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: delegated_amount);
system.advance_time(time: system.staking.get_exit_wait_window());
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_action(:delegator, :pool);
system.delegator_claim_rewards(:delegator, :pool);
// Clean up and make all parties exit.
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.staker_exit_action(:staker);
// ------------- Flow complete, now asserts -------------
// Assert pool balance is zero.
assert!(system.token.balance_of(account: pool) == 0);
// Assert all staked amounts were transferred back.
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(system.token.balance_of(account: staker.staker.address) == stake_amount);
assert!(system.token.balance_of(account: delegator.delegator.address) == delegated_amount);
// Assert staker reward address is not empty.
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
// Assert all funds that moved from rewards supplier, were moved to correct addresses.
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address),
);
}
}
// Flow:
// Staker stake with commission 100%
// Delegator delegate
// Staker update_commission to 0%
// Delegator claim rewards
// Delegator exit_intent
// Delegator exit_action, should get rewards
// Staker exit_intent
// Staker exit_action
#[derive(Drop, Copy)]
pub(crate) struct DelegatorUpdatedAfterStakerUpdateCommissionFlow {}
pub(crate) impl DelegatorUpdatedAfterStakerUpdateCommissionFlowImpl of FlowTrait<
DelegatorUpdatedAfterStakerUpdateCommissionFlow,
> {
fn test(self: DelegatorUpdatedAfterStakerUpdateCommissionFlow, ref system: SystemState) {
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let min_stake = system.staking.get_min_stake();
let stake_amount = min_stake * 2;
let delegated_amount = stake_amount;
let staker = system.new_staker(amount: stake_amount);
let delegator = system.new_delegator(amount: delegated_amount);
let commission = 10000;
// Stake with commission 100%.
system.stake(:staker, amount: stake_amount, pool_enabled: true, :commission);
system.advance_k_epochs_and_attest(:staker);
let pool = system.staking.get_pool(:staker);
system.delegate(:delegator, :pool, amount: delegated_amount);
system.advance_k_epochs_and_attest(:staker);
assert!(system.token.balance_of(account: pool).is_zero());
// Update commission to 0%.
system.set_commission(:staker, commission: Zero::zero());
system.advance_k_epochs_and_attest(:staker);
assert!(system.token.balance_of(account: pool).is_non_zero());
// Delegator claim_rewards.
system.delegator_claim_rewards(:delegator, :pool);
assert!(
system.token.balance_of(account: delegator.reward.address) == Zero::zero(),
); // TODO: Change this after implement calculate_rewards.
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: delegated_amount);
system.advance_time(time: system.staking.get_exit_wait_window());
system.delegator_exit_action(:delegator, :pool);
system.delegator_claim_rewards(:delegator, :pool);
// Clean up and make all parties exit.
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.staker_exit_action(:staker);
// ------------- Flow complete, now asserts -------------
// Assert pool balance is high.
assert!(system.token.balance_of(account: pool) > 100);
// Assert all staked amounts were transferred back.
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(system.token.balance_of(account: staker.staker.address) == stake_amount);
assert!(system.token.balance_of(account: delegator.delegator.address) == delegated_amount);
// Asserts reward addresses are not empty.
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
// Assert all funds that moved from rewards supplier, were moved to correct addresses.
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);
assert!(
initial_reward_supplier_balance == system
.token
.balance_of(account: system.reward_supplier.address)
+ system.token.balance_of(account: staker.reward.address)
+ system.token.balance_of(account: delegator.reward.address)
+ system.token.balance_of(account: pool),
);
}
}
/// Flow:
/// Staker Stake
/// Delegator delegate
/// Delegator exit_intent
/// Staker exit_intent
/// Staker exit_action
/// Delegator exit_action
#[derive(Drop, Copy)]
pub(crate) struct StakerIntentLastActionFirstFlow {}
pub(crate) impl StakerIntentLastActionFirstFlowImpl of FlowTrait<StakerIntentLastActionFirstFlow> {
fn test(self: StakerIntentLastActionFirstFlow, ref system: SystemState) {
let min_stake = system.staking.get_min_stake();
let initial_stake_amount = min_stake * 2;
let staker = system.new_staker(amount: initial_stake_amount * 2);
let initial_reward_supplier_balance = system
.token
.balance_of(account: system.reward_supplier.address);
let commission = 200;
system.stake(:staker, amount: initial_stake_amount, pool_enabled: true, :commission);
system.advance_k_epochs_and_attest(:staker);
let pool = system.staking.get_pool(:staker);
let delegator = system.new_delegator(amount: initial_stake_amount);
system.delegate(:delegator, :pool, amount: initial_stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
system.delegator_exit_intent(:delegator, :pool, amount: initial_stake_amount / 2);
system.advance_k_epochs_and_attest(:staker);
system.staker_exit_intent(:staker);
system.advance_time(time: system.staking.get_exit_wait_window());
system.staker_exit_action(:staker);
system.delegator_exit_action(:delegator, :pool);
system.delegator_claim_rewards(:delegator, :pool);
assert!(system.token.balance_of(account: system.staking.address).is_zero());
assert!(system.token.balance_of(account: pool) < 100);
assert!(
system.token.balance_of(account: staker.staker.address) == initial_stake_amount * 2,
);
assert!(
system.token.balance_of(account: delegator.delegator.address) == initial_stake_amount,
);
assert!(system.token.balance_of(account: staker.reward.address).is_non_zero());
assert!(system.token.balance_of(account: delegator.reward.address).is_non_zero());
assert!(wide_abs_diff(system.reward_supplier.get_unclaimed_rewards(), STRK_IN_FRIS) < 100);