/
router.go
446 lines (381 loc) · 14 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
package poolmanager
import (
"errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
appparams "github.com/osmosis-labs/osmosis/v14/app/params"
"github.com/osmosis-labs/osmosis/v14/x/poolmanager/types"
)
// RouteExactAmountIn defines the input denom and input amount for the first pool,
// the output of the first pool is chained as the input for the next routed pool
// transaction succeeds when final amount out is greater than tokenOutMinAmount defined.
func (k Keeper) RouteExactAmountIn(
ctx sdk.Context,
sender sdk.AccAddress,
routes []types.SwapAmountInRoute,
tokenIn sdk.Coin,
tokenOutMinAmount sdk.Int,
) (tokenOutAmount sdk.Int, err error) {
var (
isMultiHopRouted bool
routeSwapFee sdk.Dec
sumOfSwapFees sdk.Dec
)
route := types.SwapAmountInRoutes(routes)
if err := route.Validate(); err != nil {
return sdk.Int{}, err
}
// In this loop, we check if:
// - the route is of length 2
// - route 1 and route 2 don't trade via the same pool
// - route 1 contains uosmo
// - both route 1 and route 2 are incentivized pools
//
// If all of the above is true, then we collect the additive and max fee between the
// two pools to later calculate the following:
// total_swap_fee = total_swap_fee = max(swapfee1, swapfee2)
// fee_per_pool = total_swap_fee * ((pool_fee) / (swapfee1 + swapfee2))
if k.isOsmoRoutedMultihop(ctx, route, routes[0].TokenOutDenom, tokenIn.Denom) {
isMultiHopRouted = true
routeSwapFee, sumOfSwapFees, err = k.getOsmoRoutedMultihopTotalSwapFee(ctx, route)
if err != nil {
return sdk.Int{}, err
}
}
for i, route := range routes {
// 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 := sdk.NewInt(1)
if len(routes)-1 == i {
_outMinAmount = tokenOutMinAmount
}
swapModule, err := k.GetPoolModule(ctx, route.PoolId)
if err != nil {
return sdk.Int{}, err
}
// Execute the expected swap on the current routed pool
pool, poolErr := swapModule.GetPool(ctx, route.PoolId)
if poolErr != nil {
return sdk.Int{}, poolErr
}
// check if pool is active, if not error
if !pool.IsActive(ctx) {
return sdk.Int{}, fmt.Errorf("pool %d is not active", pool.GetId())
}
swapFee := pool.GetSwapFee(ctx)
// If we determined the route is an osmo multi-hop and both routes are incentivized,
// we modify the swap fee accordingly.
if isMultiHopRouted {
swapFee = routeSwapFee.Mul((swapFee.Quo(sumOfSwapFees)))
}
tokenOutAmount, err = swapModule.SwapExactAmountIn(ctx, sender, pool, tokenIn, route.TokenOutDenom, _outMinAmount, swapFee)
if err != nil {
return sdk.Int{}, err
}
// Chain output of current pool as the input for the next routed pool
tokenIn = sdk.NewCoin(route.TokenOutDenom, tokenOutAmount)
}
return tokenOutAmount, 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 sdk.Int,
) (tokenOutAmount sdk.Int, err error) {
swapModule, err := k.GetPoolModule(ctx, poolId)
if err != nil {
return sdk.Int{}, err
}
pool, poolErr := swapModule.GetPool(ctx, poolId)
if poolErr != nil {
return sdk.Int{}, poolErr
}
// check if pool is active, if not error
if !pool.IsActive(ctx) {
return sdk.Int{}, fmt.Errorf("pool %d is not active", pool.GetId())
}
swapFee := pool.GetSwapFee(ctx)
tokenOutAmount, err = swapModule.SwapExactAmountIn(ctx, sender, pool, tokenIn, tokenOutDenom, tokenOutMinAmount, swapFee)
if err != nil {
return sdk.Int{}, err
}
return tokenOutAmount, nil
}
func (k Keeper) MultihopEstimateOutGivenExactAmountIn(
ctx sdk.Context,
routes []types.SwapAmountInRoute,
tokenIn sdk.Coin,
) (tokenOutAmount sdk.Int, err error) {
var (
isMultiHopRouted bool
routeSwapFee sdk.Dec
sumOfSwapFees sdk.Dec
)
route := types.SwapAmountInRoutes(routes)
if err := route.Validate(); err != nil {
return sdk.Int{}, err
}
if k.isOsmoRoutedMultihop(ctx, route, routes[0].TokenOutDenom, tokenIn.Denom) {
isMultiHopRouted = true
routeSwapFee, sumOfSwapFees, err = k.getOsmoRoutedMultihopTotalSwapFee(ctx, route)
if err != nil {
return sdk.Int{}, err
}
}
for _, route := range routes {
swapModule, err := k.GetPoolModule(ctx, route.PoolId)
if err != nil {
return sdk.Int{}, err
}
// Execute the expected swap on the current routed pool
poolI, poolErr := swapModule.GetPool(ctx, route.PoolId)
if poolErr != nil {
return sdk.Int{}, poolErr
}
swapFee := poolI.GetSwapFee(ctx)
// If we determined the route is an osmo multi-hop and both routes are incentivized,
// we modify the swap fee accordingly.
if isMultiHopRouted {
swapFee = routeSwapFee.Mul((swapFee.Quo(sumOfSwapFees)))
}
tokenOut, err := swapModule.CalcOutAmtGivenIn(ctx, poolI, tokenIn, route.TokenOutDenom, swapFee)
if err != nil {
return sdk.Int{}, err
}
tokenOutAmount = tokenOut.Amount
if !tokenOutAmount.IsPositive() {
return sdk.Int{}, errors.New("token amount must be positive")
}
// Chain output of current pool as the input for the next routed pool
tokenIn = sdk.NewCoin(route.TokenOutDenom, tokenOutAmount)
}
return tokenOutAmount, err
}
// MultihopSwapExactAmountOut defines the output denom and output amount for the last pool.
// Calculation starts by providing the tokenOutAmount of the final pool to calculate the required tokenInAmount
// the calculated tokenInAmount is used as defined tokenOutAmount of the previous pool, calculating in reverse order of the swap
// 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,
routes []types.SwapAmountOutRoute,
tokenInMaxAmount sdk.Int,
tokenOut sdk.Coin,
) (tokenInAmount sdk.Int, err error) {
isMultiHopRouted, routeSwapFee, sumOfSwapFees := false, sdk.Dec{}, sdk.Dec{}
route := types.SwapAmountOutRoutes(routes)
if err := route.Validate(); err != nil {
return sdk.Int{}, err
}
// in this loop, we check if:
// - the route is of length 2
// - route 1 and route 2 don't trade via the same pool
// - route 1 contains uosmo
// - both route 1 and route 2 are incentivized pools
// if all of the above is true, then we collect the additive and max fee between the two pools to later calculate the following:
// total_swap_fee = total_swap_fee = max(swapfee1, swapfee2)
// fee_per_pool = total_swap_fee * ((pool_fee) / (swapfee1 + swapfee2))
if k.isOsmoRoutedMultihop(ctx, route, routes[0].TokenInDenom, tokenOut.Denom) {
isMultiHopRouted = true
routeSwapFee, sumOfSwapFees, err = k.getOsmoRoutedMultihopTotalSwapFee(ctx, route)
if err != nil {
return sdk.Int{}, err
}
}
// Determine what the estimated input would be for each pool along the multi-hop route
// if we determined the route is an osmo multi-hop and both routes are incentivized,
// we utilize a separate function that calculates the discounted swap fees
var insExpected []sdk.Int
if isMultiHopRouted {
insExpected, err = k.createOsmoMultihopExpectedSwapOuts(ctx, routes, tokenOut, routeSwapFee, sumOfSwapFees)
} else {
insExpected, err = k.createMultihopExpectedSwapOuts(ctx, routes, tokenOut)
}
if err != nil {
return sdk.Int{}, err
}
if len(insExpected) == 0 {
return sdk.Int{}, nil
}
insExpected[0] = tokenInMaxAmount
// 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, route := range routes {
swapModule, err := k.GetPoolModule(ctx, route.PoolId)
if err != nil {
return sdk.Int{}, err
}
_tokenOut := tokenOut
// If there is one pool left in the route, set the expected output of the current swap
// to the estimated input of the final pool.
if i != len(routes)-1 {
_tokenOut = sdk.NewCoin(routes[i+1].TokenInDenom, insExpected[i+1])
}
// Execute the expected swap on the current routed pool
pool, poolErr := swapModule.GetPool(ctx, route.PoolId)
if poolErr != nil {
return sdk.Int{}, poolErr
}
// check if pool is active, if not error
if !pool.IsActive(ctx) {
return sdk.Int{}, fmt.Errorf("pool %d is not active", pool.GetId())
}
swapFee := pool.GetSwapFee(ctx)
if isMultiHopRouted {
swapFee = routeSwapFee.Mul((swapFee.Quo(sumOfSwapFees)))
}
_tokenInAmount, swapErr := swapModule.SwapExactAmountOut(ctx, sender, pool, route.TokenInDenom, insExpected[i], _tokenOut, swapFee)
if swapErr != nil {
return sdk.Int{}, swapErr
}
// 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 = _tokenInAmount
}
}
return tokenInAmount, nil
}
func (k Keeper) MultihopEstimateInGivenExactAmountOut(
ctx sdk.Context,
routes []types.SwapAmountOutRoute,
tokenOut sdk.Coin,
) (tokenInAmount sdk.Int, err error) {
isMultiHopRouted, routeSwapFee, sumOfSwapFees := false, sdk.Dec{}, sdk.Dec{}
route := types.SwapAmountOutRoutes(routes)
if err := route.Validate(); err != nil {
return sdk.Int{}, err
}
if k.isOsmoRoutedMultihop(ctx, route, routes[0].TokenInDenom, tokenOut.Denom) {
isMultiHopRouted = true
routeSwapFee, sumOfSwapFees, err = k.getOsmoRoutedMultihopTotalSwapFee(ctx, route)
if err != nil {
return sdk.Int{}, err
}
}
// Determine what the estimated input would be for each pool along the multi-hop route
// if we determined the route is an osmo multi-hop and both routes are incentivized,
// we utilize a separate function that calculates the discounted swap fees
var insExpected []sdk.Int
if isMultiHopRouted {
insExpected, err = k.createOsmoMultihopExpectedSwapOuts(ctx, routes, tokenOut, routeSwapFee, sumOfSwapFees)
} else {
insExpected, err = k.createMultihopExpectedSwapOuts(ctx, routes, tokenOut)
}
if err != nil {
return sdk.Int{}, err
}
if len(insExpected) == 0 {
return sdk.Int{}, nil
}
return insExpected[0], nil
}
func (k Keeper) isOsmoRoutedMultihop(ctx sdk.Context, route types.MultihopRoute, inDenom, outDenom string) (isRouted bool) {
if route.Length() != 2 {
return false
}
intemediateDenoms := route.IntermediateDenoms()
if len(intemediateDenoms) != 1 || intemediateDenoms[0] != appparams.BaseCoinUnit {
return false
}
if inDenom == outDenom {
return false
}
poolIds := route.PoolIds()
if poolIds[0] == poolIds[1] {
return false
}
route0Incentivized := k.poolIncentivesKeeper.IsPoolIncentivized(ctx, poolIds[0])
route1Incentivized := k.poolIncentivesKeeper.IsPoolIncentivized(ctx, poolIds[1])
return route0Incentivized && route1Incentivized
}
func (k Keeper) getOsmoRoutedMultihopTotalSwapFee(ctx sdk.Context, route types.MultihopRoute) (
totalPathSwapFee sdk.Dec, sumOfSwapFees sdk.Dec, err error,
) {
additiveSwapFee := sdk.ZeroDec()
maxSwapFee := sdk.ZeroDec()
for _, poolId := range route.PoolIds() {
swapModule, err := k.GetPoolModule(ctx, poolId)
if err != nil {
return sdk.Dec{}, sdk.Dec{}, err
}
pool, poolErr := swapModule.GetPool(ctx, poolId)
if poolErr != nil {
return sdk.Dec{}, sdk.Dec{}, poolErr
}
swapFee := pool.GetSwapFee(ctx)
additiveSwapFee = additiveSwapFee.Add(swapFee)
maxSwapFee = sdk.MaxDec(maxSwapFee, swapFee)
}
averageSwapFee := additiveSwapFee.QuoInt64(2)
maxSwapFee = sdk.MaxDec(maxSwapFee, averageSwapFee)
return maxSwapFee, additiveSwapFee, nil
}
// createMultihopExpectedSwapOuts defines the output denom and output amount for the last pool in
// the route 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 route, repeating
// until the first pool is reached. It returns an array of inputs, each of which correspond to a pool ID in the
// route of pools for the original multihop transaction.
// TODO: test this.
func (k Keeper) createMultihopExpectedSwapOuts(
ctx sdk.Context,
routes []types.SwapAmountOutRoute,
tokenOut sdk.Coin,
) ([]sdk.Int, error) {
insExpected := make([]sdk.Int, len(routes))
for i := len(routes) - 1; i >= 0; i-- {
route := routes[i]
swapModule, err := k.GetPoolModule(ctx, route.PoolId)
if err != nil {
return nil, err
}
poolI, err := swapModule.GetPool(ctx, route.PoolId)
if err != nil {
return nil, err
}
tokenIn, err := swapModule.CalcInAmtGivenOut(ctx, poolI, tokenOut, route.TokenInDenom, poolI.GetSwapFee(ctx))
if err != nil {
return nil, err
}
insExpected[i] = tokenIn.Amount
tokenOut = tokenIn
}
return insExpected, nil
}
// createOsmoMultihopExpectedSwapOuts does the same as createMultihopExpectedSwapOuts, however discounts the swap fee
func (k Keeper) createOsmoMultihopExpectedSwapOuts(
ctx sdk.Context,
routes []types.SwapAmountOutRoute,
tokenOut sdk.Coin,
cumulativeRouteSwapFee, sumOfSwapFees sdk.Dec,
) ([]sdk.Int, error) {
insExpected := make([]sdk.Int, len(routes))
for i := len(routes) - 1; i >= 0; i-- {
route := routes[i]
swapModule, err := k.GetPoolModule(ctx, route.PoolId)
if err != nil {
return nil, err
}
poolI, err := swapModule.GetPool(ctx, route.PoolId)
if err != nil {
return nil, err
}
swapFee := poolI.GetSwapFee(ctx)
tokenIn, err := swapModule.CalcInAmtGivenOut(ctx, poolI, tokenOut, route.TokenInDenom, cumulativeRouteSwapFee.Mul((swapFee.Quo(sumOfSwapFees))))
if err != nil {
return nil, err
}
insExpected[i] = tokenIn.Amount
tokenOut = tokenIn
}
return insExpected, nil
}