-
Notifications
You must be signed in to change notification settings - Fork 606
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
feat(ProtoRev): Supporting Two Pool Routes #5953
Merged
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
16ef254
testing
davidterpay a65020a
changelog
davidterpay c788e3d
Merge branch 'main' into terpay/two-pool-routes
davidterpay 0d8b036
trading both ways
davidterpay c8a9dc3
Merge branch 'main' into terpay/two-pool-routes
davidterpay 5213b40
updating test
davidterpay 5e12273
nit
davidterpay 3508497
Merge branch 'main' into terpay/two-pool-routes
davidterpay fd0424f
Merge branch 'main' into terpay/two-pool-routes
devbot-wizard 92e2979
test fix
davidterpay f8b018d
protorev test update
davidterpay 8f3113b
Merge branch 'main' into terpay/two-pool-routes
davidterpay b2c792e
Update tests/e2e/configurer/chain/commands.go
czarcas7ic 476d82d
Merge branch 'main' into terpay/two-pool-routes
davidterpay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -102,6 +102,10 @@ func (k Keeper) BuildHighestLiquidityRoutes(ctx sdk.Context, tokenIn, tokenOut s | |||||
if newRoute, err := k.BuildHighestLiquidityRoute(ctx, baseDenom, tokenIn, tokenOut, poolId); err == nil { | ||||||
routes = append(routes, newRoute) | ||||||
} | ||||||
|
||||||
if newRoute, err := k.BuildTwoPoolRoute(ctx, baseDenom, tokenIn, tokenOut, poolId); err == nil { | ||||||
routes = append(routes, newRoute) | ||||||
} | ||||||
} | ||||||
|
||||||
return routes, nil | ||||||
|
@@ -149,6 +153,72 @@ func (k Keeper) BuildHighestLiquidityRoute(ctx sdk.Context, swapDenom types.Base | |||||
}, nil | ||||||
} | ||||||
|
||||||
// BuildTwoPoolRoute will attempt to create a two pool route that will rebalance pools that are paired | ||||||
// with the base denom. This is useful for pools that contain the same assets but are imbalanced. | ||||||
func (k Keeper) BuildTwoPoolRoute( | ||||||
ctx sdk.Context, | ||||||
baseDenom types.BaseDenom, | ||||||
swapIn, swapOut string, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: or SwapInDenom, SwapOutDenom
Suggested change
|
||||||
poolId uint64, | ||||||
) (RouteMetaData, error) { | ||||||
if baseDenom.Denom != swapIn && baseDenom.Denom != swapOut { | ||||||
return RouteMetaData{}, fmt.Errorf("base denom (%s) must be either tokenIn (%s) or tokenOut (%s)", baseDenom.Denom, swapIn, swapOut) | ||||||
} | ||||||
|
||||||
var ( | ||||||
tokenOutDenom string | ||||||
pool1, pool2 uint64 | ||||||
) | ||||||
|
||||||
// In the case where the base denom is the swap out, the base denom becomes more ~expensive~ on the current pool id | ||||||
// and potentially cheaper on the highest liquidity pool. So we swap first on the current pool id and then on the | ||||||
// highest liquidity pool. | ||||||
if swapOut == baseDenom.Denom { | ||||||
highestLiquidityPool, err := k.GetPoolForDenomPair(ctx, baseDenom.Denom, swapIn) | ||||||
if err != nil { | ||||||
return RouteMetaData{}, err | ||||||
} | ||||||
|
||||||
pool1, pool2 = poolId, highestLiquidityPool | ||||||
tokenOutDenom = swapIn | ||||||
} else { | ||||||
highestLiquidityPool, err := k.GetPoolForDenomPair(ctx, baseDenom.Denom, swapOut) | ||||||
if err != nil { | ||||||
return RouteMetaData{}, err | ||||||
} | ||||||
|
||||||
pool1, pool2 = highestLiquidityPool, poolId | ||||||
tokenOutDenom = swapOut | ||||||
} | ||||||
|
||||||
if pool1 == pool2 { | ||||||
return RouteMetaData{}, fmt.Errorf("cannot be trading on the same pool twice") | ||||||
} | ||||||
|
||||||
newRoute := poolmanagertypes.SwapAmountInRoutes{ | ||||||
poolmanagertypes.SwapAmountInRoute{ | ||||||
TokenOutDenom: tokenOutDenom, | ||||||
PoolId: pool1, | ||||||
}, | ||||||
poolmanagertypes.SwapAmountInRoute{ | ||||||
TokenOutDenom: baseDenom.Denom, | ||||||
PoolId: pool2, | ||||||
}, | ||||||
} | ||||||
|
||||||
// Check that the route is valid and update the number of pool points that this route will consume when simulating and executing trades | ||||||
routePoolPoints, err := k.CalculateRoutePoolPoints(ctx, newRoute) | ||||||
if err != nil { | ||||||
return RouteMetaData{}, err | ||||||
} | ||||||
|
||||||
return RouteMetaData{ | ||||||
Route: newRoute, | ||||||
PoolPoints: routePoolPoints, | ||||||
StepSize: baseDenom.StepSize, | ||||||
}, nil | ||||||
} | ||||||
|
||||||
// CalculateRoutePoolPoints calculates the number of pool points that will be consumed by a route when simulating and executing trades. This | ||||||
// is only added to the global pool point counter if the route simulated is minimally profitable i.e. it will make a profit. | ||||||
func (k Keeper) CalculateRoutePoolPoints(ctx sdk.Context, route poolmanagertypes.SwapAmountInRoutes) (uint64, error) { | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add in the comment what test these pools are for