Skip to content

Commit

Permalink
refactor(SQS): convert poolmanager taker fee calc methods to functions (
Browse files Browse the repository at this point in the history
#6821) (#6864)

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 #6785, these are used in SQS calculations directly.

(cherry picked from commit 17aab08)

Co-authored-by: Roman <roman@osmosis.team>
  • Loading branch information
mergify[bot] and p0mvn authored Nov 11, 2023
1 parent 7b7ce67 commit 6d9538d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 16 deletions.
8 changes: 0 additions & 8 deletions x/poolmanager/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions x/poolmanager/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions x/poolmanager/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions x/poolmanager/taker_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -176,15 +176,15 @@ 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))

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))
Expand Down

0 comments on commit 6d9538d

Please sign in to comment.