Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TRA-188] add cli for deposit to vault #1276

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
67 changes: 67 additions & 0 deletions protocol/x/vault/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Use these functions instead to cast?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call. updated!

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
}
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)
}
}
Loading