From 4d578a5effea9870d389db7a86475e9f5a674ada Mon Sep 17 00:00:00 2001 From: Teddy Ding Date: Thu, 28 Mar 2024 17:26:11 -0400 Subject: [PATCH] [TRA-188] add cli for deposit to vault (#1276) (#1277) * add cli for deposit to vault * use cast library Co-authored-by: Tian --- protocol/x/vault/client/cli/query.go | 10 ++-- protocol/x/vault/client/cli/tx.go | 68 ++++++++++++++++++++++++++++ protocol/x/vault/client/cli/util.go | 17 +++++++ 3 files changed, 88 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 3c16747a7ff..2e432d8da92 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 156bfb8544b..5d577f20b71 100644 --- a/protocol/x/vault/client/cli/tx.go +++ b/protocol/x/vault/client/cli/tx.go @@ -3,9 +3,14 @@ package cli import ( "fmt" + "github.com/spf13/cast" "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,68 @@ 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 := cast.ToUint32E(args[1]) + if err != nil { + return err + } + + // Parse depositor number. + depositorNumber, err := cast.ToUint32E(args[3]) + if err != nil { + return err + } + + // Parse quantums. + quantums, err := cast.ToUint64E(args[4]) + if err != nil { + return err + } + + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + // Create MsgDepositToVault. + msg := &types.MsgDepositToVault{ + VaultId: &types.VaultId{ + Type: vaultType, + Number: vaultNumber, + }, + SubaccountId: &satypes.SubaccountId{ + Owner: args[2], + Number: 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 00000000000..0d5834a595a --- /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) + } +}