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

[TRA-505] Add x/listing testing framework and test for CreateMarket #1974

Merged
merged 11 commits into from
Jul 31, 2024
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
Loading