-
Notifications
You must be signed in to change notification settings - Fork 608
/
pool_test.go
857 lines (766 loc) · 25.8 KB
/
pool_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
package model_test
import (
fmt "fmt"
"math"
"testing"
time "time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/osmoutils/osmoassert"
"github.com/osmosis-labs/osmosis/v17/app/apptesting"
clmath "github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/math"
"github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/model"
"github.com/osmosis-labs/osmosis/v17/x/concentrated-liquidity/types"
)
const (
ETH = "eth"
USDC = "usdc"
DAI = "dai"
DefaultValidPoolID = uint64(1)
DefaultTickSpacing = uint64(1)
)
var (
DefaultSpotPrice = sdk.MustNewDecFromStr("0.2")
DefaultReverseSpotPrice = sdk.NewDec(1).Quo(DefaultSpotPrice)
DefaultSqrtSpotPrice = func() osmomath.BigDec {
sqrtPrice, _ := osmomath.MonotonicSqrt(DefaultSpotPrice)
return osmomath.BigDecFromSDKDec(sqrtPrice)
}()
DefaultLiquidityAmt = sdk.MustNewDecFromStr("1517882343.751510418088349649")
DefaultCurrTick int64 = 310000
DefaultCurrPrice = sdk.NewDec(5000)
DefaultCurrSqrtPrice = func() osmomath.BigDec {
sqrtPrice, _ := osmomath.MonotonicSqrt(DefaultCurrPrice)
return osmomath.BigDecFromSDKDec(sqrtPrice)
}() // 70.710678118654752440
DefaultSpreadFactor = sdk.MustNewDecFromStr("0.01")
)
type ConcentratedPoolTestSuite struct {
apptesting.KeeperTestHelper
}
func TestConcentratedPoolTestSuite(t *testing.T) {
suite.Run(t, new(ConcentratedPoolTestSuite))
}
// TestGetAddress tests the GetAddress method of pool
func (s *ConcentratedPoolTestSuite) TestGetAddress() {
tests := []struct {
name string
expectedPanic bool
}{
{
name: "Happy path",
},
{
name: "Unhappy path: wrong bech32 encoded address",
expectedPanic: true,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
// Init suite for each test.
s.Setup()
address := s.TestAccs[0].String()
// if the test case is expected to panic, we use wrong bech32 encoded address instead
if tc.expectedPanic {
address = "osmo15l7yueqf3tx4cvpt6njvj7zxmvuhkwyrr509e9"
}
mock_pool := model.Pool{
Id: 1,
Address: address,
}
// Check that the returned address is backward compatible
osmoassert.ConditionalPanic(s.T(), tc.expectedPanic, func() {
addr := mock_pool.GetAddress()
s.Require().Equal(addr, s.TestAccs[0])
})
})
}
}
// TestGetIncentivesAddress tests the GetIncentivesAddress method of pool
func (s *ConcentratedPoolTestSuite) TestGetIncentivesAddress() {
tests := []struct {
name string
expectedPanic bool
}{
{
name: "Happy path",
},
{
name: "Unhappy path: wrong bech32 encoded address",
expectedPanic: true,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
// Init suite for each test.
s.Setup()
// Create a concentrated liquidity pool struct instance
address := s.TestAccs[0].String()
// if the test case is expected to panic, we use wrong bech32 encoded address instead
if tc.expectedPanic {
address = "osmo15l7yueqf3tx4cvpt6njvj7zxmvuhkwyrr509e9"
}
mock_pool := model.Pool{
Id: 1,
IncentivesAddress: address,
}
// Check that the returned address is backward compatible
osmoassert.ConditionalPanic(s.T(), tc.expectedPanic, func() {
addr := mock_pool.GetIncentivesAddress()
s.Require().Equal(addr, s.TestAccs[0])
})
})
}
}
// TestString tests if String method of the pool correctly json marshals the pool object
func (s *ConcentratedPoolTestSuite) TestString() {
s.Setup()
pool, err := model.NewConcentratedLiquidityPool(1, "foo", "bar", DefaultTickSpacing, DefaultSpreadFactor)
s.Require().NoError(err)
poolString := pool.String()
s.Require().Equal(poolString, "{\"address\":\"osmo19e2mf7cywkv7zaug6nk5f87d07fxrdgrladvymh2gwv5crvm3vnsuewhh7\",\"incentives_address\":\"osmo156gncm3w2hdvuxxaejue8nejxgdgsrvdf7jftntuhxnaarhxcuas4ywjxf\",\"spread_rewards_address\":\"osmo10t3u6ze74jn7et6rluuxyf9vr2arykewmhcx67svg6heuu0gte2syfudcv\",\"id\":1,\"current_tick_liquidity\":\"0.000000000000000000\",\"token0\":\"foo\",\"token1\":\"bar\",\"current_sqrt_price\":\"0.000000000000000000000000000000000000\",\"tick_spacing\":1,\"exponent_at_price_one\":-6,\"spread_factor\":\"0.010000000000000000\",\"last_liquidity_update\":\"0001-01-01T00:00:00Z\"}")
}
// TestSpotPrice tests the SpotPrice method of the ConcentratedPoolTestSuite.
func (s *ConcentratedPoolTestSuite) TestSpotPrice() {
type param struct {
baseDenom string
quoteDenom string
}
tests := []struct {
name string
param param
expectedSpotPrice sdk.Dec
expectedErr error
}{
{
name: "Happy path",
param: param{
baseDenom: ETH,
quoteDenom: USDC,
},
expectedSpotPrice: DefaultSpotPrice,
},
{
name: "Happy path: reverse spot price",
param: param{
baseDenom: USDC,
quoteDenom: ETH,
},
expectedSpotPrice: DefaultReverseSpotPrice,
},
{
name: "Error: quote asset denom does not exist in the pool",
param: param{
baseDenom: ETH,
quoteDenom: DAI,
},
expectedSpotPrice: sdk.ZeroDec(),
expectedErr: fmt.Errorf("quote asset denom (%s) is not in the pool", DAI),
},
{
name: "Error: base asset denom does not exist in the pool",
param: param{
baseDenom: DAI,
quoteDenom: ETH,
},
expectedSpotPrice: sdk.ZeroDec(),
expectedErr: fmt.Errorf("base asset denom (%s) is not in the pool", DAI),
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
// Init suite for each test.
s.Setup()
// Create a concentrated liquidity pool struct instance
mock_pool := model.Pool{
CurrentSqrtPrice: DefaultSqrtSpotPrice,
Token0: ETH,
Token1: USDC,
}
// Check the spot price of the mock pool using the SpotPrice method.
spotPriceFromMethod, err := mock_pool.SpotPrice(sdk.Context{}, tc.param.quoteDenom, tc.param.baseDenom)
if tc.expectedErr != nil {
s.Require().Error(err)
s.Require().ErrorAs(err, &tc.expectedErr)
} else {
s.Require().NoError(err)
// We use elipson due to sqrt approximation
elipson := sdk.MustNewDecFromStr("0.0000000000000001")
s.Require().True(spotPriceFromMethod.Sub(tc.expectedSpotPrice).Abs().LT(elipson))
}
})
}
}
// TestUpdateLiquidity tests the UpdateLiquidity method of the ConcentratedPoolTestSuite.
func (s *ConcentratedPoolTestSuite) TestUpdateLiquidity() {
mock_pool := model.Pool{
CurrentTickLiquidity: DefaultLiquidityAmt,
}
// Try updating the liquidity with a zero sdk.Dec value.
mock_pool.UpdateLiquidity(sdk.ZeroDec())
// Assert that the liquidity has not changed.
s.Require().Equal(DefaultLiquidityAmt, mock_pool.CurrentTickLiquidity)
// Try adding 10 to the pool liquidity.
mock_pool.UpdateLiquidity(sdk.NewDec(10))
// Assert that the liquidity has increased by 10.
s.Require().Equal(DefaultLiquidityAmt.Add(sdk.NewDec(10)), mock_pool.CurrentTickLiquidity)
}
func (s *ConcentratedPoolTestSuite) TestIsCurrentTickInRange() {
s.Setup()
currentTick := DefaultCurrTick
tests := []struct {
name string
lowerTick int64
upperTick int64
expectedResult bool
}{
{
"given lower tick tick is within range of pool tick",
DefaultCurrTick - 1,
DefaultCurrTick + 1,
true,
},
{
"range only includes current tick",
DefaultCurrTick,
DefaultCurrTick + 1,
true,
},
{
"current tick is on upper tick",
DefaultCurrTick - 3,
DefaultCurrTick,
false,
},
{
"lower tick and upper tick are equal to pool tick",
DefaultCurrTick,
DefaultCurrTick,
false,
},
{
"only lower tick is equal to the pool tick",
DefaultCurrTick,
DefaultCurrTick + 3,
true,
},
{
"only upper tick is equal to the pool tick",
DefaultCurrTick - 3,
DefaultCurrTick,
false,
},
{
"lower tick is greater then pool tick",
DefaultCurrTick + 1,
DefaultCurrTick + 3,
false,
},
{
"upper tick is lower then pool tick",
DefaultCurrTick - 3,
DefaultCurrTick - 1,
false,
},
}
for _, tc := range tests {
s.Run(tc.name, func() {
// Create a concentrated liquidity pool struct instance
mock_pool := model.Pool{
CurrentTick: currentTick,
}
// System under test
iscurrentTickInRange := mock_pool.IsCurrentTickInRange(tc.lowerTick, tc.upperTick)
if tc.expectedResult {
s.Require().True(iscurrentTickInRange)
} else {
s.Require().False(iscurrentTickInRange)
}
})
}
}
func (s *ConcentratedPoolTestSuite) TestApplySwap() {
// Set up the test suite.
s.Setup()
var (
negativeOne = osmomath.NewBigDec(-1)
negativeOneDec = sdk.OneDec().Neg()
)
tests := []struct {
name string
currentLiquidity sdk.Dec
currentTick int64
currentSqrtPrice osmomath.BigDec
newLiquidity sdk.Dec
newTick int64
newSqrtPrice osmomath.BigDec
expectErr error
}{
{
name: "positive liquidity and square root price",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt.MulInt64(2),
newTick: DefaultCurrTick * 2,
newSqrtPrice: DefaultCurrSqrtPrice.MulInt64(2),
expectErr: nil,
},
{
name: "negative liquidity",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: negativeOneDec,
newTick: DefaultCurrTick,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: types.NegativeLiquidityError{Liquidity: negativeOneDec},
},
{
name: "negative square root price",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: DefaultCurrTick,
newSqrtPrice: negativeOne,
expectErr: types.SqrtPriceNegativeError{ProvidedSqrtPrice: negativeOne},
},
{
name: "new tick is equal to max tick",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: types.MaxTick,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: nil,
},
{
name: "new tick is equal to min initialized tick",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: types.MinInitializedTick,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: nil,
},
{
name: "new tick is equal to min current tick",
currentLiquidity: DefaultLiquidityAmt,
currentTick: DefaultCurrTick,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: types.MinCurrentTick,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: nil,
},
{
name: "error: upper tick is greater than max tick",
currentLiquidity: DefaultLiquidityAmt,
currentTick: 1,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: math.MaxInt64,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: types.TickIndexNotWithinBoundariesError{
MaxTick: types.MaxTick,
MinTick: types.MinCurrentTick,
ActualTick: math.MaxInt64,
},
},
{
name: "error: lower tick is smaller than min tick",
currentLiquidity: DefaultLiquidityAmt,
currentTick: 1,
currentSqrtPrice: DefaultCurrSqrtPrice,
newLiquidity: DefaultLiquidityAmt,
newTick: math.MinInt64,
newSqrtPrice: DefaultCurrSqrtPrice,
expectErr: types.TickIndexNotWithinBoundariesError{
MaxTick: types.MaxTick,
MinTick: types.MinCurrentTick,
ActualTick: math.MinInt64,
},
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
// Create a concentrated liquidity pool struct instance
mock_pool := model.Pool{
ExponentAtPriceOne: types.ExponentAtPriceOne,
CurrentTickLiquidity: tt.currentLiquidity,
CurrentTick: tt.currentTick,
CurrentSqrtPrice: tt.currentSqrtPrice,
}
// Apply the new values to the mock pool using the ApplySwap method.
err := mock_pool.ApplySwap(tt.newLiquidity, tt.newTick, tt.newSqrtPrice)
if tt.expectErr != nil {
s.Require().ErrorIs(tt.expectErr, err)
return
}
// Assert that the values in the mock pool have been updated.
s.Require().Equal(tt.newLiquidity, mock_pool.CurrentTickLiquidity)
s.Require().Equal(tt.newTick, mock_pool.CurrentTick)
s.Require().Equal(tt.newSqrtPrice, mock_pool.CurrentSqrtPrice)
})
}
}
// TestNewConcentratedLiquidityPool is a test suite that tests the NewConcentratedLiquidityPool function.
func (s *ConcentratedPoolTestSuite) TestNewConcentratedLiquidityPool() {
type param struct {
poolId uint64
denom0 string
denom1 string
tickSpacing uint64
spreadFactor sdk.Dec
}
tests := []struct {
name string
param param
expectedPoolId uint64
expectedDenom0 string
expectedDenom1 string
expectedTickSpacing uint64
expectedErr error
}{
{
name: "Happy path",
param: param{
poolId: DefaultValidPoolID,
denom0: ETH,
denom1: USDC,
tickSpacing: DefaultTickSpacing,
spreadFactor: DefaultSpreadFactor,
},
expectedPoolId: DefaultValidPoolID,
expectedDenom0: ETH,
expectedDenom1: USDC,
expectedTickSpacing: DefaultTickSpacing,
},
{
name: "Non lexicographical order of denoms should not get reordered",
param: param{
poolId: DefaultValidPoolID,
denom0: USDC,
denom1: ETH,
tickSpacing: DefaultTickSpacing,
spreadFactor: sdk.ZeroDec(),
},
expectedPoolId: DefaultValidPoolID,
expectedDenom0: USDC,
expectedDenom1: ETH,
expectedTickSpacing: DefaultTickSpacing,
},
{
name: "Error: same denom not allowed",
param: param{
poolId: DefaultValidPoolID,
denom0: USDC,
denom1: USDC,
tickSpacing: DefaultTickSpacing,
spreadFactor: DefaultSpreadFactor,
},
expectedErr: types.MatchingDenomError{Denom: USDC},
},
{
name: "Error: negative spread factor",
param: param{
poolId: DefaultValidPoolID,
denom0: ETH,
denom1: USDC,
tickSpacing: DefaultTickSpacing,
spreadFactor: sdk.ZeroDec().Sub(sdk.SmallestDec()),
},
expectedErr: types.InvalidSpreadFactorError{ActualSpreadFactor: sdk.ZeroDec().Sub(sdk.SmallestDec())},
},
{
name: "Error: spread factor == 1",
param: param{
poolId: DefaultValidPoolID,
denom0: ETH,
denom1: USDC,
tickSpacing: DefaultTickSpacing,
spreadFactor: sdk.OneDec(),
},
expectedErr: types.InvalidSpreadFactorError{ActualSpreadFactor: sdk.OneDec()},
},
}
for _, test := range tests {
s.Run(test.name, func() {
// Init suite for each test.
s.Setup()
// Call NewConcentratedLiquidityPool with the parameters from the current test.
pool, err := model.NewConcentratedLiquidityPool(test.param.poolId, test.param.denom0, test.param.denom1, test.param.tickSpacing, test.param.spreadFactor)
if test.expectedErr != nil {
// If the test is expected to produce an error, check if it does.
s.Require().Error(err)
s.Require().ErrorContains(err, test.expectedErr.Error())
} else {
// If the test is not expected to produce an error, check if it doesn't.
s.Require().NoError(err)
// Check if the values of the returned pool match the expected values.
s.Require().Equal(test.expectedPoolId, pool.Id)
s.Require().Equal(test.expectedDenom0, pool.Token0)
s.Require().Equal(test.expectedDenom1, pool.Token1)
s.Require().Equal(test.expectedTickSpacing, pool.TickSpacing)
s.Require().Equal(test.param.spreadFactor, pool.SpreadFactor)
}
})
}
}
func (suite *ConcentratedPoolTestSuite) TestCalcActualAmounts() {
var (
tickToSqrtPrice = func(tick int64) sdk.Dec {
_, sqrtPrice, err := clmath.TickToSqrtPrice(tick)
suite.Require().NoError(err)
return sqrtPrice
}
defaultLiquidityDelta = sdk.NewDec(1000)
defaultLiquidityDeltaBigDec = osmomath.NewBigDec(1000)
lowerTick = int64(-99)
lowerSqrtPriceBigDec = osmomath.BigDecFromSDKDec(tickToSqrtPrice(lowerTick))
midtick = int64(2)
midSqrtPriceBigDec = osmomath.BigDecFromSDKDec(tickToSqrtPrice(midtick))
uppertick = int64(74)
upperSqrtPriceBigDec = osmomath.BigDecFromSDKDec(tickToSqrtPrice(uppertick))
)
tests := map[string]struct {
currentTick int64
lowerTick int64
upperTick int64
liquidityDelta sdk.Dec
shouldTestRoundingInvariant bool
expectError error
expectedAmount0 sdk.Dec
expectedAmount1 sdk.Dec
}{
"current in range, positive liquidity": {
currentTick: midtick,
lowerTick: lowerTick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta,
shouldTestRoundingInvariant: true,
expectedAmount0: clmath.CalcAmount0Delta(defaultLiquidityDeltaBigDec, midSqrtPriceBigDec, upperSqrtPriceBigDec, true).SDKDec(),
expectedAmount1: clmath.CalcAmount1Delta(defaultLiquidityDeltaBigDec, midSqrtPriceBigDec, lowerSqrtPriceBigDec, true).SDKDec(),
},
"current in range, negative liquidity": {
currentTick: midtick,
lowerTick: lowerTick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta.Neg(),
expectedAmount0: clmath.CalcAmount0Delta(defaultLiquidityDeltaBigDec.Neg(), midSqrtPriceBigDec, upperSqrtPriceBigDec, false).SDKDec(),
expectedAmount1: clmath.CalcAmount1Delta(defaultLiquidityDeltaBigDec.Neg(), midSqrtPriceBigDec, lowerSqrtPriceBigDec, false).SDKDec(),
},
"current below range, positive liquidity": {
currentTick: lowerTick,
lowerTick: midtick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta,
expectedAmount0: clmath.CalcAmount0Delta(defaultLiquidityDeltaBigDec, midSqrtPriceBigDec, upperSqrtPriceBigDec, true).SDKDec(),
expectedAmount1: sdk.ZeroDec(),
},
"current below range, negative liquidity": {
currentTick: lowerTick,
lowerTick: midtick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta.Neg(),
expectedAmount0: clmath.CalcAmount0Delta(defaultLiquidityDeltaBigDec.Neg(), midSqrtPriceBigDec, upperSqrtPriceBigDec, false).SDKDec(),
expectedAmount1: sdk.ZeroDec(),
},
"current above range, positive liquidity": {
currentTick: uppertick,
lowerTick: lowerTick,
upperTick: midtick,
liquidityDelta: defaultLiquidityDelta,
expectedAmount0: sdk.ZeroDec(),
expectedAmount1: clmath.CalcAmount1Delta(defaultLiquidityDeltaBigDec, lowerSqrtPriceBigDec, midSqrtPriceBigDec, true).SDKDec(),
},
"current above range, negative liquidity": {
currentTick: uppertick,
lowerTick: lowerTick,
upperTick: midtick,
liquidityDelta: defaultLiquidityDelta.Neg(),
expectedAmount0: sdk.ZeroDec(),
expectedAmount1: clmath.CalcAmount1Delta(defaultLiquidityDeltaBigDec.Neg(), lowerSqrtPriceBigDec, midSqrtPriceBigDec, false).SDKDec(),
},
// errors
"error: zero liqudiity": {
currentTick: midtick,
lowerTick: lowerTick,
upperTick: uppertick,
liquidityDelta: sdk.ZeroDec(),
expectError: types.ErrZeroLiquidity,
},
"error: lower tick equals upper tick": {
currentTick: lowerTick,
lowerTick: lowerTick,
upperTick: lowerTick,
liquidityDelta: defaultLiquidityDelta,
expectError: types.InvalidLowerUpperTickError{LowerTick: lowerTick, UpperTick: lowerTick},
},
"error: lower tick is greater than upper tick": {
currentTick: lowerTick,
lowerTick: lowerTick + 1,
upperTick: lowerTick,
liquidityDelta: defaultLiquidityDelta,
expectError: types.InvalidLowerUpperTickError{LowerTick: lowerTick + 1, UpperTick: lowerTick},
},
"error: lower tick is equal to upper tick": {
currentTick: lowerTick,
lowerTick: lowerTick,
upperTick: lowerTick,
liquidityDelta: defaultLiquidityDelta,
expectError: types.InvalidLowerUpperTickError{LowerTick: lowerTick, UpperTick: lowerTick},
},
}
for name, tc := range tests {
tc := tc
suite.Run(name, func() {
suite.Setup()
pool := model.Pool{
CurrentTick: tc.currentTick,
}
_, currenTicktSqrtPrice, _ := clmath.TickToSqrtPrice(pool.CurrentTick)
pool.CurrentSqrtPrice = osmomath.BigDecFromSDKDec(currenTicktSqrtPrice)
actualAmount0, actualAmount1, err := pool.CalcActualAmounts(suite.Ctx, tc.lowerTick, tc.upperTick, tc.liquidityDelta)
if tc.expectError != nil {
suite.Require().Error(err)
suite.Require().ErrorIs(err, tc.expectError)
return
}
suite.Require().NoError(err)
suite.Require().Equal(tc.expectedAmount0, actualAmount0)
suite.Require().Equal(tc.expectedAmount1, actualAmount1)
// Note: to test rounding invariants around positive and negative liquidity.
if tc.shouldTestRoundingInvariant {
actualAmount0Neg, actualAmount1Neg, err := pool.CalcActualAmounts(suite.Ctx, tc.lowerTick, tc.upperTick, tc.liquidityDelta.Neg())
suite.Require().NoError(err)
amt0Diff := actualAmount0.Sub(actualAmount0Neg.Neg())
amt1Diff := actualAmount1.Sub(actualAmount1Neg.Neg())
// Difference is between 0 and 1 due to positive liquidity rounding up and negative liquidity performing math normally.
suite.Require().True(amt0Diff.IsPositive() && amt0Diff.LT(sdk.OneDec()))
suite.Require().True(amt1Diff.IsPositive() && amt1Diff.LT(sdk.OneDec()))
}
})
}
}
func (suite *ConcentratedPoolTestSuite) TestUpdateLiquidityIfActivePosition() {
var (
defaultLiquidityDelta = sdk.NewDec(1000)
defaultLiquidityAmt = sdk.NewDec(1000)
lowerTick = int64(-99)
midtick = int64(2)
uppertick = int64(74)
)
tests := map[string]struct {
currentTick int64
lowerTick int64
upperTick int64
liquidityDelta sdk.Dec
expectError error
}{
"current in range, positive liquidity": {
currentTick: midtick,
lowerTick: lowerTick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta,
},
"current in range, negative liquidity": {
currentTick: midtick,
lowerTick: lowerTick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta.Neg(),
},
"current below range, positive liquidity": {
currentTick: lowerTick,
lowerTick: midtick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta,
},
"current below range, negative liquidity": {
currentTick: lowerTick,
lowerTick: midtick,
upperTick: uppertick,
liquidityDelta: defaultLiquidityDelta.Neg(),
},
"current above range, positive liquidity": {
currentTick: uppertick,
lowerTick: lowerTick,
upperTick: midtick,
liquidityDelta: defaultLiquidityDelta,
},
"current above range, negative liquidity": {
currentTick: uppertick,
lowerTick: lowerTick,
upperTick: midtick,
liquidityDelta: defaultLiquidityDelta.Neg(),
},
}
for name, tc := range tests {
tc := tc
suite.Run(name, func() {
suite.Setup()
pool := model.Pool{
CurrentTick: tc.currentTick,
CurrentTickLiquidity: defaultLiquidityAmt,
}
_, currenTicktSqrtPrice, _ := clmath.TickToSqrtPrice(pool.CurrentTick)
pool.CurrentSqrtPrice = osmomath.BigDecFromSDKDec(currenTicktSqrtPrice)
wasUpdated := pool.UpdateLiquidityIfActivePosition(suite.Ctx, tc.lowerTick, tc.upperTick, tc.liquidityDelta)
if tc.lowerTick <= tc.currentTick && tc.currentTick <= tc.upperTick {
suite.Require().True(wasUpdated)
expectedCurrentTickLiquidity := defaultLiquidityAmt.Add(tc.liquidityDelta)
suite.Require().Equal(expectedCurrentTickLiquidity, pool.CurrentTickLiquidity)
} else {
suite.Require().False(wasUpdated)
suite.Require().Equal(defaultLiquidityAmt, pool.CurrentTickLiquidity)
}
})
}
}
func (suite *ConcentratedPoolTestSuite) TestPoolSetMethods() {
var (
newCurrentTick = DefaultCurrTick
newCurrentSqrtPrice = DefaultCurrSqrtPrice
newTickSpacing = DefaultTickSpacing
)
tests := map[string]struct {
currentTick int64
currentSqrtPrice osmomath.BigDec
tickSpacing uint64
lastLiquidityUpdateDelta time.Duration
}{
"happy path": {
currentTick: newCurrentTick,
currentSqrtPrice: newCurrentSqrtPrice,
tickSpacing: newTickSpacing,
lastLiquidityUpdateDelta: time.Hour,
},
}
for name, tc := range tests {
tc := tc
suite.Run(name, func() {
suite.Setup()
currentBlockTime := suite.Ctx.BlockTime()
// Create the pool and check that the initial values are not equal to the new values we will set.
clPool := suite.PrepareConcentratedPool()
suite.Require().NotEqual(tc.currentTick, clPool.GetCurrentTick())
suite.Require().NotEqual(tc.currentSqrtPrice, clPool.GetCurrentSqrtPrice())
suite.Require().NotEqual(tc.tickSpacing, clPool.GetTickSpacing())
suite.Require().NotEqual(currentBlockTime.Add(tc.lastLiquidityUpdateDelta), clPool.GetLastLiquidityUpdate())
// Run the setters.
clPool.SetCurrentTick(tc.currentTick)
clPool.SetCurrentSqrtPrice(tc.currentSqrtPrice)
clPool.SetTickSpacing(tc.tickSpacing)
clPool.SetLastLiquidityUpdate(currentBlockTime.Add(tc.lastLiquidityUpdateDelta))
// Check that the values are now equal to the new values.
suite.Require().Equal(tc.currentTick, clPool.GetCurrentTick())
suite.Require().Equal(tc.currentSqrtPrice, clPool.GetCurrentSqrtPrice())
suite.Require().Equal(tc.tickSpacing, clPool.GetTickSpacing())
suite.Require().Equal(currentBlockTime.Add(tc.lastLiquidityUpdateDelta), clPool.GetLastLiquidityUpdate())
})
}
}