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

Minor protorev allocation speedup #7498

Merged
merged 1 commit into from
Feb 16, 2024
Merged
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
8 changes: 5 additions & 3 deletions x/protorev/keeper/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (k Keeper) ConvertProfits(ctx sdk.Context, inputCoin sdk.Coin, profit osmom
// by estimating the amount out given the amount in for the first pool in the route
// 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.NewCoin(inputDenom, amount)
tokenIn := sdk.Coin{Denom: inputDenom, Amount: amount}
amtOut, err := k.poolmanagerKeeper.MultihopEstimateOutGivenExactAmountIn(ctx, route, tokenIn)
if err != nil {
return sdk.Coin{}, osmomath.ZeroInt(), err
Expand All @@ -106,6 +106,8 @@ func (k Keeper) EstimateMultihopProfit(ctx sdk.Context, inputDenom string, amoun
return tokenIn, profit, nil
}

var oneInt, twoInt = osmomath.OneInt(), osmomath.NewInt(2)

// FindMaxProfitRoute runs a binary search to find the max profit for a given route
func (k Keeper) FindMaxProfitForRoute(ctx sdk.Context, route RouteMetaData, remainingTxPoolPoints, remainingBlockPoolPoints *uint64) (sdk.Coin, osmomath.Int, error) {
// Track the tokenIn amount/denom and the profit
Expand Down Expand Up @@ -146,8 +148,8 @@ func (k Keeper) FindMaxProfitForRoute(ctx sdk.Context, route RouteMetaData, rema

// Binary search to find the max profit
for iteration := 0; curLeft.LT(curRight) && iteration < types.MaxIterations; iteration++ {
curMid := (curLeft.Add(curRight)).Quo(osmomath.NewInt(2))
curMidPlusOne := curMid.Add(osmomath.OneInt())
curMid := (curLeft.Add(curRight)).Quo(twoInt)
curMidPlusOne := curMid.Add(oneInt)

// Short circuit profit searching if there is an error in the GAMM module
tokenInMid, profitMid, err := k.EstimateMultihopProfit(ctx, inputDenom, curMid.Mul(route.StepSize), route.Route)
Expand Down