Skip to content

Commit

Permalink
add query versioned markets
Browse files Browse the repository at this point in the history
  • Loading branch information
jgimeno committed Sep 15, 2023
1 parent 83f6f97 commit 1e65fc0
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 74 deletions.
4 changes: 3 additions & 1 deletion proto/nibiru/perp/v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ message AmmMarket {
AMM amm = 2 [ (gogoproto.nullable) = false ];
}

message QueryMarketsRequest {}
message QueryMarketsRequest {
bool versioned = 1;
}

message QueryMarketsResponse {
repeated AmmMarket amm_markets = 1 [ (gogoproto.nullable) = false ];
Expand Down
13 changes: 12 additions & 1 deletion x/perp/v2/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
types "github.com/NibiruChain/nibiru/x/perp/v2/types"
)

const FlagVersioned = "versioned"

// GetQueryCmd returns the cli query commands for this module
func GetQueryCmd() *cobra.Command {
// Group stablecoin queries under a subcommand
Expand Down Expand Up @@ -156,7 +158,14 @@ func CmdQueryMarkets() *cobra.Command {

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.QueryMarkets(cmd.Context(), &types.QueryMarketsRequest{})
versioned, err := cmd.Flags().GetBool(FlagVersioned)
if err != nil {
return err
}

res, err := queryClient.QueryMarkets(cmd.Context(), &types.QueryMarketsRequest{
Versioned: versioned,
})
if err != nil {
return err
}
Expand All @@ -165,6 +174,8 @@ func CmdQueryMarkets() *cobra.Command {
},
}

cmd.Flags().Bool(FlagVersioned, false, "show all markets with version, enabled and disabled")

flags.AddQueryFlagsToCmd(cmd)

return cmd
Expand Down
31 changes: 22 additions & 9 deletions x/perp/v2/integration/action/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (c createMarketAction) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context
}

// CreateCustomMarket creates a market with custom parameters
func CreateCustomMarket(pair asset.Pair, marketModifiers ...marketModifier) action.Action {
func CreateCustomMarket(pair asset.Pair, marketModifiers ...MarketModifier) action.Action {
market := types.DefaultMarket(pair)
amm := types.AMM{
Pair: pair,
Expand All @@ -75,52 +75,65 @@ func CreateCustomMarket(pair asset.Pair, marketModifiers ...marketModifier) acti
}
}

type marketModifier func(market *types.Market, amm *types.AMM)
type MarketModifier func(market *types.Market, amm *types.AMM)

func WithPrepaidBadDebt(amount sdkmath.Int) marketModifier {
func WithPrepaidBadDebt(amount sdkmath.Int) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
market.PrepaidBadDebt = sdk.NewCoin(market.Pair.QuoteDenom(), amount)
}
}

func WithPricePeg(newValue sdk.Dec) marketModifier {
func WithPricePeg(newValue sdk.Dec) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
amm.PriceMultiplier = newValue
}
}

func WithTotalLong(amount sdk.Dec) marketModifier {
func WithTotalLong(amount sdk.Dec) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
amm.TotalLong = amount
}
}

func WithTotalShort(amount sdk.Dec) marketModifier {
func WithTotalShort(amount sdk.Dec) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
amm.TotalShort = amount
}
}

func WithSqrtDepth(amount sdk.Dec) marketModifier {
func WithSqrtDepth(amount sdk.Dec) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
amm.SqrtDepth = amount
amm.BaseReserve = amount
amm.QuoteReserve = amount
}
}

func WithLatestMarketCPF(amount sdk.Dec) marketModifier {
func WithLatestMarketCPF(amount sdk.Dec) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
market.LatestCumulativePremiumFraction = amount
}
}

func WithMaxFundingRate(amount sdk.Dec) marketModifier {
func WithMaxFundingRate(amount sdk.Dec) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
market.MaxFundingRate = amount
}
}

func WithVersion(version uint64) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
market.Version = version
amm.Version = version
}
}

func WithEnabled(enabled bool) MarketModifier {
return func(market *types.Market, amm *types.AMM) {
market.Enabled = enabled
}
}

type editPriceMultiplier struct {
pair asset.Pair
newValue sdk.Dec
Expand Down
25 changes: 22 additions & 3 deletions x/perp/v2/integration/action/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ func (q queryPositionNotFound) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Cont
Trader: q.traderAddress.String(),
})
if !errors.Is(err, collections.ErrNotFound) {
return ctx, fmt.Errorf("expected position not found, but found a position for pair %s, trader %s", q.pair, q.traderAddress), false
return ctx, fmt.Errorf(
"expected position not found, but found a position for pair %s, trader %s",
q.pair,
q.traderAddress,
), false
}

return ctx, nil, false
Expand All @@ -145,13 +149,16 @@ func QueryPositionNotFound(pair asset.Pair, traderAddress sdk.AccAddress) action
}

type queryMarkets struct {
versioned bool
allResponseCheckers []QueryMarketsChecker
}

func (q queryMarkets) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, error, bool) {
queryServer := keeper.NewQuerier(app.PerpKeeperV2)

resp, err := queryServer.QueryMarkets(sdk.WrapSDKContext(ctx), &types.QueryMarketsRequest{})
resp, err := queryServer.QueryMarkets(sdk.WrapSDKContext(ctx), &types.QueryMarketsRequest{
Versioned: q.versioned,
})
if err != nil {
return ctx, err, false
}
Expand All @@ -165,8 +172,10 @@ func (q queryMarkets) Do(app *app.NibiruApp, ctx sdk.Context) (sdk.Context, erro
return ctx, nil, false
}

func QueryMarkets(responseCheckers ...QueryMarketsChecker) action.Action {
// QueryMarkets queries all markets, versioned contains active and inactive markets
func QueryMarkets(versioned bool, responseCheckers ...QueryMarketsChecker) action.Action {
return queryMarkets{
versioned: versioned,
allResponseCheckers: responseCheckers,
}
}
Expand All @@ -188,6 +197,16 @@ func QueryMarkets_MarketsShouldContain(expectedMarket types.Market) QueryMarkets
}
}

func QueryMarkets_ShouldLength(length int) QueryMarketsChecker {
return func(resp []types.AmmMarket) error {
if len(resp) != length {
return fmt.Errorf("expected markets to have length %d, got %d", length, len(resp))
}

return nil
}
}

type queryModuleAccounts struct {
allResponseCheckers []QueryModuleAccountsChecker
}
Expand Down
4 changes: 2 additions & 2 deletions x/perp/v2/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ func (q queryServer) ModuleAccounts(
}

func (q queryServer) QueryMarkets(
goCtx context.Context, _ *types.QueryMarketsRequest,
goCtx context.Context, req *types.QueryMarketsRequest,
) (*types.QueryMarketsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

var ammMarkets []types.AmmMarket
markets := q.k.Markets.Iterate(ctx, collections.Range[collections.Pair[asset.Pair, uint64]]{}).Values()
for _, market := range markets {
// disabled markets are not returned
if !market.Enabled {
if !req.Versioned && !market.Enabled {
continue
}

Expand Down
16 changes: 15 additions & 1 deletion x/perp/v2/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func TestQueryMarkets(t *testing.T) {
),
).
Then(
QueryMarkets(QueryMarkets_MarketsShouldContain(types.DefaultMarket(pair))),
QueryMarkets(false, QueryMarkets_MarketsShouldContain(types.DefaultMarket(pair))),
QueryModuleAccounts(QueryModuleAccounts_ModulesBalanceShouldBe(
map[string]sdk.Coins{
"perp_ef": sdk.NewCoins(
Expand All @@ -358,6 +358,20 @@ func TestQueryMarkets(t *testing.T) {
},
)),
),
TC("versioned, all markets (active and inactive)").Given(
CreateCustomMarket("BTC:USD", WithVersion(1), WithEnabled(true)),
CreateCustomMarket("ETC:USD", WithVersion(1), WithEnabled(false)),
CreateCustomMarket("ETC:USD", WithVersion(2), WithEnabled(true)),
).Then(
QueryMarkets(true, QueryMarkets_ShouldLength(3)),
),
TC("not versioned, only active markets").Given(
CreateCustomMarket("BTC:USD", WithVersion(1), WithEnabled(true)),
CreateCustomMarket("ETC:USD", WithVersion(1), WithEnabled(false)),
CreateCustomMarket("ETC:USD", WithVersion(2), WithEnabled(true)),
).Then(
QueryMarkets(true, QueryMarkets_ShouldLength(3)),
),
}

NewTestSuite(t).WithTestCases(tc...).Run()
Expand Down
2 changes: 1 addition & 1 deletion x/perp/v2/keeper/twap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

/*
Gets the time-weighted average price from [ ctx.BlockTime() - interval, ctx.BlockTime() )
CalcTwap Gets the time-weighted average price from [ ctx.BlockTime() - interval, ctx.BlockTime() )
Note the open-ended right bracket.
args:
Expand Down
Loading

0 comments on commit 1e65fc0

Please sign in to comment.