Skip to content

Commit

Permalink
more moves
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Jan 4, 2024
1 parent d485a13 commit 1209f78
Show file tree
Hide file tree
Showing 49 changed files with 2,772 additions and 375 deletions.
10 changes: 5 additions & 5 deletions app/sidecar_query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import (
chaininfousecase "github.com/osmosis-labs/sqs/chaininfo/usecase"
poolsHttpDelivery "github.com/osmosis-labs/sqs/pools/delivery/http"
poolsUseCase "github.com/osmosis-labs/sqs/pools/usecase"
"github.com/osmosis-labs/sqs/sqsdomain/repository"
redisrepo "github.com/osmosis-labs/sqs/sqsdomain/repository/redis"
chaininforedisrepo "github.com/osmosis-labs/sqs/sqsdomain/repository/redis/chaininfo"
poolsredisrepo "github.com/osmosis-labs/sqs/sqsdomain/repository/redis/pools"
routerredisrepo "github.com/osmosis-labs/sqs/sqsdomain/repository/redis/router"
tokensUseCase "github.com/osmosis-labs/sqs/tokens/usecase"
"github.com/osmosis-labs/sqsdomain/repository"
redisrepo "github.com/osmosis-labs/sqsdomain/repository/redis"
chaininforedisrepo "github.com/osmosis-labs/sqsdomain/repository/redis/chaininfo"
poolsredisrepo "github.com/osmosis-labs/sqsdomain/repository/redis/pools"
routerredisrepo "github.com/osmosis-labs/sqsdomain/repository/redis/router"

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/cache"
Expand Down
4 changes: 2 additions & 2 deletions chaininfo/usecase/chain_info_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"time"

"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/sqsdomain/repository"
chaininforedisrepo "github.com/osmosis-labs/sqs/sqsdomain/repository/redis/chaininfo"

"github.com/osmosis-labs/sqs/domain/mvc"
)
Expand Down
19 changes: 9 additions & 10 deletions sqsdomain/mocks/pool_mock.go → domain/mocks/pool_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"cosmossdk.io/math"

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

"github.com/osmosis-labs/osmosis/osmomath"
"github.com/osmosis-labs/osmosis/v21/x/gamm/pool-models/balancer"
poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
"github.com/osmosis-labs/sqs/domain"
)

type MockRoutablePool struct {
Expand All @@ -27,7 +26,7 @@ type MockRoutablePool struct {
mockedTokenOut sdk.Coin
}

// CalcSpotPrice implements domain.RoutablePool.
// CalcSpotPrice implements sqsdomain.RoutablePool.
func (mp *MockRoutablePool) CalcSpotPrice(baseDenom string, quoteDenom string) (osmomath.BigDec, error) {
if mp.PoolType == poolmanagertypes.CosmWasm {
return osmomath.OneBigDec(), nil
Expand All @@ -41,20 +40,20 @@ func (mp *MockRoutablePool) CalcSpotPrice(baseDenom string, quoteDenom string) (
return spotPrice, nil
}

// GetSpreadFactor implements domain.RoutablePool.
// GetSpreadFactor implements sqsdomain.RoutablePool.
func (mp *MockRoutablePool) GetSpreadFactor() math.LegacyDec {
return mp.SpreadFactor
}

// SetTokenOutDenom implements domain.RoutablePool.
// SetTokenOutDenom implements sqsdomain.RoutablePool.
func (*MockRoutablePool) SetTokenOutDenom(tokenOutDenom string) {
panic("unimplemented")
}

var DefaultSpreadFactor = osmomath.MustNewDecFromStr("0.005")

var (
_ domain.RoutablePool = &MockRoutablePool{}
_ sqsdomain.RoutablePool = &MockRoutablePool{}
)

// GetUnderlyingPool implements routerusecase.RoutablePool.
Expand Down Expand Up @@ -92,12 +91,12 @@ func (mp *MockRoutablePool) CalculateTokenOutByTokenIn(tokenIn sdk.Coin) (sdk.Co
return balancerPool.CalcOutAmtGivenIn(sdk.Context{}, sdk.NewCoins(tokenIn), mp.TokenOutDenom, mp.SpreadFactor)
}

// String implements domain.RoutablePool.
// String implements sqsdomain.RoutablePool.
func (*MockRoutablePool) String() string {
panic("unimplemented")
}

// GetTickModel implements domain.RoutablePool.
// GetTickModel implements sqsdomain.RoutablePool.
func (mp *MockRoutablePool) GetTickModel() (*sqsdomain.TickModel, error) {
return mp.TickModel, nil
}
Expand All @@ -119,7 +118,7 @@ func (mp *MockRoutablePool) GetTokenOutDenom() string {
return mp.TokenOutDenom
}

// ChargeTakerFee implements domain.RoutablePool.
// ChargeTakerFee implements sqsdomain.RoutablePool.
func (mp *MockRoutablePool) ChargeTakerFeeExactIn(tokenIn sdk.Coin) (tokenInAfterFee sdk.Coin) {
return tokenIn.Sub(sdk.NewCoin(tokenIn.Denom, mp.TakerFee.Mul(tokenIn.Amount.ToLegacyDec()).TruncateInt()))
}
Expand All @@ -130,7 +129,7 @@ func (mp *MockRoutablePool) GetTakerFee() math.LegacyDec {
}

var _ sqsdomain.PoolI = &MockRoutablePool{}
var _ domain.RoutablePool = &MockRoutablePool{}
var _ sqsdomain.RoutablePool = &MockRoutablePool{}

// GetId implements sqsdomain.PoolI.
func (mp *MockRoutablePool) GetId() uint64 {
Expand Down
4 changes: 2 additions & 2 deletions domain/mvc/pools.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package mvc
import (
"context"

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

"github.com/osmosis-labs/sqs/router/usecase/route"
)
Expand All @@ -14,7 +14,7 @@ type PoolsUsecase interface {

// 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 sqsdomain.TakerFeeMap, tokenInDenom, tokenOutDenom string) ([]route.RouteImpl, error)
GetRoutesFromCandidates(ctx context.Context, candidateRoutes sqsdomain.CandidateRoutes, takerFeeMap sqsdomain.TakerFeeMap, tokenInDenom, tokenOutDenom string) ([]route.RouteImpl, error)

GetTickModelMap(ctx context.Context, poolIDs []uint64) (map[uint64]sqsdomain.TickModel, error)
// GetPool returns the pool with the given ID.
Expand Down
7 changes: 3 additions & 4 deletions domain/mvc/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

// RouterUsecase represent the router's usecases
Expand All @@ -21,13 +20,13 @@ type RouterUsecase interface {
// If such route is not found it returns an error.
GetCustomQuote(ctx context.Context, tokenIn sdk.Coin, tokenOutDenom string, poolIDs []uint64) (domain.Quote, error)
// GetCandidateRoutes returns the candidate routes for the given tokenIn and tokenOutDenom.
GetCandidateRoutes(ctx context.Context, tokenInDenom, tokenOutDenom string) (route.CandidateRoutes, error)
GetCandidateRoutes(ctx context.Context, tokenInDenom, tokenOutDenom string) (sqsdomain.CandidateRoutes, error)
// GetTakerFee returns the taker fee for all token pairs in a pool.
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.
GetCachedCandidateRoutes(ctx context.Context, tokenInDenom, tokenOutDenom string) (route.CandidateRoutes, error)
GetCachedCandidateRoutes(ctx context.Context, tokenInDenom, tokenOutDenom string) (sqsdomain.CandidateRoutes, error)
// StoreRoutes stores all router state in the files locally. Used for debugging.
StoreRouterStateFiles(ctx context.Context) error
}
14 changes: 7 additions & 7 deletions domain/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"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"
"github.com/osmosis-labs/sqs/sqsdomain"

poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
)
Expand Down Expand Up @@ -88,22 +88,22 @@ func (s *RouterTestSuite) TestPrepareResultPools() {
testcases := map[string]struct {
route route.RouteImpl

expectedPools []domain.RoutablePool
expectedPools []sqsdomain.RoutablePool
}{
"empty route": {
route: emptyRoute,

expectedPools: []domain.RoutablePool{},
expectedPools: []sqsdomain.RoutablePool{},
},
"single balancer pool in route": {
route: WithRoutePools(
emptyRoute,
[]domain.RoutablePool{
[]sqsdomain.RoutablePool{
mocks.WithChainPoolModel(mocks.WithTokenOutDenom(DefaultPool, DenomOne), balancerPool),
},
),

expectedPools: []domain.RoutablePool{
expectedPools: []sqsdomain.RoutablePool{
pools.NewRoutableResultPool(
balancerPoolID,
poolmanagertypes.Balancer,
Expand Down Expand Up @@ -132,6 +132,6 @@ func (s *RouterTestSuite) TestPrepareResultPools() {
}
}

func WithRoutePools(r route.RouteImpl, pools []domain.RoutablePool) route.RouteImpl {
func WithRoutePools(r route.RouteImpl, pools []sqsdomain.RoutablePool) route.RouteImpl {
return routertesting.WithRoutePools(r, pools)
}
31 changes: 3 additions & 28 deletions domain/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,18 @@ package domain

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

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

type RoutablePool interface {
GetId() uint64

GetType() poolmanagertypes.PoolType

GetPoolDenoms() []string

GetTokenOutDenom() string

CalcSpotPrice(baseDenom string, quoteDenom string) (osmomath.BigDec, error)

CalculateTokenOutByTokenIn(tokenIn sdk.Coin) (sdk.Coin, error)
ChargeTakerFeeExactIn(tokenIn sdk.Coin) (tokenInAfterFee sdk.Coin)

// SetTokenOutDenom sets the token out denom on the routable pool.
SetTokenOutDenom(tokenOutDenom string)

GetTakerFee() osmomath.Dec

GetSpreadFactor() osmomath.Dec

String() string
}

type RoutableResultPool interface {
RoutablePool
sqsdomain.RoutablePool
GetBalances() sdk.Coins
}

type Route interface {
GetPools() []RoutablePool
GetPools() []sqsdomain.RoutablePool
// AddPool adds pool to route.
AddPool(pool sqsdomain.PoolI, tokenOut string, takerFee osmomath.Dec)
// CalculateTokenOutByTokenIn calculates the token out amount given the token in amount.
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ require (
cosmossdk.io/math v1.1.3-rc.1
github.com/cosmos/cosmos-sdk v0.50.1
github.com/go-redis/redis v6.15.9+incompatible
github.com/json-iterator/go v1.1.12
github.com/labstack/echo v3.3.10+incompatible
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231230033922-af0ba43e918a
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231230033922-af0ba43e918a
github.com/osmosis-labs/osmosis/v21 v21.0.1-0.20240101030300-a42faab1a51d
github.com/osmosis-labs/sqsdomain v0.0.0-0.20231230033922-af0ba43e918a
github.com/prometheus/client_golang v1.17.0
github.com/redis/go-redis/v9 v9.3.1
github.com/sirupsen/logrus v1.9.3
Expand All @@ -26,6 +24,7 @@ require (
github.com/creachadair/taskgroup v0.4.2 // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
)

Expand Down Expand Up @@ -219,8 +218,6 @@ replace (
github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.10
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

github.com/osmosis-labs/sqsdomain => ./sqsdomain

// github.com/osmosis-labs/osmosis/osmomath => ../osmosis/osmomath

// github.com/osmosis-labs/osmosis/osmoutils => ../osmosis/osmoutils
Expand Down
10 changes: 5 additions & 5 deletions pools/usecase/pools_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import (
"context"
"time"

"github.com/osmosis-labs/sqsdomain"
"github.com/osmosis-labs/sqs/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"
"github.com/osmosis-labs/sqs/sqsdomain/repository"
poolsredisrepo "github.com/osmosis-labs/sqs/sqsdomain/repository/redis/pools"
)

type poolsUseCase struct {
Expand Down Expand Up @@ -47,7 +47,7 @@ func (p *poolsUseCase) GetAllPools(ctx context.Context) ([]sqsdomain.PoolI, erro
}

// GetRoutesFromCandidates implements mvc.PoolsUsecase.
func (p *poolsUseCase) GetRoutesFromCandidates(ctx context.Context, candidateRoutes route.CandidateRoutes, takerFeeMap sqsdomain.TakerFeeMap, tokenInDenom, tokenOutDenom string) ([]route.RouteImpl, error) {
func (p *poolsUseCase) GetRoutesFromCandidates(ctx context.Context, candidateRoutes sqsdomain.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 All @@ -73,7 +73,7 @@ func (p *poolsUseCase) GetRoutesFromCandidates(ctx context.Context, candidateRou
routes := make([]route.RouteImpl, 0, len(candidateRoutes.Routes))
for _, candidateRoute := range candidateRoutes.Routes {
previousTokenOutDenom := tokenInDenom
routablePools := make([]domain.RoutablePool, 0, len(candidateRoute.Pools))
routablePools := make([]sqsdomain.RoutablePool, 0, len(candidateRoute.Pools))
for _, candidatePool := range candidateRoute.Pools {
// Get the pool data for routing
pool, ok := poolsData[candidatePool.ID]
Expand Down
19 changes: 10 additions & 9 deletions pools/usecase/pools_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/sqsdomain"
"github.com/osmosis-labs/sqs/sqsdomain"
"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/sqs/domain"
"github.com/osmosis-labs/sqs/domain/mocks"
"github.com/osmosis-labs/sqs/pools/usecase"
"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"
sqsdomainmocks "github.com/osmosis-labs/sqs/sqsdomain/mocks"
)

type PoolsUsecaseTestSuite struct {
Expand Down Expand Up @@ -65,10 +66,10 @@ func (s *PoolsUsecaseTestSuite) TestGetRoutesFromCandidates() {
defaultPool,
}

validCandidateRoutes := route.CandidateRoutes{
Routes: []route.CandidateRoute{
validCandidateRoutes := sqsdomain.CandidateRoutes{
Routes: []sqsdomain.CandidateRoute{
{
Pools: []route.CandidatePool{
Pools: []sqsdomain.CandidatePool{
{
ID: defaultPoolID,
TokenOutDenom: denomTwo,
Expand All @@ -89,7 +90,7 @@ func (s *PoolsUsecaseTestSuite) TestGetRoutesFromCandidates() {
name string

pools []sqsdomain.PoolI
candidateRoutes route.CandidateRoutes
candidateRoutes sqsdomain.CandidateRoutes
takerFeeMap sqsdomain.TakerFeeMap
tokenInDenom string
tokenOutDenom string
Expand All @@ -110,7 +111,7 @@ func (s *PoolsUsecaseTestSuite) TestGetRoutesFromCandidates() {

expectedRoutes: []route.RouteImpl{
{
Pools: []domain.RoutablePool{
Pools: []sqsdomain.RoutablePool{
pools.NewRoutablePool(defaultPool, denomTwo, defaultTakerFee),
},
},
Expand All @@ -130,7 +131,7 @@ func (s *PoolsUsecaseTestSuite) TestGetRoutesFromCandidates() {

expectedRoutes: []route.RouteImpl{
{
Pools: []domain.RoutablePool{
Pools: []sqsdomain.RoutablePool{
pools.NewRoutablePool(defaultPool, denomTwo, sqsdomain.DefaultTakerFee),
},
},
Expand Down Expand Up @@ -163,7 +164,7 @@ func (s *PoolsUsecaseTestSuite) TestGetRoutesFromCandidates() {
s.Run(tc.name, func() {

// Create repository mock
poolsRepository := &mocks.RedisPoolsRepositoryMock{
poolsRepository := &sqsdomainmocks.RedisPoolsRepositoryMock{
Pools: tc.pools,
}

Expand Down
Loading

0 comments on commit 1209f78

Please sign in to comment.