Skip to content

Commit

Permalink
[TRA-188] add cli for deposit to vault (#1276) (#1277)
Browse files Browse the repository at this point in the history
* add cli for deposit to vault

* use cast library

Co-authored-by: Tian <tian@dydx.exchange>
  • Loading branch information
teddyding and tqin7 committed Mar 28, 2024
1 parent 60b94df commit 4d578a5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
10 changes: 3 additions & 7 deletions protocol/x/vault/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions protocol/x/vault/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
17 changes: 17 additions & 0 deletions protocol/x/vault/client/cli/util.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 4d578a5

Please sign in to comment.