From 0a5c637dbc0e356da7d2d9a5f23c81730da7237f Mon Sep 17 00:00:00 2001 From: Amit Yadav Date: Fri, 1 Nov 2024 21:51:02 +0530 Subject: [PATCH] [Tradeshield]: Close order (#897) * updates * fix tests * fix migration --- .../client/cli/tx_perpetual_order.go | 30 ++++++++----------- .../client/cli/tx_perpetual_order_test.go | 1 - x/tradeshield/migrations/v4_migrator.go | 2 ++ x/tradeshield/migrations/v5_migrator.go | 11 ------- x/tradeshield/module.go | 2 +- .../types/messages_pending_perpetual_order.go | 2 -- 6 files changed, 16 insertions(+), 32 deletions(-) delete mode 100644 x/tradeshield/migrations/v5_migrator.go diff --git a/x/tradeshield/client/cli/tx_perpetual_order.go b/x/tradeshield/client/cli/tx_perpetual_order.go index 1d82d02c0..370d1ac82 100644 --- a/x/tradeshield/client/cli/tx_perpetual_order.go +++ b/x/tradeshield/client/cli/tx_perpetual_order.go @@ -17,9 +17,9 @@ import ( func CmdCreatePerpetualOpenOrder() *cobra.Command { cmd := &cobra.Command{ - Use: "create-perpetual-open-order [order-type] [position] [leverage] [pool-id] [trading-asset] [collateral] [trigger-price]", + Use: "create-perpetual-open-order [position] [leverage] [pool-id] [trading-asset] [collateral] [trigger-price]", Short: "Create a new perpetual open order", - Args: cobra.ExactArgs(7), + Args: cobra.ExactArgs(6), RunE: func(cmd *cobra.Command, args []string) (err error) { clientCtx, err := client.GetClientTxContext(cmd) if err != nil { @@ -31,30 +31,28 @@ func CmdCreatePerpetualOpenOrder() *cobra.Command { return errors.New("signer address is missing") } - orderType := types.GetPerpetualOrderTypeFromString(args[0]) + position := types.PerpetualPosition(perptypes.GetPositionFromString(args[0])) - position := types.PerpetualPosition(perptypes.GetPositionFromString(args[1])) - - leverage, err := sdk.NewDecFromStr(args[2]) + leverage, err := sdk.NewDecFromStr(args[1]) if err != nil { return err } - poolId, err := strconv.ParseUint(args[3], 10, 64) + poolId, err := strconv.ParseUint(args[2], 10, 64) if err != nil { return err } - tradingAsset := args[4] + tradingAsset := args[3] - collateral, err := sdk.ParseCoinNormalized(args[5]) + collateral, err := sdk.ParseCoinNormalized(args[4]) if err != nil { return err } triggerPrice := types.TriggerPrice{ TradingAssetDenom: tradingAsset, - Rate: sdk.MustNewDecFromStr(args[6]), + Rate: sdk.MustNewDecFromStr(args[5]), } takeProfitPriceStr, err := cmd.Flags().GetString(perpcli.FlagTakeProfitPrice) @@ -89,7 +87,6 @@ func CmdCreatePerpetualOpenOrder() *cobra.Command { msg := types.NewMsgCreatePerpetualOpenOrder( signer.String(), - orderType, triggerPrice, collateral, tradingAsset, @@ -116,9 +113,9 @@ func CmdCreatePerpetualOpenOrder() *cobra.Command { func CmdCreatePerpetualCloseOrder() *cobra.Command { cmd := &cobra.Command{ - Use: "create-perpetual-close-order [order-type] [trigger-price] [position-id]", + Use: "create-perpetual-close-order [trigger-price] [position-id]", Short: "Create a new perpetual close order", - Args: cobra.ExactArgs(8), + Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) (err error) { clientCtx, err := client.GetClientTxContext(cmd) if err != nil { @@ -126,20 +123,19 @@ func CmdCreatePerpetualCloseOrder() *cobra.Command { } addr := clientCtx.GetFromAddress().String() - orderType := types.GetPerpetualOrderTypeFromString(args[0]) // trading asset will be filled by message handler triggerPrice := types.TriggerPrice{ TradingAssetDenom: "", - Rate: sdk.MustNewDecFromStr(args[1]), + Rate: sdk.MustNewDecFromStr(args[0]), } - positionId, err := strconv.ParseUint(args[2], 10, 64) + positionId, err := strconv.ParseUint(args[1], 10, 64) if err != nil { return err } - msg := types.NewMsgCreatePerpetualCloseOrder(addr, orderType, triggerPrice, positionId) + msg := types.NewMsgCreatePerpetualCloseOrder(addr, triggerPrice, positionId) if err := msg.ValidateBasic(); err != nil { return err } diff --git a/x/tradeshield/client/cli/tx_perpetual_order_test.go b/x/tradeshield/client/cli/tx_perpetual_order_test.go index ac3075551..6a5374c08 100644 --- a/x/tradeshield/client/cli/tx_perpetual_order_test.go +++ b/x/tradeshield/client/cli/tx_perpetual_order_test.go @@ -37,7 +37,6 @@ func TestCreatePerpetualOpenOrder(t *testing.T) { fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(net.Config.BondDenom, sdkmath.NewInt(10))).String()), - "limitopen", // order type "long", // position "10", // leverage "1", // pool id diff --git a/x/tradeshield/migrations/v4_migrator.go b/x/tradeshield/migrations/v4_migrator.go index 1f69f9c9f..45de04b4c 100644 --- a/x/tradeshield/migrations/v4_migrator.go +++ b/x/tradeshield/migrations/v4_migrator.go @@ -10,6 +10,8 @@ func (m Migrator) V4Migration(ctx sdk.Context) error { for _, order := range orders { m.keeper.RemovePendingPerpetualOrder(ctx, order.OrderId) } + m.keeper.DeleteAllPendingPerpetualOrder(ctx) + m.keeper.DeleteAllPendingSpotOrder(ctx) return nil } diff --git a/x/tradeshield/migrations/v5_migrator.go b/x/tradeshield/migrations/v5_migrator.go deleted file mode 100644 index e97582d83..000000000 --- a/x/tradeshield/migrations/v5_migrator.go +++ /dev/null @@ -1,11 +0,0 @@ -package migrations - -import ( - sdk "github.com/cosmos/cosmos-sdk/types" -) - -func (m Migrator) V5Migration(ctx sdk.Context) error { - m.keeper.DeleteAllPendingPerpetualOrder(ctx) - m.keeper.DeleteAllPendingSpotOrder(ctx) - return nil -} diff --git a/x/tradeshield/module.go b/x/tradeshield/module.go index 75421c741..20909b262 100644 --- a/x/tradeshield/module.go +++ b/x/tradeshield/module.go @@ -117,7 +117,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper)) types.RegisterQueryServer(cfg.QueryServer(), am.keeper) m := migrations.NewMigrator(am.keeper) - err := cfg.RegisterMigration(types.ModuleName, 4, m.V5Migration) + err := cfg.RegisterMigration(types.ModuleName, 3, m.V4Migration) if err != nil { panic(err) } diff --git a/x/tradeshield/types/messages_pending_perpetual_order.go b/x/tradeshield/types/messages_pending_perpetual_order.go index e1a6a3857..d8f750de2 100644 --- a/x/tradeshield/types/messages_pending_perpetual_order.go +++ b/x/tradeshield/types/messages_pending_perpetual_order.go @@ -17,7 +17,6 @@ var _ sdk.Msg = &MsgCreatePerpetualOpenOrder{} func NewMsgCreatePerpetualOpenOrder( ownerAddress string, - orderType PerpetualOrderType, triggerPrice TriggerPrice, collateral sdk.Coin, tradingAsset string, @@ -119,7 +118,6 @@ var _ sdk.Msg = &MsgCreatePerpetualOpenOrder{} func NewMsgCreatePerpetualCloseOrder( ownerAddress string, - orderType PerpetualOrderType, triggerPrice TriggerPrice, positionId uint64, ) *MsgCreatePerpetualCloseOrder {