Skip to content

Commit

Permalink
[TRA-480] Implement functionality to acquire next market id (dydxprot…
Browse files Browse the repository at this point in the history
…ocol#1867)

Signed-off-by: Shrenuj Bansal <[email protected]>
  • Loading branch information
shrenujb authored Jul 8, 2024
1 parent 5b62cfa commit 0c8b7ed
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
34 changes: 34 additions & 0 deletions protocol/x/prices/keeper/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper
import (
"fmt"

gogotypes "github.com/cosmos/gogoproto/types"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/daemons/pricefeed/metrics"
Expand Down Expand Up @@ -115,3 +117,35 @@ func (k Keeper) GetAllMarketParamPrices(ctx sdk.Context) ([]types.MarketParamPri
}
return marketParamPrices, nil
}

// GetNextMarketID returns the next market id to be used from the module store
func (k Keeper) GetNextMarketID(ctx sdk.Context) uint32 {
store := ctx.KVStore(k.storeKey)
b := store.Get([]byte(types.NextMarketIDKey))
var result gogotypes.UInt32Value
k.cdc.MustUnmarshal(b, &result)
return result.Value
}

// SetNextMarketID sets the next market id to be used
func (k Keeper) SetNextMarketID(ctx sdk.Context, nextID uint32) {
store := ctx.KVStore(k.storeKey)
value := gogotypes.UInt32Value{Value: nextID}
store.Set([]byte(types.NextMarketIDKey), k.cdc.MustMarshal(&value))
}

// AcquireNextMarketID returns the next market id to be used and increments the next market id
func (k Keeper) AcquireNextMarketID(ctx sdk.Context) uint32 {
nextID := k.GetNextMarketID(ctx)
// if market id already exists, increment until we find one that doesn't
for {
_, exists := k.GetMarketParam(ctx, nextID)
if !exists {
break
}
nextID++
}

k.SetNextMarketID(ctx, nextID+1)
return nextID
}
48 changes: 48 additions & 0 deletions protocol/x/prices/keeper/market_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,51 @@ func TestGetAllMarketParamPrices(t *testing.T) {
allParamPrices,
)
}

func TestAcquireNextMarketID(t *testing.T) {
ctx, keeper, _, _, mockTimeProvider, _ := keepertest.PricesKeepers(t)
mockTimeProvider.On("Now").Return(constants.TimeT)
ctx = ctx.WithTxBytes(constants.TestTxBytes)

keepertest.CreateNMarkets(t, ctx, keeper, 10)

// Get the highest market ID from the existing markets.
allParams := keeper.GetAllMarketParams(ctx)
highestMarketID := uint32(0)
for _, param := range allParams {
if param.Id > highestMarketID {
highestMarketID = param.Id
}
}

// Acquire the next market ID.
nextMarketID := keeper.AcquireNextMarketID(ctx)
require.Equal(t, highestMarketID+1, nextMarketID)

// Verify the next market ID is stored in the module store.
nextMarketIDFromStore := keeper.GetNextMarketID(ctx)
require.Equal(t, nextMarketID+1, nextMarketIDFromStore)

// Create a market with the next market ID outside of acquire flow
_, err := keeper.CreateMarket(
ctx,
types.MarketParam{
Id: nextMarketIDFromStore,
Pair: "TEST-USD",
Exponent: int32(-6),
ExchangeConfigJson: `{"test_config_placeholder":{}}`,
MinExchanges: 2,
MinPriceChangePpm: uint32(9_999),
},
types.MarketPrice{
Id: nextMarketIDFromStore,
Exponent: int32(-6),
Price: constants.FiveBillion,
},
)
require.NoError(t, err)

// Verify the next market ID is incremented.
nextMarketID = keeper.AcquireNextMarketID(ctx)
require.Equal(t, nextMarketIDFromStore+1, nextMarketID)
}
3 changes: 3 additions & 0 deletions protocol/x/prices/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ const (

// MarketPriceKeyPrefix is the prefix to retrieve all MarketPrices
MarketPriceKeyPrefix = "Price:"

// NextIDKey is the key for the next market ID
NextMarketIDKey = "NextMarketID"
)

0 comments on commit 0c8b7ed

Please sign in to comment.