From 6fbc9958cbbc98ee9342a912d45a77ad744d30af Mon Sep 17 00:00:00 2001 From: Tian Qin Date: Thu, 28 Mar 2024 16:54:22 -0400 Subject: [PATCH 1/2] add cli for deposit to vault --- protocol/x/vault/client/cli/query.go | 10 ++--- protocol/x/vault/client/cli/tx.go | 67 ++++++++++++++++++++++++++++ protocol/x/vault/client/cli/util.go | 17 +++++++ 3 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 protocol/x/vault/client/cli/util.go diff --git a/protocol/x/vault/client/cli/query.go b/protocol/x/vault/client/cli/query.go index 3c16747a7f..2e432d8da9 100644 --- a/protocol/x/vault/client/cli/query.go +++ b/protocol/x/vault/client/cli/query.go @@ -63,13 +63,9 @@ func CmdQueryVault() *cobra.Command { queryClient := types.NewQueryClient(clientCtx) // Parse vault type. - rawType := args[0] - var vaultType types.VaultType - switch rawType { - case "clob": - vaultType = types.VaultType_VAULT_TYPE_CLOB - default: - return fmt.Errorf("invalid vault type %s", rawType) + vaultType, err := GetVaultTypeFromString(args[0]) + if err != nil { + return err } // Parse vault number. diff --git a/protocol/x/vault/client/cli/tx.go b/protocol/x/vault/client/cli/tx.go index 156bfb8544..2e15d7f343 100644 --- a/protocol/x/vault/client/cli/tx.go +++ b/protocol/x/vault/client/cli/tx.go @@ -2,10 +2,15 @@ package cli import ( "fmt" + "strconv" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/dydxprotocol/v4-chain/protocol/dtypes" + satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types" "github.com/dydxprotocol/v4-chain/protocol/x/vault/types" ) @@ -18,5 +23,67 @@ func GetTxCmd() *cobra.Command { SuggestionsMinimumDistance: 2, RunE: client.ValidateCmd, } + + cmd.AddCommand(CmdDepositToVault()) + + return cmd +} + +func CmdDepositToVault() *cobra.Command { + cmd := &cobra.Command{ + Use: "deposit-to-vault [vault_type] [vault_number] [depositor_owner] [depositor_number] [quantums]", + Short: "Broadcast message DepositToVault", + Args: cobra.ExactArgs(5), + RunE: func(cmd *cobra.Command, args []string) (err error) { + // Parse vault type. + vaultType, err := GetVaultTypeFromString(args[0]) + if err != nil { + return err + } + + // Parse vault number. + vaultNumber, err := strconv.ParseUint(args[1], 10, 32) + if err != nil { + return err + } + + // Parse depositor number. + depositorNumber, err := strconv.ParseUint(args[3], 10, 32) + if err != nil { + return err + } + + // Parse quantums. + quantums, err := strconv.ParseUint(args[4], 10, 64) + if err != nil { + return err + } + + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + msg := &types.MsgDepositToVault{ + VaultId: &types.VaultId{ + Type: vaultType, + Number: uint32(vaultNumber), + }, + SubaccountId: &satypes.SubaccountId{ + Owner: args[2], + Number: uint32(depositorNumber), + }, + QuoteQuantums: dtypes.NewIntFromUint64(quantums), + } + + if err := msg.ValidateBasic(); err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + return cmd } diff --git a/protocol/x/vault/client/cli/util.go b/protocol/x/vault/client/cli/util.go new file mode 100644 index 0000000000..0d5834a595 --- /dev/null +++ b/protocol/x/vault/client/cli/util.go @@ -0,0 +1,17 @@ +package cli + +import ( + "fmt" + + "github.com/dydxprotocol/v4-chain/protocol/x/vault/types" +) + +// GetVaultTypeFromString returns a vault type from a string. +func GetVaultTypeFromString(rawType string) (vaultType types.VaultType, err error) { + switch rawType { + case "clob": + return types.VaultType_VAULT_TYPE_CLOB, nil + default: + return vaultType, fmt.Errorf("invalid vault type: %s", rawType) + } +} From 423e52abeabc7af9471f0048a51c3b60964aebe2 Mon Sep 17 00:00:00 2001 From: Tian Qin Date: Thu, 28 Mar 2024 17:08:48 -0400 Subject: [PATCH 2/2] use cast library --- protocol/x/vault/client/cli/tx.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/protocol/x/vault/client/cli/tx.go b/protocol/x/vault/client/cli/tx.go index 2e15d7f343..5d577f20b7 100644 --- a/protocol/x/vault/client/cli/tx.go +++ b/protocol/x/vault/client/cli/tx.go @@ -2,8 +2,8 @@ package cli import ( "fmt" - "strconv" + "github.com/spf13/cast" "github.com/spf13/cobra" "github.com/cosmos/cosmos-sdk/client" @@ -42,19 +42,19 @@ func CmdDepositToVault() *cobra.Command { } // Parse vault number. - vaultNumber, err := strconv.ParseUint(args[1], 10, 32) + vaultNumber, err := cast.ToUint32E(args[1]) if err != nil { return err } // Parse depositor number. - depositorNumber, err := strconv.ParseUint(args[3], 10, 32) + depositorNumber, err := cast.ToUint32E(args[3]) if err != nil { return err } // Parse quantums. - quantums, err := strconv.ParseUint(args[4], 10, 64) + quantums, err := cast.ToUint64E(args[4]) if err != nil { return err } @@ -64,21 +64,22 @@ func CmdDepositToVault() *cobra.Command { return err } + // Create MsgDepositToVault. msg := &types.MsgDepositToVault{ VaultId: &types.VaultId{ Type: vaultType, - Number: uint32(vaultNumber), + Number: vaultNumber, }, SubaccountId: &satypes.SubaccountId{ Owner: args[2], - Number: uint32(depositorNumber), + Number: depositorNumber, }, QuoteQuantums: dtypes.NewIntFromUint64(quantums), } - if err := msg.ValidateBasic(); err != nil { return err } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) }, }