From 4c36356306267c9ff23b6e2984786823c8bd781f Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Fri, 26 Jul 2024 12:30:23 -0400 Subject: [PATCH 1/9] wip Signed-off-by: Shrenuj Bansal --- protocol/testutil/keeper/listing.go | 100 ++++++++++++++++++++++ protocol/x/listing/keeper/listing_test.go | 47 ++++++++++ 2 files changed, 147 insertions(+) create mode 100644 protocol/testutil/keeper/listing.go create mode 100644 protocol/x/listing/keeper/listing_test.go diff --git a/protocol/testutil/keeper/listing.go b/protocol/testutil/keeper/listing.go new file mode 100644 index 0000000000..7ca5e2454a --- /dev/null +++ b/protocol/testutil/keeper/listing.go @@ -0,0 +1,100 @@ +package keeper + +import ( + storetypes "cosmossdk.io/store/types" + dbm "github.com/cosmos/cosmos-db" + "github.com/cosmos/cosmos-sdk/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" + sdk "github.com/cosmos/cosmos-sdk/types" + bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + "github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager" + "github.com/dydxprotocol/v4-chain/protocol/lib" + "github.com/dydxprotocol/v4-chain/protocol/mocks" + clobkeeper "github.com/dydxprotocol/v4-chain/protocol/x/clob/keeper" + perpetualskeeper "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/keeper" + priceskeeper "github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper" + marketmapkeeper "github.com/skip-mev/slinky/x/marketmap/keeper" + "github.com/stretchr/testify/mock" + + "testing" + + "github.com/dydxprotocol/v4-chain/protocol/x/listing/keeper" + "github.com/dydxprotocol/v4-chain/protocol/x/listing/types" +) + +func ListingKeepers( + t testing.TB, + bankKeeper bankkeeper.Keeper, + indexerEventManager indexer_manager.IndexerEventManager, +) ( + ctx sdk.Context, + keeper *keeper.Keeper, + storeKey storetypes.StoreKey, + mockTimeProvider *mocks.TimeProvider, + pricesKeeper *priceskeeper.Keeper, + perpetualsKeeper *perpetualskeeper.Keeper, + clobKeeper *clobkeeper.Keeper, + marketMapKeeper *marketmapkeeper.Keeper, +) { + ctx = initKeepers( + t, func( + db *dbm.MemDB, + registry codectypes.InterfaceRegistry, + cdc *codec.ProtoCodec, + stateStore storetypes.CommitMultiStore, + transientStoreKey storetypes.StoreKey, + ) []GenesisInitializer { + // Define necessary keepers here for unit tests + memClob := &mocks.MemClob{} + memClob.On("SetClobKeeper", mock.Anything).Return() + ck := NewClobKeepersTestContext(t, nil, bankKeeper, indexerEventManager) + + keeper, storeKey, mockTimeProvider = + createListingKeeper( + stateStore, + db, + cdc, + ck.PricesKeeper, + ck.PerpetualsKeeper, + ck.ClobKeeper, + ck.MarketMapKeeper, + ) + + return []GenesisInitializer{keeper} + }, + ) + + return ctx, keeper, storeKey, mockTimeProvider, pricesKeeper, perpetualsKeeper, clobKeeper, marketMapKeeper +} + +func createListingKeeper( + stateStore storetypes.CommitMultiStore, + db *dbm.MemDB, + cdc *codec.ProtoCodec, + pricesKeeper *priceskeeper.Keeper, + perpetualsKeeper *perpetualskeeper.Keeper, + clobKeeper *clobkeeper.Keeper, + marketMapKeeper *marketmapkeeper.Keeper, +) ( + *keeper.Keeper, + storetypes.StoreKey, + *mocks.TimeProvider, +) { + storeKey := storetypes.NewKVStoreKey(types.StoreKey) + stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db) + mockTimeProvider := &mocks.TimeProvider{} + + k := keeper.NewKeeper( + cdc, + storeKey, + []string{ + lib.GovModuleAddress.String(), + }, + pricesKeeper, + clobKeeper, + marketMapKeeper, + perpetualsKeeper, + ) + + return k, storeKey, mockTimeProvider +} diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go new file mode 100644 index 0000000000..8b31bd7eb6 --- /dev/null +++ b/protocol/x/listing/keeper/listing_test.go @@ -0,0 +1,47 @@ +package keeper + +import ( + "testing" + + "github.com/dydxprotocol/v4-chain/protocol/mocks" + "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" + "github.com/stretchr/testify/require" +) + +func TestCreateMarket(t *testing.T) { + + tests := map[string]struct { + ticker string + + expectedErr error + }{ + "success": { + ticker: "TEST-USD", + expectedErr: nil, + }, + "invalid market": { + ticker: "INVALID-USD", + expectedErr: nil, // TODO: Add expected error + }, + } + + for name, tc := range tests { + t.Run( + name, func(t *testing.T) { + mockIndexerEventManager := &mocks.IndexerEventManager{} + ctx, keeper, _, _, _, _, _, _ := keeper.ListingKeepers( + t, + &mocks.BankKeeper{}, + mockIndexerEventManager, + ) + + _, err := keeper.CreateMarket(ctx, tc.ticker) + if tc.expectedErr != nil { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }, + ) + } +} From 55ba0dae8853497e3048978da9cace0cb555660b Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Fri, 26 Jul 2024 13:57:01 -0400 Subject: [PATCH 2/9] more wip Signed-off-by: Shrenuj Bansal --- protocol/testutil/keeper/listing.go | 2 +- protocol/x/listing/keeper/listing.go | 2 +- protocol/x/listing/keeper/listing_test.go | 6 ++++-- protocol/x/listing/types/errors.go | 5 ++++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/protocol/testutil/keeper/listing.go b/protocol/testutil/keeper/listing.go index 7ca5e2454a..a8941ae034 100644 --- a/protocol/testutil/keeper/listing.go +++ b/protocol/testutil/keeper/listing.go @@ -47,7 +47,7 @@ func ListingKeepers( // Define necessary keepers here for unit tests memClob := &mocks.MemClob{} memClob.On("SetClobKeeper", mock.Anything).Return() - ck := NewClobKeepersTestContext(t, nil, bankKeeper, indexerEventManager) + ck := NewClobKeepersTestContext(t, memClob, bankKeeper, indexerEventManager) keeper, storeKey, mockTimeProvider = createListingKeeper( diff --git a/protocol/x/listing/keeper/listing.go b/protocol/x/listing/keeper/listing.go index 2c864164d4..87c8ad1034 100644 --- a/protocol/x/listing/keeper/listing.go +++ b/protocol/x/listing/keeper/listing.go @@ -40,7 +40,7 @@ func (k Keeper) CreateMarket( // Get market details from marketmap marketMapDetails, err := k.MarketMapKeeper.GetMarket(ctx, ticker) if err != nil { - return 0, err + return 0, types.ErrMarketNotFound } // Create a new market diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index 8b31bd7eb6..2e3a70facd 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -1,8 +1,10 @@ -package keeper +package keeper_test import ( "testing" + "github.com/dydxprotocol/v4-chain/protocol/x/listing/types" + "github.com/dydxprotocol/v4-chain/protocol/mocks" "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" "github.com/stretchr/testify/require" @@ -21,7 +23,7 @@ func TestCreateMarket(t *testing.T) { }, "invalid market": { ticker: "INVALID-USD", - expectedErr: nil, // TODO: Add expected error + expectedErr: types.ErrMarketNotFound, }, } diff --git a/protocol/x/listing/types/errors.go b/protocol/x/listing/types/errors.go index a9fa89a594..1f125746b0 100644 --- a/protocol/x/listing/types/errors.go +++ b/protocol/x/listing/types/errors.go @@ -1,5 +1,8 @@ package types +import errorsmod "cosmossdk.io/errors" + var ( -// Add x/listing specific errors here + // Add x/listing specific errors here + ErrMarketNotFound = errorsmod.Register(ModuleName, 1, "market not found") ) From 1db0f0874bb9d1da7d27edeb61ac179a15066da2 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Fri, 26 Jul 2024 16:30:47 -0400 Subject: [PATCH 3/9] more wip --- protocol/app/app.go | 9 ++++++++- protocol/testutil/app/app.go | 4 ++++ protocol/testutil/keeper/listing.go | 4 ++++ protocol/x/listing/keeper/listing_test.go | 23 +++++++++++++++++++++-- protocol/x/listing/module.go | 18 +++++++++++++++--- 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/protocol/app/app.go b/protocol/app/app.go index 4938eeb0ba..c8381f83ea 100644 --- a/protocol/app/app.go +++ b/protocol/app/app.go @@ -1168,7 +1168,14 @@ func New( &app.MarketMapKeeper, app.PerpetualsKeeper, ) - listingModule := listingmodule.NewAppModule(appCodec, app.ListingKeeper) + listingModule := listingmodule.NewAppModule( + appCodec, + app.ListingKeeper, + app.PricesKeeper, + app.ClobKeeper, + &app.MarketMapKeeper, + app.PerpetualsKeeper, + ) app.AccountPlusKeeper = *accountplusmodulekeeper.NewKeeper( appCodec, diff --git a/protocol/testutil/app/app.go b/protocol/testutil/app/app.go index 72a9dfeb2a..44f34b4b70 100644 --- a/protocol/testutil/app/app.go +++ b/protocol/testutil/app/app.go @@ -15,6 +15,8 @@ import ( "testing" "time" + listingtypes "github.com/dydxprotocol/v4-chain/protocol/x/listing/types" + "cosmossdk.io/log" "cosmossdk.io/store/rootmulti" storetypes "cosmossdk.io/store/types" @@ -265,6 +267,8 @@ func UpdateGenesisDocWithAppStateForModule[T GenesisStates](genesisDoc *types.Ge moduleName = revsharetypes.ModuleName case marketmapmoduletypes.GenesisState: moduleName = marketmapmoduletypes.ModuleName + case listingtypes.GenesisState: + moduleName = listingtypes.ModuleName default: panic(fmt.Errorf("Unsupported type %T", t)) } diff --git a/protocol/testutil/keeper/listing.go b/protocol/testutil/keeper/listing.go index a8941ae034..bbd74d2bcb 100644 --- a/protocol/testutil/keeper/listing.go +++ b/protocol/testutil/keeper/listing.go @@ -59,6 +59,10 @@ func ListingKeepers( ck.ClobKeeper, ck.MarketMapKeeper, ) + pricesKeeper = ck.PricesKeeper + perpetualsKeeper = ck.PerpetualsKeeper + clobKeeper = ck.ClobKeeper + marketMapKeeper = ck.MarketMapKeeper return []GenesisInitializer{keeper} }, diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index 2e3a70facd..adb6a589a8 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -3,10 +3,12 @@ package keeper_test import ( "testing" + pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" + "github.com/dydxprotocol/v4-chain/protocol/x/listing/types" "github.com/dydxprotocol/v4-chain/protocol/mocks" - "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" + keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" "github.com/stretchr/testify/require" ) @@ -31,12 +33,29 @@ func TestCreateMarket(t *testing.T) { t.Run( name, func(t *testing.T) { mockIndexerEventManager := &mocks.IndexerEventManager{} - ctx, keeper, _, _, _, _, _, _ := keeper.ListingKeepers( + ctx, keeper, _, _, _, _, _, marketMapperKeeper := keepertest.ListingKeepers( t, &mocks.BankKeeper{}, mockIndexerEventManager, ) + testMarketParams := pricestypes.MarketParam{ + Pair: "TEST-USD", + Exponent: int32(-6), + ExchangeConfigJson: `{"test_config_placeholder":{}}`, + MinExchanges: 2, + MinPriceChangePpm: uint32(800), + } + + keepertest.CreateMarketsInMarketMapFromParams( + t, + ctx, + marketMapperKeeper, + []pricestypes.MarketParam{ + testMarketParams, + }, + ) + _, err := keeper.CreateMarket(ctx, tc.ticker) if tc.expectedErr != nil { require.Error(t, err) diff --git a/protocol/x/listing/module.go b/protocol/x/listing/module.go index 79d2a6c3a4..07f2fdef9a 100644 --- a/protocol/x/listing/module.go +++ b/protocol/x/listing/module.go @@ -98,16 +98,28 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { type AppModule struct { AppModuleBasic - keeper keeper.Keeper + keeper keeper.Keeper + pricesKeeper types.PricesKeeper + clobKeeper types.ClobKeeper + marketMapKeeper types.MarketMapKeeper + perpetualsKeeper types.PerpetualsKeeper } func NewAppModule( cdc codec.Codec, keeper keeper.Keeper, + pricesKeeper types.PricesKeeper, + clobKeeper types.ClobKeeper, + marketMapKeeper types.MarketMapKeeper, + perpetualsKeeper types.PerpetualsKeeper, ) AppModule { return AppModule{ - AppModuleBasic: NewAppModuleBasic(cdc), - keeper: keeper, + AppModuleBasic: NewAppModuleBasic(cdc), + keeper: keeper, + pricesKeeper: pricesKeeper, + clobKeeper: clobKeeper, + marketMapKeeper: marketMapKeeper, + perpetualsKeeper: perpetualsKeeper, } } From 06e4b434e1e4676fbdf6f5c1dad58d462b5ef92d Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Tue, 30 Jul 2024 11:58:28 -0400 Subject: [PATCH 4/9] fix test framework and add test for CreatePerpetual --- protocol/testutil/keeper/listing.go | 115 +++++++++++++++++++--- protocol/x/listing/keeper/listing_test.go | 112 ++++++++++++++++++++- 2 files changed, 208 insertions(+), 19 deletions(-) diff --git a/protocol/testutil/keeper/listing.go b/protocol/testutil/keeper/listing.go index bbd74d2bcb..e36116b34f 100644 --- a/protocol/testutil/keeper/listing.go +++ b/protocol/testutil/keeper/listing.go @@ -10,6 +10,7 @@ import ( "github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager" "github.com/dydxprotocol/v4-chain/protocol/lib" "github.com/dydxprotocol/v4-chain/protocol/mocks" + "github.com/dydxprotocol/v4-chain/protocol/testutil/constants" clobkeeper "github.com/dydxprotocol/v4-chain/protocol/x/clob/keeper" perpetualskeeper "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/keeper" priceskeeper "github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper" @@ -47,22 +48,104 @@ func ListingKeepers( // Define necessary keepers here for unit tests memClob := &mocks.MemClob{} memClob.On("SetClobKeeper", mock.Anything).Return() - ck := NewClobKeepersTestContext(t, memClob, bankKeeper, indexerEventManager) - - keeper, storeKey, mockTimeProvider = - createListingKeeper( - stateStore, - db, - cdc, - ck.PricesKeeper, - ck.PerpetualsKeeper, - ck.ClobKeeper, - ck.MarketMapKeeper, - ) - pricesKeeper = ck.PricesKeeper - perpetualsKeeper = ck.PerpetualsKeeper - clobKeeper = ck.ClobKeeper - marketMapKeeper = ck.MarketMapKeeper + revShareKeeper, _, _ := createRevShareKeeper(stateStore, db, cdc) + marketMapKeeper, _ = createMarketMapKeeper(stateStore, db, cdc) + pricesKeeper, _, _, mockTimeProvider = createPricesKeeper( + stateStore, + db, + cdc, + transientStoreKey, + revShareKeeper, + marketMapKeeper, + ) + // Mock time provider response for market creation. + mockTimeProvider.On("Now").Return(constants.TimeT) + epochsKeeper, _ := createEpochsKeeper(stateStore, db, cdc) + perpetualsKeeper, _ = createPerpetualsKeeper( + stateStore, + db, + cdc, + pricesKeeper, + epochsKeeper, + transientStoreKey, + ) + assetsKeeper, _ := createAssetsKeeper( + stateStore, + db, + cdc, + pricesKeeper, + transientStoreKey, + true, + ) + blockTimeKeeper, _ := createBlockTimeKeeper(stateStore, db, cdc) + statsKeeper, _ := createStatsKeeper( + stateStore, + epochsKeeper, + db, + cdc, + ) + vaultKeeper, _ := createVaultKeeper( + stateStore, + db, + cdc, + transientStoreKey, + ) + feeTiersKeeper, _ := createFeeTiersKeeper( + stateStore, + statsKeeper, + vaultKeeper, + db, + cdc, + ) + rewardsKeeper, _ := createRewardsKeeper( + stateStore, + assetsKeeper, + bankKeeper, + feeTiersKeeper, + pricesKeeper, + indexerEventManager, + db, + cdc, + ) + subaccountsKeeper, _ := createSubaccountsKeeper( + stateStore, + db, + cdc, + assetsKeeper, + bankKeeper, + perpetualsKeeper, + blockTimeKeeper, + revShareKeeper, + transientStoreKey, + true, + ) + clobKeeper, _, _ = createClobKeeper( + stateStore, + db, + cdc, + memClob, + assetsKeeper, + blockTimeKeeper, + bankKeeper, + feeTiersKeeper, + perpetualsKeeper, + pricesKeeper, + statsKeeper, + rewardsKeeper, + subaccountsKeeper, + indexerEventManager, + transientStoreKey, + ) + // Create the listing keeper + keeper, storeKey, _ = createListingKeeper( + stateStore, + db, + cdc, + pricesKeeper, + perpetualsKeeper, + clobKeeper, + marketMapKeeper, + ) return []GenesisInitializer{keeper} }, diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index adb6a589a8..4d8808abf8 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -3,6 +3,11 @@ package keeper_test import ( "testing" + perpetualtypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" + oracletypes "github.com/skip-mev/slinky/pkg/types" + marketmaptypes "github.com/skip-mev/slinky/x/marketmap/types" + "github.com/skip-mev/slinky/x/marketmap/types/tickermetadata" + pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" "github.com/dydxprotocol/v4-chain/protocol/x/listing/types" @@ -33,7 +38,7 @@ func TestCreateMarket(t *testing.T) { t.Run( name, func(t *testing.T) { mockIndexerEventManager := &mocks.IndexerEventManager{} - ctx, keeper, _, _, _, _, _, marketMapperKeeper := keepertest.ListingKeepers( + ctx, keeper, _, _, pricesKeeper, _, _, marketMapKeeper := keepertest.ListingKeepers( t, &mocks.BankKeeper{}, mockIndexerEventManager, @@ -50,19 +55,120 @@ func TestCreateMarket(t *testing.T) { keepertest.CreateMarketsInMarketMapFromParams( t, ctx, - marketMapperKeeper, + marketMapKeeper, []pricestypes.MarketParam{ testMarketParams, }, ) - _, err := keeper.CreateMarket(ctx, tc.ticker) + marketId, err := keeper.CreateMarket(ctx, tc.ticker) if tc.expectedErr != nil { require.Error(t, err) } else { require.NoError(t, err) + + // Check if the market was created + market, exists := pricesKeeper.GetMarketParam(ctx, marketId) + require.True(t, exists) + require.Equal(t, testMarketParams.Pair, market.Pair) + require.Equal(t, testMarketParams.Exponent, market.Exponent) + require.Equal(t, testMarketParams.MinExchanges, market.MinExchanges) + require.Equal(t, testMarketParams.MinPriceChangePpm, types.MinPriceChangePpm_LongTail) } }, ) } } + +func TestCreatePerpetual(t *testing.T) { + + tests := map[string]struct { + ticker string + referencePrice uint64 + + expectedErr error + }{ + "success": { + ticker: "TEST-USD", + referencePrice: 1000000000, + expectedErr: nil, + }, + "failure - reference price 0": { + ticker: "TEST-USD", + referencePrice: 0, + expectedErr: types.ErrReferencePriceZero, + }, + "failure - invalid market": { + ticker: "INVALID-USD", + expectedErr: types.ErrMarketNotFound, + }, + } + + for name, tc := range tests { + t.Run( + name, func(t *testing.T) { + mockIndexerEventManager := &mocks.IndexerEventManager{} + ctx, keeper, _, _, pricesKeeper, perpetualsKeeper, _, marketMapKeeper := keepertest.ListingKeepers( + t, + &mocks.BankKeeper{}, + mockIndexerEventManager, + ) + keepertest.CreateLiquidityTiersAndNPerpetuals(t, ctx, perpetualsKeeper, pricesKeeper, 10) + + // Create a marketmap with a single market + dydxMetadata, err := tickermetadata.MarshalDyDx( + tickermetadata.DyDx{ + ReferencePrice: tc.referencePrice, + Liquidity: 0, + AggregateIDs: nil, + }, + ) + require.NoError(t, err) + + market := marketmaptypes.Market{ + Ticker: marketmaptypes.Ticker{ + CurrencyPair: oracletypes.CurrencyPair{Base: "TEST", Quote: "USD"}, + Decimals: 6, + MinProviderCount: 2, + Enabled: false, + Metadata_JSON: string(dydxMetadata), + }, + ProviderConfigs: []marketmaptypes.ProviderConfig{ + { + Name: "binance_ws", + OffChainTicker: "TESTUSDT", + }, + }, + } + err = marketMapKeeper.CreateMarket(ctx, market) + require.NoError(t, err) + + marketId, err := keeper.CreateMarket(ctx, tc.ticker) + require.NoError(t, err) + + perpetualId, err := keeper.CreatePerpetual(ctx, marketId, tc.ticker) + if tc.expectedErr != nil { + require.Error(t, err) + } else { + require.NoError(t, err) + + // Check if the perpetual was created + perpetual, err := perpetualsKeeper.GetPerpetual(ctx, perpetualId) + require.NoError(t, err) + require.Equal(t, uint32(10), perpetual.GetId()) + require.Equal(t, marketId, perpetual.Params.MarketId) + require.Equal(t, tc.ticker, perpetual.Params.Ticker) + // Expected resolution = -6 - Floor(log10(1000000000)) = -15 + require.Equal(t, -15, perpetual.Params.AtomicResolution) + require.Equal(t, types.DefaultFundingPpm, perpetual.Params.DefaultFundingPpm) + require.Equal(t, types.LiquidityTier_LongTail, perpetual.Params.LiquidityTier) + require.Equal( + t, perpetualtypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED, + perpetual.Params.MarketType, + ) + } + + }, + ) + } +} From d8530f519aa0cd8ca0cc9938e74bb83ccac4a09b Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Tue, 30 Jul 2024 14:55:18 -0400 Subject: [PATCH 5/9] use slinky currencyPair when querying marketmap --- protocol/x/listing/keeper/listing.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/protocol/x/listing/keeper/listing.go b/protocol/x/listing/keeper/listing.go index ceaa5845cd..061a3e1cb5 100644 --- a/protocol/x/listing/keeper/listing.go +++ b/protocol/x/listing/keeper/listing.go @@ -3,6 +3,8 @@ package keeper import ( "math" + "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" + sdk "github.com/cosmos/cosmos-sdk/types" gogotypes "github.com/cosmos/gogoproto/types" clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types" @@ -40,7 +42,11 @@ func (k Keeper) CreateMarket( marketId = k.PricesKeeper.AcquireNextMarketID(ctx) // Get market details from marketmap - marketMapDetails, err := k.MarketMapKeeper.GetMarket(ctx, ticker) + marketMapPair, err := slinky.MarketPairToCurrencyPair(ticker) + if err != nil { + return 0, err + } + marketMapDetails, err := k.MarketMapKeeper.GetMarket(ctx, marketMapPair.String()) if err != nil { return 0, types.ErrMarketNotFound } From 1a5313538a954ccd0a03d4e927f4f0c0f639d539 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Tue, 30 Jul 2024 15:57:03 -0400 Subject: [PATCH 6/9] fix tests Signed-off-by: Shrenuj Bansal --- protocol/x/listing/keeper/listing.go | 17 ++++++---- protocol/x/listing/keeper/listing_test.go | 39 +++++++++++++++-------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/protocol/x/listing/keeper/listing.go b/protocol/x/listing/keeper/listing.go index 061a3e1cb5..bc10bed17a 100644 --- a/protocol/x/listing/keeper/listing.go +++ b/protocol/x/listing/keeper/listing.go @@ -34,7 +34,6 @@ func (k Keeper) GetMarketsHardCap(ctx sdk.Context) (hardCap uint32) { // Function to wrap the creation of a new market // Note: This will only list long-tail/isolated markets -// TODO (TRA-505): Add tests once market mapper testutils become available func (k Keeper) CreateMarket( ctx sdk.Context, ticker string, @@ -42,6 +41,7 @@ func (k Keeper) CreateMarket( marketId = k.PricesKeeper.AcquireNextMarketID(ctx) // Get market details from marketmap + // TODO: change to use util from marketmap when available marketMapPair, err := slinky.MarketPairToCurrencyPair(ticker) if err != nil { return 0, err @@ -58,9 +58,10 @@ func (k Keeper) CreateMarket( Id: marketId, Pair: ticker, // Set the price exponent to the negative of the number of decimals - Exponent: int32(marketMapDetails.Ticker.Decimals) * -1, - MinExchanges: uint32(marketMapDetails.Ticker.MinProviderCount), - MinPriceChangePpm: types.MinPriceChangePpm_LongTail, + Exponent: int32(marketMapDetails.Ticker.Decimals) * -1, + MinExchanges: uint32(marketMapDetails.Ticker.MinProviderCount), + MinPriceChangePpm: types.MinPriceChangePpm_LongTail, + ExchangeConfigJson: "{}", // Placeholder. TODO (TRA-513): Deprecate this field }, pricestypes.MarketPrice{ Id: marketId, @@ -102,7 +103,6 @@ func (k Keeper) CreateClobPair( // Function to wrap the creation of a new perpetual // Note: This will only list long-tail/isolated markets -// TODO: Add tests pending marketmap testutils func (k Keeper) CreatePerpetual( ctx sdk.Context, marketId uint32, @@ -111,7 +111,12 @@ func (k Keeper) CreatePerpetual( perpetualId = k.PerpetualsKeeper.AcquireNextPerpetualID(ctx) // Get reference price from market map - marketMapDetails, err := k.MarketMapKeeper.GetMarket(ctx, ticker) + // TODO: change to use util from marketmap when available + marketMapPair, err := slinky.MarketPairToCurrencyPair(ticker) + if err != nil { + return 0, err + } + marketMapDetails, err := k.MarketMapKeeper.GetMarket(ctx, marketMapPair.String()) if err != nil { return 0, err } diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index 4d8808abf8..b9e90d4e29 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -18,19 +18,26 @@ import ( ) func TestCreateMarket(t *testing.T) { - tests := map[string]struct { - ticker string + ticker string + duplicateMarket bool expectedErr error }{ "success": { - ticker: "TEST-USD", - expectedErr: nil, + ticker: "TEST-USD", + duplicateMarket: false, + expectedErr: nil, }, - "invalid market": { - ticker: "INVALID-USD", - expectedErr: types.ErrMarketNotFound, + "failure - invalid market": { + ticker: "INVALID-USD", + duplicateMarket: false, + expectedErr: types.ErrMarketNotFound, + }, + "failure - duplicate market": { + ticker: "TEST-USD", + duplicateMarket: true, + expectedErr: nil, }, } @@ -75,13 +82,17 @@ func TestCreateMarket(t *testing.T) { require.Equal(t, testMarketParams.MinExchanges, market.MinExchanges) require.Equal(t, testMarketParams.MinPriceChangePpm, types.MinPriceChangePpm_LongTail) } + + if tc.duplicateMarket { + _, err = keeper.CreateMarket(ctx, tc.ticker) + require.ErrorContains(t, err, pricestypes.ErrMarketParamPairAlreadyExists.Error()) + } }, ) } } func TestCreatePerpetual(t *testing.T) { - tests := map[string]struct { ticker string referencePrice uint64 @@ -144,7 +155,10 @@ func TestCreatePerpetual(t *testing.T) { require.NoError(t, err) marketId, err := keeper.CreateMarket(ctx, tc.ticker) - require.NoError(t, err) + if tc.expectedErr == types.ErrMarketNotFound { + require.Error(t, err) + return + } perpetualId, err := keeper.CreatePerpetual(ctx, marketId, tc.ticker) if tc.expectedErr != nil { @@ -159,15 +173,14 @@ func TestCreatePerpetual(t *testing.T) { require.Equal(t, marketId, perpetual.Params.MarketId) require.Equal(t, tc.ticker, perpetual.Params.Ticker) // Expected resolution = -6 - Floor(log10(1000000000)) = -15 - require.Equal(t, -15, perpetual.Params.AtomicResolution) - require.Equal(t, types.DefaultFundingPpm, perpetual.Params.DefaultFundingPpm) - require.Equal(t, types.LiquidityTier_LongTail, perpetual.Params.LiquidityTier) + require.Equal(t, int32(-15), perpetual.Params.AtomicResolution) + require.Equal(t, int32(types.DefaultFundingPpm), perpetual.Params.DefaultFundingPpm) + require.Equal(t, uint32(types.LiquidityTier_LongTail), perpetual.Params.LiquidityTier) require.Equal( t, perpetualtypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_ISOLATED, perpetual.Params.MarketType, ) } - }, ) } From 7a1c06829f4283f9e335b612c1bddcc07d5d6fac Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Tue, 30 Jul 2024 16:09:47 -0400 Subject: [PATCH 7/9] fix lint --- protocol/x/listing/keeper/listing_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index b9e90d4e29..f50b96f031 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -1,6 +1,7 @@ package keeper_test import ( + "errors" "testing" perpetualtypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types" @@ -155,8 +156,8 @@ func TestCreatePerpetual(t *testing.T) { require.NoError(t, err) marketId, err := keeper.CreateMarket(ctx, tc.ticker) - if tc.expectedErr == types.ErrMarketNotFound { - require.Error(t, err) + if errors.Is(tc.expectedErr, types.ErrMarketNotFound) { + require.ErrorContains(t, err, tc.expectedErr.Error()) return } From 701accd3dd082da3db22ee4cbac74e0e80d25e84 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 31 Jul 2024 11:10:48 -0400 Subject: [PATCH 8/9] address nits Signed-off-by: Shrenuj Bansal --- protocol/x/listing/keeper/listing_test.go | 26 +++++++++-------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index f50b96f031..646cdc0cc0 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -26,19 +26,16 @@ func TestCreateMarket(t *testing.T) { expectedErr error }{ "success": { - ticker: "TEST-USD", - duplicateMarket: false, - expectedErr: nil, + ticker: "TEST-USD", + expectedErr: nil, }, "failure - invalid market": { - ticker: "INVALID-USD", - duplicateMarket: false, - expectedErr: types.ErrMarketNotFound, + ticker: "INVALID-USD", + expectedErr: types.ErrMarketNotFound, }, "failure - duplicate market": { - ticker: "TEST-USD", - duplicateMarket: true, - expectedErr: nil, + ticker: "BTC-USD", + expectedErr: pricestypes.ErrMarketParamPairAlreadyExists, }, } @@ -52,6 +49,8 @@ func TestCreateMarket(t *testing.T) { mockIndexerEventManager, ) + keepertest.CreateTestMarkets(t, ctx, pricesKeeper) + testMarketParams := pricestypes.MarketParam{ Pair: "TEST-USD", Exponent: int32(-6), @@ -71,7 +70,7 @@ func TestCreateMarket(t *testing.T) { marketId, err := keeper.CreateMarket(ctx, tc.ticker) if tc.expectedErr != nil { - require.Error(t, err) + require.ErrorContains(t, err, tc.expectedErr.Error()) } else { require.NoError(t, err) @@ -83,11 +82,6 @@ func TestCreateMarket(t *testing.T) { require.Equal(t, testMarketParams.MinExchanges, market.MinExchanges) require.Equal(t, testMarketParams.MinPriceChangePpm, types.MinPriceChangePpm_LongTail) } - - if tc.duplicateMarket { - _, err = keeper.CreateMarket(ctx, tc.ticker) - require.ErrorContains(t, err, pricestypes.ErrMarketParamPairAlreadyExists.Error()) - } }, ) } @@ -102,7 +96,7 @@ func TestCreatePerpetual(t *testing.T) { }{ "success": { ticker: "TEST-USD", - referencePrice: 1000000000, + referencePrice: 1000000000, // $1000 expectedErr: nil, }, "failure - reference price 0": { From 4d3d6c5dfc10b09683da7bd875b1f263cd26f8b4 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 31 Jul 2024 11:11:40 -0400 Subject: [PATCH 9/9] fix test name Signed-off-by: Shrenuj Bansal --- protocol/x/listing/keeper/listing_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/protocol/x/listing/keeper/listing_test.go b/protocol/x/listing/keeper/listing_test.go index 646cdc0cc0..2679aa68c9 100644 --- a/protocol/x/listing/keeper/listing_test.go +++ b/protocol/x/listing/keeper/listing_test.go @@ -29,7 +29,7 @@ func TestCreateMarket(t *testing.T) { ticker: "TEST-USD", expectedErr: nil, }, - "failure - invalid market": { + "failure - market not found": { ticker: "INVALID-USD", expectedErr: types.ErrMarketNotFound, }, @@ -104,7 +104,7 @@ func TestCreatePerpetual(t *testing.T) { referencePrice: 0, expectedErr: types.ErrReferencePriceZero, }, - "failure - invalid market": { + "failure - market not found": { ticker: "INVALID-USD", expectedErr: types.ErrMarketNotFound, },