Skip to content

Commit

Permalink
persist token metadata for the test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Feb 23, 2024
1 parent 345a628 commit 4377308
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 18 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ sqs-update-mainnet-state:
curl -X POST "http:/localhost:9092/router/store-state"
mv pools.json router/usecase/routertesting/parsing/pools.json
mv taker_fees.json router/usecase/routertesting/parsing/taker_fees.json

curl -X POST "http:/localhost:9092/tokens/store-state"
mv tokens.json router/usecase/routertesting/parsing/tokens.json
8 changes: 4 additions & 4 deletions router/usecase/candidate_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (s *RouterTestSuite) TestGetCandidateRoutesBFS_OSMOATOM() {
config.MaxPoolsPerRoute = 5
config.MaxRoutes = 10

router, _, _ := s.SetupMainnetRouter(config)
router, _ := s.SetupMainnetRouter(config)

candidateRoutes, err := router.GetCandidateRoutes(UOSMO, ATOM)
s.Require().NoError(err)
Expand All @@ -37,7 +37,7 @@ func (s *RouterTestSuite) TestGetCandidateRoutesBFS_OSMOstOSMO() {
config.MaxRoutes = 10
config.MinOSMOLiquidity = 1000

router, _, _ := s.SetupMainnetRouter(config)
router, _ := s.SetupMainnetRouter(config)

candidateRoutesUOSMOIn, err := router.GetCandidateRoutes(UOSMO, stOSMO)
s.Require().NoError(err)
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *RouterTestSuite) TestGetCandidateRoutesBFS_ATOMUSDT() {
RouteCacheEnabled: false,
}

router, _, _ := s.SetupMainnetRouter(config)
router, _ := s.SetupMainnetRouter(config)

candidateRoutesUOSMOIn, err := router.GetCandidateRoutes(ATOM, USDT)
s.Require().NoError(err)
Expand All @@ -91,7 +91,7 @@ func (s *RouterTestSuite) TestGetCandidateRoutesBFS_Top10VolumePairs() {
config := defaultRouterConfig
config.MaxPoolsPerRoute = 3
config.MaxRoutes = 10
router, _, _ := s.SetupMainnetRouter(config)
router, _ := s.SetupMainnetRouter(config)

// Manually taken from https://info.osmosis.zone/ in Nov 2023.
top10ByVolumeDenoms := []string{
Expand Down
10 changes: 5 additions & 5 deletions router/usecase/optimized_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,10 +664,10 @@ func (s *RouterTestSuite) TestGetOptimalQuote_Mainnet() {
}

// Setup mainnet router
router, tickMap, takerFeeMap := s.SetupMainnetRouter(config)
router, mainnetState := s.SetupMainnetRouter(config)

// Mock router use case.
routerUsecase, _ := s.setupRouterAndPoolsUsecase(router, tickMap, takerFeeMap, cache.New(), cache.NewNoOpRoutesOverwrite())
routerUsecase, _ := s.setupRouterAndPoolsUsecase(router, mainnetState.TickMap, mainnetState.TakerFeeMap, cache.New(), cache.NewNoOpRoutesOverwrite())

// System under test
quote, err := routerUsecase.GetOptimalQuote(context.Background(), sdk.NewCoin(tc.tokenInDenom, tc.amountIn), tc.tokenOutDenom)
Expand Down Expand Up @@ -697,18 +697,18 @@ func (s *RouterTestSuite) TestGetCustomQuote_GetCustomDirectQuote_Mainnet_UOSMOU
amountIn = osmomath.NewInt(5000000)
)

router, tickMap, takerFeeMap := s.SetupMainnetRouter(config)
router, mainnetState := s.SetupMainnetRouter(config)

// Setup router repository mock
routerRepositoryMock := sqsdomainmocks.RedisRouterRepositoryMock{
TakerFees: takerFeeMap,
TakerFees: mainnetState.TakerFeeMap,
}
routerusecase.WithRouterRepository(router, &routerRepositoryMock)

// Setup pools usecase mock.
poolsRepositoryMock := sqsdomainmocks.RedisPoolsRepositoryMock{
Pools: router.GetSortedPools(),
TickModel: tickMap,
TickModel: mainnetState.TickMap,
}
poolsUsecase := poolsusecase.NewPoolsUsecase(time.Hour, &poolsRepositoryMock, nil, &domain.PoolsConfig{}, "node-uri-placeholder")
routerusecase.WithPoolsUsecase(router, poolsUsecase)
Expand Down
10 changes: 5 additions & 5 deletions router/usecase/router_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ func (s *RouterTestSuite) TestGetOptimalQuote_Cache_Overwrites() {
config.MaxRoutes = 1

// Setup mainnet router
router, tickMap, takerFeeMap := s.SetupMainnetRouter(config)
router, mainnetState := s.SetupMainnetRouter(config)

rankedRouteCache := cache.New()
routesOverwrite := cache.NewRoutesOverwrite()
Expand All @@ -601,7 +601,7 @@ func (s *RouterTestSuite) TestGetOptimalQuote_Cache_Overwrites() {
}

// Mock router use case.
routerUsecase, _ := s.setupRouterAndPoolsUsecase(router, tickMap, takerFeeMap, rankedRouteCache, routesOverwrite)
routerUsecase, _ := s.setupRouterAndPoolsUsecase(router, mainnetState.TickMap, mainnetState.TakerFeeMap, rankedRouteCache, routesOverwrite)

// System under test
quote, err := routerUsecase.GetOptimalQuote(context.Background(), sdk.NewCoin(defaultTokenInDenom, tc.amountIn), defaultTokenOutDenom)
Expand Down Expand Up @@ -648,10 +648,10 @@ func (s *RouterTestSuite) TestOverwriteRoutes_LoadOverwriteRoutes() {
config.MaxRoutes = 1

// Setup mainnet router
router, tickMap, takerFeeMap := s.SetupMainnetRouter(config)
router, mainnetState := s.SetupMainnetRouter(config)

// Mock router use case.
routerUsecase, _ := s.setupRouterAndPoolsUsecase(router, tickMap, takerFeeMap, cache.New(), cache.NewRoutesOverwrite())
routerUsecase, _ := s.setupRouterAndPoolsUsecase(router, mainnetState.TickMap, mainnetState.TakerFeeMap, cache.New(), cache.NewRoutesOverwrite())
routerUsecase = usecase.WithOverwriteRoutesPath(routerUsecase, tempPath)

// Without overwrite this is the pool ID we expect given the amount in.
Expand All @@ -678,7 +678,7 @@ func (s *RouterTestSuite) TestOverwriteRoutes_LoadOverwriteRoutes() {
s.validatePoolIDInRoute(routerUsecase, sdk.NewCoin(UOSMO, defaultAmountInCache), ATOM, poolID1135Concentrated)

// Now, drop the original use case and create a new one
routerUsecase, _ = s.setupRouterAndPoolsUsecase(router, tickMap, takerFeeMap, cache.New(), cache.NewRoutesOverwrite())
routerUsecase, _ = s.setupRouterAndPoolsUsecase(router, mainnetState.TickMap, mainnetState.TakerFeeMap, cache.New(), cache.NewRoutesOverwrite())
routerUsecase = usecase.WithOverwriteRoutesPath(routerUsecase, tempPath)

// // Without overwrite this is the pool ID we expect given the amount in.
Expand Down
40 changes: 40 additions & 0 deletions router/usecase/routertesting/parsing/mainnet_pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ func StoreTakerFees(takerFeesFile string, takerFeesMap sqsdomain.TakerFeeMap) er
return nil
}

// StoreTokensMetadata stores the tokens meta data to disk at the given path.
func StoreTokensMetadata(tokensMetaData map[string]domain.Token, tokensFile string) error {
_, err := os.Stat(tokensFile)
if os.IsNotExist(err) {
file, err := os.Create(tokensFile)
if err != nil {
return err
}
defer file.Close()

takerFeesJSON, err := json.Marshal(tokensMetaData)
if err != nil {
return err
}

_, err = file.Write(takerFeesJSON)
if err != nil {
return err
}
}

return nil
}

// ReadPools reads the pools from a file and returns them
func ReadPools(poolsFile string) ([]sqsdomain.PoolI, map[uint64]sqsdomain.TickModel, error) {
poolBytes, err := os.ReadFile(poolsFile)
Expand Down Expand Up @@ -145,6 +169,22 @@ func ReadTakerFees(takerFeeFileName string) (sqsdomain.TakerFeeMap, error) {
return takerFeeMap, nil
}

// ReadTokensMetadata reads the tokens meta data from disk at the given path and returns them.
func ReadTokensMetadata(tokensMetadataFileName string) (map[string]domain.Token, error) {
tokensMetadataBytes, err := os.ReadFile(tokensMetadataFileName)
if err != nil {
return nil, err
}

tokensMetadata := map[string]domain.Token{}
err = json.Unmarshal(tokensMetadataBytes, &tokensMetadata)
if err != nil {
return nil, err
}

return tokensMetadata, nil
}

// MarshalPool marshals a pool to JSON.
func MarshalPool(pool sqsdomain.PoolI) (json.RawMessage, error) {
poolType := pool.GetType()
Expand Down
1 change: 1 addition & 0 deletions router/usecase/routertesting/parsing/tokens.json

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions router/usecase/routertesting/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ type RouterTestHelper struct {
apptesting.ConcentratedKeeperTestHelper
}

// Mock mainnet state
type MockMainnetState struct {
TickMap map[uint64]sqsdomain.TickModel
TakerFeeMap sqsdomain.TakerFeeMap
TokensMetadata map[string]domain.Token
}

const (
DefaultPoolID = uint64(1)

relativePathMainnetFiles = "/router/usecase/routertesting/parsing/"
poolsFileName = "pools.json"
takerFeesFileName = "taker_fees.json"
tokensMetadataFileName = "tokens.json"
)

var (
Expand Down Expand Up @@ -192,7 +200,7 @@ func (s *RouterTestHelper) ValidateRoutePools(expectedPools []sqsdomain.Routable
}
}

func (s *RouterTestHelper) SetupDefaultMainnetRouter() (*usecase.Router, map[uint64]sqsdomain.TickModel, sqsdomain.TakerFeeMap) {
func (s *RouterTestHelper) SetupDefaultMainnetRouter() (*usecase.Router, MockMainnetState) {
routerConfig := domain.RouterConfig{
PreferredPoolIDs: []uint64{},
MaxRoutes: 4,
Expand All @@ -206,17 +214,24 @@ func (s *RouterTestHelper) SetupDefaultMainnetRouter() (*usecase.Router, map[uin
return s.SetupMainnetRouter(routerConfig)
}

func (s *RouterTestHelper) SetupMainnetRouter(config domain.RouterConfig) (*usecase.Router, map[uint64]sqsdomain.TickModel, sqsdomain.TakerFeeMap) {
func (s *RouterTestHelper) SetupMainnetRouter(config domain.RouterConfig) (*usecase.Router, MockMainnetState) {
pools, tickMap, err := parsing.ReadPools(absolutePathToStateFiles + poolsFileName)
s.Require().NoError(err)

takerFeeMap, err := parsing.ReadTakerFees(absolutePathToStateFiles + takerFeesFileName)
s.Require().NoError(err)

tokensMetadata, err := parsing.ReadTokensMetadata(absolutePathToStateFiles + tokensMetadataFileName)
s.Require().NoError(err)

logger, err := log.NewLogger(false, "", "info")
s.Require().NoError(err)
router := usecase.NewRouter(config.PreferredPoolIDs, config.MaxPoolsPerRoute, config.MaxRoutes, config.MaxSplitRoutes, config.MaxSplitIterations, config.MinOSMOLiquidity, logger)
router = usecase.WithSortedPools(router, pools)

return router, tickMap, takerFeeMap
return router, MockMainnetState{
TickMap: tickMap,
TakerFeeMap: takerFeeMap,
TokensMetadata: tokensMetadata,
}
}
18 changes: 18 additions & 0 deletions tokens/delivery/http/tokens_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/mvc"
"github.com/osmosis-labs/sqs/log"
"github.com/osmosis-labs/sqs/router/usecase/routertesting/parsing"
"github.com/osmosis-labs/sqs/tokens/usecase/pricing"

_ "github.com/osmosis-labs/sqs/docs"
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewTokensHandler(e *echo.Echo, ts mvc.TokensUsecase, ru mvc.RouterUsecase,
e.GET(formatTokensResource("/metadata"), handler.GetMetadata)
e.GET(formatTokensResource("/prices"), handler.GetPrices)
e.GET(formatTokensResource("/usd-price-test"), handler.GetUSDPriceTest)
e.POST(formatTokensResource("/store-state"), handler.StoreTokensStateInFiles)

defaultQuoteChainDenom, err = ts.GetChainDenom(context.Background(), defaultQuoteHumanDenom)
if err != nil {
Expand Down Expand Up @@ -219,3 +221,19 @@ func (a *TokensHandler) GetUSDPriceTest(c echo.Context) (err error) {

return c.JSON(http.StatusOK, prices)
}

func (a *TokensHandler) StoreTokensStateInFiles(c echo.Context) error {
ctx := c.Request().Context()

tokensMetadata, err := a.TUsecase.GetFullTokenMetadata(ctx)
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.ResponseError{Message: err.Error()})
}

err = parsing.StoreTokensMetadata(tokensMetadata, "tokens.json")
if err != nil {
return c.JSON(http.StatusInternalServerError, domain.ResponseError{Message: err.Error()})
}

return c.JSON(http.StatusOK, "Tokens metadata state stored in files")
}
2 changes: 1 addition & 1 deletion tokens/usecase/tokens_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (s *TokensUseCaseTestSuite) TestParseExponents_Testnet() {
}

func (s *TokensUseCaseTestSuite) TestGetPrices() {
_, _, _ = s.SetupDefaultMainnetRouter()
_, _ = s.SetupDefaultMainnetRouter()

// usecase.NewTokensUsecase(time.Second)
}

0 comments on commit 4377308

Please sign in to comment.