Skip to content
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

BE-654 | Add liquidity cap for each routable pool in the quote and sum #565

Open
wants to merge 4 commits into
base: v27.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions domain/routable_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type RoutablePool interface {

ChargeTakerFeeExactIn(tokenIn sdk.Coin) (tokenInAfterFee sdk.Coin)

GetLiquidityCap() osmomath.Int

GetTakerFee() osmomath.Dec

GetSpreadFactor() osmomath.Dec
Expand Down
2 changes: 2 additions & 0 deletions domain/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
DefaultCoin0 = routertesting.DefaultCoin0
DefaultCoin1 = routertesting.DefaultCoin1

DefaultLiquidityCap = routertesting.DefaultLiquidityCap
DefaultLiquidityAmt = routertesting.DefaultLiquidityAmt

// router specific variables
Expand Down Expand Up @@ -117,6 +118,7 @@ func (s *RouterTestSuite) TestPrepareResultPools() {
DefaultSpreadFactor,
DenomOne,
DefaultTakerFee,
DefaultLiquidityCap,
notCosmWasmPoolCodeID,
),
},
Expand Down
2 changes: 1 addition & 1 deletion ingest/usecase/plugins/orderbook/claimbot/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/osmosis-labs/sqs/domain/mvc"
orderbookdomain "github.com/osmosis-labs/sqs/domain/orderbook"

txfeestypes "github.com/osmosis-labs/osmosis/v27/x/txfees/types"
"github.com/osmosis-labs/osmosis/v27/app/params"
txfeestypes "github.com/osmosis-labs/osmosis/v27/x/txfees/types"

"github.com/osmosis-labs/osmosis/osmomath"

Expand Down
2 changes: 1 addition & 1 deletion pools/usecase/pools_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func (s *PoolsUsecaseTestSuite) TestCalcExitCFMMPool_HappyPath() {
s.Require().NoError(err)

// Create sqs pool
sqsPool := sqsdomain.NewPool(cfmmPool, cfmmPool.GetSpreadFactor(s.Ctx), poolBalances)
sqsPool := sqsdomain.NewPool(cfmmPool, cfmmPool.GetSpreadFactor(s.Ctx), poolBalances, s.PoolOneLiquidityCap())

// Create default use case
poolsUseCase := s.newDefaultPoolsUseCase()
Expand Down
3 changes: 2 additions & 1 deletion router/usecase/pools/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ func NewRoutableCosmWasmPoolWithCustomModel(
cosmWasmPoolsParams cosmwasmdomain.CosmWasmPoolsParams,
tokenOutDenom string,
takerFee osmomath.Dec,
liquidityCap osmomath.Int,
) (domain.RoutablePool, error) {
return newRoutableCosmWasmPoolWithCustomModel(pool, cosmwasmPool, cosmWasmPoolsParams, tokenOutDenom, takerFee)
return newRoutableCosmWasmPoolWithCustomModel(pool, cosmwasmPool, cosmWasmPoolsParams, tokenOutDenom, takerFee, liquidityCap)
}

func (r *routableAlloyTransmuterPoolImpl) CheckStaticRateLimiter(tokenInCoin sdk.Coin) error {
Expand Down
12 changes: 10 additions & 2 deletions router/usecase/pools/pool_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func NewRoutablePool(pool sqsdomain.PoolI, tokenOutDenom string, takerFee osmoma
TickModel: tickModel,
TokenOutDenom: tokenOutDenom,
TakerFee: takerFee,
LiquidityCap: pool.GetPoolLiquidityCap(),
}, nil
}

Expand All @@ -58,6 +59,7 @@ func NewRoutablePool(pool sqsdomain.PoolI, tokenOutDenom string, takerFee osmoma
ChainPool: balancerPool,
TokenOutDenom: tokenOutDenom,
TakerFee: takerFee,
LiquidityCap: pool.GetPoolLiquidityCap(),
}, nil
}

Expand All @@ -82,6 +84,7 @@ func NewRoutablePool(pool sqsdomain.PoolI, tokenOutDenom string, takerFee osmoma
ChainPool: stableswapPool,
TokenOutDenom: tokenOutDenom,
TakerFee: takerFee,
LiquidityCap: pool.GetPoolLiquidityCap(),
}, nil
}

Expand All @@ -93,6 +96,7 @@ func NewRoutablePool(pool sqsdomain.PoolI, tokenOutDenom string, takerFee osmoma
func newRoutableCosmWasmPool(pool sqsdomain.PoolI, tokenOutDenom string, takerFee osmomath.Dec, cosmWasmPoolsParams cosmwasmdomain.CosmWasmPoolsParams) (domain.RoutablePool, error) {
chainPool := pool.GetUnderlyingPool()
poolType := pool.GetType()
liquidityCap := pool.GetPoolLiquidityCap()

cosmwasmPool, ok := chainPool.(*cwpoolmodel.CosmWasmPool)
if !ok {
Expand All @@ -116,6 +120,7 @@ func newRoutableCosmWasmPool(pool sqsdomain.PoolI, tokenOutDenom string, takerFe
TokenOutDenom: tokenOutDenom,
TakerFee: takerFee,
SpreadFactor: spreadFactor,
LiquidityCap: liquidityCap,
}, nil
}

Expand All @@ -125,10 +130,10 @@ func newRoutableCosmWasmPool(pool sqsdomain.PoolI, tokenOutDenom string, takerFe

// for most other CosmWasm pools, interaction with the chain will
// be required. As a result, we have a custom implementation.
return NewRoutableCosmWasmPool(cosmwasmPool, balances, tokenOutDenom, takerFee, spreadFactor, cosmWasmPoolsParams), nil
return NewRoutableCosmWasmPool(cosmwasmPool, balances, tokenOutDenom, takerFee, spreadFactor, liquidityCap, cosmWasmPoolsParams), nil
}

return newRoutableCosmWasmPoolWithCustomModel(pool, cosmwasmPool, cosmWasmPoolsParams, tokenOutDenom, takerFee)
return newRoutableCosmWasmPoolWithCustomModel(pool, cosmwasmPool, cosmWasmPoolsParams, tokenOutDenom, takerFee, liquidityCap)
}

// newRoutableCosmWasmPoolWithCustomModel creates a new RoutablePool for CosmWasm pools that require a custom CosmWasmPoolModel.
Expand All @@ -142,6 +147,7 @@ func newRoutableCosmWasmPoolWithCustomModel(
cosmWasmPoolsParams cosmwasmdomain.CosmWasmPoolsParams,
tokenOutDenom string,
takerFee osmomath.Dec,
liquidityCap osmomath.Int,
) (domain.RoutablePool, error) {
sqsPoolModel := pool.GetSQSPoolModel()

Expand Down Expand Up @@ -169,6 +175,7 @@ func newRoutableCosmWasmPoolWithCustomModel(
TokenOutDenom: tokenOutDenom,
TakerFee: takerFee,
SpreadFactor: spreadFactor,
LiquidityCap: liquidityCap,
}, nil
}

Expand All @@ -187,6 +194,7 @@ func newRoutableCosmWasmPoolWithCustomModel(
TokenOutDenom: tokenOutDenom,
TakerFee: takerFee,
SpreadFactor: spreadFactor,
LiquidityCap: liquidityCap,
OrderbookData: model.Data.Orderbook,
}, nil
}
Expand Down
5 changes: 4 additions & 1 deletion router/usecase/pools/pool_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestNewRoutableCosmWasmPoolWithCustomModel(t *testing.T) {
cosmWasmConfig domain.CosmWasmPoolRouterConfig
tokenOutDenom string
takerFee osmomath.Dec
liquidityCap osmomath.Int
expectedRoutablePool domain.RoutablePool
expectedError error
}{
Expand Down Expand Up @@ -137,13 +138,15 @@ func TestNewRoutableCosmWasmPoolWithCustomModel(t *testing.T) {
},
tokenOutDenom: "quote",
takerFee: orderbookTakerFee,
liquidityCap: osmomath.NewInt(851594865),
expectedRoutablePool: &pools.RoutableOrderbookPoolImpl{
ChainPool: &orderbookCosmWasmPool,
OrderbookData: orderbookModel.Data.Orderbook,
Balances: orderbookBalances,
TokenOutDenom: "quote",
TakerFee: orderbookTakerFee,
SpreadFactor: orderbookSpreadFactor,
LiquidityCap: osmomath.NewInt(851594865),
},
},
{
Expand Down Expand Up @@ -194,7 +197,7 @@ func TestNewRoutableCosmWasmPoolWithCustomModel(t *testing.T) {
cosmWasmPoolsParams := cosmwasmdomain.CosmWasmPoolsParams{
Config: tt.cosmWasmConfig,
}
routablePool, err := pools.NewRoutableCosmWasmPoolWithCustomModel(tt.pool, tt.cosmwasmPool, cosmWasmPoolsParams, tt.tokenOutDenom, tt.takerFee)
routablePool, err := pools.NewRoutableCosmWasmPoolWithCustomModel(tt.pool, tt.cosmwasmPool, cosmWasmPoolsParams, tt.tokenOutDenom, tt.takerFee, tt.liquidityCap)

if tt.expectedError != nil {
require.Equal(t, tt.expectedError, err)
Expand Down
14 changes: 10 additions & 4 deletions router/usecase/pools/routable_balancer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
var _ domain.RoutablePool = &routableBalancerPoolImpl{}

type routableBalancerPoolImpl struct {
ChainPool *balancer.Pool "json:\"pool\""
TokenInDenom string "json:\"token_in_denom,omitempty\""
TokenOutDenom string "json:\"token_out_denom,omitempty\""
TakerFee osmomath.Dec "json:\"taker_fee\""
ChainPool *balancer.Pool `json:"pool"`
TokenInDenom string `json:"token_in_denom,omitempty"`
TokenOutDenom string `json:"token_out_denom,omitempty"`
TakerFee osmomath.Dec `json:"taker_fee"`
LiquidityCap osmomath.Int `json:"liquidity_cap"`
}

// CalculateTokenOutByTokenIn implements RoutablePool.
Expand Down Expand Up @@ -62,6 +63,11 @@ func (r *routableBalancerPoolImpl) GetTakerFee() math.LegacyDec {
return r.TakerFee
}

// GetLiquidtyCap implements domain.RoutablePool.
func (r *routableBalancerPoolImpl) GetLiquidityCap() osmomath.Int {
return r.LiquidityCap
}

// SetTokenInDenom implements domain.RoutablePool.
func (r *routableBalancerPoolImpl) SetTokenInDenom(tokenInDenom string) {
r.TokenInDenom = tokenInDenom
Expand Down
16 changes: 11 additions & 5 deletions router/usecase/pools/routable_concentrated_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ var _ domain.RoutablePool = &routableConcentratedPoolImpl{}
var smallestDec = osmomath.BigDecFromDec(osmomath.SmallestDec())

type routableConcentratedPoolImpl struct {
ChainPool *concentratedmodel.Pool "json:\"cl_pool\""
TickModel *sqsdomain.TickModel "json:\"tick_model\""
TokenInDenom string "json:\"token_in_denom,omitempty\""
TokenOutDenom string "json:\"token_out_denom,omitempty\""
TakerFee osmomath.Dec "json:\"taker_fee\""
ChainPool *concentratedmodel.Pool `json:"cl_pool"`
TickModel *sqsdomain.TickModel `json:"tick_model"`
TokenInDenom string `json:"token_in_denom,omitempty"`
TokenOutDenom string `json:"token_out_denom,omitempty"`
TakerFee osmomath.Dec `json:"taker_fee"`
LiquidityCap osmomath.Int `json:"liquidity_cap"`
}

// Size is roughly `keys * (2.5 * Key_size + 2*value_size)`. (Plus whatever excess overhead hashmaps internally have)
Expand Down Expand Up @@ -73,6 +74,11 @@ func (r *routableConcentratedPoolImpl) GetTakerFee() math.LegacyDec {
return r.TakerFee
}

// GetLiquidityCap implements domain.RoutablePool.
func (r *routableConcentratedPoolImpl) GetLiquidityCap() osmomath.Int {
return r.LiquidityCap
}

// CalculateTokenOutByTokenIn implements domain.RoutablePool.
// It calculates the amount of token out given the amount of token in for a concentrated liquidity pool.
// Fails if:
Expand Down
20 changes: 13 additions & 7 deletions router/usecase/pools/routable_cw_alloy_transmuter_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import (
var _ domain.RoutablePool = &routableAlloyTransmuterPoolImpl{}

type routableAlloyTransmuterPoolImpl struct {
ChainPool *cwpoolmodel.CosmWasmPool "json:\"pool\""
AlloyTransmuterData *cosmwasmpool.AlloyTransmuterData "json:\"alloy_transmuter_data\""
Balances sdk.Coins "json:\"balances\""
TokenInDenom string "json:\"token_in_denom,omitempty\""
TokenOutDenom string "json:\"token_out_denom,omitempty\""
TakerFee osmomath.Dec "json:\"taker_fee\""
SpreadFactor osmomath.Dec "json:\"spread_factor\""
ChainPool *cwpoolmodel.CosmWasmPool `json:"pool"`
AlloyTransmuterData *cosmwasmpool.AlloyTransmuterData `json:"alloy_transmuter_data"`
Balances sdk.Coins `json:"balances"`
TokenInDenom string `json:"token_in_denom,omitempty"`
TokenOutDenom string `json:"token_out_denom,omitempty"`
TakerFee osmomath.Dec `json:"taker_fee"`
SpreadFactor osmomath.Dec `json:"spread_factor"`
LiquidityCap osmomath.Int `json:"liquidity_cap"`
}

// GetId implements domain.RoutablePool.
Expand All @@ -52,6 +53,11 @@ func (r *routableAlloyTransmuterPoolImpl) GetSpreadFactor() math.LegacyDec {
return r.SpreadFactor
}

// GetLiquidityCap implements domain.RoutablePool.
func (r *routableAlloyTransmuterPoolImpl) GetLiquidityCap() osmomath.Int {
return r.LiquidityCap
}

// CalculateTokenOutByTokenIn implements domain.RoutablePool.
// It calculates the amount of token out given the amount of token in for a transmuter pool.
// Transmuter pool allows no slippage swaps. For v3, the ratio of token in to token out is dependent on the normalization factor.
Expand Down
20 changes: 13 additions & 7 deletions router/usecase/pools/routable_cw_orderbook_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ var oneBigDec = osmomath.OneBigDec()
var _ domain.RoutablePool = &routableOrderbookPoolImpl{}

type routableOrderbookPoolImpl struct {
ChainPool *cwpoolmodel.CosmWasmPool "json:\"pool\""
Balances sdk.Coins "json:\"balances\""
TokenInDenom string "json:\"token_in_denom,omitempty\""
TokenOutDenom string "json:\"token_out_denom,omitempty\""
TakerFee osmomath.Dec "json:\"taker_fee\""
SpreadFactor osmomath.Dec "json:\"spread_factor\""
OrderbookData *cosmwasmpool.OrderbookData "json:\"orderbook_data\""
ChainPool *cwpoolmodel.CosmWasmPool `json:"pool"`
Balances sdk.Coins `json:"balances"`
TokenInDenom string `json:"token_in_denom,omitempty"`
TokenOutDenom string `json:"token_out_denom,omitempty"`
TakerFee osmomath.Dec `json:"taker_fee"`
SpreadFactor osmomath.Dec `json:"spread_factor"`
LiquidityCap osmomath.Int `json:"liquidity_cap"`
OrderbookData *cosmwasmpool.OrderbookData `json:"orderbook_data"`
}

// GetId implements domain.RoutablePool.
Expand All @@ -51,6 +52,11 @@ func (r *routableOrderbookPoolImpl) GetSpreadFactor() math.LegacyDec {
return r.SpreadFactor
}

// GetLiquidityCap implements domain.RoutablePool.
func (r *routableOrderbookPoolImpl) GetLiquidityCap() osmomath.Int {
return r.LiquidityCap
}

// CalculateTokenOutByTokenIn implements sqsdomain.RoutablePool.
// It calculates the amount of token out given the amount of token in for a orderbook pool.
// Fails if:
Expand Down
34 changes: 25 additions & 9 deletions router/usecase/pools/routable_cw_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,27 @@ var _ domain.RoutablePool = &routableCosmWasmPoolImpl{}
// routableCosmWasmPool is an implemenation of the cosm wasm pool
// that interacts with the chain for quotes and spot price.
type routableCosmWasmPoolImpl struct {
ChainPool *cwpoolmodel.CosmWasmPool "json:\"pool\""
Balances sdk.Coins "json:\"balances\""
TokenOutDenom string "json:\"token_out_denom,omitempty\""
TokenInDenom string "json:\"token_in_denom,omitempty\""
TakerFee osmomath.Dec "json:\"taker_fee\""
SpreadFactor osmomath.Dec "json:\"spread_factor\""
wasmClient wasmtypes.QueryClient "json:\"-\""
spotPriceQuoteCalculator domain.SpotPriceQuoteCalculator "json:\"-\""
ChainPool *cwpoolmodel.CosmWasmPool `json:"pool"`
Balances sdk.Coins `json:"balances"`
TokenOutDenom string `json:"token_out_denom,omitempty"`
TokenInDenom string `json:"token_in_denom,omitempty"`
TakerFee osmomath.Dec `json:"taker_fee"`
SpreadFactor osmomath.Dec `json:"spread_factor"`
LiquidityCap osmomath.Int `json:"liquidity_cap"`
wasmClient wasmtypes.QueryClient `json:"-"`
spotPriceQuoteCalculator domain.SpotPriceQuoteCalculator `json:"-"`
}

// NewRoutableCosmWasmPool returns a new routable cosmwasm pool with the given parameters.
func NewRoutableCosmWasmPool(pool *cwpoolmodel.CosmWasmPool, balances sdk.Coins, tokenOutDenom string, takerFee osmomath.Dec, spreadFactor osmomath.Dec, cosmWasmPoolsParams cosmwasmdomain.CosmWasmPoolsParams) domain.RoutablePool {
func NewRoutableCosmWasmPool(
pool *cwpoolmodel.CosmWasmPool,
balances sdk.Coins,
tokenOutDenom string,
takerFee osmomath.Dec,
spreadFactor osmomath.Dec,
liquidityCap osmomath.Int,
cosmWasmPoolsParams cosmwasmdomain.CosmWasmPoolsParams,
) domain.RoutablePool {
// Initializa routable cosmwasm pool
routableCosmWasmPool := &routableCosmWasmPoolImpl{
ChainPool: pool,
Expand All @@ -53,6 +62,8 @@ func NewRoutableCosmWasmPool(pool *cwpoolmodel.CosmWasmPool, balances sdk.Coins,
// Note, that there is no calculator set
// since we need to wire quote calculation callback to it.
spotPriceQuoteCalculator: nil,

LiquidityCap: liquidityCap,
}

// Initialize spot price calculator.
Expand Down Expand Up @@ -84,6 +95,11 @@ func (r *routableCosmWasmPoolImpl) GetSpreadFactor() math.LegacyDec {
return r.SpreadFactor
}

// GetLiquidityCap implements domain.RoutablePool.
func (r *routableCosmWasmPoolImpl) GetLiquidityCap() osmomath.Int {
return r.LiquidityCap
}

// CalculateTokenOutByTokenIn implements domain.RoutablePool.
// It calculates the amount of token out given the amount of token in for a transmuter pool.
// Transmuter pool allows no slippage swaps. It just returns the same amount of token out as token in
Expand Down
18 changes: 12 additions & 6 deletions router/usecase/pools/routable_cw_transmuter_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import (
var _ domain.RoutablePool = &routableTransmuterPoolImpl{}

type routableTransmuterPoolImpl struct {
ChainPool *cwpoolmodel.CosmWasmPool "json:\"pool\""
Balances sdk.Coins "json:\"balances\""
TokenInDenom string "json:\"token_in_denom,omitempty\""
TokenOutDenom string "json:\"token_out_denom,omitempty\""
TakerFee osmomath.Dec "json:\"taker_fee\""
SpreadFactor osmomath.Dec "json:\"spread_factor\""
ChainPool *cwpoolmodel.CosmWasmPool `json:"pool"`
Balances sdk.Coins `json:"balances"`
TokenInDenom string `json:"token_in_denom,omitempty"`
TokenOutDenom string `json:"token_out_denom,omitempty"`
TakerFee osmomath.Dec `json:"taker_fee"`
SpreadFactor osmomath.Dec `json:"spread_factor"`
LiquidityCap osmomath.Int `json:"liquidity_cap"`
}

// GetId implements domain.RoutablePool.
Expand All @@ -46,6 +47,11 @@ func (r *routableTransmuterPoolImpl) GetSpreadFactor() math.LegacyDec {
return r.SpreadFactor
}

// GetLiquidityCap implements domain.RoutablePool.
func (r *routableTransmuterPoolImpl) GetLiquidityCap() osmomath.Int {
return r.LiquidityCap
}

// CalculateTokenOutByTokenIn implements domain.RoutablePool.
// It calculates the amount of token out given the amount of token in for a transmuter pool.
// Transmuter pool allows no slippage swaps. It just returns the same amount of token out as token in
Expand Down
Loading
Loading