Skip to content

Commit

Permalink
Add MsgCreateMarketPermissionless msg
Browse files Browse the repository at this point in the history
Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
  • Loading branch information
shrenujb committed Jul 30, 2024
1 parent c8b7745 commit 8cdc632
Show file tree
Hide file tree
Showing 9 changed files with 646 additions and 25 deletions.
130 changes: 130 additions & 0 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/listing/tx.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SubaccountId, SubaccountIdSDKType } from "../subaccounts/subaccount";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/**
Expand Down Expand Up @@ -28,6 +29,36 @@ export interface MsgSetMarketsHardCapResponse {}
/** MsgSetMarketsHardCapResponse defines the MsgSetMarketsHardCap response */

export interface MsgSetMarketsHardCapResponseSDKType {}
/** MsgCreateMarketPermissionless is a message used to create new markets without // going through x/gov */

export interface MsgCreateMarketPermissionless {
/** The name of the `Perpetual` (e.g. `BTC-USD`). */
ticker: string;
/** The subaccount to deposit from. */

subaccountId?: SubaccountId;
/** Number of quote quantums to deposit. */

quoteQuantums: Uint8Array;
}
/** MsgCreateMarketPermissionless is a message used to create new markets without // going through x/gov */

export interface MsgCreateMarketPermissionlessSDKType {
/** The name of the `Perpetual` (e.g. `BTC-USD`). */
ticker: string;
/** The subaccount to deposit from. */

subaccount_id?: SubaccountIdSDKType;
/** Number of quote quantums to deposit. */

quote_quantums: Uint8Array;
}
/** MsgCreateMarketPermissionlessResponse defines the MsgCreateMarketPermissionless response */

export interface MsgCreateMarketPermissionlessResponse {}
/** MsgCreateMarketPermissionlessResponse defines the MsgCreateMarketPermissionless response */

export interface MsgCreateMarketPermissionlessResponseSDKType {}

function createBaseMsgSetMarketsHardCap(): MsgSetMarketsHardCap {
return {
Expand Down Expand Up @@ -116,4 +147,103 @@ export const MsgSetMarketsHardCapResponse = {
return message;
}

};

function createBaseMsgCreateMarketPermissionless(): MsgCreateMarketPermissionless {
return {
ticker: "",
subaccountId: undefined,
quoteQuantums: new Uint8Array()
};
}

export const MsgCreateMarketPermissionless = {
encode(message: MsgCreateMarketPermissionless, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.ticker !== "") {
writer.uint32(10).string(message.ticker);
}

if (message.subaccountId !== undefined) {
SubaccountId.encode(message.subaccountId, writer.uint32(18).fork()).ldelim();
}

if (message.quoteQuantums.length !== 0) {
writer.uint32(26).bytes(message.quoteQuantums);
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): MsgCreateMarketPermissionless {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseMsgCreateMarketPermissionless();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.ticker = reader.string();
break;

case 2:
message.subaccountId = SubaccountId.decode(reader, reader.uint32());
break;

case 3:
message.quoteQuantums = reader.bytes();
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<MsgCreateMarketPermissionless>): MsgCreateMarketPermissionless {
const message = createBaseMsgCreateMarketPermissionless();
message.ticker = object.ticker ?? "";
message.subaccountId = object.subaccountId !== undefined && object.subaccountId !== null ? SubaccountId.fromPartial(object.subaccountId) : undefined;
message.quoteQuantums = object.quoteQuantums ?? new Uint8Array();
return message;
}

};

function createBaseMsgCreateMarketPermissionlessResponse(): MsgCreateMarketPermissionlessResponse {
return {};
}

export const MsgCreateMarketPermissionlessResponse = {
encode(_: MsgCreateMarketPermissionlessResponse, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): MsgCreateMarketPermissionlessResponse {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseMsgCreateMarketPermissionlessResponse();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(_: DeepPartial<MsgCreateMarketPermissionlessResponse>): MsgCreateMarketPermissionlessResponse {
const message = createBaseMsgCreateMarketPermissionlessResponse();
return message;
}

};
24 changes: 24 additions & 0 deletions proto/dydxprotocol/listing/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package dydxprotocol.listing;

import "cosmos_proto/cosmos.proto";
import "cosmos/msg/v1/msg.proto";
import "gogoproto/gogo.proto";
import "dydxprotocol/subaccounts/subaccount.proto";


option go_package = "github.com/dydxprotocol/v4-chain/protocol/x/listing/types";

Expand All @@ -27,3 +30,24 @@ message MsgSetMarketsHardCap {

// MsgSetMarketsHardCapResponse defines the MsgSetMarketsHardCap response
message MsgSetMarketsHardCapResponse {}

// MsgCreateMarketPermissionless is a message used to create new markets without // going through x/gov
message MsgCreateMarketPermissionless {
option (cosmos.msg.v1.signer) = "subaccount_id";

// The name of the `Perpetual` (e.g. `BTC-USD`).
string ticker = 1;

// The subaccount to deposit from.
dydxprotocol.subaccounts.SubaccountId subaccount_id = 2;

// Number of quote quantums to deposit.
bytes quote_quantums = 3 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
}

// MsgCreateMarketPermissionlessResponse defines the MsgCreateMarketPermissionless response
message MsgCreateMarketPermissionlessResponse {}
6 changes: 4 additions & 2 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ var (
"/dydxprotocol.govplus.MsgSlashValidatorResponse": {},

// listing
"/dydxprotocol.listing.MsgSetMarketsHardCap": {},
"/dydxprotocol.listing.MsgSetMarketsHardCapResponse": {},
"/dydxprotocol.listing.MsgSetMarketsHardCap": {},
"/dydxprotocol.listing.MsgSetMarketsHardCapResponse": {},
"/dydxprotocol.listing.MsgCreateMarketPermissionless": {},
"/dydxprotocol.listing.MsgCreateMarketPermissionlessResponse": {},

// perpetuals
"/dydxprotocol.perpetuals.MsgAddPremiumVotes": {},
Expand Down
6 changes: 4 additions & 2 deletions protocol/app/msgs/internal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ var (
"/dydxprotocol.govplus.MsgSlashValidatorResponse": nil,

// listing
"/dydxprotocol.listing.MsgSetMarketsHardCap": &listing.MsgSetMarketsHardCap{},
"/dydxprotocol.listing.MsgSetMarketsHardCapResponse": nil,
"/dydxprotocol.listing.MsgSetMarketsHardCap": &listing.MsgSetMarketsHardCap{},
"/dydxprotocol.listing.MsgSetMarketsHardCapResponse": nil,
"/dydxprotocol.listing.MsgCreateMarketPermissionless": &listing.MsgCreateMarketPermissionless{},
"/dydxprotocol.listing.MsgCreateMarketPermissionlessResponse": nil,

// perpetuals
"/dydxprotocol.perpetuals.MsgCreatePerpetual": &perpetuals.MsgCreatePerpetual{},
Expand Down
2 changes: 2 additions & 0 deletions protocol/app/msgs/internal_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func TestInternalMsgSamples_Gov_Key(t *testing.T) {
// listing
"/dydxprotocol.listing.MsgSetMarketsHardCap",
"/dydxprotocol.listing.MsgSetMarketsHardCapResponse",
"/dydxprotocol.listing.MsgCreateMarketPermissionless",
"/dydxprotocol.listing.MsgCreateMarketPermissionlessResponse",

// perpeutals
"/dydxprotocol.perpetuals.MsgCreatePerpetual",
Expand Down
40 changes: 40 additions & 0 deletions protocol/x/listing/keeper/msg_create_market_permissionless.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package keeper

import (
"context"

"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/listing/types"
)

func (k msgServer) CreateMarketPermissionless(
goCtx context.Context,
msg *types.MsgCreateMarketPermissionless,
) (*types.MsgCreateMarketPermissionlessResponse, error) {
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

// Check if the number of listed markets is above the hard cap
numPerpetuals := len(k.PerpetualsKeeper.GetAllPerpetuals(ctx))
if uint32(numPerpetuals) > k.Keeper.GetMarketsHardCap(ctx) {
return nil, types.ErrMarketsHardCapReached
}

marketId, err := k.Keeper.CreateMarket(ctx, msg.Ticker)
if err != nil {
return nil, err
}

perpetualId, err := k.Keeper.CreatePerpetual(ctx, marketId, msg.Ticker)
if err != nil {
return nil, err
}

_, err = k.Keeper.CreateClobPair(ctx, perpetualId)
if err != nil {
return nil, err
}

// TODO: vault deposit for PML

return &types.MsgCreateMarketPermissionlessResponse{}, nil
}
6 changes: 6 additions & 0 deletions protocol/x/listing/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ var (
1,
"reference price is zero",
)

ErrMarketsHardCapReached = errorsmod.Register(
ModuleName,
2,
"listed markets hard cap reached",
)
)
1 change: 1 addition & 0 deletions protocol/x/listing/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ type PerpetualsKeeper interface {
marketType perpetualtypes.PerpetualMarketType,
) (perpetualtypes.Perpetual, error)
AcquireNextPerpetualID(ctx sdk.Context) uint32
GetAllPerpetuals(ctx sdk.Context) (list []perpetualtypes.Perpetual)
}
Loading

0 comments on commit 8cdc632

Please sign in to comment.