-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathrouter.go
1170 lines (1024 loc) · 43.7 KB
/
router.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
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
package poolmanager
import (
"errors"
"fmt"
"math/big"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/osmoutils"
gammtypes "github.com/osmosis-labs/osmosis/v26/x/gamm/types"
"github.com/osmosis-labs/osmosis/v26/x/poolmanager/client/queryproto"
"github.com/osmosis-labs/osmosis/v26/x/poolmanager/types"
)
var (
// 1 << 256 - 1 where 256 is the max bit length defined for osmomath.Int
intMaxValue = osmomath.NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)))
// lessPoolIFunc is used for sorting pools by poolID
lessPoolIFunc = func(i, j types.PoolI) bool {
return i.GetId() < j.GetId()
}
)
func (k *Keeper) GetPoolModuleAndPool(ctx sdk.Context, poolId uint64) (swapModule types.PoolModuleI, pool types.PoolI, err error) {
// Get the pool-specific module implementation to ensure that
// swaps are routed to the pool type corresponding to pool ID's pool.
swapModule, err = k.GetPoolModule(ctx, poolId)
if err != nil {
return
}
// Get pool as a general pool type. Note that the underlying function used
// still varies with the pool type.
pool, err = swapModule.GetPool(ctx, poolId)
if err != nil {
return
}
return
}
// RouteExactAmountIn processes a swap along the given route using the swap function
// corresponding to poolID's pool type. It takes in the input denom and amount for
// the initial swap against the first pool and chains the output as the input for the
// next routed pool until the last pool is reached.
// Transaction succeeds if final amount out is greater than tokenOutMinAmount defined
// and no errors are encountered along the way.
func (k Keeper) RouteExactAmountIn(
ctx sdk.Context,
sender sdk.AccAddress,
route []types.SwapAmountInRoute,
tokenIn sdk.Coin,
tokenOutMinAmount osmomath.Int,
) (tokenOutAmount osmomath.Int, err error) {
// Ensure that provided route is not empty and has valid denom format.
if err := types.SwapAmountInRoutes(route).Validate(); err != nil {
return osmomath.Int{}, err
}
totalTakerFeesCharged := sdk.Coins{}
denomsInvolvedInRoute := []string{tokenIn.Denom}
// Iterate through the route and execute a series of swaps through each pool.
for i, routeStep := range route {
// To prevent the multihop swap from being interrupted prematurely, we keep
// the minimum expected output at a very low number until the last pool
_outMinAmount := osmomath.NewInt(1)
if len(route)-1 == i {
_outMinAmount = tokenOutMinAmount
}
var takerFeeCharged sdk.Coin
tokenOutAmount, takerFeeCharged, err = k.SwapExactAmountIn(ctx, sender, routeStep.PoolId, tokenIn, routeStep.TokenOutDenom, _outMinAmount)
if err != nil {
return osmomath.Int{}, err
}
// Chain output of current pool as the input for the next routed pool
tokenIn = sdk.NewCoin(routeStep.TokenOutDenom, tokenOutAmount)
// Track taker fees charged
totalTakerFeesCharged = totalTakerFeesCharged.Add(takerFeeCharged)
// Add the token out denom to the denoms involved in the route, IFF it is not already in the slice
if !osmoutils.Contains(denomsInvolvedInRoute, routeStep.TokenOutDenom) {
denomsInvolvedInRoute = append(denomsInvolvedInRoute, routeStep.TokenOutDenom)
}
}
// Run taker fee skim logic
err = k.TakerFeeSkim(ctx, denomsInvolvedInRoute, totalTakerFeesCharged)
if err != nil {
return osmomath.Int{}, err
}
return tokenOutAmount, nil
}
// SplitRouteExactAmountIn routes the swap across multiple multihop paths
// to get the desired token out. This is useful for achieving the most optimal execution. However, note that the responsibility
// of determining the optimal split is left to the client. This method simply route the swap across the given route.
// The route must end with the same token out and begin with the same token in.
//
// It performs the price impact protection check on the combination of tokens out from all multihop paths. The given tokenOutMinAmount
// is used for comparison.
//
// Returns error if:
// - route are empty
// - route contain duplicate multihop paths
// - last token out denom is not the same for all multihop paths in routeStep
// - one of the multihop swaps fails for internal reasons
// - final token out computed is not positive
// - final token out computed is smaller than tokenOutMinAmount
func (k Keeper) SplitRouteExactAmountIn(
ctx sdk.Context,
sender sdk.AccAddress,
routes []types.SwapAmountInSplitRoute,
tokenInDenom string,
tokenOutMinAmount osmomath.Int,
) (osmomath.Int, error) {
if err := types.ValidateSwapAmountInSplitRoute(routes); err != nil {
return osmomath.Int{}, err
}
var (
// We start the multihop min amount as zero because we want
// to perform a price impact protection check on the combination of tokens out
// from all multihop paths.
multihopStartTokenOutMinAmount = osmomath.ZeroInt()
totalOutAmount = osmomath.ZeroInt()
)
for _, multihopRoute := range routes {
tokenOutAmount, err := k.RouteExactAmountIn(
ctx,
sender,
types.SwapAmountInRoutes(multihopRoute.Pools),
sdk.NewCoin(tokenInDenom, multihopRoute.TokenInAmount),
multihopStartTokenOutMinAmount)
if err != nil {
return osmomath.Int{}, err
}
totalOutAmount = totalOutAmount.Add(tokenOutAmount)
}
if !totalOutAmount.IsPositive() {
return osmomath.Int{}, types.FinalAmountIsNotPositiveError{IsAmountOut: true, Amount: totalOutAmount}
}
if totalOutAmount.LT(tokenOutMinAmount) {
return osmomath.Int{}, types.PriceImpactProtectionExactInError{Actual: totalOutAmount, MinAmount: tokenOutMinAmount}
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeMsgSplitRouteSwapExactAmountIn,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyTokensOut, totalOutAmount.String()),
),
})
return totalOutAmount, nil
}
// SwapExactAmountIn is an API for swapping an exact amount of tokens
// as input to a pool to get a minimum amount of the desired token out.
// The method succeeds when tokenOutAmount is greater than tokenOutMinAmount defined.
// Errors otherwise. Also, errors if the pool id is invalid, if tokens do not belong to the pool with given
// id or if sender does not have the swapped-in tokenIn.
func (k Keeper) SwapExactAmountIn(
ctx sdk.Context,
sender sdk.AccAddress,
poolId uint64,
tokenIn sdk.Coin,
tokenOutDenom string,
tokenOutMinAmount osmomath.Int,
) (tokenOutAmount osmomath.Int, takerFeeCharged sdk.Coin, err error) {
swapModule, pool, err := k.GetPoolModuleAndPool(ctx, poolId)
if err != nil {
return osmomath.Int{}, sdk.Coin{}, err
}
// Check if pool has swaps enabled.
if !pool.IsActive(ctx) {
return osmomath.Int{}, sdk.Coin{}, fmt.Errorf("pool %d is not active", pool.GetId())
}
tokenInAfterSubTakerFee, takerFeeCharged, err := k.chargeTakerFee(ctx, tokenIn, tokenOutDenom, sender, true)
if err != nil {
return osmomath.Int{}, sdk.Coin{}, err
}
// routeStep to the pool-specific SwapExactAmountIn implementation.
tokenOutAmount, err = swapModule.SwapExactAmountIn(ctx, sender, pool, tokenInAfterSubTakerFee, tokenOutDenom, tokenOutMinAmount, pool.GetSpreadFactor(ctx))
if err != nil {
return osmomath.Int{}, sdk.Coin{}, err
}
// Track volume for volume-splitting incentives
k.trackVolume(ctx, pool.GetId(), tokenIn)
return tokenOutAmount, takerFeeCharged, nil
}
// SwapExactAmountInNoTakerFee is an API for swapping an exact amount of tokens
// as input to a pool to get a minimum amount of the desired token out.
// This method does NOT charge a taker fee, and should only be used in txfees hooks
// when swapping taker fees. This prevents us from charging taker fees
// on top of taker fees.
func (k Keeper) SwapExactAmountInNoTakerFee(
ctx sdk.Context,
sender sdk.AccAddress,
poolId uint64,
tokenIn sdk.Coin,
tokenOutDenom string,
tokenOutMinAmount osmomath.Int,
) (tokenOutAmount osmomath.Int, err error) {
swapModule, pool, err := k.GetPoolModuleAndPool(ctx, poolId)
if err != nil {
return osmomath.Int{}, err
}
// Check if pool has swaps enabled.
if !pool.IsActive(ctx) {
return osmomath.Int{}, fmt.Errorf("pool %d is not active", pool.GetId())
}
// routeStep to the pool-specific SwapExactAmountIn implementation.
tokenOutAmount, err = swapModule.SwapExactAmountIn(ctx, sender, pool, tokenIn, tokenOutDenom, tokenOutMinAmount, pool.GetSpreadFactor(ctx))
if err != nil {
return osmomath.Int{}, err
}
// Track volume for volume-splitting incentives
k.trackVolume(ctx, pool.GetId(), tokenIn)
return tokenOutAmount, nil
}
func (k Keeper) MultihopEstimateOutGivenExactAmountInNoTakerFee(
ctx sdk.Context,
route []types.SwapAmountInRoute,
tokenIn sdk.Coin,
) (tokenOutAmount osmomath.Int, err error) {
return k.multihopEstimateOutGivenExactAmountInInternal(ctx, route, tokenIn, false)
}
func (k Keeper) MultihopEstimateOutGivenExactAmountIn(
ctx sdk.Context,
route []types.SwapAmountInRoute,
tokenIn sdk.Coin,
) (tokenOutAmount osmomath.Int, err error) {
return k.multihopEstimateOutGivenExactAmountInInternal(ctx, route, tokenIn, true)
}
func (k Keeper) multihopEstimateOutGivenExactAmountInInternal(
ctx sdk.Context,
route []types.SwapAmountInRoute,
tokenIn sdk.Coin,
applyTakerFee bool,
) (tokenOutAmount osmomath.Int, err error) {
// recover from panic
defer func() {
if r := recover(); r != nil {
tokenOutAmount = osmomath.Int{}
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function MultihopEstimateOutGivenExactAmountIn failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function MultihopEstimateOutGivenExactAmountIn failed due to internal reason: %v", r)
}
}
}()
if err := types.SwapAmountInRoutes(route).Validate(); err != nil {
return osmomath.Int{}, err
}
for _, routeStep := range route {
swapModule, poolI, err := k.GetPoolModuleAndPool(ctx, routeStep.PoolId)
if err != nil {
return osmomath.Int{}, err
}
spreadFactor := poolI.GetSpreadFactor(ctx)
actualTokenIn := tokenIn
// apply taker fee if applicable
if applyTakerFee {
takerFee, err := k.GetTradingPairTakerFee(ctx, tokenIn.Denom, routeStep.TokenOutDenom)
if err != nil {
return osmomath.Int{}, err
}
actualTokenIn, _ = CalcTakerFeeExactIn(tokenIn, takerFee)
}
tokenOut, err := swapModule.CalcOutAmtGivenIn(ctx, poolI, actualTokenIn, routeStep.TokenOutDenom, spreadFactor)
if err != nil {
return osmomath.Int{}, err
}
tokenOutAmount = tokenOut.Amount
if !tokenOutAmount.IsPositive() {
return osmomath.Int{}, errors.New("token amount must be positive")
}
// Chain output of current pool as the input for the next routed pool
// We don't need to validate the denom,
// as CalcOutAmtGivenIn is responsible for ensuring the denom exists in the pool.
tokenIn = sdk.Coin{Denom: routeStep.TokenOutDenom, Amount: tokenOutAmount}
}
return tokenOutAmount, err
}
// RouteExactAmountOut processes a swap along the given route using the swap function corresponding
// to poolID's pool type. This function is responsible for computing the optimal output amount
// for a given input amount when swapping tokens, taking into account the current price of the
// tokens in the pool and any slippage.
// Transaction succeeds if the calculated tokenInAmount of the first pool is less than the defined
// tokenInMaxAmount defined.
func (k Keeper) RouteExactAmountOut(ctx sdk.Context,
sender sdk.AccAddress,
route []types.SwapAmountOutRoute,
tokenInMaxAmount osmomath.Int,
tokenOut sdk.Coin,
) (tokenInAmount osmomath.Int, err error) {
isMultiHopRouted, routeSpreadFactor, sumOfSpreadFactors := false, osmomath.Dec{}, osmomath.Dec{}
// Ensure that provided route is not empty and has valid denom format.
if err := types.SwapAmountOutRoutes(route).Validate(); err != nil {
return osmomath.Int{}, err
}
defer func() {
if r := recover(); r != nil {
tokenInAmount = osmomath.Int{}
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function RouteExactAmountOut failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function RouteExactAmountOut failed due to internal reason: %v", r)
}
}
}()
var insExpected []osmomath.Int
insExpected, err = k.createMultihopExpectedSwapOuts(ctx, route, tokenOut)
if err != nil {
return osmomath.Int{}, err
}
if len(insExpected) == 0 {
return osmomath.Int{}, nil
}
insExpected[0] = tokenInMaxAmount
totalTakerFeesCharged := sdk.Coins{}
denomsInvolvedInRoute := []string{tokenOut.Denom}
// Iterates through each routed pool and executes their respective swaps. Note that all of the work to get the return
// value of this method is done when we calculate insExpected – this for loop primarily serves to execute the actual
// swaps on each pool.
for i, routeStep := range route {
swapModule, pool, err := k.GetPoolModuleAndPool(ctx, routeStep.PoolId)
if err != nil {
return osmomath.Int{}, err
}
_tokenOut := tokenOut
// If there is one pool left in the routeStep, set the expected output of the current swap
// to the estimated input of the final pool.
if i != len(route)-1 {
_tokenOut = sdk.NewCoin(route[i+1].TokenInDenom, insExpected[i+1])
}
// check if pool is active, if not error
if !pool.IsActive(ctx) {
return osmomath.Int{}, types.InactivePoolError{PoolId: pool.GetId()}
}
spreadFactor := pool.GetSpreadFactor(ctx)
// If we determined the routeStep is an osmo multi-hop and both route are incentivized,
// we modify the swap fee accordingly.
if isMultiHopRouted {
spreadFactor = routeSpreadFactor.Mul((spreadFactor.Quo(sumOfSpreadFactors)))
}
curTokenInAmount, swapErr := swapModule.SwapExactAmountOut(ctx, sender, pool, routeStep.TokenInDenom, insExpected[i], _tokenOut, spreadFactor)
if swapErr != nil {
return osmomath.Int{}, swapErr
}
tokenIn := sdk.NewCoin(routeStep.TokenInDenom, curTokenInAmount)
tokenInAfterAddTakerFee, takerFeeCharged, err := k.chargeTakerFee(ctx, tokenIn, _tokenOut.Denom, sender, false)
if err != nil {
return osmomath.Int{}, err
}
// Track volume for volume-splitting incentives
k.trackVolume(ctx, pool.GetId(), sdk.NewCoin(routeStep.TokenInDenom, tokenIn.Amount))
// Sets the final amount of tokens that need to be input into the first pool. Even though this is the final return value for the
// whole method and will not change after the first iteration, we still iterate through the rest of the pools to execute their respective
// swaps.
if i == 0 {
tokenInAmount = tokenInAfterAddTakerFee.Amount
}
// Track taker fees charged
totalTakerFeesCharged = totalTakerFeesCharged.Add(takerFeeCharged)
// Add the token in denom to the denoms involved in the route, IFF it is not already in the slice
if !osmoutils.Contains(denomsInvolvedInRoute, routeStep.TokenInDenom) {
denomsInvolvedInRoute = append(denomsInvolvedInRoute, routeStep.TokenInDenom)
}
}
// Run taker fee skim logic
err = k.TakerFeeSkim(ctx, denomsInvolvedInRoute, totalTakerFeesCharged)
if err != nil {
return osmomath.Int{}, err
}
return tokenInAmount, nil
}
// SplitRouteExactAmountOut route the swap across multiple multihop paths
// to get the desired token in. This is useful for achieving the most optimal execution. However, note that the responsibility
// of determining the optimal split is left to the client. This method simply route the swap across the given route.
// The route must end with the same token out and begin with the same token in.
//
// It performs the price impact protection check on the combination of tokens in from all multihop paths. The given tokenInMaxAmount
// is used for comparison.
//
// Returns error if:
// - route are empty
// - route contain duplicate multihop paths
// - last token out denom is not the same for all multihop paths in routeStep
// - one of the multihop swaps fails for internal reasons
// - final token out computed is not positive
// - final token out computed is smaller than tokenInMaxAmount
func (k Keeper) SplitRouteExactAmountOut(
ctx sdk.Context,
sender sdk.AccAddress,
route []types.SwapAmountOutSplitRoute,
tokenOutDenom string,
tokenInMaxAmount osmomath.Int,
) (osmomath.Int, error) {
if err := types.ValidateSwapAmountOutSplitRoute(route); err != nil {
return osmomath.Int{}, err
}
var (
// We start the multihop min amount as int max value
// that is defined as one under the max bit length of osmomath.Int
// which is 256. This is to ensure that we utilize price impact protection
// on the total of in amount from all multihop paths.
multihopStartTokenInMaxAmount = intMaxValue
totalInAmount = osmomath.ZeroInt()
)
for _, multihopRoute := range route {
tokenOutAmount, err := k.RouteExactAmountOut(
ctx,
sender,
types.SwapAmountOutRoutes(multihopRoute.Pools),
multihopStartTokenInMaxAmount,
sdk.NewCoin(tokenOutDenom, multihopRoute.TokenOutAmount))
if err != nil {
return osmomath.Int{}, err
}
totalInAmount = totalInAmount.Add(tokenOutAmount)
}
if !totalInAmount.IsPositive() {
return osmomath.Int{}, types.FinalAmountIsNotPositiveError{IsAmountOut: false, Amount: totalInAmount}
}
if totalInAmount.GT(tokenInMaxAmount) {
return osmomath.Int{}, types.PriceImpactProtectionExactOutError{Actual: totalInAmount, MaxAmount: tokenInMaxAmount}
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.TypeMsgSplitRouteSwapExactAmountOut,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
sdk.NewAttribute(types.AttributeKeyTokensOut, totalInAmount.String()),
),
})
return totalInAmount, nil
}
func (k Keeper) RouteGetPoolDenoms(
ctx sdk.Context,
poolId uint64,
) (denoms []string, err error) {
swapModule, err := k.GetPoolModule(ctx, poolId)
if err != nil {
return []string{}, err
}
denoms, err = swapModule.GetPoolDenoms(ctx, poolId)
if err != nil {
return []string{}, err
}
return denoms, nil
}
func (k Keeper) RouteCalculateSpotPrice(
ctx sdk.Context,
poolId uint64,
quoteAssetDenom string,
baseAssetDenom string,
) (price osmomath.BigDec, err error) {
swapModule, err := k.GetPoolModule(ctx, poolId)
if err != nil {
return osmomath.BigDec{}, err
}
price, err = swapModule.CalculateSpotPrice(ctx, poolId, quoteAssetDenom, baseAssetDenom)
if err != nil {
return osmomath.BigDec{}, err
}
return price, nil
}
func (k Keeper) MultihopEstimateInGivenExactAmountOut(
ctx sdk.Context,
route []types.SwapAmountOutRoute,
tokenOut sdk.Coin,
) (tokenInAmount osmomath.Int, err error) {
var insExpected []osmomath.Int
// recover from panic
defer func() {
if r := recover(); r != nil {
insExpected = []osmomath.Int{}
if isErr, d := osmoutils.IsOutOfGasError(r); isErr {
err = fmt.Errorf("function MultihopEstimateInGivenExactAmountOut failed due to lack of gas: %v", d)
} else {
err = fmt.Errorf("function MultihopEstimateInGivenExactAmountOut failed due to internal reason: %v", r)
}
}
}()
routeStep := types.SwapAmountOutRoutes(route)
if err := routeStep.Validate(); err != nil {
return osmomath.Int{}, err
}
// Determine what the estimated input would be for each pool along the multi-hop route
insExpected, err = k.createMultihopExpectedSwapOuts(ctx, route, tokenOut)
if err != nil {
return osmomath.Int{}, err
}
if len(insExpected) == 0 {
return osmomath.Int{}, nil
}
return insExpected[0], nil
}
func (k Keeper) GetPool(
ctx sdk.Context,
poolId uint64,
) (types.PoolI, error) {
_, pool, err := k.GetPoolModuleAndPool(ctx, poolId)
return pool, err
}
// AllPools returns all pools sorted by their ids
// from every pool module registered in the
// pool manager keeper.
func (k Keeper) AllPools(
ctx sdk.Context,
) ([]types.PoolI, error) {
// Allocate the slice with the exact capacity to avoid reallocations.
poolCount := k.GetNextPoolId(ctx)
sortedPools := make([]types.PoolI, 0, poolCount)
for _, poolModule := range k.poolModules {
currentModulePools, err := poolModule.GetPools(ctx)
if err != nil {
return nil, err
}
sortedPools = osmoutils.MergeSlices(sortedPools, currentModulePools, lessPoolIFunc)
}
return sortedPools, nil
}
// ListPoolsByDenom returns all pools by denom sorted by their ids
// from every pool module registered in the
// pool manager keeper.
// N.B. It is possible for incorrectly implemented pools to be skipped
func (k Keeper) ListPoolsByDenom(
ctx sdk.Context,
denom string,
) ([]types.PoolI, error) {
var sortedPools []types.PoolI
for _, poolModule := range k.poolModules {
currentModulePools, err := poolModule.GetPools(ctx)
if err != nil {
return nil, err
}
var poolsByDenom []types.PoolI
for _, pool := range currentModulePools {
// If the pool is incorrectly implemented and we can't get the PoolDenoms
// skip the pool.
poolDenoms, err := poolModule.GetPoolDenoms(ctx, pool.GetId())
if err != nil {
ctx.Logger().Debug(fmt.Sprintf("Error getting pool denoms for pool %d: %s", pool.GetId(), err.Error()))
continue
}
if osmoutils.Contains(poolDenoms, denom) {
poolsByDenom = append(poolsByDenom, pool)
}
}
sortedPools = osmoutils.MergeSlices(sortedPools, poolsByDenom, lessPoolIFunc)
}
return sortedPools, nil
}
// createMultihopExpectedSwapOuts defines the output denom and output amount for the last pool in
// the routeStep of pools the caller is intending to hop through in a fixed-output multihop tx. It estimates the input
// amount for this last pool and then chains that input as the output of the previous pool in the routeStep, repeating
// until the first pool is reached. It returns an array of inputs, each of which correspond to a pool ID in the
// routeStep of pools for the original multihop transaction.
func (k Keeper) createMultihopExpectedSwapOuts(
ctx sdk.Context,
route []types.SwapAmountOutRoute,
tokenOut sdk.Coin,
) ([]osmomath.Int, error) {
insExpected := make([]osmomath.Int, len(route))
for i := len(route) - 1; i >= 0; i-- {
routeStep := route[i]
swapModule, poolI, err := k.GetPoolModuleAndPool(ctx, routeStep.PoolId)
if err != nil {
return nil, err
}
spreadFactor := poolI.GetSpreadFactor(ctx)
takerFee, err := k.GetTradingPairTakerFee(ctx, routeStep.TokenInDenom, tokenOut.Denom)
if err != nil {
return nil, err
}
tokenIn, err := swapModule.CalcInAmtGivenOut(ctx, poolI, tokenOut, routeStep.TokenInDenom, spreadFactor)
if err != nil {
return nil, err
}
tokenInAfterTakerFee, _ := CalcTakerFeeExactOut(tokenIn, takerFee)
insExpected[i] = tokenInAfterTakerFee.Amount
tokenOut = tokenInAfterTakerFee
}
return insExpected, nil
}
// GetTotalPoolLiquidity gets the total liquidity for a given poolId.
func (k Keeper) GetTotalPoolLiquidity(ctx sdk.Context, poolId uint64) (sdk.Coins, error) {
swapModule, err := k.GetPoolModule(ctx, poolId)
if err != nil {
return nil, err
}
coins, err := swapModule.GetTotalPoolLiquidity(ctx, poolId)
if err != nil {
return coins, err
}
return coins, nil
}
// TotalLiquidity gets the total liquidity across all pools.
func (k Keeper) TotalLiquidity(ctx sdk.Context) (sdk.Coins, error) {
totalGammLiquidity, err := k.gammKeeper.GetTotalLiquidity(ctx)
if err != nil {
return nil, err
}
totalConcentratedLiquidity, err := k.concentratedKeeper.GetTotalLiquidity(ctx)
if err != nil {
return nil, err
}
totalCosmwasmLiquidity, err := k.cosmwasmpoolKeeper.GetTotalLiquidity(ctx)
if err != nil {
return nil, err
}
totalLiquidity := totalGammLiquidity.Add(totalConcentratedLiquidity...).Add(totalCosmwasmLiquidity...)
return totalLiquidity, nil
}
// nolint: unused
// trackVolume converts the input token into OSMO units and adds it to the global tracked volume for the given pool ID.
// Fails quietly if an OSMO paired pool cannot be found, although this should only happen in rare scenarios where OSMO is
// removed as a base denom from the protorev module (which this function relies on).
//
// CONTRACT: `volumeGenerated` corresponds to one of the denoms in the pool
// CONTRACT: pool with `poolId` exists
func (k Keeper) trackVolume(ctx sdk.Context, poolId uint64, volumeGenerated sdk.Coin) {
// If the denom is already denominated in uosmo, we can just use it directly
OSMO, err := k.stakingKeeper.BondDenom(ctx)
if err != nil {
panic(err)
}
if volumeGenerated.Denom == OSMO {
k.addVolume(ctx, poolId, volumeGenerated)
return
}
// Get the most liquid OSMO-paired pool with `volumeGenerated`'s denom using `GetPoolForDenomPair`
osmoPairedPoolId, err := k.protorevKeeper.GetPoolForDenomPair(ctx, OSMO, volumeGenerated.Denom)
// If no pool is found, fail quietly.
//
// This is a rare scenario that should only happen if OSMO-paired pools are all removed from the protorev module.
// Since this removal scenario is all-or-nothing, this is functionally equiavalent to freezing the tracked volume amounts
// where they were prior to the disabling, which seems an appropriate response.
//
// This branch would also get triggered in the case where there is a token that has no OSMO-paired pool on the entire chain.
// We simply do not track volume in these cases. Importantly, volume splitting gauge logic should prevent a gauge from being
// created for such a pool that includes such a token, although it is okay to no-op in these cases regardless.
if err != nil {
return
}
// Since we want to ultimately multiply the volume by this spot price, we want to quote OSMO in terms of the input token.
// This is so that once we multiply the volume by the spot price, we get the volume in units of OSMO.
osmoPerInputToken, err := k.RouteCalculateSpotPrice(ctx, osmoPairedPoolId, OSMO, volumeGenerated.Denom)
// We expect that if a pool is found, there should always be an available spot price as well.
// That being said, if there is an error finding the spot price, we fail quietly and leave tracked volume unchanged.
// This is because we do not want to escalate an issue with finding spot price to locking all swaps involving the given asset.
if err != nil {
return
}
// Multiply `volumeGenerated.Amount.ToDec()` by this spot price.
// While rounding does not particularly matter here, we round down to ensure that we do not overcount volume.
volumeInOsmo := osmomath.BigDecFromSDKInt(volumeGenerated.Amount).Mul(osmoPerInputToken).Dec().TruncateInt()
// Add this new volume to the global tracked volume for the pool ID
k.addVolume(ctx, poolId, sdk.NewCoin(OSMO, volumeInOsmo))
}
// addVolume adds the given volume to the global tracked volume for the given pool ID.
func (k Keeper) addVolume(ctx sdk.Context, poolId uint64, volumeGenerated sdk.Coin) {
// Get the current volume for the pool ID
currentTotalVolume := k.GetTotalVolumeForPool(ctx, poolId)
// Add newly generated volume to existing volume and set updated volume in state
newTotalVolume := currentTotalVolume.Add(volumeGenerated)
k.SetVolume(ctx, poolId, newTotalVolume)
}
// SetVolume sets the given volume to the global tracked volume for the given pool ID.
// Note that this function is exported for cross-module testing purposes and should not be
// called directly from other modules.
func (k Keeper) SetVolume(ctx sdk.Context, poolId uint64, totalVolume sdk.Coins) {
storedVolume := types.TrackedVolume{Amount: totalVolume}
osmoutils.MustSet(ctx.KVStore(k.storeKey), types.KeyPoolVolume(poolId), &storedVolume)
}
// GetTotalVolumeForPool gets the total historical volume in all supported denominations for a given pool ID.
func (k Keeper) GetTotalVolumeForPool(ctx sdk.Context, poolId uint64) sdk.Coins {
var currentTrackedVolume types.TrackedVolume
volumeFound, err := osmoutils.Get(ctx.KVStore(k.storeKey), types.KeyPoolVolume(poolId), ¤tTrackedVolume)
if err != nil {
// We can only encounter an error if a database or serialization errors occurs, so we panic here.
// Normally this would be handled by `osmoutils.MustGet`, but since we want to specifically use `osmoutils.Get`,
// we also have to manually panic here.
panic(err)
}
// If no volume was found, we treat the existing volume as 0.
// While we can technically require volume to exist, we would need to store empty coins in state for each pool (past and present),
// which is a high storage cost to pay for a weak guardrail.
currentTotalVolume := sdk.NewCoins()
if volumeFound {
currentTotalVolume = currentTrackedVolume.Amount
}
return currentTotalVolume
}
// GetOsmoVolumeForPool gets the total OSMO-denominated historical volume for a given pool ID.
func (k Keeper) GetOsmoVolumeForPool(ctx sdk.Context, poolId uint64) osmomath.Int {
totalVolume := k.GetTotalVolumeForPool(ctx, poolId)
OSMO, err := k.stakingKeeper.BondDenom(ctx)
if err != nil {
panic(err)
}
return totalVolume.AmountOf(OSMO)
}
// EstimateTradeBasedOnPriceImpactBalancerPool estimates a trade based on price impact for a balancer pool type.
// For a balancer pool if an amount entered is greater than the total pool liquidity the trade estimated would be
// the full liquidity of the other token. If the amount is small it would return a close 1:1 trade of the
// smallest units.
func (k Keeper) EstimateTradeBasedOnPriceImpactBalancerPool(
ctx sdk.Context,
req queryproto.EstimateTradeBasedOnPriceImpactRequest,
spotPrice, adjustedMaxPriceImpact osmomath.Dec,
swapModule types.PoolModuleI,
poolI types.PoolI,
) (*queryproto.EstimateTradeBasedOnPriceImpactResponse, error) {
tokenOut, err := swapModule.CalcOutAmtGivenIn(ctx, poolI, req.FromCoin, req.ToCoinDenom, types.ZeroDec)
if err != nil {
if errors.Is(err, gammtypes.ErrInvalidMathApprox) {
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: sdk.NewCoin(req.FromCoin.Denom, osmomath.ZeroInt()),
OutputCoin: sdk.NewCoin(req.ToCoinDenom, osmomath.ZeroInt()),
}, nil
}
return nil, status.Error(codes.Internal, err.Error())
}
if tokenOut.IsZero() {
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: sdk.NewCoin(req.FromCoin.Denom, osmomath.ZeroInt()),
OutputCoin: sdk.NewCoin(req.ToCoinDenom, osmomath.ZeroInt()),
}, nil
}
// Validate if the trade as is respects the price impact, if it does re-estimate it with a swap fee and return
// the result.
priceDeviation := calculatePriceDeviation(req.FromCoin, tokenOut, spotPrice)
if priceDeviation.LTE(adjustedMaxPriceImpact) {
tokenOut, err = swapModule.CalcOutAmtGivenIn(
ctx, poolI, req.FromCoin, req.ToCoinDenom, poolI.GetSpreadFactor(ctx),
)
if err != nil {
if errors.Is(err, gammtypes.ErrInvalidMathApprox) {
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: sdk.NewCoin(req.FromCoin.Denom, osmomath.ZeroInt()),
OutputCoin: sdk.NewCoin(req.ToCoinDenom, osmomath.ZeroInt()),
}, nil
}
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: req.FromCoin,
OutputCoin: tokenOut,
}, nil
}
// Define low and high amount to search between. Start from 1 and req.FromCoin.Amount as initial range.
lowAmount := osmomath.OneInt()
highAmount := req.FromCoin.Amount
currFromCoin := req.FromCoin
// Repeat the above process using the binary search algorithm which iteratively narrows down the optimal trade
// amount within a given maximum price impact range.
//
// The algorithm iteratively:
// 1) Calculates the middle amount of the current range ('midAmount').
// 2) Tries to execute a trade using this middle amount.
// 3) Calculates the resulting price deviation between the spot price and the
// price of the tried trade.
//
// Depending on whether the price deviation is within the allowed 'adjustedMaxPriceImpact',
// the algorithm adjusts the 'lowAmount' or 'highAmount' for the next iteration.
//
// This process continues until 'lowAmount' is greater than 'highAmount', at which
// point the optimal amount respecting the max price impact will have been found.
for lowAmount.LTE(highAmount) {
// Calculate currFromCoin as the new middle amount to try trade.
midAmount := lowAmount.Add(highAmount).Quo(osmomath.NewInt(2))
currFromCoin = sdk.NewCoin(req.FromCoin.Denom, midAmount)
tokenOut, err := swapModule.CalcOutAmtGivenIn(
ctx, poolI, currFromCoin, req.ToCoinDenom, types.ZeroDec,
)
if err != nil {
if errors.Is(err, gammtypes.ErrInvalidMathApprox) {
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: sdk.NewCoin(req.FromCoin.Denom, osmomath.ZeroInt()),
OutputCoin: sdk.NewCoin(req.ToCoinDenom, osmomath.ZeroInt()),
}, nil
}
return nil, status.Error(codes.Internal, err.Error())
}
if tokenOut.IsZero() {
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: sdk.NewCoin(req.FromCoin.Denom, osmomath.ZeroInt()),
OutputCoin: sdk.NewCoin(req.ToCoinDenom, osmomath.ZeroInt()),
}, nil
}
priceDeviation := calculatePriceDeviation(currFromCoin, tokenOut, spotPrice)
if priceDeviation.LTE(adjustedMaxPriceImpact) {
lowAmount = midAmount.Add(osmomath.OneInt())
} else {
highAmount = midAmount.Sub(osmomath.OneInt())
}
}
// highAmount is 0 it means the loop has iterated to the end without finding a viable trade that respects
// the price impact.
if highAmount.IsZero() {
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: sdk.NewCoin(req.FromCoin.Denom, osmomath.ZeroInt()),
OutputCoin: sdk.NewCoin(req.ToCoinDenom, osmomath.ZeroInt()),
}, nil
}
tokenOut, err = swapModule.CalcOutAmtGivenIn(
ctx, poolI, currFromCoin, req.ToCoinDenom, poolI.GetSpreadFactor(ctx),
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: currFromCoin,
OutputCoin: tokenOut,
}, nil
}
// EstimateTradeBasedOnPriceImpactStableSwapPool estimates a trade based on price impact for a stableswap pool type.
// For a stableswap pool if an amount entered is greater than the total pool liquidity the trade estimated would
// `panic`. If the amount is small it would return an error, in the case of a `panic` we should ignore it
// and keep attempting lower input amounts while if it's a normal error we should return an empty trade.
func (k Keeper) EstimateTradeBasedOnPriceImpactStableSwapPool(
ctx sdk.Context,
req queryproto.EstimateTradeBasedOnPriceImpactRequest,
spotPrice, adjustedMaxPriceImpact osmomath.Dec,
swapModule types.PoolModuleI,
poolI types.PoolI,
) (*queryproto.EstimateTradeBasedOnPriceImpactResponse, error) {
var tokenOut sdk.Coin
var err error
err = osmoutils.ApplyFuncIfNoError(ctx, func(ctx sdk.Context) error {
tokenOut, err = swapModule.CalcOutAmtGivenIn(ctx, poolI, req.FromCoin, req.ToCoinDenom, types.ZeroDec)
return err
})
// Find out if the error is because the amount is too large or too little. The calculation should error
// if the amount is too small, and it should panic if the amount is too large. If the amount is too large
// we want to continue to iterate to find attempt to find a smaller value. StableSwap panics on amounts that
// are too large due to the maths involved, while Balancer pool types do not.
if err != nil && !strings.Contains(err.Error(), "panic") {
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: sdk.NewCoin(req.FromCoin.Denom, osmomath.ZeroInt()),
OutputCoin: sdk.NewCoin(req.ToCoinDenom, osmomath.ZeroInt()),
}, nil
} else if err == nil {
// Validate if the trade as is respects the price impact, if it does re-estimate it with a swap fee and return
// the result.
priceDeviation := calculatePriceDeviation(req.FromCoin, tokenOut, spotPrice)
if priceDeviation.LTE(adjustedMaxPriceImpact) {
tokenOut, err = swapModule.CalcOutAmtGivenIn(
ctx, poolI, req.FromCoin, req.ToCoinDenom, poolI.GetSpreadFactor(ctx),
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &queryproto.EstimateTradeBasedOnPriceImpactResponse{
InputCoin: req.FromCoin,
OutputCoin: tokenOut,
}, nil
}
}
// Define low and high amount to search between. Start from 1 and req.FromCoin.Amount as initial range.
lowAmount := osmomath.OneInt()
highAmount := req.FromCoin.Amount
currFromCoin := req.FromCoin
// Repeat the above process using the binary search algorithm which iteratively narrows down the optimal trade
// amount within a given maximum price impact range.
//
// The algorithm iteratively:
// 1) Calculates the middle amount of the current range ('midAmount').
// 2) Tries to execute a trade using this middle amount.
// 3) Calculates the resulting price deviation between the spot price and the
// price of the tried trade.
//
// Depending on whether the price deviation is within the allowed 'adjustedMaxPriceImpact',
// the algorithm adjusts the 'lowAmount' or 'highAmount' for the next iteration.
//
// This process continues until 'lowAmount' is greater than 'highAmount', at which
// point the optimal amount respecting the max price impact will have been found.