mesg-foundation/core

View on GitHub
x/ownership/client/cli/tx.go

Summary

Maintainability
A
1 hr
Test Coverage
package cli

import (
    "bufio"
    "fmt"
    "strings"

    "github.com/cosmos/cosmos-sdk/client"
    "github.com/cosmos/cosmos-sdk/client/context"
    "github.com/cosmos/cosmos-sdk/client/flags"
    "github.com/cosmos/cosmos-sdk/codec"
    sdk "github.com/cosmos/cosmos-sdk/types"
    "github.com/cosmos/cosmos-sdk/x/auth"
    "github.com/cosmos/cosmos-sdk/x/auth/client/utils"
    "github.com/mesg-foundation/engine/hash"
    "github.com/mesg-foundation/engine/x/ownership/internal/types"
    "github.com/spf13/cobra"
)

// GetTxCmd returns the transaction commands for this module
func GetTxCmd(cdc *codec.Codec) *cobra.Command {
    ownershipTxCmd := &cobra.Command{
        Use:                        types.ModuleName,
        Short:                      fmt.Sprintf("%s transactions subcommands", strings.Title(types.ModuleName)),
        DisableFlagParsing:         true,
        SuggestionsMinimumDistance: 2,
        RunE:                       client.ValidateCmd,
    }

    ownershipTxCmd.AddCommand(flags.PostCommands(
        GetCmdWithdraw(cdc),
    )...)
    return ownershipTxCmd
}

// GetCmdWithdraw is the CLI command for sending a Withdraw transaction
func GetCmdWithdraw(cdc *codec.Codec) *cobra.Command {
    return &cobra.Command{
        Use:   "withdraw-coins [resourceHash] [amount]",
        Short: "Withdraw coins from a resource",
        Args:  cobra.ExactArgs(2),
        RunE: func(cmd *cobra.Command, args []string) error {
            inBuf := bufio.NewReader(cmd.InOrStdin())
            cliCtx := context.NewCLIContext().WithCodec(cdc)

            txBldr := auth.NewTxBuilderFromCLI(inBuf).WithTxEncoder(utils.GetTxEncoder(cdc))

            resourceHash, err := hash.Decode(args[0])
            if err != nil {
                return err
            }

            msg := types.MsgWithdraw{
                Owner:        cliCtx.GetFromAddress(),
                ResourceHash: resourceHash,
                Amount:       args[1],
            }
            if err := msg.ValidateBasic(); err != nil {
                return err
            }

            return utils.GenerateOrBroadcastMsgs(cliCtx, txBldr, []sdk.Msg{msg})
        },
    }
}