-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtests.move
1875 lines (1680 loc) · 69.4 KB
/
tests.move
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
#[test_only]
// This test module uses the same design schema as the emojicoin dot fun core test module.
module emojicoin_arena::tests {
use aptos_framework::account::{
create_resource_address,
create_signer_for_test as get_signer
};
use aptos_framework::aptos_coin::AptosCoin;
use aptos_framework::coin;
use aptos_framework::event::{emitted_events};
use aptos_framework::randomness;
use aptos_framework::timestamp;
use aptos_framework::transaction_context;
use black_cat_market::coin_factory::{Emojicoin as BlackCat, EmojicoinLP as BlackCatLP};
use emojicoin_dot_fun::emojicoin_dot_fun::{
Self,
MarketMetadata,
get_BASE_VIRTUAL_CEILING,
get_EMOJICOIN_REMAINDER,
get_QUOTE_REAL_CEILING,
get_QUOTE_VIRTUAL_FLOOR,
market_metadata_by_market_id,
simulate_swap,
unpack_market_metadata
};
use emojicoin_arena::emojicoin_arena::{
Enter,
EscrowView,
ExchangeRate,
Exit,
Melee,
RegistryView,
Swap,
VaultBalanceUpdate,
enter_for_test as enter,
escrow,
escrow_exists,
exchange_rate,
exit_for_test as exit,
fund_vault,
get_DEFAULT_AVAILABLE_REWARDS,
get_DEFAULT_DURATION,
get_DEFAULT_MAX_MATCH_PERCENTAGE,
get_DEFAULT_MAX_MATCH_AMOUNT,
get_INTEGRATOR_FEE_RATE_BPS_DOUBLE_ROUTE,
get_INTEGRATOR_FEE_RATE_BPS_SINGLE_ROUTE,
get_REGISTRY_SEED,
init_module_test_only,
match_amount,
melee,
melee_ids_by_market_ids_contains_for_test as melee_ids_by_market_ids_contains,
random_market_id_for_test as random_market_id,
registry,
set_melee_available_rewards,
set_melee_max_match_amount,
set_next_melee_available_rewards,
set_next_melee_duration,
set_next_melee_max_match_amount,
set_next_melee_max_match_percentage,
swap_for_test as swap,
unpack_enter,
unpack_escrow_view,
unpack_exchange_rate,
unpack_exit,
unpack_melee,
unpack_registry_view,
unpack_swap,
unpack_vault_balance_update,
withdraw_from_vault
};
use emojicoin_dot_fun::test_acquisitions::mint_aptos_coin_to;
use emojicoin_dot_fun::tests as emojicoin_dot_fun_tests;
use std::option;
use std::vector;
use zebra_market::coin_factory::{Emojicoin as Zebra, EmojicoinLP as ZebraLP};
struct MockEnter has copy, drop, store {
user: address,
melee_id: u64,
input_amount: u64,
quote_volume: u64,
integrator_fee: u64,
match_amount: u64,
emojicoin_0_proceeds: u64,
emojicoin_1_proceeds: u64,
emojicoin_0_exchange_rate: ExchangeRate,
emojicoin_1_exchange_rate: ExchangeRate
}
struct MockEscrowView has copy, drop, store {
melee_id: u64,
emojicoin_0_balance: u64,
emojicoin_1_balance: u64,
match_amount: u64
}
struct MockExchangeRate has copy, drop, store {
base: u64,
quote: u64
}
struct MockExit has copy, drop, store {
user: address,
melee_id: u64,
tap_out_fee: u64,
emojicoin_0_proceeds: u64,
emojicoin_1_proceeds: u64,
emojicoin_0_exchange_rate: ExchangeRate,
emojicoin_1_exchange_rate: ExchangeRate
}
struct MockMarketMetadata has copy, drop, store {
market_id: u64,
market_address: address,
emoji_bytes: vector<u8>
}
struct MockMelee has copy, drop, store {
melee_id: u64,
emojicoin_0_market_address: address,
emojicoin_1_market_address: address,
start_time: u64,
duration: u64,
max_match_percentage: u64,
max_match_amount: u64,
available_rewards: u64
}
struct MockRegistryView has copy, drop, store {
n_melees: u64,
vault_address: address,
vault_balance: u64,
next_melee_duration: u64,
next_melee_available_rewards: u64,
next_melee_max_match_percentage: u64,
next_melee_max_match_amount: u64
}
struct MockSwap has copy, drop, store {
user: address,
melee_id: u64,
quote_volume: u64,
integrator_fee: u64,
emojicoin_0_proceeds: u64,
emojicoin_1_proceeds: u64,
emojicoin_0_exchange_rate: ExchangeRate,
emojicoin_1_exchange_rate: ExchangeRate
}
struct MockVaultBalanceUpdate has copy, drop, store {
new_balance: u64
}
struct SimulatedSwapInputs has copy, drop, store {
market_address: address,
input_amount: u64,
selling_emojicoins: bool,
integrator_fee_rate: u8
}
struct SimulatedSwapStats has copy, drop, store {
quote_volume: u64,
integrator_fee: u64,
net_proceeds: u64
}
// Test market emoji bytes, in order of market ID.
const BLACK_CAT: vector<u8> = x"f09f9088e2808de2ac9b";
const BLACK_HEART: vector<u8> = x"f09f96a4";
const YELLOW_HEART: vector<u8> = x"f09f929b";
const YIN_YANG: vector<u8> = x"e298afefb88f";
const ZEBRA: vector<u8> = x"f09fa693";
const ZOMBIE: vector<u8> = x"f09fa79f";
const PARTICIPANT: address = @0x1234567890abcdef;
const MAX_PERCENTAGE: u64 = 100;
public fun assert_escrow_view(
self: MockEscrowView, actual: EscrowView
) {
let (melee_id, emojicoin_0_balance, emojicoin_1_balance, match_amount) =
unpack_escrow_view(actual);
assert!(self.melee_id == melee_id);
assert!(self.emojicoin_0_balance == emojicoin_0_balance);
assert!(self.emojicoin_1_balance == emojicoin_1_balance);
assert!(self.match_amount == match_amount);
}
public fun assert_crank_global_state_and_events() {
// Assert state.
let registry_view = base_registry_view();
registry_view.n_melees = 2;
registry_view.assert_registry_view(registry());
let melee_view_1 = base_melee();
melee_view_1.assert_melee(melee(melee_view_1.melee_id));
let melee_view_2 = melee_view_1;
melee_view_2.melee_id = 2;
melee_view_2.emojicoin_0_market_address = @black_heart_market;
melee_view_2.emojicoin_1_market_address = @yellow_heart_market;
melee_view_2.start_time = base_start_time() + get_DEFAULT_DURATION();
melee_view_2.assert_melee(melee(melee_view_2.melee_id));
// Assert emitted melee events.
let melee_events = emitted_events<Melee>();
assert!(melee_events.length() == 2);
melee_view_1.assert_melee(melee_events[0]);
melee_view_2.assert_melee(melee_events[1]);
}
public fun assert_exchange_rate(
self: MockExchangeRate, actual: ExchangeRate
) {
let (base, quote) = unpack_exchange_rate(actual);
assert!(self.base == base);
assert!(self.quote == quote);
}
public fun assert_enter(self: MockEnter, actual: Enter) {
let (
user,
melee_id,
input_amount,
quote_volume,
integrator_fee,
match_amount,
emojicoin_0_proceeds,
emojicoin_1_proceeds,
emojicoin_0_exchange_rate,
emojicoin_1_exchange_rate
) = unpack_enter(actual);
assert!(self.user == user);
assert!(self.melee_id == melee_id);
assert!(self.input_amount == input_amount);
assert!(self.quote_volume == quote_volume);
assert!(self.integrator_fee == integrator_fee);
assert!(self.match_amount == match_amount);
assert!(self.emojicoin_0_proceeds == emojicoin_0_proceeds);
assert!(self.emojicoin_1_proceeds == emojicoin_1_proceeds);
assert!(self.emojicoin_0_exchange_rate == emojicoin_0_exchange_rate);
assert!(self.emojicoin_1_exchange_rate == emojicoin_1_exchange_rate);
}
public fun assert_exit(self: MockExit, actual: Exit) {
let (
user,
melee_id,
tap_out_fee,
emojicoin_0_proceeds,
emojicoin_1_proceeds,
emojicoin_0_exchange_rate,
emojicoin_1_exchange_rate
) = unpack_exit(actual);
assert!(self.user == user);
assert!(self.melee_id == melee_id);
assert!(self.tap_out_fee == tap_out_fee);
assert!(self.emojicoin_0_proceeds == emojicoin_0_proceeds);
assert!(self.emojicoin_1_proceeds == emojicoin_1_proceeds);
assert!(self.emojicoin_0_exchange_rate == emojicoin_0_exchange_rate);
assert!(self.emojicoin_1_exchange_rate == emojicoin_1_exchange_rate);
}
public fun assert_market_metadata(
self: MockMarketMetadata, actual: MarketMetadata
) {
let (market_id, market_address, emoji_bytes) = unpack_market_metadata(actual);
assert!(self.market_id == market_id);
assert!(self.market_address == market_address);
assert!(self.emoji_bytes == emoji_bytes);
}
public fun assert_melee(self: MockMelee, actual: Melee) {
let (
melee_id,
emojicoin_0_market_address,
emojicoin_1_market_address,
start_time,
duration,
max_match_percentage,
max_match_amount,
available_rewards
) = unpack_melee(actual);
assert!(self.melee_id == melee_id);
assert!(self.emojicoin_0_market_address == emojicoin_0_market_address);
assert!(self.emojicoin_1_market_address == emojicoin_1_market_address);
assert!(self.start_time == start_time);
assert!(self.duration == duration);
assert!(self.max_match_percentage == max_match_percentage);
assert!(self.max_match_amount == max_match_amount);
assert!(self.available_rewards == available_rewards);
}
public fun assert_registry_view(
self: MockRegistryView, actual: RegistryView
) {
let (
n_melees,
vault_address,
vault_balance,
next_melee_duration,
next_melee_available_rewards,
next_melee_max_match_percentage,
next_melee_max_match_amount
) = unpack_registry_view(actual);
assert!(self.n_melees == n_melees);
assert!(self.vault_address == vault_address);
assert!(self.vault_balance == vault_balance);
assert!(self.next_melee_duration == next_melee_duration);
assert!(self.next_melee_available_rewards == next_melee_available_rewards);
assert!(self.next_melee_max_match_percentage == next_melee_max_match_percentage);
assert!(self.next_melee_max_match_amount == next_melee_max_match_amount);
}
public fun assert_swap(self: MockSwap, actual: Swap) {
let (
user,
melee_id,
quote_volume,
integrator_fee,
emojicoin_0_proceeds,
emojicoin_1_proceeds,
emojicoin_0_exchange_rate,
emojicoin_1_exchange_rate
) = unpack_swap(actual);
assert!(self.user == user);
assert!(self.melee_id == melee_id);
assert!(self.quote_volume == quote_volume);
assert!(self.integrator_fee == integrator_fee);
assert!(self.emojicoin_0_proceeds == emojicoin_0_proceeds);
assert!(self.emojicoin_1_proceeds == emojicoin_1_proceeds);
assert!(self.emojicoin_0_exchange_rate == emojicoin_0_exchange_rate);
assert!(self.emojicoin_1_exchange_rate == emojicoin_1_exchange_rate);
}
public fun assert_vault_balance_update(
self: MockVaultBalanceUpdate, actual: VaultBalanceUpdate
) {
let new_balance = unpack_vault_balance_update(actual);
assert!(self.new_balance == new_balance);
}
public fun base_enter_amount(): u64 {
get_QUOTE_REAL_CEILING() / 2
}
public fun base_enter_amount_post_bonding_curve(): u64 {
get_QUOTE_REAL_CEILING() * 2
}
public fun base_escrow_view(): MockEscrowView {
MockEscrowView {
melee_id: 1,
emojicoin_0_balance: 0,
emojicoin_1_balance: 0,
match_amount: 0
}
}
public fun base_melee(): MockMelee {
MockMelee {
melee_id: 1,
emojicoin_0_market_address: @black_cat_market,
emojicoin_1_market_address: @zebra_market,
start_time: base_start_time(),
duration: get_DEFAULT_DURATION(),
max_match_percentage: get_DEFAULT_MAX_MATCH_PERCENTAGE(),
max_match_amount: get_DEFAULT_MAX_MATCH_AMOUNT(),
available_rewards: get_DEFAULT_AVAILABLE_REWARDS()
}
}
/// 1.5x the default melee duration.
public fun base_publish_time(): u64 {
get_DEFAULT_DURATION() + get_DEFAULT_DURATION() / 2
}
public fun base_registry_view(): MockRegistryView {
MockRegistryView {
n_melees: 1,
vault_address: base_vault_address(),
vault_balance: get_DEFAULT_AVAILABLE_REWARDS(),
next_melee_duration: get_DEFAULT_DURATION(),
next_melee_available_rewards: get_DEFAULT_AVAILABLE_REWARDS(),
next_melee_max_match_percentage: get_DEFAULT_MAX_MATCH_PERCENTAGE(),
next_melee_max_match_amount: get_DEFAULT_MAX_MATCH_AMOUNT()
}
}
public fun base_start_time(): u64 {
get_DEFAULT_DURATION()
}
public fun base_vault_address(): address {
create_resource_address(&@emojicoin_arena, get_REGISTRY_SEED())
}
/// Fund `PARTICIPANT` with initial APT and emojicoin supply so balance checks do not abort
/// during swap simulation, based on `arena_octas_enter_amount` to be initially entered: by
/// buying `arena_octas_enter_amount` worth of initial supply for each emojicoin, the user's
/// balance will always suffice for the swap simulation.
public fun fund_participant<Coin0, LP0, Coin1, LP1>(
arena_octas_enter_amount: u64, market_address_0: address, market_address_1: address
): (u64, u64) {
mint_aptos_coin_to(PARTICIPANT, arena_octas_enter_amount * 3);
emojicoin_dot_fun::swap<Coin0, LP0>(
&get_signer(PARTICIPANT),
market_address_0,
arena_octas_enter_amount,
false,
@integrator,
0,
1
);
emojicoin_dot_fun::swap<Coin1, LP1>(
&get_signer(PARTICIPANT),
market_address_1,
arena_octas_enter_amount,
false,
@integrator,
0,
1
);
(coin::balance<Coin0>(PARTICIPANT), coin::balance<Coin1>(PARTICIPANT))
}
/// Initialize emojicoin dot fun with test markets.
public fun init_emojicoin_dot_fun_with_test_markets() {
emojicoin_dot_fun_tests::init_package();
vector::for_each_ref(
&vector[BLACK_CAT, BLACK_HEART, YELLOW_HEART, YIN_YANG, ZEBRA, ZOMBIE],
|bytes_ref| {
emojicoin_dot_fun_tests::init_market(vector[*bytes_ref]);
}
);
}
public fun init_module_with_funded_vault() {
init_emojicoin_dot_fun_with_test_markets();
// Set global time to base publish time.
timestamp::update_global_time_for_test(base_publish_time());
// Initialize module.
init_module_test_only(&get_signer(@emojicoin_arena));
// Fund admin, then fund vault.
mint_aptos_coin_to(@emojicoin_arena, get_DEFAULT_AVAILABLE_REWARDS());
fund_vault(&get_signer(@emojicoin_arena), get_DEFAULT_AVAILABLE_REWARDS());
}
public fun init_module_with_funded_vault_and_participant(): (u64, u64) {
init_module_with_funded_vault();
fund_participant<BlackCat, BlackCatLP, Zebra, ZebraLP>(
base_enter_amount(),
@black_cat_market,
@zebra_market
)
}
public fun init_module_with_funded_vault_and_participant_post_bonding_curve(): (u64, u64) {
init_module_with_funded_vault();
fund_participant<BlackCat, BlackCatLP, Zebra, ZebraLP>(
base_enter_amount_post_bonding_curve(),
@black_cat_market,
@zebra_market
)
}
#[lint::allow_unsafe_randomness]
/// Set the randomness seed such that a single crank will hit all coverage branches in
/// underlying crank logic. This seed was derived using a scripted brute force approach to
/// increment the value, then re-run tests until `randomness_seed_for_crank_coverage` passed.
public fun set_randomness_seed_for_crank_coverage() {
randomness::initialize_for_testing(&get_signer(@aptos_framework));
randomness::set_seed(
x"0000000000000000000000000000000000000000000000000000000000000060"
);
}
public fun simulated_swap_stats<Emojicoin, EmojicoinLP>(
simulated_swap_inputs: SimulatedSwapInputs
): SimulatedSwapStats {
let (
_,
_,
_,
_,
_,
_,
_,
_,
net_proceeds,
_,
quote_volume,
_,
integrator_fee,
_,
_,
_,
_,
_
) =
emojicoin_dot_fun::unpack_swap(
simulate_swap<Emojicoin, EmojicoinLP>(
PARTICIPANT,
simulated_swap_inputs.market_address,
simulated_swap_inputs.input_amount,
simulated_swap_inputs.selling_emojicoins,
@integrator,
simulated_swap_inputs.integrator_fee_rate
)
);
SimulatedSwapStats { quote_volume, integrator_fee, net_proceeds }
}
public fun simulate_enter_post_bonding_curve():
(
MockEscrowView, MockEnter, SimulatedSwapStats
) {
// Simulate swap into escrow.
let swap_inputs_enter = SimulatedSwapInputs {
market_address: @black_cat_market,
input_amount: base_enter_amount_post_bonding_curve(),
selling_emojicoins: false,
integrator_fee_rate: get_INTEGRATOR_FEE_RATE_BPS_SINGLE_ROUTE()
};
let swap_stats_enter =
simulated_swap_stats<BlackCat, BlackCatLP>(swap_inputs_enter);
// Get escrow view.
let escrow_view = base_escrow_view();
escrow_view.emojicoin_0_balance = swap_stats_enter.net_proceeds;
// Construct enter event.
let enter_event = MockEnter {
user: PARTICIPANT,
melee_id: 1,
input_amount: base_enter_amount_post_bonding_curve(),
quote_volume: swap_stats_enter.quote_volume,
integrator_fee: swap_stats_enter.integrator_fee,
match_amount: 0,
emojicoin_0_proceeds: swap_stats_enter.net_proceeds,
emojicoin_1_proceeds: 0,
// Must be updated after actual enter.
emojicoin_0_exchange_rate: exchange_rate<BlackCat, BlackCatLP>(
@black_cat_market
),
// Must be updated after actual enter.
emojicoin_1_exchange_rate: exchange_rate<Zebra, ZebraLP>(@zebra_market)
};
(escrow_view, enter_event, swap_stats_enter)
}
#[test]
public fun admin_functions() {
// Initialize emojicoin dot fun.
init_emojicoin_dot_fun_with_test_markets();
// Set global time to base publish time.
timestamp::update_global_time_for_test(base_publish_time());
// Initialize module.
init_module_test_only(&get_signer(@emojicoin_arena));
// Fund admin, then fund vault.
mint_aptos_coin_to(@emojicoin_arena, get_DEFAULT_AVAILABLE_REWARDS());
fund_vault(&get_signer(@emojicoin_arena), get_DEFAULT_AVAILABLE_REWARDS());
// Assert vault balance update event.
let vault_balance_update_events = emitted_events<VaultBalanceUpdate>();
assert!(vault_balance_update_events.length() == 1);
MockVaultBalanceUpdate { new_balance: get_DEFAULT_AVAILABLE_REWARDS() }.assert_vault_balance_update(
vault_balance_update_events[0]
);
// Withdraw from vault.
let withdrawn_octas = 1;
withdraw_from_vault(&get_signer(@emojicoin_arena), withdrawn_octas);
// Assert vault balance update event.
let vault_balance_update_events = emitted_events<VaultBalanceUpdate>();
assert!(vault_balance_update_events.length() == 2);
MockVaultBalanceUpdate {
new_balance: get_DEFAULT_AVAILABLE_REWARDS() - withdrawn_octas
}.assert_vault_balance_update(vault_balance_update_events[1]);
// Set next melee parameters.
let (
next_available_rewards,
next_duration,
next_max_match_amount,
next_max_match_percentage
) = (2, 3, 4, 5);
set_next_melee_available_rewards(
&get_signer(@emojicoin_arena), next_available_rewards
);
set_next_melee_duration(&get_signer(@emojicoin_arena), next_duration);
set_next_melee_max_match_amount(
&get_signer(@emojicoin_arena), next_max_match_amount
);
set_next_melee_max_match_percentage(
&get_signer(@emojicoin_arena), next_max_match_percentage
);
// Verify registry view.
let registry_view = base_registry_view();
registry_view.next_melee_available_rewards = next_available_rewards;
registry_view.next_melee_duration = next_duration;
registry_view.next_melee_max_match_amount = next_max_match_amount;
registry_view.next_melee_max_match_percentage = next_max_match_percentage;
registry_view.vault_balance -= withdrawn_octas;
registry_view.assert_registry_view(registry());
}
#[test]
#[lint::allow_unsafe_randomness]
public fun crank_by_enter() {
let (emojicoin_0_balance, emojicoin_1_balance) =
init_module_with_funded_vault_and_participant_post_bonding_curve();
set_randomness_seed_for_crank_coverage();
// Set time to middle of a new melee.
let time = base_publish_time();
time += get_DEFAULT_DURATION();
timestamp::update_global_time_for_test(time + 1);
// Verify that nothing can be matched since melee duration has elapsed.
assert!(
match_amount<BlackCat, BlackCatLP, Zebra, ZebraLP>(
PARTICIPANT, base_enter_amount_post_bonding_curve(), 1
) == 0
);
// Crank via enter API.
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, BlackCat>(
&get_signer(PARTICIPANT),
base_enter_amount_post_bonding_curve(),
false
);
// Verify that nothing can be matched since melee is inactive.
assert!(
match_amount<BlackCat, BlackCatLP, Zebra, ZebraLP>(
PARTICIPANT, base_enter_amount_post_bonding_curve(), 1
) == 0
);
// Assert user state.
assert!(
!escrow_exists<BlackCat, BlackCatLP, Zebra, ZebraLP>(PARTICIPANT)
);
assert!(coin::balance<BlackCat>(PARTICIPANT) == emojicoin_0_balance);
assert!(coin::balance<Zebra>(PARTICIPANT) == emojicoin_1_balance);
assert!(
coin::balance<AptosCoin>(PARTICIPANT)
== base_enter_amount_post_bonding_curve()
);
// Assert global state and crank events.
assert_crank_global_state_and_events();
}
#[test]
#[lint::allow_unsafe_randomness]
public fun crank_by_exit() {
let (emojicoin_0_balance, emojicoin_1_balance) =
init_module_with_funded_vault_and_participant_post_bonding_curve();
set_randomness_seed_for_crank_coverage();
// Get escrow view and enter event post-enter.
let (escrow_view, enter_event, swap_stats_enter) =
simulate_enter_post_bonding_curve();
// Enter.
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, BlackCat>(
&get_signer(PARTICIPANT),
base_enter_amount_post_bonding_curve(),
false
);
// Update exchange rates in enter event to be real-time.
enter_event.emojicoin_0_exchange_rate = exchange_rate<BlackCat, BlackCatLP>(
@black_cat_market
);
enter_event.emojicoin_1_exchange_rate = exchange_rate<Zebra, ZebraLP>(
@zebra_market
);
// Assert user state.
escrow_view.assert_escrow_view(
escrow<BlackCat, BlackCatLP, Zebra, ZebraLP>(PARTICIPANT)
);
assert!(coin::balance<AptosCoin>(PARTICIPANT) == 0);
assert!(coin::balance<BlackCat>(PARTICIPANT) == emojicoin_0_balance);
assert!(coin::balance<Zebra>(PARTICIPANT) == emojicoin_1_balance);
// Assert enter event.
let enter_events = emitted_events<Enter>();
assert!(enter_events.length() == 1);
enter_event.assert_enter(enter_events[0]);
// Set time to middle of a new melee.
let time = base_publish_time();
time += get_DEFAULT_DURATION();
timestamp::update_global_time_for_test(time + 1);
// Crank via exit API.
exit<BlackCat, BlackCatLP, Zebra, ZebraLP>(&get_signer(PARTICIPANT));
// Assert user state.
escrow_view.emojicoin_0_balance = 0;
escrow_view.assert_escrow_view(
escrow<BlackCat, BlackCatLP, Zebra, ZebraLP>(PARTICIPANT)
);
assert!(coin::balance<AptosCoin>(PARTICIPANT) == 0);
assert!(
coin::balance<BlackCat>(PARTICIPANT)
== emojicoin_0_balance + swap_stats_enter.net_proceeds
);
assert!(coin::balance<Zebra>(PARTICIPANT) == emojicoin_1_balance);
// Assert emitted exit event.
let exit_events = emitted_events<Exit>();
assert!(exit_events.length() == 1);
let exit_event = MockExit {
user: PARTICIPANT,
melee_id: 1,
tap_out_fee: 0,
emojicoin_0_proceeds: swap_stats_enter.net_proceeds,
emojicoin_1_proceeds: 0,
emojicoin_0_exchange_rate: exchange_rate<BlackCat, BlackCatLP>(
@black_cat_market
),
emojicoin_1_exchange_rate: exchange_rate<Zebra, ZebraLP>(@zebra_market)
};
exit_event.assert_exit(exit_events[0]);
assert_crank_global_state_and_events();
}
#[test]
#[lint::allow_unsafe_randomness]
public fun crank_by_swap() {
let (emojicoin_0_balance, emojicoin_1_balance) =
init_module_with_funded_vault_and_participant_post_bonding_curve();
set_randomness_seed_for_crank_coverage();
// Get escrow view and enter event post-enter.
let (escrow_view, enter_event, swap_stats_enter) =
simulate_enter_post_bonding_curve();
// Enter.
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, BlackCat>(
&get_signer(PARTICIPANT),
base_enter_amount_post_bonding_curve(),
false
);
// Update exchange rates in enter event to be real-time.
enter_event.emojicoin_0_exchange_rate = exchange_rate<BlackCat, BlackCatLP>(
@black_cat_market
);
enter_event.emojicoin_1_exchange_rate = exchange_rate<Zebra, ZebraLP>(
@zebra_market
);
// Assert user state.
escrow_view.assert_escrow_view(
escrow<BlackCat, BlackCatLP, Zebra, ZebraLP>(PARTICIPANT)
);
assert!(coin::balance<AptosCoin>(PARTICIPANT) == 0);
assert!(coin::balance<BlackCat>(PARTICIPANT) == emojicoin_0_balance);
assert!(coin::balance<Zebra>(PARTICIPANT) == emojicoin_1_balance);
// Assert enter event.
let enter_events = emitted_events<Enter>();
assert!(enter_events.length() == 1);
enter_event.assert_enter(enter_events[0]);
// Set time to middle of a new melee.
let time = base_publish_time();
time += get_DEFAULT_DURATION();
timestamp::update_global_time_for_test(time + 1);
// Simulate swap within escrow.
let swap_inputs_swap_route_0 = SimulatedSwapInputs {
market_address: @black_cat_market,
input_amount: swap_stats_enter.net_proceeds,
selling_emojicoins: true,
integrator_fee_rate: get_INTEGRATOR_FEE_RATE_BPS_DOUBLE_ROUTE()
};
let swap_stats_swap_route_0 =
simulated_swap_stats<BlackCat, BlackCatLP>(swap_inputs_swap_route_0);
let swap_inputs_swap_route_1 = SimulatedSwapInputs {
market_address: @zebra_market,
input_amount: swap_stats_swap_route_0.net_proceeds,
selling_emojicoins: false,
integrator_fee_rate: get_INTEGRATOR_FEE_RATE_BPS_DOUBLE_ROUTE()
};
let swap_stats_swap_route_1 =
simulated_swap_stats<Zebra, ZebraLP>(swap_inputs_swap_route_1);
// Crank via swap API.
swap<BlackCat, BlackCatLP, Zebra, ZebraLP>(&get_signer(PARTICIPANT));
// Assert user state.
escrow_view.emojicoin_0_balance = 0;
escrow_view.assert_escrow_view(
escrow<BlackCat, BlackCatLP, Zebra, ZebraLP>(PARTICIPANT)
);
assert!(coin::balance<AptosCoin>(PARTICIPANT) == 0);
assert!(coin::balance<BlackCat>(PARTICIPANT) == emojicoin_0_balance);
assert!(
coin::balance<Zebra>(PARTICIPANT)
== emojicoin_1_balance + swap_stats_swap_route_1.net_proceeds
);
// Assert emitted swap event.
let swap_events = emitted_events<Swap>();
assert!(swap_events.length() == 1);
let swap_event = MockSwap {
user: PARTICIPANT,
melee_id: 1,
quote_volume: swap_stats_swap_route_1.quote_volume,
integrator_fee: swap_stats_swap_route_0.integrator_fee
+ swap_stats_swap_route_1.integrator_fee,
emojicoin_0_proceeds: 0,
emojicoin_1_proceeds: swap_stats_swap_route_1.net_proceeds,
emojicoin_0_exchange_rate: exchange_rate<BlackCat, BlackCatLP>(
@black_cat_market
),
emojicoin_1_exchange_rate: exchange_rate<Zebra, ZebraLP>(@zebra_market)
};
swap_event.assert_swap(swap_events[0]);
// Assert emitted exit event.
let exit_events = emitted_events<Exit>();
assert!(exit_events.length() == 1);
let exit_event = MockExit {
user: PARTICIPANT,
melee_id: 1,
tap_out_fee: 0,
emojicoin_0_proceeds: 0,
emojicoin_1_proceeds: swap_stats_swap_route_1.net_proceeds,
emojicoin_0_exchange_rate: exchange_rate<BlackCat, BlackCatLP>(
@black_cat_market
),
emojicoin_1_exchange_rate: exchange_rate<Zebra, ZebraLP>(@zebra_market)
};
exit_event.assert_exit(exit_events[0]);
assert_crank_global_state_and_events();
}
#[test]
#[lint::allow_unsafe_randomness]
#[
expected_failure(
abort_code = emojicoin_arena::emojicoin_arena::E_ENTER_COIN_BALANCE_0,
location = emojicoin_arena::emojicoin_arena
)
]
public fun enter_invalid_escrow_coin_balance_0() {
init_module_with_funded_vault_and_participant();
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, BlackCat>(
&get_signer(PARTICIPANT),
base_enter_amount(),
true
);
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, Zebra>(
&get_signer(PARTICIPANT),
base_enter_amount(),
true
);
}
#[test]
#[lint::allow_unsafe_randomness]
#[
expected_failure(
abort_code = emojicoin_arena::emojicoin_arena::E_ENTER_COIN_BALANCE_1,
location = emojicoin_arena::emojicoin_arena
)
]
public fun enter_invalid_escrow_coin_balance_1() {
init_module_with_funded_vault_and_participant();
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, Zebra>(
&get_signer(PARTICIPANT),
base_enter_amount(),
true
);
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, BlackCat>(
&get_signer(PARTICIPANT),
base_enter_amount(),
true
);
}
#[test]
#[lint::allow_unsafe_randomness]
#[
expected_failure(
abort_code = emojicoin_arena::emojicoin_arena::E_INVALID_ESCROW_COIN_TYPE,
location = emojicoin_arena::emojicoin_arena
)
]
public fun enter_invalid_escrow_coin_type() {
init_module_with_funded_vault_and_participant();
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, BlackCatLP>(
&get_signer(PARTICIPANT),
base_enter_amount(),
true
);
}
#[test]
#[lint::allow_unsafe_randomness]
public fun enter_lock_in_top_off_tap_out() {
init_module_with_funded_vault_and_participant();
// Assert emitted vault balance event.
let vault_balance_update_events = emitted_events<VaultBalanceUpdate>();
assert!(vault_balance_update_events.length() == 1);
let vault_balance_update_event = MockVaultBalanceUpdate {
new_balance: get_DEFAULT_AVAILABLE_REWARDS()
};
vault_balance_update_event.assert_vault_balance_update(
vault_balance_update_events[0]
);
// Assign input amount for first entry to first melee, determine user remaining octas.
let melee_id = 1;
let input_amount = 10000;
// Determine raw match amount without any other corrections.
let max_match_percentage = get_DEFAULT_MAX_MATCH_PERCENTAGE();
// Correct for max match percentage.
let raw_match_amount = input_amount * max_match_percentage / 100;
// Correct for time elapsed since start of melee.
raw_match_amount =
raw_match_amount * (base_publish_time() - get_DEFAULT_DURATION())
/ get_DEFAULT_DURATION();
// Assert raw match amount.
assert!(
raw_match_amount
== match_amount<BlackCat, BlackCatLP, Zebra, ZebraLP>(
PARTICIPANT, input_amount, melee_id
)
);
// Simulate swap into escrow.
let swap_inputs_enter = SimulatedSwapInputs {
market_address: @black_cat_market,
input_amount: input_amount + raw_match_amount,
selling_emojicoins: false,
integrator_fee_rate: get_INTEGRATOR_FEE_RATE_BPS_SINGLE_ROUTE()
};
let swap_stats_enter =
simulated_swap_stats<BlackCat, BlackCatLP>(swap_inputs_enter);
// Enter melee with locking in.
let lock_in = true;
enter<BlackCat, BlackCatLP, Zebra, ZebraLP, BlackCat>(
&get_signer(PARTICIPANT),
input_amount,
lock_in
);
// Assert state.
let registry_view = base_registry_view();
registry_view.vault_balance -= raw_match_amount;
registry_view.assert_registry_view(registry());
let melee_view = base_melee();
melee_view.available_rewards -= raw_match_amount;
melee_view.assert_melee(melee(melee_view.melee_id));
let escrow_view = base_escrow_view();
escrow_view.match_amount += raw_match_amount;
escrow_view.emojicoin_0_balance = swap_stats_enter.net_proceeds;
escrow_view.assert_escrow_view(
escrow<BlackCat, BlackCatLP, Zebra, ZebraLP>(PARTICIPANT)
);
// Assert emitted enter event.
let enter_events = emitted_events<Enter>();
assert!(enter_events.length() == 1);
let enter_event = MockEnter {
user: PARTICIPANT,
melee_id: 1,
input_amount,
quote_volume: swap_stats_enter.quote_volume,
integrator_fee: swap_stats_enter.integrator_fee,
match_amount: raw_match_amount,
emojicoin_0_proceeds: swap_stats_enter.net_proceeds,
emojicoin_1_proceeds: 0,
emojicoin_0_exchange_rate: exchange_rate<BlackCat, BlackCatLP>(
@black_cat_market
),
emojicoin_1_exchange_rate: exchange_rate<Zebra, ZebraLP>(@zebra_market)