Skip to content

Commit

Permalink
feat: cli for sending Wallet{Spend}Action transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLarson committed Apr 21, 2022
1 parent 8289deb commit ddb9629
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
42 changes: 42 additions & 0 deletions golang/cosmos/x/swingset/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
FlagSpend = "spend"
)

func GetTxCmd(storeKey string) *cobra.Command {
swingsetTxCmd := &cobra.Command{
Use: types.ModuleName,
Expand Down Expand Up @@ -117,6 +121,44 @@ func GetCmdProvisionOne() *cobra.Command {
return cmd
}

// GetCmdWalletAction is the CLI command for sending a WalletAction or WalletSpendAction transaction
func GetCmdWalletAction() *cobra.Command {
cmd := &cobra.Command{
Use: "wallet-action [json string]",
Short: "perform a wallet action",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

owner := clientCtx.GetFromAddress()
action := args[0]

spend, err := cmd.Flags().GetBool(FlagSpend)
if err != nil {
return err
}
var msg sdk.Msg
if spend {
msg = types.NewMsgWalletSpendAction(owner, action)
} else {
msg = types.NewMsgWalletAction(owner, action)
}
err = msg.ValidateBasic()
if err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().Bool(FlagSpend, false, "Send a WalletSpendAction transaction if true")
flags.AddTxFlagsToCmd(cmd)
return cmd
}

func NewCmdSubmitCoreEvalProposal() *cobra.Command {
cmd := &cobra.Command{
Use: "swingset-core-eval [[permit.json] [code.js]]...",
Expand Down
21 changes: 21 additions & 0 deletions golang/cosmos/x/swingset/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"encoding/json"
"strings"

"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
Expand Down Expand Up @@ -91,6 +92,13 @@ func (msg MsgDeliverInbound) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Submitter}
}

func NewMsgWalletAction(owner sdk.AccAddress, action string) *MsgWalletAction {
return &MsgWalletAction{
Owner: owner,
Action: action,
}
}

func (msg MsgWalletAction) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
Expand All @@ -103,9 +111,19 @@ func (msg MsgWalletAction) ValidateBasic() error {
if len(strings.TrimSpace(msg.Action)) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Action cannot be empty")
}
if !json.Valid([]byte(msg.Action)) {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, "Wallet action must be valid JSON")
}
return nil
}

func NewMsgWalletSpendAction(owner sdk.AccAddress, spendAction string) *MsgWalletSpendAction {
return &MsgWalletSpendAction{
Owner: owner,
SpendAction: spendAction,
}
}

func (msg MsgWalletSpendAction) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Owner}
}
Expand All @@ -118,6 +136,9 @@ func (msg MsgWalletSpendAction) ValidateBasic() error {
if len(strings.TrimSpace(msg.SpendAction)) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Spend action cannot be empty")
}
if !json.Valid([]byte(msg.SpendAction)) {
return sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, "Wallet spend action must be valid JSON")
}
return nil
}

Expand Down

0 comments on commit ddb9629

Please sign in to comment.