-
Notifications
You must be signed in to change notification settings - Fork 608
/
hooks_test.go
959 lines (869 loc) · 30.1 KB
/
hooks_test.go
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
package keeper_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/v23/app/apptesting"
"github.com/osmosis-labs/osmosis/v23/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v23/x/gamm/pool-models/stableswap"
poolmanagertypes "github.com/osmosis-labs/osmosis/v23/x/poolmanager/types"
"github.com/osmosis-labs/osmosis/v23/x/protorev/types"
)
// Tests the hook implementation that is called after swapping
func (s *KeeperTestSuite) TestSwapping() {
type param struct {
expectedTrades []types.Trade
executeSwap func()
}
tests := []struct {
name string
param param
expectPass bool
}{
{
name: "swap exact amount in",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
executeSwap: func() {
_, err := s.App.PoolManagerKeeper.SwapExactAmountIn(s.Ctx, s.TestAccs[0], 1, sdk.NewCoin("akash", osmomath.NewInt(100)), "Atom", osmomath.NewInt(1))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "swap route exact amount in",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
executeSwap: func() {
route := []poolmanagertypes.SwapAmountInRoute{{PoolId: 1, TokenOutDenom: "Atom"}}
_, err := s.App.PoolManagerKeeper.RouteExactAmountIn(s.Ctx, s.TestAccs[0], route, sdk.NewCoin("akash", osmomath.NewInt(100)), osmomath.NewInt(1))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "swap route exact amount out",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
executeSwap: func() {
route := []poolmanagertypes.SwapAmountOutRoute{{PoolId: 1, TokenInDenom: "akash"}}
_, err := s.App.PoolManagerKeeper.RouteExactAmountOut(s.Ctx, s.TestAccs[0], route, osmomath.NewInt(10000), sdk.NewCoin("Atom", osmomath.NewInt(100)))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "swap route exact amount in - 2 routes",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
{
Pool: 1,
TokenIn: "Atom",
TokenOut: "akash",
},
},
executeSwap: func() {
route := []poolmanagertypes.SwapAmountInRoute{{PoolId: 1, TokenOutDenom: "Atom"}, {PoolId: 1, TokenOutDenom: "akash"}}
_, err := s.App.PoolManagerKeeper.RouteExactAmountIn(s.Ctx, s.TestAccs[0], route, sdk.NewCoin("akash", osmomath.NewInt(100)), osmomath.NewInt(1))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "swap route exact amount in - Concentrated Liquidity",
param: param{
expectedTrades: []types.Trade{
{
Pool: 50,
TokenIn: "uosmo",
TokenOut: "epochTwo",
},
},
executeSwap: func() {
route := []poolmanagertypes.SwapAmountInRoute{{PoolId: 50, TokenOutDenom: "epochTwo"}}
_, err := s.App.PoolManagerKeeper.RouteExactAmountIn(s.Ctx, s.TestAccs[0], route, sdk.NewCoin("uosmo", osmomath.NewInt(10)), osmomath.NewInt(1))
s.Require().NoError(err)
},
},
expectPass: true,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
tc.param.executeSwap()
routes, err := s.App.ProtoRevKeeper.GetSwapsToBackrun(s.Ctx)
s.Require().NoError(err)
s.Require().Equal(tc.param.expectedTrades, routes.Trades)
})
}
}
// Tests the hook implementation that is called after liquidity providing
func (s *KeeperTestSuite) TestLiquidityChanging() {
type param struct {
expectedTrades []types.Trade
executeLiquidityProviding func()
}
tests := []struct {
name string
param param
expectPass bool
}{
{
name: "GAMM - Join Swap Exact Amount In",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
executeLiquidityProviding: func() {
_, err := s.App.GAMMKeeper.JoinSwapExactAmountIn(s.Ctx, s.TestAccs[0], 1, sdk.NewCoins(sdk.NewCoin("akash", osmomath.NewInt(100))), osmomath.NewInt(1))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "GAMM - Join Swap Share Amount Out",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
executeLiquidityProviding: func() {
_, err := s.App.GAMMKeeper.JoinSwapShareAmountOut(s.Ctx, s.TestAccs[0], 1, "akash", osmomath.NewInt(1000), osmomath.NewInt(10000))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "GAMM - Exit Swap Exact Amount Out",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
executeLiquidityProviding: func() {
_, err := s.App.GAMMKeeper.ExitSwapExactAmountOut(s.Ctx, s.TestAccs[0], 1, sdk.NewCoin("Atom", osmomath.NewInt(1)), osmomath.NewInt(1002141106353159235))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "GAMM - Exit Swap Share Amount In",
param: param{
expectedTrades: []types.Trade{
{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
executeLiquidityProviding: func() {
_, err := s.App.GAMMKeeper.ExitSwapShareAmountIn(s.Ctx, s.TestAccs[0], 1, "Atom", osmomath.NewInt(1000000000000000000), osmomath.NewInt(1))
s.Require().NoError(err)
},
},
expectPass: true,
},
{
name: "GAMM - Exit Swap Share Amount In - Low Shares",
param: param{
expectedTrades: []types.Trade(nil),
executeLiquidityProviding: func() {
_, err := s.App.GAMMKeeper.ExitSwapShareAmountIn(s.Ctx, s.TestAccs[0], 1, "Atom", osmomath.NewInt(1000), osmomath.NewInt(0))
s.Require().NoError(err)
},
},
expectPass: true,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
tc.param.executeLiquidityProviding()
routes, err := s.App.ProtoRevKeeper.GetSwapsToBackrun(s.Ctx)
if tc.expectPass {
s.Require().NoError(err)
s.Require().Equal(tc.param.expectedTrades, routes.Trades)
}
})
}
}
// Tests the hook implementation that is called after pool creation with coins
func (s *KeeperTestSuite) TestPoolCreation() {
type param struct {
matchDenom string
executePoolCreation func() uint64
}
tests := []struct {
name string
param param
expectPass bool
}{
{
name: "GAMM - Create Pool",
param: param{
matchDenom: "hookGamm",
executePoolCreation: func() uint64 {
poolId := s.createGAMMPool([]balancer.PoolAsset{
{
Token: sdk.NewCoin("hookGamm", osmomath.NewInt(1000000000)),
Weight: osmomath.NewInt(1),
},
{
Token: sdk.NewCoin(types.OsmosisDenomination, osmomath.NewInt(1000000000)),
Weight: osmomath.NewInt(1),
},
},
osmomath.NewDecWithPrec(2, 3),
osmomath.NewDecWithPrec(0, 2))
return poolId
},
},
expectPass: true,
},
{
name: "Concentrated Liquidity - Create Pool w/ No Liqudity",
param: param{
matchDenom: "hookCL",
executePoolCreation: func() uint64 {
clPool := s.PrepareConcentratedPool()
return clPool.GetId()
},
},
expectPass: false,
},
{
name: "Concentrated Liquidity - Create Pool w/ Liqudity",
param: param{
matchDenom: "hookCL",
executePoolCreation: func() uint64 {
clPool := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("hookCL", "uosmo")
return clPool.GetId()
},
},
expectPass: true,
},
{
name: "Create Balancer Pool First, Then Concentrated Liquidity w/ Liquidity - CL with more liquidity so should be stored",
param: param{
matchDenom: "hook",
executePoolCreation: func() uint64 {
// Create balancer pool first with a new denom pair
balancerPoolId := s.createGAMMPool([]balancer.PoolAsset{
{
Token: sdk.NewCoin("hook", osmomath.NewInt(1)),
Weight: osmomath.NewInt(1),
},
{
Token: sdk.NewCoin("uosmo", osmomath.NewInt(1)),
Weight: osmomath.NewInt(1),
},
},
osmomath.NewDecWithPrec(1, 1),
osmomath.NewDecWithPrec(0, 2),
)
// Ensure that the balancer pool is stored since no other pool exists for the denom pair
setPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, "uosmo", "hook")
s.Require().NoError(err)
s.Require().Equal(balancerPoolId, setPoolId)
// Create Concentrated Liquidity pool with the same denom pair and more liquidity
// The returned pool id should be what is finally stored in the protorev keeper
clPool := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("hook", "uosmo")
return clPool.GetId()
},
},
expectPass: true,
},
{
name: "Create Concentrated Liquidity Pool w/ Liquidity First, Then Balancer Pool - Balancer with more liquidity so should be stored",
param: param{
matchDenom: "hook",
executePoolCreation: func() uint64 {
// Create Concentrated Liquidity pool with a denom pair not already stored
clPool := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("hook", "uosmo")
// Ensure that the concentrated pool is stored since no other pool exists for the denom pair
setPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, "uosmo", "hook")
s.Require().NoError(err)
s.Require().Equal(clPool.GetId(), setPoolId)
// Get clPool liquidity
clPoolLiquidity, err := s.App.PoolManagerKeeper.GetTotalPoolLiquidity(s.Ctx, clPool.GetId())
s.Require().NoError(err)
// Create balancer pool with the same denom pair and more liquidity
balancerPoolId := s.createGAMMPool([]balancer.PoolAsset{
{
Token: sdk.NewCoin(clPoolLiquidity[0].Denom, clPoolLiquidity[0].Amount.Add(osmomath.NewInt(1))),
Weight: osmomath.NewInt(1),
},
{
Token: sdk.NewCoin(clPoolLiquidity[1].Denom, clPoolLiquidity[1].Amount.Add(osmomath.NewInt(1))),
Weight: osmomath.NewInt(1),
},
},
osmomath.NewDecWithPrec(1, 1),
osmomath.NewDecWithPrec(0, 2),
)
// The returned pool id should be what is finally stored in the protorev keeper since it has more liquidity
return balancerPoolId
},
},
expectPass: true,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
poolId := tc.param.executePoolCreation()
setPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, types.OsmosisDenomination, tc.param.matchDenom)
if tc.expectPass {
s.Require().NoError(err)
s.Require().Equal(poolId, setPoolId)
} else {
s.Require().Error(err)
s.Require().NotEqual(poolId, setPoolId)
}
})
}
}
// Helper function tests
// Tests StoreSwap stores a swap properly
func (s *KeeperTestSuite) TestStoreSwap() {
type param struct {
expectedSwap types.Trade
prepareState func()
expectedSwapsStoredLen int
}
tests := []struct {
name string
param param
expectPass bool
}{
{
name: "Store Swap Without An Existing Swap Stored",
param: param{
expectedSwap: types.Trade{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
prepareState: func() {
s.App.ProtoRevKeeper.DeleteSwapsToBackrun(s.Ctx)
},
expectedSwapsStoredLen: 1,
},
expectPass: true,
},
{
name: "Store Swap With With An Existing Swap Stored",
param: param{
expectedSwap: types.Trade{
Pool: 2,
TokenIn: "uosmo",
TokenOut: "test",
},
prepareState: func() {
s.App.ProtoRevKeeper.SetSwapsToBackrun(s.Ctx, types.Route{
Trades: []types.Trade{
{
Pool: 1,
TokenIn: "Atom",
TokenOut: "akash",
},
},
StepSize: osmomath.NewInt(1),
})
},
expectedSwapsStoredLen: 2,
},
expectPass: true,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
// Run any state preparation
tc.param.prepareState()
// Store the swap
s.App.ProtoRevKeeper.StoreSwap(s.Ctx, tc.param.expectedSwap.Pool, tc.param.expectedSwap.TokenIn, tc.param.expectedSwap.TokenOut)
routes, err := s.App.ProtoRevKeeper.GetSwapsToBackrun(s.Ctx)
if tc.expectPass {
s.Require().NoError(err)
s.Require().Equal(tc.param.expectedSwapsStoredLen, len(routes.Trades))
s.Require().Equal(tc.param.expectedSwap, routes.Trades[len(routes.Trades)-1])
}
})
}
}
// Tests GetComparablePoolLiquidity gets the comparable liquidity of a pool by multiplying the amounts of the pool coins.
func (s *KeeperTestSuite) TestGetComparablePoolLiquidity() {
type param struct {
executePoolCreation func() uint64
expectedComparableLiquidity osmomath.Int
}
tests := []struct {
name string
param param
expectPass bool
}{
{
name: "Get Balancer Pool Comparable Liquidity",
param: param{
executePoolCreation: func() uint64 {
return s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(10)), sdk.NewCoin("juno", osmomath.NewInt(10)))
},
expectedComparableLiquidity: osmomath.NewInt(100),
},
expectPass: true,
},
{
name: "Get Stable Swap Pool Comparable Liquidity",
param: param{
executePoolCreation: func() uint64 {
return s.createStableswapPool(
sdk.NewCoins(
sdk.NewCoin("uosmo", osmomath.NewInt(10)),
sdk.NewCoin("juno", osmomath.NewInt(10)),
),
stableswap.PoolParams{
SwapFee: osmomath.NewDecWithPrec(1, 4),
ExitFee: osmomath.NewDecWithPrec(0, 2),
},
[]uint64{1, 1},
)
},
expectedComparableLiquidity: osmomath.NewInt(100),
},
expectPass: true,
},
{
name: "Get Concentrated Liquidity Pool Comparable Liquidity",
param: param{
executePoolCreation: func() uint64 {
pool := s.PrepareConcentratedPool()
err := s.App.BankKeeper.SendCoins(
s.Ctx,
s.TestAccs[0],
pool.GetAddress(),
sdk.NewCoins(sdk.NewCoin("eth", osmomath.NewInt(10)), sdk.NewCoin("usdc", osmomath.NewInt(10))))
s.Require().NoError(err)
return pool.GetId()
},
expectedComparableLiquidity: osmomath.NewInt(100),
},
expectPass: true,
},
{
name: "Catch overflow error on liquidity multiplication",
param: param{
executePoolCreation: func() uint64 {
return s.PrepareBalancerPoolWithCoins(
sdk.NewCoin("uosmo", osmomath.Int(osmomath.NewUintFromString("999999999999999999999999999999999999999"))),
sdk.NewCoin("juno", osmomath.Int(osmomath.NewUintFromString("999999999999999999999999999999999999999"))))
},
expectedComparableLiquidity: osmomath.Int{},
},
expectPass: false,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
// Create the pool
poolId := tc.param.executePoolCreation()
// Get the comparable liquidity
comparableLiquidity, err := s.App.ProtoRevKeeper.GetComparablePoolLiquidity(s.Ctx, poolId)
if tc.expectPass {
s.Require().NoError(err)
s.Require().Equal(tc.param.expectedComparableLiquidity, comparableLiquidity)
} else {
s.Require().Error(err)
s.Require().Equal(tc.param.expectedComparableLiquidity, comparableLiquidity)
}
})
}
}
// Tests StoreJoinExitPoolSwaps stores the swaps associated with GAMM join/exit pool messages in the store, depending on if it is a join or exit.
func (s *KeeperTestSuite) TestStoreJoinExitPoolSwaps() {
type param struct {
poolId uint64
denom string
isJoin bool
expectedSwap types.Trade
}
tests := []struct {
name string
param param
expectPass bool
}{
{
name: "Store Join Pool Swap",
param: param{
poolId: 1,
denom: "akash",
isJoin: true,
expectedSwap: types.Trade{
Pool: 1,
TokenIn: "akash",
TokenOut: "Atom",
},
},
expectPass: true,
},
{
name: "Store Exit Pool Swap",
param: param{
poolId: 1,
denom: "akash",
isJoin: false,
expectedSwap: types.Trade{
Pool: 1,
TokenIn: "Atom",
TokenOut: "akash",
},
},
expectPass: true,
},
{
name: "Non-Gamm Pool, Return Early Do Not Store Any Swaps",
param: param{
poolId: 50,
denom: "uosmo",
isJoin: true,
expectedSwap: types.Trade{},
},
expectPass: false,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
// All pools are already created in the setup
s.App.ProtoRevKeeper.StoreJoinExitPoolSwaps(s.Ctx, s.TestAccs[0], tc.param.poolId, tc.param.denom, tc.param.isJoin)
// Get the swaps to backrun after storing the swap via StoreJoinExitPoolSwaps
swapsToBackrun, err := s.App.ProtoRevKeeper.GetSwapsToBackrun(s.Ctx)
if tc.expectPass {
s.Require().NoError(err)
s.Require().Equal(tc.param.expectedSwap, swapsToBackrun.Trades[len(swapsToBackrun.Trades)-1])
} else {
s.Require().Equal(swapsToBackrun, types.Route{})
}
})
}
}
// Tests CompareAndStorePool compares the comparable liquidity of a pool with the stored pool, storing the new pool if it has higher comparable liquidity.
// Note: This test calls DeleteAllPoolsForBaseDenom in the prepareStateAndGetPoolIdToCompare function because the
// hooks are triggered by default and we want to test the CompareAndStorePool on the state before the hooks are triggered.
func (s *KeeperTestSuite) TestCompareAndStorePool() {
type param struct {
baseDenom string
matchDenom string
prepareStateAndGetPoolIdToCompare func() (uint64, uint64)
}
tests := []struct {
name string
param param
expectPass bool
}{
{
name: "Nothing Stored, Store Balancer",
param: param{
baseDenom: "uosmo",
matchDenom: "juno",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Prepare a balancer pool with coins
poolId := s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(10)), sdk.NewCoin("juno", osmomath.NewInt(10)))
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
return poolId, poolId
},
},
expectPass: true,
},
{
name: "Nothing Stored, Store Concentrated Liquidity Pool w/ Coins",
param: param{
baseDenom: "uosmo",
matchDenom: "stake",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Prepare a concentrated liquidity pool with coins
poolId := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("uosmo", "stake").GetId()
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
return poolId, poolId
},
},
expectPass: true,
},
{
name: "Balancer Previously Stored w/ Less liquidity, Compare Concentrated Liquidity Pool w/ More liqudidity, Ensure CL Gets Stored",
param: param{
baseDenom: "uosmo",
matchDenom: "stake",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Create a concentrated liquidity pool with more liquidity
clPoolId := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("uosmo", "stake").GetId()
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
preparedPoolId := s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(10)), sdk.NewCoin("stake", osmomath.NewInt(10)))
storedPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, "uosmo", "stake")
s.Require().NoError(err)
s.Require().Equal(preparedPoolId, storedPoolId)
// Return the cl pool id as expected since it has higher liquidity, compare the cl pool id
return clPoolId, clPoolId
},
},
expectPass: true,
},
{
name: "Balancer Previously Stored w/ More liquidity, Compare Concentrated Liquidity Pool w/ Less liqudidity, Ensure Balancer Stays Stored",
param: param{
baseDenom: "uosmo",
matchDenom: "stake",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Create a concentrated liquidity pool with more liquidity
clPoolId := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("uosmo", "stake").GetId()
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
// Prepare a balancer pool with more liquidity
balancerPoolId := s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(2000000000000000000)), sdk.NewCoin("stake", osmomath.NewInt(1000000000000000000)))
storedPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, "uosmo", "stake")
s.Require().NoError(err)
s.Require().Equal(balancerPoolId, storedPoolId)
// Return the balancer pool id as expected since it has higher liquidity, compare the cl pool id
return balancerPoolId, clPoolId
},
},
expectPass: true,
},
{
name: "Concentrated Liquidity Previously Stored w/ Less liquidity, Compare Balancer Pool w/ More liqudidity, Ensure Balancer Gets Stored",
param: param{
baseDenom: "uosmo",
matchDenom: "stake",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Prepare a balancer pool with more liquidity
balancerPoolId := s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(2000000000000000000)), sdk.NewCoin("stake", osmomath.NewInt(1000000000000000000)))
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
// Prepare a concentrated liquidity pool with less liquidity, should be stored since nothing is stored
clPoolId := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("uosmo", "stake").GetId()
storedPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, "uosmo", "stake")
s.Require().NoError(err)
s.Require().Equal(clPoolId, storedPoolId)
// Return the balancer pool id as expected since it has higher liquidity, compare the balancer pool id
return balancerPoolId, balancerPoolId
},
},
expectPass: true,
},
{
name: "Concentrated Liquidity Previously Stored w/ More liquidity, Compare Balancer Pool w/ Less liqudidity, Ensure CL Stays Stored",
param: param{
baseDenom: "uosmo",
matchDenom: "stake",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Prepare a balancer pool with less liquidity
balancerPoolId := s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(500000000000000000)), sdk.NewCoin("stake", osmomath.NewInt(1000000000000000000)))
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
// Prepare a concentrated liquidity pool with less liquidity, should be stored since nothing is stored
clPoolId := s.PrepareConcentratedPoolWithCoinsAndFullRangePosition("uosmo", "stake").GetId()
storedPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, "uosmo", "stake")
s.Require().NoError(err)
s.Require().Equal(clPoolId, storedPoolId)
// Return the cl pool id as expected since it has higher liquidity, compare the balancer pool id
return clPoolId, balancerPoolId
},
},
expectPass: true,
},
{
name: "Catch overflow error when getting newPoolLiquidity - Ensure test doesn't panic",
param: param{
baseDenom: "uosmo",
matchDenom: "stake",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Prepare a balancer pool with liquidity levels that will overflow when multiplied
overflowPoolId := s.PrepareBalancerPoolWithCoins(
sdk.NewCoin("uosmo", osmomath.Int(osmomath.NewUintFromString("999999999999999999999999999999999999999"))),
sdk.NewCoin("stake", osmomath.Int(osmomath.NewUintFromString("999999999999999999999999999999999999999"))))
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
// Prepare a balancer pool with normal liquidity
poolId := s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(10)), sdk.NewCoin("stake", osmomath.NewInt(10)))
// The normal liquidity pool should be stored since the function will return early when catching the overflow error
return poolId, overflowPoolId
},
},
expectPass: true,
},
{
name: "Catch overflow error when getting storedPoolLiquidity - Ensure test doesn't panic",
param: param{
baseDenom: "uosmo",
matchDenom: "stake",
prepareStateAndGetPoolIdToCompare: func() (uint64, uint64) {
// Prepare a balancer pool with normal liquidity
poolId := s.PrepareBalancerPoolWithCoins(sdk.NewCoin("uosmo", osmomath.NewInt(10)), sdk.NewCoin("stake", osmomath.NewInt(10)))
// Delete all pools for the base denom uosmo so that all tests start with a clean slate
s.App.ProtoRevKeeper.DeleteAllPoolsForBaseDenom(s.Ctx, "uosmo")
// Prepare a balancer pool with liquidity levels that will overflow when multiplied
overflowPoolId := s.PrepareBalancerPoolWithCoins(
sdk.NewCoin("uosmo", osmomath.Int(osmomath.NewUintFromString("999999999999999999999999999999999999999"))),
sdk.NewCoin("stake", osmomath.Int(osmomath.NewUintFromString("999999999999999999999999999999999999999"))))
// The overflow pool should be stored since the function will return early when catching the overflow error
return overflowPoolId, poolId
},
},
expectPass: true,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
// Run any state preparation and get the pool id to compare to the stored pool
expectedStoredPoolId, comparePoolId := tc.param.prepareStateAndGetPoolIdToCompare()
// Compare and store the pool
s.App.ProtoRevKeeper.CompareAndStorePool(s.Ctx, comparePoolId, tc.param.baseDenom, tc.param.matchDenom)
// Get the stored pool id for the highest liquidity pool in protorev
storedPoolId, err := s.App.ProtoRevKeeper.GetPoolForDenomPair(s.Ctx, tc.param.baseDenom, tc.param.matchDenom)
if tc.expectPass {
s.Require().NoError(err)
s.Require().Equal(expectedStoredPoolId, storedPoolId)
}
})
}
}
func (s *KeeperTestSuite) TestAfterEpochEnd() {
tests := []struct {
name string
arbProfits sdk.Coins
}{
{
name: "osmo denom only",
arbProfits: sdk.NewCoins(sdk.NewCoin("uosmo", osmomath.NewInt(100000000))),
},
{
name: "osmo denom and another base denom",
arbProfits: sdk.NewCoins(sdk.NewCoin("uosmo", osmomath.NewInt(100000000)),
sdk.NewCoin("juno", osmomath.NewInt(100000000))),
},
{
name: "osmo denom, another base denom, and a non base denom",
arbProfits: sdk.NewCoins(sdk.NewCoin("uosmo", osmomath.NewInt(100000000)),
sdk.NewCoin("juno", osmomath.NewInt(100000000)),
sdk.NewCoin("eth", osmomath.NewInt(100000000))),
},
{
name: "no profits",
arbProfits: sdk.Coins{},
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
s.SetupTest()
// Set base denoms
baseDenoms := []types.BaseDenom{
{
Denom: types.OsmosisDenomination,
StepSize: osmomath.NewInt(1_000_000),
},
{
Denom: "juno",
StepSize: osmomath.NewInt(1_000_000),
},
}
s.App.ProtoRevKeeper.SetBaseDenoms(s.Ctx, baseDenoms)
// Set protorev developer account
devAccount := apptesting.CreateRandomAccounts(1)[0]
s.App.ProtoRevKeeper.SetDeveloperAccount(s.Ctx, devAccount)
err := s.App.BankKeeper.MintCoins(s.Ctx, types.ModuleName, tc.arbProfits)
s.Require().NoError(err)
communityPoolBalancePre := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(distrtypes.ModuleName))
// System under test
err = s.App.ProtoRevKeeper.AfterEpochEnd(s.Ctx, "day", 1)
expectedDevProfit := sdk.Coins{}
expectedOsmoBurn := sdk.Coins{}
arbProfitsBaseDenoms := sdk.Coins{}
arbProfitsNonBaseDenoms := sdk.Coins{}
// Split the profits into base and non base denoms
for _, coin := range tc.arbProfits {
isBaseDenom := false
for _, baseDenom := range baseDenoms {
if coin.Denom == baseDenom.Denom {
isBaseDenom = true
break
}
}
if isBaseDenom {
arbProfitsBaseDenoms = append(arbProfitsBaseDenoms, coin)
} else {
arbProfitsNonBaseDenoms = append(arbProfitsNonBaseDenoms, coin)
}
}
profitSplit := types.ProfitSplitPhase1
for _, arbProfit := range arbProfitsBaseDenoms {
devProfitAmount := arbProfit.Amount.MulRaw(profitSplit).QuoRaw(100)
expectedDevProfit = append(expectedDevProfit, sdk.NewCoin(arbProfit.Denom, devProfitAmount))
}
// Get the developer account balance
devAccountBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, devAccount)
s.Require().Equal(expectedDevProfit, devAccountBalance)
// Get the burn address balance
burnAddressBalance := s.App.BankKeeper.GetAllBalances(s.Ctx, types.DefaultNullAddress)
if arbProfitsBaseDenoms.AmountOf(types.OsmosisDenomination).IsPositive() {
expectedOsmoBurn = sdk.NewCoins(sdk.NewCoin(types.OsmosisDenomination, arbProfitsBaseDenoms.AmountOf(types.OsmosisDenomination).Sub(expectedDevProfit.AmountOf(types.OsmosisDenomination))))
s.Require().Equal(expectedOsmoBurn, burnAddressBalance)
} else {
s.Require().Equal(sdk.Coins{}, burnAddressBalance)
}
// Get the community pool balance
communityPoolBalancePost := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(distrtypes.ModuleName))
actualCommunityPool := communityPoolBalancePost.Sub(communityPoolBalancePre...)
expectedCommunityPool := arbProfitsBaseDenoms.Sub(expectedDevProfit...).Sub(expectedOsmoBurn...)
s.Require().Equal(expectedCommunityPool, actualCommunityPool)
// The protorev module account should only contain the non base denoms if there are any
protorevModuleAccount := s.App.BankKeeper.GetAllBalances(s.Ctx, s.App.AccountKeeper.GetModuleAddress(types.ModuleName))
s.Require().Equal(arbProfitsNonBaseDenoms, protorevModuleAccount)
})
}
}