Skip to content

Commit

Permalink
fix: apply swap fee to token in/out amount for each hop in a multi ho…
Browse files Browse the repository at this point in the history
…p scenario (#466)

* fix: fix swap fee issue

* test: fix unit tests
  • Loading branch information
cosmic-vagabond authored Apr 23, 2024
1 parent b34873e commit ffb0c9b
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions x/amm/keeper/keeper_swap_exact_amount_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (k Keeper) SwapExactAmountIn(

// Settles balances between the tx sender and the pool to match the swap that was executed earlier.
// Also emits a swap event and updates related liquidity metrics.
_, err = k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOutCoin, sdk.ZeroDec(), swapFee, weightBalanceBonus)
swapOutFee, err := k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOutCoin, sdk.ZeroDec(), swapFee, weightBalanceBonus)
if err != nil {
return math.Int{}, err
}
Expand All @@ -68,5 +68,5 @@ func (k Keeper) SwapExactAmountIn(
k.TrackSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenOutCoin.Denom, slippageAmount.RoundInt()))

// Subtract swap out fee from the token out amount.
return tokenOutAmount, nil
return tokenOutAmount.Sub(swapOutFee), nil
}
2 changes: 1 addition & 1 deletion x/amm/keeper/keeper_swap_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (suite *KeeperTestSuite) TestSwapExactAmountIn() {
swapFeeOut: sdk.ZeroDec(),
tokenIn: sdk.NewInt64Coin("uusda", 10000),
tokenOutMin: sdk.ZeroInt(),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9802),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9704),
weightBalanceBonus: sdk.ZeroDec(),
isOraclePool: false,
useNewRecipient: false,
Expand Down
6 changes: 4 additions & 2 deletions x/amm/keeper/keeper_swap_exact_amount_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ func (k Keeper) SwapExactAmountOut(
return math.Int{}, errorsmod.Wrapf(types.ErrLimitMaxAmount, "swap requires %s, which is greater than the amount %s", tokenIn, tokenInMaxAmount)
}

_, err = k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOut, swapFee, sdk.ZeroDec(), weightBalanceBonus)
swapOutFee, err := k.UpdatePoolForSwap(ctx, pool, sender, recipient, tokenIn, tokenOut, swapFee, sdk.ZeroDec(), weightBalanceBonus)
if err != nil {
return math.Int{}, err
}

// track slippage
k.TrackSlippage(ctx, pool.PoolId, sdk.NewCoin(tokenIn.Denom, slippageAmount.RoundInt()))
return tokenInAmount, nil

// Subtract swap out fee from the token out amount.
return tokenInAmount.Sub(swapOutFee), nil
}
2 changes: 1 addition & 1 deletion x/amm/keeper/msg_server_swap_by_denom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (suite *KeeperTestSuite) TestMsgServerSwapByDenom() {
swapFee: sdk.NewDecWithPrec(1, 2), // 1%
tokenIn: sdk.NewInt64Coin(ptypes.Elys, 10000),
tokenOutMin: sdk.ZeroInt(),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9802),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9704),
expSenderBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 990000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1009704)},
expPass: true,
},
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/msg_server_swap_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (suite *KeeperTestSuite) TestMsgServerSwapExactAmountIn() {
swapFee: sdk.NewDecWithPrec(1, 2), // 1%
tokenIn: sdk.NewInt64Coin(ptypes.Elys, 10000),
tokenOutMin: sdk.ZeroInt(),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9802),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9704),
swapRoute: []types.SwapAmountInRoute{
{
PoolId: 1,
Expand Down
2 changes: 1 addition & 1 deletion x/amm/keeper/route_exact_amount_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (suite *KeeperTestSuite) TestRouteExactAmountIn() {
swapFeeOut: sdk.ZeroDec(),
tokenIn: sdk.NewInt64Coin(ptypes.Elys, 10000),
tokenOutMin: sdk.ZeroInt(),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9802),
tokenOut: sdk.NewInt64Coin(ptypes.BaseCurrency, 9704),
weightBalanceBonus: sdk.ZeroDec(),
expSenderBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 990000), sdk.NewInt64Coin(ptypes.BaseCurrency, 1009704)},
expPoolBalance: sdk.Coins{sdk.NewInt64Coin(ptypes.Elys, 1010000), sdk.NewInt64Coin(ptypes.BaseCurrency, 990198)},
Expand Down

0 comments on commit ffb0c9b

Please sign in to comment.