From d182266f1cf226c118629a9787875229cf93475a Mon Sep 17 00:00:00 2001 From: Roman Date: Sun, 5 Nov 2023 18:26:38 -0500 Subject: [PATCH] refactor(SQS): convert poolmanager taker fee calc methods to functions (#6821) Closes: https://app.clickup.com/t/86a1af4tp ## What is the purpose of the change This PR converts some poolmanager taker fee calc methods to functions. In https://github.com/osmosis-labs/osmosis/pull/6785, these are used in SQS calculations directly. (cherry picked from commit 17aab08375429f0cc786a8868ddc9351b288be36) --- x/poolmanager/export_test.go | 8 -------- x/poolmanager/router.go | 4 ++-- x/poolmanager/router_test.go | 4 ++-- x/poolmanager/taker_fee.go | 8 ++++---- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/x/poolmanager/export_test.go b/x/poolmanager/export_test.go index fbbe703cd17..b4fe00e882b 100644 --- a/x/poolmanager/export_test.go +++ b/x/poolmanager/export_test.go @@ -45,14 +45,6 @@ func (k Keeper) CreateMultihopExpectedSwapOuts( return k.createMultihopExpectedSwapOuts(ctx, route, tokenOut) } -func (k Keeper) CalcTakerFeeExactIn(tokenIn sdk.Coin, takerFee osmomath.Dec) (sdk.Coin, sdk.Coin) { - return k.calcTakerFeeExactIn(tokenIn, takerFee) -} - -func (k Keeper) CalcTakerFeeExactOut(tokenOut sdk.Coin, takerFee osmomath.Dec) (sdk.Coin, sdk.Coin) { - return k.calcTakerFeeExactOut(tokenOut, takerFee) -} - func (k Keeper) TrackVolume(ctx sdk.Context, poolId uint64, volumeGenerated sdk.Coin) { k.trackVolume(ctx, poolId, volumeGenerated) } diff --git a/x/poolmanager/router.go b/x/poolmanager/router.go index e13eb8b049e..7563f73dd86 100644 --- a/x/poolmanager/router.go +++ b/x/poolmanager/router.go @@ -258,7 +258,7 @@ func (k Keeper) MultihopEstimateOutGivenExactAmountIn( return osmomath.Int{}, err } - tokenInAfterSubTakerFee, _ := k.calcTakerFeeExactIn(tokenIn, takerFee) + tokenInAfterSubTakerFee, _ := CalcTakerFeeExactIn(tokenIn, takerFee) tokenOut, err := swapModule.CalcOutAmtGivenIn(ctx, poolI, tokenInAfterSubTakerFee, routeStep.TokenOutDenom, spreadFactor) if err != nil { @@ -584,7 +584,7 @@ func (k Keeper) createMultihopExpectedSwapOuts( return nil, err } - tokenInAfterTakerFee, _ := k.calcTakerFeeExactOut(tokenIn, takerFee) + tokenInAfterTakerFee, _ := CalcTakerFeeExactOut(tokenIn, takerFee) insExpected[i] = tokenInAfterTakerFee.Amount tokenOut = tokenInAfterTakerFee diff --git a/x/poolmanager/router_test.go b/x/poolmanager/router_test.go index 8d89dcd1a03..2eb2274ba50 100644 --- a/x/poolmanager/router_test.go +++ b/x/poolmanager/router_test.go @@ -1340,7 +1340,7 @@ func (s *KeeperTestSuite) calcInGivenOutAmountAsSeparateSwaps(routes []types.Swa s.Require().NoError(err) tokenInCoin := sdk.NewCoin(hop.TokenInDenom, tokenInAmt) - tokenInCoinAfterAddTakerFee, _ := s.App.PoolManagerKeeper.CalcTakerFeeExactOut(tokenInCoin, takerFee) + tokenInCoinAfterAddTakerFee, _ := poolmanager.CalcTakerFeeExactOut(tokenInCoin, takerFee) nextTokenOut = tokenInCoinAfterAddTakerFee } @@ -1365,7 +1365,7 @@ func (s *KeeperTestSuite) calcOutGivenInAmountAsSeparatePoolSwaps(routes []types takerFee, err := s.App.PoolManagerKeeper.GetTradingPairTakerFee(cacheCtx, hop.TokenOutDenom, nextTokenIn.Denom) s.Require().NoError(err) - nextTokenInAfterSubTakerFee, _ := s.App.PoolManagerKeeper.CalcTakerFeeExactIn(nextTokenIn, takerFee) + nextTokenInAfterSubTakerFee, _ := poolmanager.CalcTakerFeeExactIn(nextTokenIn, takerFee) // we then do individual swaps until we reach the end of the swap route tokenOut, err := swapModule.SwapExactAmountIn(cacheCtx, s.TestAccs[0], pool, nextTokenInAfterSubTakerFee, hop.TokenOutDenom, osmomath.OneInt(), spreadFactor) diff --git a/x/poolmanager/taker_fee.go b/x/poolmanager/taker_fee.go index bc1ee14be61..dfc15d9056b 100644 --- a/x/poolmanager/taker_fee.go +++ b/x/poolmanager/taker_fee.go @@ -100,9 +100,9 @@ func (k Keeper) chargeTakerFee(ctx sdk.Context, tokenIn sdk.Coin, tokenOutDenom var tokenInAfterTakerFee sdk.Coin var takerFeeCoin sdk.Coin if exactIn { - tokenInAfterTakerFee, takerFeeCoin = k.calcTakerFeeExactIn(tokenIn, takerFee) + tokenInAfterTakerFee, takerFeeCoin = CalcTakerFeeExactIn(tokenIn, takerFee) } else { - tokenInAfterTakerFee, takerFeeCoin = k.calcTakerFeeExactOut(tokenIn, takerFee) + tokenInAfterTakerFee, takerFeeCoin = CalcTakerFeeExactOut(tokenIn, takerFee) } // N.B. We truncate from the community pool calculation, then remove that from the total, and use the remaining for staking rewards. @@ -176,7 +176,7 @@ func (k Keeper) chargeTakerFee(ctx sdk.Context, tokenIn sdk.Coin, tokenOutDenom // Returns remaining amount in to swap, and takerFeeCoins. // returns (1 - takerFee) * tokenIn, takerFee * tokenIn -func (k Keeper) calcTakerFeeExactIn(tokenIn sdk.Coin, takerFee osmomath.Dec) (sdk.Coin, sdk.Coin) { +func CalcTakerFeeExactIn(tokenIn sdk.Coin, takerFee osmomath.Dec) (sdk.Coin, sdk.Coin) { amountInAfterSubTakerFee := tokenIn.Amount.ToLegacyDec().MulTruncate(osmomath.OneDec().Sub(takerFee)) tokenInAfterSubTakerFee := sdk.NewCoin(tokenIn.Denom, amountInAfterSubTakerFee.TruncateInt()) takerFeeCoin := sdk.NewCoin(tokenIn.Denom, tokenIn.Amount.Sub(tokenInAfterSubTakerFee.Amount)) @@ -184,7 +184,7 @@ func (k Keeper) calcTakerFeeExactIn(tokenIn sdk.Coin, takerFee osmomath.Dec) (sd return tokenInAfterSubTakerFee, takerFeeCoin } -func (k Keeper) calcTakerFeeExactOut(tokenIn sdk.Coin, takerFee osmomath.Dec) (sdk.Coin, sdk.Coin) { +func CalcTakerFeeExactOut(tokenIn sdk.Coin, takerFee osmomath.Dec) (sdk.Coin, sdk.Coin) { amountInAfterAddTakerFee := tokenIn.Amount.ToLegacyDec().Quo(osmomath.OneDec().Sub(takerFee)) tokenInAfterAddTakerFee := sdk.NewCoin(tokenIn.Denom, amountInAfterAddTakerFee.Ceil().TruncateInt()) takerFeeCoin := sdk.NewCoin(tokenIn.Denom, tokenInAfterAddTakerFee.Amount.Sub(tokenIn.Amount))