Skip to content

Commit

Permalink
fix: apply multi hop swap calc to swap estimation queries
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Apr 17, 2024
1 parent 304f955 commit 7b07b7a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions x/amm/keeper/calc_in_route_spot_price.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ func (k Keeper) CalcInRouteSpotPrice(ctx sdk.Context,
// The final token out denom
var tokenOutDenom string

isMultiHopRouted, routeSwapFee, sumOfSwapFees := false, sdk.Dec{}, sdk.Dec{}

// convert routes []*types.SwapAmountInRoute to []types.SwapAmountInRoute
routesNoPtr := make([]types.SwapAmountInRoute, len(routes))
for i, route := range routes {
routesNoPtr[i] = *route
}

route := types.SwapAmountInRoutes(routesNoPtr)
if err := route.Validate(); err != nil {
return sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), 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 uelys
// - 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.isElysRoutedMultihop(ctx, route, routes[0].TokenOutDenom, tokenIn.Denom) {
isMultiHopRouted = true
var err error
routeSwapFee, sumOfSwapFees, err = k.getElysRoutedMultihopTotalSwapFee(ctx, route)
if err != nil {
return sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), sdk.Coin{}, sdk.ZeroDec(), sdk.ZeroDec(), err
}
}

// Initialize the total discounted swap fee
totalDiscountedSwapFee := sdk.ZeroDec()

Expand All @@ -43,6 +75,12 @@ func (k Keeper) CalcInRouteSpotPrice(ctx sdk.Context,
// Get Pool swap fee
swapFee := pool.GetPoolParams().SwapFee

// If we determined the route is an elys multi-hop and both routes are incentivized,
// we modify the swap fee accordingly.
if isMultiHopRouted && sumOfSwapFees.IsPositive() {
swapFee = routeSwapFee.Mul((swapFee.Quo(sumOfSwapFees)))
}

// Override swap fee if applicable
if overrideSwapFee.IsPositive() {
swapFee = overrideSwapFee
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/route_exact_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (k Keeper) RouteExactAmountIn(

swapFee := pool.GetPoolParams().SwapFee

// If we determined the route is an osmo multi-hop and both routes are incentivized,
// If we determined the route is an elys multi-hop and both routes are incentivized,
// we modify the swap fee accordingly.
if isMultiHopRouted && sumOfSwapFees.IsPositive() {
swapFee = routeSwapFee.Mul((swapFee.Quo(sumOfSwapFees)))
Expand Down

0 comments on commit 7b07b7a

Please sign in to comment.