diff --git a/cmd/ibc_denom/ibc_denom.go b/cmd/ibc_denom/ibc_denom.go new file mode 100644 index 0000000000..4a03402ee6 --- /dev/null +++ b/cmd/ibc_denom/ibc_denom.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + + ibctransfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types" + "github.com/spf13/cobra" +) + +// ibcDenomCmd create the cli cmd for making ibc denom by base denom and channel-id +func ibcDenomCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "ibc-denom [base-denom] [channel-id]", + Short: "Create an ibc denom by base denom and channel id", + Args: cobra.ExactArgs(2), + Long: `Create an ibc denom by base denom and channel id. + +Example: +$ ibc-denom uumee channel-22`, + RunE: func(cmd *cobra.Command, args []string) error { + + ibcDenom, err := ibcDenom(args[0], args[1]) + if err != nil { + return err + } + cmd.Println(ibcDenom) + return nil + }, + } + + return cmd +} + +func ibcDenom(baseDenom, channelID string) (string, error) { + denomTrace := ibctransfertypes.DenomTrace{ + Path: fmt.Sprintf("transfer/%s", channelID), + BaseDenom: baseDenom, + } + + if err := denomTrace.Validate(); err != nil { + return "", err + } + + return denomTrace.IBCDenom(), nil +} diff --git a/cmd/ibc_denom/ibc_denom_test.go b/cmd/ibc_denom/ibc_denom_test.go new file mode 100644 index 0000000000..a539b29c94 --- /dev/null +++ b/cmd/ibc_denom/ibc_denom_test.go @@ -0,0 +1,52 @@ +package main + +import ( + "testing" + + "gotest.tools/v3/assert" +) + +func TestIBCDenom(t *testing.T) { + + tests := []struct { + name string + baseDenom string + channelID string + errExpected string + execptedResult string + }{ + { + name: "invalid base denom", + baseDenom: "", + channelID: "channel-12", + errExpected: "base denomination cannot be blank", + execptedResult: "", + }, + { + name: "invalid channel-id", + baseDenom: "uakt", + channelID: "channel", + errExpected: "invalid channel ID", + execptedResult: "", + }, + { + name: "success", + baseDenom: "uakt", + channelID: "channel-12", + errExpected: "", + execptedResult: "ibc/8DF58541612917752DA1CCACC8441FCFE367F9960E51151968A75CE22671D717", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ibcDenom, err := ibcDenom(test.baseDenom, test.channelID) + if len(test.errExpected) != 0 { + assert.ErrorContains(t, err, test.errExpected) + } else { + assert.NilError(t, err) + assert.Equal(t, ibcDenom, test.execptedResult) + } + }) + } +} diff --git a/cmd/ibc_denom/main.go b/cmd/ibc_denom/main.go new file mode 100644 index 0000000000..0b63588f36 --- /dev/null +++ b/cmd/ibc_denom/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "os" + + "github.com/spf13/cobra" +) + +func NewRootCmd() *cobra.Command { + return ibcDenomCmd() +} + +func main() { + rootCmd := NewRootCmd() + if err := rootCmd.Execute(); err != nil { + os.Exit(1) + } +}