Skip to content

Commit

Permalink
refactor/test: prepare for ranked route caching; clean up mainnet tes…
Browse files Browse the repository at this point in the history
…ts (osmosis-labs#7087) (osmosis-labs#7092)

* refactor/test: prepare for ranked route caching; clean up mainnet tests

* Update ingest/sqs/router/usecase/router_usecase.go

* Update ingest/sqs/router/usecase/router_usecase.go

* lint

(cherry picked from commit acb26bd)

Co-authored-by: Roman <roman@osmosis.team>
  • Loading branch information
mergify[bot] and p0mvn authored Dec 12, 2023
1 parent 7e45df1 commit b0aee00
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 229 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@
"-test.timeout",
"30m",
"-test.run",
"TestRouterTestSuite/TestGetCustomQuote_Mainnet_UOSMOUION",
"TestRouterTestSuite/TestGetOptimalQuote",
"-test.v"
],
},
Expand Down
4 changes: 2 additions & 2 deletions ingest/sqs/router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
### Quote

```bash
curl "localhost:9092/quote?tokenIn=5000000uosmo&tokenOutDenom=uusdc" | jq .
curl "localhost:9092/router/quote?tokenIn=5000000uosmo&tokenOutDenom=uion" | jq .
```

### Pools

```bash
curl "localhost:9092/all-pools" | jq .
curl "localhost:9092/pools/all" | jq .
```

## Trade-offs To Re-evaluate
Expand Down
6 changes: 1 addition & 5 deletions ingest/sqs/router/usecase/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ func (r *routerUseCaseImpl) InitializeRouter() *Router {
}

func (r *routerUseCaseImpl) HandleRoutes(ctx context.Context, router *Router, tokenInDenom, tokenOutDenom string) (candidateRoutes route.CandidateRoutes, err error) {
return r.handleRoutes(ctx, router, tokenInDenom, tokenOutDenom)
}

func (r *Router) GetOptimalQuote(tokenIn sdk.Coin, routes []route.RouteImpl) (domain.Quote, error) {
return r.getOptimalQuote(tokenIn, routes)
return r.handleCandidateRoutes(ctx, router, tokenInDenom, tokenOutDenom)
}

func (r *Router) EstimateBestSingleRouteQuote(routes []route.RouteImpl, tokenIn sdk.Coin) (domain.Quote, []RouteWithOutAmount, error) {
Expand Down
69 changes: 0 additions & 69 deletions ingest/sqs/router/usecase/optimized_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,73 +13,6 @@ import (
"github.com/osmosis-labs/osmosis/v21/ingest/sqs/router/usecase/route"
)

// getOptimalQuote returns the optimal quote by estimating the optimal route(s) through pools
// Considers all routes and splits.
// Returns error if router repository is not set on the router.
func (r *Router) getOptimalQuote(tokenIn sdk.Coin, routes []route.RouteImpl) (domain.Quote, error) {
if r.routerRepository == nil {
return nil, ErrNilRouterRepository
}
if r.poolsUsecase == nil {
return nil, ErrNilPoolsRepository
}

bestSingleRouteQuote, routesSortedByAmtOut, err := r.estimateBestSingleRouteQuote(routes, tokenIn)
if err != nil {
return nil, err
}

if r.maxSplitRoutes == 0 {
return bestSingleRouteQuote, nil
}

numRoutes := len(routesSortedByAmtOut)

// If there are more routes than the max split routes, keep only the top routes
if len(routesSortedByAmtOut) > r.maxSplitRoutes {
// Keep only top routes for splits
routes = routes[:r.maxSplitRoutes]
numRoutes = r.maxSplitRoutes
}

// Convert routes sorted by amount out to routes
for i := 0; i < numRoutes; i++ {
// Update routes with the top routes
routes[i] = routesSortedByAmtOut[i].RouteImpl
}

r.logger.Info("bestSingleRouteQuote ", zap.Stringer("quote", bestSingleRouteQuote))

bestSplitRouteQuote, err := r.GetSplitQuote(routes, tokenIn)
if err != nil {
return nil, err
}

r.logger.Info("bestSplitRouteQuote ", zap.Any("out", bestSingleRouteQuote.GetAmountOut()))

finalQuote := bestSingleRouteQuote

// If the split route quote is better than the single route quote, return the split route quote
if bestSplitRouteQuote.GetAmountOut().GT(bestSingleRouteQuote.GetAmountOut()) {
routes := bestSplitRouteQuote.GetRoute()

r.logger.Debug("split route selected", zap.Int("route_count", len(routes)))
for _, route := range routes {
r.logger.Debug("route", zap.Stringer("route", route))
}

finalQuote = bestSplitRouteQuote
}

r.logger.Debug("single route selected", zap.Stringer("route", bestSingleRouteQuote.GetRoute()[0]))

if finalQuote.GetAmountOut().IsZero() {
return nil, errors.New("best we can do is no tokens out")
}

return finalQuote, nil
}

// getSingleRouteQuote returns the best single route quote for the given tokenIn and tokenOutDenom.
// Returns error if router repository is not set on the router.
func (r *Router) getBestSingleRouteQuote(tokenIn sdk.Coin, routes []route.RouteImpl) (quote domain.Quote, err error) {
Expand All @@ -95,8 +28,6 @@ func (r *Router) getBestSingleRouteQuote(tokenIn sdk.Coin, routes []route.RouteI
return nil, err
}

r.logger.Info("bestSingleRouteQuote ", zap.Any("out", bestSingleRouteQuote.GetAmountOut()))

return bestSingleRouteQuote, nil
}

Expand Down
Loading

0 comments on commit b0aee00

Please sign in to comment.