Skip to content

Commit

Permalink
Minor protorev mutation speedup (#7498)
Browse files Browse the repository at this point in the history
(cherry picked from commit ec6bd51)
  • Loading branch information
ValarDragon authored and mergify[bot] committed Feb 16, 2024
1 parent 8627f01 commit 11c14f4
Showing 1 changed file with 5 additions and 3 deletions.
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

0 comments on commit 11c14f4

Please sign in to comment.