Skip to content

Commit

Permalink
move more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Jan 4, 2024
1 parent d40f54e commit d485a13
Show file tree
Hide file tree
Showing 41 changed files with 431 additions and 378 deletions.
19 changes: 9 additions & 10 deletions app/sidecar_query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/cache"
"github.com/osmosis-labs/sqs/domain/mvc"
"github.com/osmosis-labs/sqs/log"
"github.com/osmosis-labs/sqs/middleware"

Expand All @@ -38,9 +37,9 @@ import (
// and exposes endpoints for querying formatter and processed data from frontend.
type SideCarQueryServer interface {
GetTxManager() repository.TxManager
GetPoolsRepository() mvc.PoolsRepository
GetChainInfoRepository() mvc.ChainInfoRepository
GetRouterRepository() mvc.RouterRepository
GetPoolsRepository() poolsredisrepo.PoolsRepository
GetChainInfoRepository() chaininforedisrepo.ChainInfoRepository
GetRouterRepository() routerredisrepo.RouterRepository
GetTokensUseCase() domain.TokensUsecase
GetLogger() log.Logger
Shutdown(context.Context) error
Expand All @@ -49,9 +48,9 @@ type SideCarQueryServer interface {

type sideCarQueryServer struct {
txManager repository.TxManager
poolsRepository mvc.PoolsRepository
chainInfoRepository mvc.ChainInfoRepository
routerRepository mvc.RouterRepository
poolsRepository poolsredisrepo.PoolsRepository
chainInfoRepository chaininforedisrepo.ChainInfoRepository
routerRepository routerredisrepo.RouterRepository
tokensUseCase domain.TokensUsecase
e *echo.Echo
sqsAddress string
Expand All @@ -64,16 +63,16 @@ func (sqs *sideCarQueryServer) GetTokensUseCase() domain.TokensUsecase {
}

// GetPoolsRepository implements SideCarQueryServer.
func (sqs *sideCarQueryServer) GetPoolsRepository() mvc.PoolsRepository {
func (sqs *sideCarQueryServer) GetPoolsRepository() poolsredisrepo.PoolsRepository {
return sqs.poolsRepository
}

func (sqs *sideCarQueryServer) GetChainInfoRepository() mvc.ChainInfoRepository {
func (sqs *sideCarQueryServer) GetChainInfoRepository() chaininforedisrepo.ChainInfoRepository {
return sqs.chainInfoRepository
}

// GetRouterRepository implements SideCarQueryServer.
func (sqs *sideCarQueryServer) GetRouterRepository() mvc.RouterRepository {
func (sqs *sideCarQueryServer) GetRouterRepository() routerredisrepo.RouterRepository {
return sqs.routerRepository
}

Expand Down
5 changes: 3 additions & 2 deletions chaininfo/usecase/chain_info_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqsdomain/repository"
chaininforedisrepo "github.com/osmosis-labs/sqsdomain/repository/redis/chaininfo"

"github.com/osmosis-labs/sqs/domain/mvc"
)

type chainInfoUseCase struct {
contextTimeout time.Duration
chainInfoRepository mvc.ChainInfoRepository
chainInfoRepository chaininforedisrepo.ChainInfoRepository
redisRepositoryManager repository.TxManager

// N.B. sometimes the node gets stuck and does not make progress.
Expand All @@ -32,7 +33,7 @@ const MaxAllowedHeightUpdateTimeDeltaSecs = 30

var _ mvc.ChainInfoUsecase = &chainInfoUseCase{}

func NewChainInfoUsecase(timeout time.Duration, chainInfoRepository mvc.ChainInfoRepository, redisRepositoryManager repository.TxManager) mvc.ChainInfoUsecase {
func NewChainInfoUsecase(timeout time.Duration, chainInfoRepository chaininforedisrepo.ChainInfoRepository, redisRepositoryManager repository.TxManager) mvc.ChainInfoUsecase {
return &chainInfoUseCase{
contextTimeout: timeout,
chainInfoRepository: chainInfoRepository,
Expand Down
11 changes: 0 additions & 11 deletions domain/mvc/chainInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@ package mvc

import (
"context"

"github.com/osmosis-labs/sqsdomain/repository"
)

// ChainInfoRepository represents the contract for a repository handling chain information
type ChainInfoRepository interface {
// StoreLatestHeight stores the latest blockchain height
StoreLatestHeight(ctx context.Context, tx repository.Tx, height uint64) error

// GetLatestHeight retrieves the latest blockchain height
GetLatestHeight(ctx context.Context) (uint64, error)
}

type ChainInfoUsecase interface {
GetLatestHeight(ctx context.Context) (uint64, error)
}
30 changes: 6 additions & 24 deletions domain/mvc/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,20 @@ package mvc
import (
"context"

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqsdomain"

"github.com/osmosis-labs/sqs/router/usecase/route"
"github.com/osmosis-labs/sqsdomain/repository"
)

// PoolsRepository represent the pool's repository contract
type PoolsRepository interface {
// GetAllPools atomically reads and returns all on-chain pools sorted by ID.
// Note that this does NOT return tick models for the concentrated pools
GetAllPools(context.Context) ([]domain.PoolI, error)

// GetPools atomically reads and returns the pools with the given IDs.
// Note that this does NOT return tick models for the concentrated pools
GetPools(ctx context.Context, poolIDs map[uint64]struct{}) (map[uint64]domain.PoolI, error)

GetTickModelForPools(ctx context.Context, pools []uint64) (map[uint64]domain.TickModel, error)

// StorePools atomically stores the given pools.
StorePools(ctx context.Context, tx repository.Tx, pools []domain.PoolI) error
// ClearAllPools atomically clears all pools.
ClearAllPools(ctx context.Context, tx repository.Tx) error
}

// PoolsUsecase represent the pool's usecases
type PoolsUsecase interface {
GetAllPools(ctx context.Context) ([]domain.PoolI, error)
GetAllPools(ctx context.Context) ([]sqsdomain.PoolI, error)

// GetRoutesFromCandidates converts candidate routes to routes intrusmented with all the data necessary for estimating
// a swap. This data entails the pool data, the taker fee.
GetRoutesFromCandidates(ctx context.Context, candidateRoutes route.CandidateRoutes, takerFeeMap domain.TakerFeeMap, tokenInDenom, tokenOutDenom string) ([]route.RouteImpl, error)
GetRoutesFromCandidates(ctx context.Context, candidateRoutes route.CandidateRoutes, takerFeeMap sqsdomain.TakerFeeMap, tokenInDenom, tokenOutDenom string) ([]route.RouteImpl, error)

GetTickModelMap(ctx context.Context, poolIDs []uint64) (map[uint64]domain.TickModel, error)
GetTickModelMap(ctx context.Context, poolIDs []uint64) (map[uint64]sqsdomain.TickModel, error)
// GetPool returns the pool with the given ID.
GetPool(ctx context.Context, poolID uint64) (domain.PoolI, error)
GetPool(ctx context.Context, poolID uint64) (sqsdomain.PoolI, error)
}
25 changes: 2 additions & 23 deletions domain/mvc/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,12 @@ import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/sqsdomain/repository"

"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/router/usecase/route"
"github.com/osmosis-labs/sqsdomain"
)

// RouterRepository represent the router's repository contract
type RouterRepository interface {
GetTakerFee(ctx context.Context, denom0, denom1 string) (osmomath.Dec, error)
GetAllTakerFees(ctx context.Context) (domain.TakerFeeMap, error)
SetTakerFee(ctx context.Context, tx repository.Tx, denom0, denom1 string, takerFee osmomath.Dec) error
// SetRoutesTx sets the routes for the given denoms in the given transaction.
// Sorts denom0 and denom1 lexicographically before setting the routes.
// Returns error if the transaction fails.
SetRoutesTx(ctx context.Context, tx repository.Tx, denom0, denom1 string, routes route.CandidateRoutes) error
// SetRoutes sets the routes for the given denoms. Creates a new transaction and executes it.
// Sorts denom0 and denom1 lexicographically before setting the routes.
// Returns error if the transaction fails.
SetRoutes(ctx context.Context, denom0, denom1 string, routes route.CandidateRoutes) error
// GetRoutes returns the routes for the given denoms.
// Sorts denom0 and denom1 lexicographically before setting the routes.
// Returns empty slice and no error if no routes are present.
// Returns error if the routes are not found.
GetRoutes(ctx context.Context, denom0, denom1 string) (route.CandidateRoutes, error)
}

// RouterUsecase represent the router's usecases
type RouterUsecase interface {
// GetOptimalQuote returns the optimal quote for the given tokenIn and tokenOutDenom.
Expand All @@ -44,7 +23,7 @@ type RouterUsecase interface {
// GetCandidateRoutes returns the candidate routes for the given tokenIn and tokenOutDenom.
GetCandidateRoutes(ctx context.Context, tokenInDenom, tokenOutDenom string) (route.CandidateRoutes, error)
// GetTakerFee returns the taker fee for all token pairs in a pool.
GetTakerFee(ctx context.Context, poolID uint64) ([]domain.TakerFeeForPair, error)
GetTakerFee(ctx context.Context, poolID uint64) ([]sqsdomain.TakerFeeForPair, error)
// GetCachedCandidateRoutes returns the candidate routes for the given tokenIn and tokenOutDenom from cache.
// It does not recompute the routes if they are not present in cache.
// Returns error if cache is disabled.
Expand Down
2 changes: 1 addition & 1 deletion domain/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/mocks"
"github.com/osmosis-labs/sqs/router/usecase/pools"
"github.com/osmosis-labs/sqs/router/usecase/route"
"github.com/osmosis-labs/sqs/router/usecase/routertesting"
"github.com/osmosis-labs/sqsdomain/mocks"

poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
)
Expand Down
104 changes: 2 additions & 102 deletions domain/router.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package domain

import (
"fmt"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/sqsdomain"

"github.com/osmosis-labs/osmosis/osmomath"
poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
"github.com/osmosis-labs/sqs/domain/json"
)

type RoutablePool interface {
Expand Down Expand Up @@ -43,7 +40,7 @@ type RoutableResultPool interface {
type Route interface {
GetPools() []RoutablePool
// AddPool adds pool to route.
AddPool(pool PoolI, tokenOut string, takerFee osmomath.Dec)
AddPool(pool sqsdomain.PoolI, tokenOut string, takerFee osmomath.Dec)
// CalculateTokenOutByTokenIn calculates the token out amount given the token in amount.
// Returns error if the calculation fails.
CalculateTokenOutByTokenIn(tokenIn sdk.Coin) (sdk.Coin, error)
Expand Down Expand Up @@ -95,100 +92,3 @@ type RouterConfig struct {
// The number of seconds to cache routes for before expiry.
RouteCacheExpirySeconds uint64 `mapstructure:"route_cache_expiry_seconds"`
}

// DenomPair encapsulates a pair of denoms.
// The order of the denoms ius that Denom0 precedes
// Denom1 lexicographically.
type DenomPair struct {
Denom0 string
Denom1 string
}

// TakerFeeMap is a map of DenomPair to taker fee.
// It sorts the denoms lexicographically before looking up the taker fee.
type TakerFeeMap map[DenomPair]osmomath.Dec

var _ json.Marshaler = &TakerFeeMap{}
var _ json.Unmarshaler = &TakerFeeMap{}

// MarshalJSON implements json.Marshaler.
func (tfm TakerFeeMap) MarshalJSON() ([]byte, error) {
serializedMap := map[string]osmomath.Dec{}
for key, value := range tfm {
// Convert DenomPair to a string representation
keyString := fmt.Sprintf("%s-%s", key.Denom0, key.Denom1)
serializedMap[keyString] = value
}

return json.Marshal(serializedMap)
}

// UnmarshalJSON implements json.Unmarshaler.
func (tfm TakerFeeMap) UnmarshalJSON(data []byte) error {
var serializedMap map[string]osmomath.Dec
if err := json.Unmarshal(data, &serializedMap); err != nil {
return err
}

// Convert string keys back to DenomPair
for keyString, value := range serializedMap {
parts := strings.Split(keyString, "-")
if len(parts) != 2 {
return fmt.Errorf("invalid key format: %s", keyString)
}
denomPair := DenomPair{Denom0: parts[0], Denom1: parts[1]}
(tfm)[denomPair] = value
}

return nil
}

// Has returns true if the taker fee for the given denoms is found.
// It sorts the denoms lexicographically before looking up the taker fee.
func (tfm TakerFeeMap) Has(denom0, denom1 string) bool {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

_, found := tfm[DenomPair{Denom0: denom0, Denom1: denom1}]
return found
}

// GetTakerFee returns the taker fee for the given denoms.
// It sorts the denoms lexicographically before looking up the taker fee.
// Returns error if the taker fee is not found.
func (tfm TakerFeeMap) GetTakerFee(denom0, denom1 string) osmomath.Dec {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

takerFee, found := tfm[DenomPair{Denom0: denom0, Denom1: denom1}]

if !found {
return DefaultTakerFee
}

return takerFee
}

// SetTakerFee sets the taker fee for the given denoms.
// It sorts the denoms lexicographically before setting the taker fee.
func (tfm TakerFeeMap) SetTakerFee(denom0, denom1 string, takerFee osmomath.Dec) {
// Ensure increasing lexicographic order.
if denom1 < denom0 {
denom0, denom1 = denom1, denom0
}

tfm[DenomPair{Denom0: denom0, Denom1: denom1}] = takerFee
}

// TakerFeeForPair represents the taker fee for a pair of tokens
type TakerFeeForPair struct {
Denom0 string
Denom1 string
TakerFee osmomath.Dec
}

var DefaultTakerFee = osmomath.MustNewDecFromStr("0.001000000000000000")
15 changes: 9 additions & 6 deletions pools/usecase/pools_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ import (
"context"
"time"

"github.com/osmosis-labs/sqsdomain"

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/mvc"
"github.com/osmosis-labs/sqs/router/usecase/pools"
"github.com/osmosis-labs/sqs/router/usecase/route"

poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
"github.com/osmosis-labs/sqsdomain/repository"
poolsredisrepo "github.com/osmosis-labs/sqsdomain/repository/redis/pools"
)

type poolsUseCase struct {
contextTimeout time.Duration
poolsRepository mvc.PoolsRepository
poolsRepository poolsredisrepo.PoolsRepository
redisRepositoryManager repository.TxManager
}

var _ mvc.PoolsUsecase = &poolsUseCase{}

// NewPoolsUsecase will create a new pools use case object
func NewPoolsUsecase(timeout time.Duration, poolsRepository mvc.PoolsRepository, redisRepositoryManager repository.TxManager) mvc.PoolsUsecase {
func NewPoolsUsecase(timeout time.Duration, poolsRepository poolsredisrepo.PoolsRepository, redisRepositoryManager repository.TxManager) mvc.PoolsUsecase {
return &poolsUseCase{
contextTimeout: timeout,
poolsRepository: poolsRepository,
Expand All @@ -31,7 +34,7 @@ func NewPoolsUsecase(timeout time.Duration, poolsRepository mvc.PoolsRepository,
}

// GetAllPools returns all pools from the repository.
func (p *poolsUseCase) GetAllPools(ctx context.Context) ([]domain.PoolI, error) {
func (p *poolsUseCase) GetAllPools(ctx context.Context) ([]sqsdomain.PoolI, error) {
ctx, cancel := context.WithTimeout(ctx, p.contextTimeout)
defer cancel()

Expand All @@ -44,7 +47,7 @@ func (p *poolsUseCase) GetAllPools(ctx context.Context) ([]domain.PoolI, error)
}

// GetRoutesFromCandidates implements mvc.PoolsUsecase.
func (p *poolsUseCase) GetRoutesFromCandidates(ctx context.Context, candidateRoutes route.CandidateRoutes, takerFeeMap domain.TakerFeeMap, tokenInDenom, tokenOutDenom string) ([]route.RouteImpl, error) {
func (p *poolsUseCase) GetRoutesFromCandidates(ctx context.Context, candidateRoutes route.CandidateRoutes, takerFeeMap sqsdomain.TakerFeeMap, tokenInDenom, tokenOutDenom string) ([]route.RouteImpl, error) {
// Get all pools
poolsData, err := p.poolsRepository.GetPools(ctx, candidateRoutes.UniquePoolIDs)
if err != nil {
Expand Down Expand Up @@ -108,7 +111,7 @@ func (p *poolsUseCase) GetRoutesFromCandidates(ctx context.Context, candidateRou
}

// GetTickModelMap implements mvc.PoolsUsecase.
func (p *poolsUseCase) GetTickModelMap(ctx context.Context, poolIDs []uint64) (map[uint64]domain.TickModel, error) {
func (p *poolsUseCase) GetTickModelMap(ctx context.Context, poolIDs []uint64) (map[uint64]sqsdomain.TickModel, error) {
ctx, cancel := context.WithTimeout(ctx, p.contextTimeout)
defer cancel()

Expand All @@ -121,7 +124,7 @@ func (p *poolsUseCase) GetTickModelMap(ctx context.Context, poolIDs []uint64) (m
}

// GetPool implements mvc.PoolsUsecase.
func (p *poolsUseCase) GetPool(ctx context.Context, poolID uint64) (domain.PoolI, error) {
func (p *poolsUseCase) GetPool(ctx context.Context, poolID uint64) (sqsdomain.PoolI, error) {
pools, err := p.poolsRepository.GetPools(ctx, map[uint64]struct {
}{
poolID: {},
Expand Down
Loading

0 comments on commit d485a13

Please sign in to comment.