Skip to content

Commit

Permalink
[TRA-505] Add x/listing testing framework and test for CreateMarket (#…
Browse files Browse the repository at this point in the history
…1974)

Signed-off-by: Shrenuj Bansal <[email protected]>
  • Loading branch information
shrenujb authored Jul 31, 2024
1 parent 181aad3 commit 757cd57
Show file tree
Hide file tree
Showing 7 changed files with 421 additions and 12 deletions.
9 changes: 8 additions & 1 deletion protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,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,
Expand Down
4 changes: 4 additions & 0 deletions protocol/testutil/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}
Expand Down
187 changes: 187 additions & 0 deletions protocol/testutil/keeper/listing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
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"
"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"
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()
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}
},
)

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
}
25 changes: 18 additions & 7 deletions protocol/x/listing/keeper/listing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -32,18 +34,22 @@ 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,
) (marketId uint32, err error) {
marketId = k.PricesKeeper.AcquireNextMarketID(ctx)

// Get market details from marketmap
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, types.ErrMarketNotFound
}

// Create a new market
market, err := k.PricesKeeper.CreateMarket(
Expand All @@ -52,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,
Expand Down Expand Up @@ -96,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,
Expand All @@ -105,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
}
Expand Down
Loading

0 comments on commit 757cd57

Please sign in to comment.