Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Remove TakerFee from protorev estimation #7562

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* [#7250](https://github.com/osmosis-labs/osmosis/pull/7250) Further filter spam gauges from epoch distribution.
* [#7472](https://github.com/osmosis-labs/osmosis/pull/7472) Refactor TWAP keys to only require a single key format. Significantly lowers TWAP-caused writes
* [#7499](https://github.com/osmosis-labs/osmosis/pull/7499) Slight speed/gas improvements to CL CreatePosition and AddToPosition
* [#7562](https://github.com/osmosis-labs/osmosis/pull/7562) Speedup Protorev estimation logic by removing unnecessary taker fee simulations.

## v23.0.0

Expand Down
33 changes: 27 additions & 6 deletions x/poolmanager/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,27 @@ func (k Keeper) SwapExactAmountInNoTakerFee(
return tokenOutAmount, nil
}

func (k Keeper) MultihopEstimateOutGivenExactAmountInNoTakerFee(
ctx sdk.Context,
route []types.SwapAmountInRoute,
tokenIn sdk.Coin,
) (tokenOutAmount osmomath.Int, err error) {
return k.multihopEstimateOutGivenExactAmountInInternal(ctx, route, tokenIn, false)
}

func (k Keeper) MultihopEstimateOutGivenExactAmountIn(
ctx sdk.Context,
route []types.SwapAmountInRoute,
tokenIn sdk.Coin,
) (tokenOutAmount osmomath.Int, err error) {
return k.multihopEstimateOutGivenExactAmountInInternal(ctx, route, tokenIn, true)
}

func (k Keeper) multihopEstimateOutGivenExactAmountInInternal(
ctx sdk.Context,
route []types.SwapAmountInRoute,
tokenIn sdk.Coin,
applyTakerFee bool,
) (tokenOutAmount osmomath.Int, err error) {
// recover from panic
defer func() {
Expand All @@ -254,14 +271,18 @@ func (k Keeper) MultihopEstimateOutGivenExactAmountIn(

spreadFactor := poolI.GetSpreadFactor(ctx)

takerFee, err := k.GetTradingPairTakerFee(ctx, routeStep.TokenOutDenom, tokenIn.Denom)
if err != nil {
return osmomath.Int{}, err
}
actualTokenIn := tokenIn
// apply taker fee if applicable
if applyTakerFee {
takerFee, err := k.GetTradingPairTakerFee(ctx, routeStep.TokenOutDenom, tokenIn.Denom)
if err != nil {
return osmomath.Int{}, err
}

tokenInAfterSubTakerFee, _ := CalcTakerFeeExactIn(tokenIn, takerFee)
actualTokenIn, _ = CalcTakerFeeExactIn(tokenIn, takerFee)
}

tokenOut, err := swapModule.CalcOutAmtGivenIn(ctx, poolI, tokenInAfterSubTakerFee, routeStep.TokenOutDenom, spreadFactor)
tokenOut, err := swapModule.CalcOutAmtGivenIn(ctx, poolI, actualTokenIn, routeStep.TokenOutDenom, spreadFactor)
if err != nil {
return osmomath.Int{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/protorev/keeper/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (k Keeper) ConvertProfits(ctx sdk.Context, inputCoin sdk.Coin, profit osmom
// and then subtracting the amount in from the amount out to get the profit
func (k Keeper) EstimateMultihopProfit(ctx sdk.Context, inputDenom string, amount osmomath.Int, route poolmanagertypes.SwapAmountInRoutes) (sdk.Coin, osmomath.Int, error) {
tokenIn := sdk.Coin{Denom: inputDenom, Amount: amount}
amtOut, err := k.poolmanagerKeeper.MultihopEstimateOutGivenExactAmountIn(ctx, route, tokenIn)
amtOut, err := k.poolmanagerKeeper.MultihopEstimateOutGivenExactAmountInNoTakerFee(ctx, route, tokenIn)
if err != nil {
return sdk.Coin{}, osmomath.ZeroInt(), err
}
Expand Down
2 changes: 1 addition & 1 deletion x/protorev/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type PoolManagerKeeper interface {
tokenIn sdk.Coin,
tokenOutMinAmount osmomath.Int) (tokenOutAmount osmomath.Int, err error)

MultihopEstimateOutGivenExactAmountIn(
MultihopEstimateOutGivenExactAmountInNoTakerFee(
ctx sdk.Context,
routes []poolmanagertypes.SwapAmountInRoute,
tokenIn sdk.Coin,
Expand Down