diff --git a/proto/perp/v1/tx.proto b/proto/perp/v1/tx.proto index 3a24c519a..f187d8fae 100644 --- a/proto/perp/v1/tx.proto +++ b/proto/perp/v1/tx.proto @@ -5,6 +5,7 @@ package nibiru.perp.v1; import "google/api/annotations.proto"; import "cosmos/base/v1beta1/coin.proto"; import "gogoproto/gogo.proto"; +import "perp/v1/state.proto"; option go_package="github.com/NibiruChain/nibiru/x/perp/types"; @@ -18,6 +19,9 @@ service Msg { rpc AddMargin(MsgAddMargin) returns (MsgAddMarginResponse) { option (google.api.http).post = "/nibiru/perp/add_margin"; } + + rpc OpenPosition(MsgOpenPosition) returns (MsgOpenPositionResponse) {} + } /* MsgRemoveMargin: Msg to remove margin. */ @@ -44,4 +48,23 @@ message MsgAddMargin { message MsgAddMarginResponse { // MarginOut: tokens transferred back to the trader +} + +message MsgOpenPosition { + string sender = 1; + string token_pair = 2; + nibiru.perp.v1.Side side = 3; + string quote_asset_amount = 4 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", + (gogoproto.nullable) = false]; + string leverage = 5 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false]; + string base_asset_amount_limit = 6 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", + (gogoproto.nullable) = false]; +} + +message MsgOpenPositionResponse { + } \ No newline at end of file diff --git a/x/perp/client/cli/cli.go b/x/perp/client/cli/cli.go index ab027a3b6..864f7bfe2 100644 --- a/x/perp/client/cli/cli.go +++ b/x/perp/client/cli/cli.go @@ -5,6 +5,8 @@ import ( "fmt" "strings" + "github.com/NibiruChain/nibiru/x/common" + "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" @@ -84,11 +86,76 @@ func GetTxCmd() *cobra.Command { txCmd.AddCommand( RemoveMarginCmd(), AddMarginCmd(), + OpenPositionCmd(), ) return txCmd } +func OpenPositionCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "open-position [buy/sell] [pair] [leverage] [amount/sdk.Dec] [base asset amount limit/sdk.Dec]", + Short: "Opens a position", + Args: cobra.ExactArgs(5), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + txf := tx.NewFactoryCLI(clientCtx, cmd.Flags()).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever) + + var side types.Side + switch args[0] { + case "buy": + side = types.Side_BUY + case "sell": + side = types.Side_SELL + default: + return fmt.Errorf("invalid side: %s", args[0]) + } + + _, err = common.NewTokenPairFromStr(args[1]) + if err != nil { + return err + } + + leverage, err := sdk.NewDecFromStr(args[2]) + if err != nil { + return err + } + + amount, ok := sdk.NewIntFromString(args[3]) + if !ok { + return fmt.Errorf("invalid quote amount: %s", args[3]) + } + + baseAssetAmountLimit, ok := sdk.NewIntFromString(args[4]) + if !ok { + return fmt.Errorf("invalid base amount limit: %s", args[3]) + } + + msg := &types.MsgOpenPosition{ + Sender: clientCtx.GetFromAddress().String(), + TokenPair: args[1], + Side: side, + QuoteAssetAmount: amount, + Leverage: leverage, + BaseAssetAmountLimit: baseAssetAmountLimit, + } + + if err := msg.ValidateBasic(); err != nil { + return err + } + + return tx.GenerateOrBroadcastTxWithFactory(clientCtx, txf, msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + return cmd +} + /* RemoveMarginCmd is a CLI command that removes margin from a position, realizing any outstanding funding payments and decreasing the margin ratio. diff --git a/x/perp/keeper/clearing_house.go b/x/perp/keeper/clearing_house.go index c6ef132de..70e92391f 100644 --- a/x/perp/keeper/clearing_house.go +++ b/x/perp/keeper/clearing_house.go @@ -5,13 +5,12 @@ import ( "fmt" "time" - "github.com/NibiruChain/nibiru/x/common" - "github.com/NibiruChain/nibiru/x/perp/events" - pooltypes "github.com/NibiruChain/nibiru/x/vpool/types" - sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/NibiruChain/nibiru/x/common" + "github.com/NibiruChain/nibiru/x/perp/events" "github.com/NibiruChain/nibiru/x/perp/types" + pooltypes "github.com/NibiruChain/nibiru/x/vpool/types" ) // TODO test: OpenPosition | https://github.com/NibiruChain/nibiru/issues/299 @@ -45,7 +44,7 @@ func (k Keeper) OpenPosition( var positionResp *types.PositionResp sameSideLong := position.Size_.IsPositive() && side == types.Side_BUY sameSideShort := position.Size_.IsNegative() && side == types.Side_SELL - var openSideMatchesPosition bool = (sameSideLong || sameSideShort) + var openSideMatchesPosition = sameSideLong || sameSideShort switch { case isNewPosition || openSideMatchesPosition: // increase position case diff --git a/x/perp/keeper/msg_server.go b/x/perp/keeper/msg_server.go index 762b62a53..e6d225b39 100644 --- a/x/perp/keeper/msg_server.go +++ b/x/perp/keeper/msg_server.go @@ -3,45 +3,48 @@ package keeper import ( "context" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/NibiruChain/nibiru/x/common" "github.com/NibiruChain/nibiru/x/perp/types" ) type msgServer struct { - Keeper + k Keeper } // NewMsgServerImpl returns an implementation of the MsgServer interface // for the provided Keeper. func NewMsgServerImpl(keeper Keeper) types.MsgServer { - return &msgServer{Keeper: keeper} + return &msgServer{k: keeper} } var _ types.MsgServer = msgServer{} -/* -Args: - goCtx - -Returns - MsgRemoveMarginResponse: - error: -*/ -func (k msgServer) MsgRemoveMargin(goCtx context.Context, msg *types.MsgRemoveMargin, -) (*types.MsgRemoveMarginResponse, error) { - removeMarginResponse, err := k.RemoveMargin(goCtx, msg) +func (k msgServer) RemoveMargin(ctx context.Context, margin *types.MsgRemoveMargin) (*types.MsgRemoveMarginResponse, error) { + return k.k.RemoveMargin(ctx, margin) +} + +func (k msgServer) AddMargin(ctx context.Context, margin *types.MsgAddMargin) (*types.MsgAddMarginResponse, error) { + return k.k.AddMargin(ctx, margin) +} + +func (k msgServer) OpenPosition(ctx context.Context, req *types.MsgOpenPosition) (*types.MsgOpenPositionResponse, error) { + pair, err := common.NewTokenPairFromStr(req.TokenPair) if err != nil { - return nil, err + panic(err) // must not happen } - return removeMarginResponse, nil -} + addr, err := sdk.AccAddressFromBech32(req.Sender) + if err != nil { + panic(err) // must not happen + } -func (k msgServer) MsgAddMargin(goCtx context.Context, msg *types.MsgAddMargin, -) (*types.MsgAddMarginResponse, error) { - removeMarginResponse, err := k.AddMargin(goCtx, msg) + sdkCtx := sdk.UnwrapSDKContext(ctx) + err = k.k.OpenPosition(sdkCtx, pair, req.Side, addr, req.QuoteAssetAmount, req.Leverage, req.BaseAssetAmountLimit.ToDec()) if err != nil { return nil, err } - return removeMarginResponse, nil + return &types.MsgOpenPositionResponse{}, nil } diff --git a/x/perp/module.go b/x/perp/module.go index 4ea5dcc2d..48d048e64 100644 --- a/x/perp/module.go +++ b/x/perp/module.go @@ -180,7 +180,7 @@ func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sd // module-specific GRPC queries. func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterQueryServer(cfg.QueryServer(), am.keeper) - types.RegisterMsgServer(cfg.MsgServer(), am.keeper) + types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) } // RegisterInvariants registers the capability module's invariants. diff --git a/x/perp/types/codec.go b/x/perp/types/codec.go index 4f6f61906..56717ab47 100644 --- a/x/perp/types/codec.go +++ b/x/perp/types/codec.go @@ -18,6 +18,7 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { /* implementations */ &MsgRemoveMargin{}, &MsgAddMargin{}, + &MsgOpenPosition{}, ) msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) diff --git a/x/perp/types/msgs.go b/x/perp/types/msgs.go index 6f362262a..0bc178150 100644 --- a/x/perp/types/msgs.go +++ b/x/perp/types/msgs.go @@ -1,11 +1,16 @@ package types import ( + "fmt" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/NibiruChain/nibiru/x/common" ) var _ sdk.Msg = &MsgRemoveMargin{} var _ sdk.Msg = &MsgAddMargin{} +var _ sdk.Msg = &MsgOpenPosition{} // MsgRemoveMargin @@ -42,3 +47,35 @@ func (m MsgAddMargin) GetSigners() []sdk.AccAddress { sender, _ := sdk.AccAddressFromBech32(m.Sender) return []sdk.AccAddress{sender} } + +func (m *MsgOpenPosition) ValidateBasic() error { + if m.Side != Side_SELL && m.Side != Side_BUY { + return fmt.Errorf("invalid side") + } + if _, err := common.NewTokenPairFromStr(m.TokenPair); err != nil { + return err + } + if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil { + return err + } + if !m.Leverage.GT(sdk.ZeroDec()) { + return fmt.Errorf("leverage must always be greater than zero") + } + if !m.BaseAssetAmountLimit.GT(sdk.ZeroInt()) { + return fmt.Errorf("base asset amount limit must always be greater than zero") + } + if m.QuoteAssetAmount.GT(sdk.ZeroInt()) { + return fmt.Errorf("quote asset amount must be always greater than zero") + } + + return nil +} + +func (m *MsgOpenPosition) GetSigners() []sdk.AccAddress { + addr, err := sdk.AccAddressFromBech32(m.Sender) + if err != nil { + panic(err) + } + + return []sdk.AccAddress{addr} +} diff --git a/x/perp/types/tx.pb.go b/x/perp/types/tx.pb.go index dad3e8cbc..5e632d60a 100644 --- a/x/perp/types/tx.pb.go +++ b/x/perp/types/tx.pb.go @@ -235,47 +235,157 @@ func (m *MsgAddMarginResponse) XXX_DiscardUnknown() { var xxx_messageInfo_MsgAddMarginResponse proto.InternalMessageInfo +type MsgOpenPosition struct { + Sender string `protobuf:"bytes,1,opt,name=sender,proto3" json:"sender,omitempty"` + TokenPair string `protobuf:"bytes,2,opt,name=token_pair,json=tokenPair,proto3" json:"token_pair,omitempty"` + Side Side `protobuf:"varint,3,opt,name=side,proto3,enum=nibiru.perp.v1.Side" json:"side,omitempty"` + QuoteAssetAmount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,4,opt,name=quote_asset_amount,json=quoteAssetAmount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"quote_asset_amount"` + Leverage github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,5,opt,name=leverage,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"leverage"` + BaseAssetAmountLimit github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,6,opt,name=base_asset_amount_limit,json=baseAssetAmountLimit,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"base_asset_amount_limit"` +} + +func (m *MsgOpenPosition) Reset() { *m = MsgOpenPosition{} } +func (m *MsgOpenPosition) String() string { return proto.CompactTextString(m) } +func (*MsgOpenPosition) ProtoMessage() {} +func (*MsgOpenPosition) Descriptor() ([]byte, []int) { + return fileDescriptor_28f06b306d51dcfb, []int{4} +} +func (m *MsgOpenPosition) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgOpenPosition) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgOpenPosition.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgOpenPosition) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgOpenPosition.Merge(m, src) +} +func (m *MsgOpenPosition) XXX_Size() int { + return m.Size() +} +func (m *MsgOpenPosition) XXX_DiscardUnknown() { + xxx_messageInfo_MsgOpenPosition.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgOpenPosition proto.InternalMessageInfo + +func (m *MsgOpenPosition) GetSender() string { + if m != nil { + return m.Sender + } + return "" +} + +func (m *MsgOpenPosition) GetTokenPair() string { + if m != nil { + return m.TokenPair + } + return "" +} + +func (m *MsgOpenPosition) GetSide() Side { + if m != nil { + return m.Side + } + return Side_BUY +} + +type MsgOpenPositionResponse struct { +} + +func (m *MsgOpenPositionResponse) Reset() { *m = MsgOpenPositionResponse{} } +func (m *MsgOpenPositionResponse) String() string { return proto.CompactTextString(m) } +func (*MsgOpenPositionResponse) ProtoMessage() {} +func (*MsgOpenPositionResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_28f06b306d51dcfb, []int{5} +} +func (m *MsgOpenPositionResponse) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *MsgOpenPositionResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_MsgOpenPositionResponse.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *MsgOpenPositionResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_MsgOpenPositionResponse.Merge(m, src) +} +func (m *MsgOpenPositionResponse) XXX_Size() int { + return m.Size() +} +func (m *MsgOpenPositionResponse) XXX_DiscardUnknown() { + xxx_messageInfo_MsgOpenPositionResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_MsgOpenPositionResponse proto.InternalMessageInfo + func init() { proto.RegisterType((*MsgRemoveMargin)(nil), "nibiru.perp.v1.MsgRemoveMargin") proto.RegisterType((*MsgRemoveMarginResponse)(nil), "nibiru.perp.v1.MsgRemoveMarginResponse") proto.RegisterType((*MsgAddMargin)(nil), "nibiru.perp.v1.MsgAddMargin") proto.RegisterType((*MsgAddMarginResponse)(nil), "nibiru.perp.v1.MsgAddMarginResponse") + proto.RegisterType((*MsgOpenPosition)(nil), "nibiru.perp.v1.MsgOpenPosition") + proto.RegisterType((*MsgOpenPositionResponse)(nil), "nibiru.perp.v1.MsgOpenPositionResponse") } func init() { proto.RegisterFile("perp/v1/tx.proto", fileDescriptor_28f06b306d51dcfb) } var fileDescriptor_28f06b306d51dcfb = []byte{ - // 467 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x53, 0x41, 0x6b, 0x13, 0x41, - 0x18, 0xcd, 0xb4, 0x12, 0xc8, 0x58, 0x5a, 0x59, 0x4a, 0x9b, 0x2e, 0x71, 0x53, 0x16, 0xd1, 0x22, - 0x38, 0x43, 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0x74, 0x7b, 0xc1, 0x94, 0x15, 0x45, 0xed, 0xd8, 0x3f, 0x3c, 0x9f, 0x24, 0xe8, 0x62, 0x92, 0xa0, - 0xef, 0x93, 0x04, 0x7d, 0x9c, 0x26, 0x8d, 0x8b, 0x69, 0xd2, 0xf8, 0x3a, 0x4d, 0x1a, 0xaf, 0x1e, - 0x5e, 0xdb, 0xf0, 0x73, 0x3f, 0x7c, 0x70, 0xcc, 0xa4, 0x9a, 0x0b, 0x9d, 0x06, 0x29, 0xbf, 0xe9, - 0xbc, 0xe9, 0xbf, 0x8a, 0xc7, 0xbf, 0x02, 0x00, 0x00, 0xff, 0xff, 0x5e, 0xb4, 0x54, 0x9f, 0x8d, - 0x03, 0x00, 0x00, + // 612 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x54, 0x4f, 0x6b, 0x13, 0x4f, + 0x18, 0xce, 0xb6, 0xfd, 0x85, 0x5f, 0xc6, 0xd2, 0x96, 0x31, 0xb4, 0xdb, 0x50, 0x37, 0x65, 0x11, + 0x2d, 0x82, 0x33, 0xa4, 0x1e, 0xbc, 0x09, 0xfd, 0x73, 0x51, 0x8c, 0x0d, 0xeb, 0x41, 0x28, 0xc2, + 0x32, 0xc9, 0xbe, 0x6e, 0x87, 0x66, 0x67, 0xd6, 0x9d, 0xd9, 0xd0, 0x82, 0x08, 0xea, 0x17, 0x10, + 0xfc, 0x26, 0x7e, 0x8a, 0x1e, 0x2b, 0x5e, 0xc4, 0x43, 0x91, 0xc4, 0x0f, 0x22, 0x3b, 0xbb, 0x89, + 0x49, 0x08, 0xfe, 0xc9, 0xc5, 0x53, 0x26, 0xf3, 0x3e, 0xf3, 0xbc, 0xcf, 0x3c, 0xef, 0x33, 0x8b, + 0xd6, 0x62, 0x48, 0x62, 0xda, 0x6b, 0x50, 0x7d, 0x46, 0xe2, 0x44, 0x6a, 0x89, 0x57, 0x04, 0x6f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 0xfe, 0x4e, + 0x3d, 0x89, 0xda, 0xed, 0xdf, 0x00, 0x46, 0x11, 0x70, 0xdf, 0x7e, 0xfe, 0xfe, 0x61, 0x61, 0xcb, + 0xad, 0xd1, 0xfc, 0x00, 0x35, 0x0f, 0x3f, 0x31, 0x50, 0x3f, 0x8f, 0x0f, 0x8e, 0x51, 0xe5, 0x67, + 0x76, 0xb7, 0x66, 0x30, 0x8f, 0xaa, 0xb5, 0x9b, 0xbf, 0xaa, 0x8e, 0x9a, 0xd6, 0x4d, 0xd3, 0x4d, + 0x77, 0x63, 0xa2, 0x29, 0x0b, 0x82, 0x61, 0xc7, 0x63, 0xb4, 0x3c, 0x11, 0xca, 0x59, 0xf7, 0x1d, + 0x07, 0xcc, 0xbc, 0xef, 0x2c, 0x47, 0xdd, 0xd2, 0xfe, 0xe1, 0x45, 0xdf, 0xb1, 0x2e, 0xfb, 0x8e, + 0xf5, 0xad, 0xef, 0x58, 0xef, 0x07, 0x4e, 0xe9, 0x72, 0xe0, 0x94, 0xbe, 0x0c, 0x9c, 0xd2, 0xf1, + 0x9d, 0xb1, 0x31, 0x3e, 0x31, 0x74, 0x07, 0x27, 0x8c, 0x8b, 0xa1, 0xc8, 0xb3, 0x5c, 0xa6, 0x19, + 0x67, 0xbb, 0x6c, 0x3e, 0x89, 0xf7, 0x7e, 0x04, 0x00, 0x00, 0xff, 0xff, 0xb1, 0x1c, 0x03, 0xb7, + 0x9f, 0x05, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -292,6 +402,7 @@ const _ = grpc.SupportPackageIsVersion4 type MsgClient interface { RemoveMargin(ctx context.Context, in *MsgRemoveMargin, opts ...grpc.CallOption) (*MsgRemoveMarginResponse, error) AddMargin(ctx context.Context, in *MsgAddMargin, opts ...grpc.CallOption) (*MsgAddMarginResponse, error) + OpenPosition(ctx context.Context, in *MsgOpenPosition, opts ...grpc.CallOption) (*MsgOpenPositionResponse, error) } type msgClient struct { @@ -320,10 +431,20 @@ func (c *msgClient) AddMargin(ctx context.Context, in *MsgAddMargin, opts ...grp return out, nil } +func (c *msgClient) OpenPosition(ctx context.Context, in *MsgOpenPosition, opts ...grpc.CallOption) (*MsgOpenPositionResponse, error) { + out := new(MsgOpenPositionResponse) + err := c.cc.Invoke(ctx, "/nibiru.perp.v1.Msg/OpenPosition", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // MsgServer is the server API for Msg service. type MsgServer interface { RemoveMargin(context.Context, *MsgRemoveMargin) (*MsgRemoveMarginResponse, error) AddMargin(context.Context, *MsgAddMargin) (*MsgAddMarginResponse, error) + OpenPosition(context.Context, *MsgOpenPosition) (*MsgOpenPositionResponse, error) } // UnimplementedMsgServer can be embedded to have forward compatible implementations. @@ -336,6 +457,9 @@ func (*UnimplementedMsgServer) RemoveMargin(ctx context.Context, req *MsgRemoveM func (*UnimplementedMsgServer) AddMargin(ctx context.Context, req *MsgAddMargin) (*MsgAddMarginResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method AddMargin not implemented") } +func (*UnimplementedMsgServer) OpenPosition(ctx context.Context, req *MsgOpenPosition) (*MsgOpenPositionResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method OpenPosition not implemented") +} func RegisterMsgServer(s grpc1.Server, srv MsgServer) { s.RegisterService(&_Msg_serviceDesc, srv) @@ -377,6 +501,24 @@ func _Msg_AddMargin_Handler(srv interface{}, ctx context.Context, dec func(inter return interceptor(ctx, in, info, handler) } +func _Msg_OpenPosition_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(MsgOpenPosition) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(MsgServer).OpenPosition(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/nibiru.perp.v1.Msg/OpenPosition", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(MsgServer).OpenPosition(ctx, req.(*MsgOpenPosition)) + } + return interceptor(ctx, in, info, handler) +} + var _Msg_serviceDesc = grpc.ServiceDesc{ ServiceName: "nibiru.perp.v1.Msg", HandlerType: (*MsgServer)(nil), @@ -389,6 +531,10 @@ var _Msg_serviceDesc = grpc.ServiceDesc{ MethodName: "AddMargin", Handler: _Msg_AddMargin_Handler, }, + { + MethodName: "OpenPosition", + Handler: _Msg_OpenPosition_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "perp/v1/tx.proto", @@ -554,6 +700,101 @@ func (m *MsgAddMarginResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *MsgOpenPosition) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MsgOpenPosition) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgOpenPosition) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size := m.BaseAssetAmountLimit.Size() + i -= size + if _, err := m.BaseAssetAmountLimit.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x32 + { + size := m.Leverage.Size() + i -= size + if _, err := m.Leverage.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x2a + { + size := m.QuoteAssetAmount.Size() + i -= size + if _, err := m.QuoteAssetAmount.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintTx(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x22 + if m.Side != 0 { + i = encodeVarintTx(dAtA, i, uint64(m.Side)) + i-- + dAtA[i] = 0x18 + } + if len(m.TokenPair) > 0 { + i -= len(m.TokenPair) + copy(dAtA[i:], m.TokenPair) + i = encodeVarintTx(dAtA, i, uint64(len(m.TokenPair))) + i-- + dAtA[i] = 0x12 + } + if len(m.Sender) > 0 { + i -= len(m.Sender) + copy(dAtA[i:], m.Sender) + i = encodeVarintTx(dAtA, i, uint64(len(m.Sender))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func (m *MsgOpenPositionResponse) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *MsgOpenPositionResponse) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *MsgOpenPositionResponse) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + return len(dAtA) - i, nil +} + func encodeVarintTx(dAtA []byte, offset int, v uint64) int { offset -= sovTx(v) base := offset @@ -625,6 +866,41 @@ func (m *MsgAddMarginResponse) Size() (n int) { return n } +func (m *MsgOpenPosition) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Sender) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + l = len(m.TokenPair) + if l > 0 { + n += 1 + l + sovTx(uint64(l)) + } + if m.Side != 0 { + n += 1 + sovTx(uint64(m.Side)) + } + l = m.QuoteAssetAmount.Size() + n += 1 + l + sovTx(uint64(l)) + l = m.Leverage.Size() + n += 1 + l + sovTx(uint64(l)) + l = m.BaseAssetAmountLimit.Size() + n += 1 + l + sovTx(uint64(l)) + return n +} + +func (m *MsgOpenPositionResponse) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + return n +} + func sovTx(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -1092,6 +1368,291 @@ func (m *MsgAddMarginResponse) Unmarshal(dAtA []byte) error { } return nil } +func (m *MsgOpenPosition) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgOpenPosition: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgOpenPosition: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Sender", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Sender = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field TokenPair", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.TokenPair = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 3: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field Side", wireType) + } + m.Side = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.Side |= Side(b&0x7F) << shift + if b < 0x80 { + break + } + } + case 4: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field QuoteAssetAmount", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.QuoteAssetAmount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Leverage", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Leverage.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 6: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field BaseAssetAmountLimit", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthTx + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthTx + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.BaseAssetAmountLimit.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *MsgOpenPositionResponse) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTx + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: MsgOpenPositionResponse: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: MsgOpenPositionResponse: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + default: + iNdEx = preIndex + skippy, err := skipTx(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTx + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipTx(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/x/testutil/mock/perp_interfaces.go b/x/testutil/mock/perp_interfaces.go index 52eb5560c..e3cddc8c2 100644 --- a/x/testutil/mock/perp_interfaces.go +++ b/x/testutil/mock/perp_interfaces.go @@ -8,12 +8,13 @@ import ( reflect "reflect" time "time" - common "github.com/NibiruChain/nibiru/x/common" - types "github.com/NibiruChain/nibiru/x/pricefeed/types" - types0 "github.com/NibiruChain/nibiru/x/vpool/types" types1 "github.com/cosmos/cosmos-sdk/types" types2 "github.com/cosmos/cosmos-sdk/x/auth/types" gomock "github.com/golang/mock/gomock" + + common "github.com/NibiruChain/nibiru/x/common" + types "github.com/NibiruChain/nibiru/x/pricefeed/types" + types0 "github.com/NibiruChain/nibiru/x/vpool/types" ) // MockAccountKeeper is a mock of AccountKeeper interface.