Skip to content

Commit

Permalink
Add cli for submit tx transaction (#2179)
Browse files Browse the repository at this point in the history
## Description

Add cli command for `SubmitTx` message server method.

closes: #2062

---

Before we can merge this PR, please make sure that all the following items have been
checked off. If any of the checklist items are not applicable, please leave them but
write a little note why.

- [ ] Targeted PR against correct branch (see [CONTRIBUTING.md](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
- [ ] Code follows the [module structure standards](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules/structure.md).
- [ ] Wrote unit and integration [tests](https://github.com/cosmos/ibc-go/blob/master/CONTRIBUTING.md#testing)
- [ ] Updated relevant documentation (`docs/`) or specification (`x/<module>/spec/`)
- [ ] Added relevant `godoc` [comments](https://blog.golang.org/godoc-documenting-go-code).
- [ ] Added a relevant changelog entry to the `Unreleased` section in `CHANGELOG.md`
- [ ] Re-reviewed `Files changed` in the Github PR explorer
- [ ] Review `Codecov Report` in the comment section below once CI passes
  • Loading branch information
Anmol1696 authored Sep 8, 2022
1 parent e678779 commit 44773b0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
72 changes: 71 additions & 1 deletion modules/apps/27-interchain-accounts/controller/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package cli

import (
"fmt"
"os"
"strings"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/spf13/cobra"

"github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/controller/types"
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types"
clienttypes "github.com/cosmos/ibc-go/v5/modules/core/02-client/types"
)

const (
// The controller chain channel version
flagVersion = "version"
flagVersion = "version"
flagPacketTimeoutHeight = "packet-timeout-height"
flagPacketTimeoutTimestamp = "packet-timeout-timestamp"
)

// NewTxCmd creates and returns the tx command
Expand All @@ -28,6 +35,7 @@ func NewTxCmd() *cobra.Command {

cmd.AddCommand(
newRegisterAccountCmd(),
newSubmitTxCmd(),
)

return cmd
Expand Down Expand Up @@ -68,3 +76,65 @@ the associated capability.`),

return cmd
}

func newSubmitTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "submit [connection-id] [path/to/packet_msg.json]",
Short: "Submit an interchain account txn on the provided connection.",
Long: strings.TrimSpace(`Submits pre-built packet data containing messages to be executed on the host chain
and attempts to send the packet. Packet data is provided as json, file or string. An
appropriate relative timeoutTimestamp must be provided with flag {packet-timeout-timestamp}, along with a timeoutHeight
via {packet-timeout-timestamp}`),
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry)

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

// attempt to unmarshal ica msg data argument
var icaMsgData icatypes.InterchainAccountPacketData
msgContentOrFileName := args[1]
if err := cdc.UnmarshalInterfaceJSON([]byte(msgContentOrFileName), &icaMsgData); err != nil {

// check for file path if JSON input is not provided
contents, err := os.ReadFile(msgContentOrFileName)
if err != nil {
return fmt.Errorf("neither JSON input nor path to .json file for client state were provided: %w", err)
}

if err := cdc.UnmarshalInterfaceJSON(contents, &icaMsgData); err != nil {
return fmt.Errorf("error unmarshalling client state file: %w", err)
}
}

timeoutHeightStr, err := cmd.Flags().GetString(flagPacketTimeoutHeight)
if err != nil {
return err
}
timeoutHeight, err := clienttypes.ParseHeight(timeoutHeightStr)
if err != nil {
return err
}

timeoutTimestamp, err := cmd.Flags().GetUint64(flagPacketTimeoutTimestamp)
if err != nil {
return err
}

msg := types.NewMsgSubmitTx(owner, connectionID, timeoutHeight, timeoutTimestamp, icaMsgData)

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

cmd.Flags().String(flagPacketTimeoutHeight, icatypes.DefaultRelativePacketTimeoutHeight, "Packet timeout block height. The timeout is disabled when set to 0-0.")
cmd.Flags().Uint64(flagPacketTimeoutTimestamp, icatypes.DefaultRelativePacketTimeoutTimestamp, "Packet timeout timestamp in nanoseconds from now. Default is 10 minutes. The timeout is disabled when set to 0.")
flags.AddTxFlagsToCmd(cmd)

return cmd
}
15 changes: 15 additions & 0 deletions modules/apps/27-interchain-accounts/types/packet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import (
"time"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -9,6 +11,19 @@ import (
// MaxMemoCharLength defines the maximum length for the InterchainAccountPacketData memo field
const MaxMemoCharLength = 256

var (
// DefaultRelativePacketTimeoutHeight is the default packet timeout height (in blocks) relative
// to the current block height of the counterparty chain provided by the client state. The
// timeout is disabled when set to 0.
DefaultRelativePacketTimeoutHeight = "0-1000"

// DefaultRelativePacketTimeoutTimestamp is the default packet timeout timestamp (in nanoseconds)
// relative to the current block timestamp of the counterparty chain provided by the client
// state. The timeout is disabled when set to 0. The default is currently set to a 10 minute
// timeout.
DefaultRelativePacketTimeoutTimestamp = uint64((time.Duration(10) * time.Minute).Nanoseconds())
)

// ValidateBasic performs basic validation of the interchain account packet data.
// The memo may be empty.
func (iapd InterchainAccountPacketData) ValidateBasic() error {
Expand Down

0 comments on commit 44773b0

Please sign in to comment.