Skip to content

Commit

Permalink
chore: add util cmd for create ibc denom by base denom and channel-id (
Browse files Browse the repository at this point in the history
…#2187)

* add util cmd for create ibc denom by base denom and channel-id

* Add ibc-denom binary

* delete root.go and move ibc_denom into cmd
  • Loading branch information
gsk967 authored Aug 10, 2023
1 parent d2c2573 commit 17a447e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
45 changes: 45 additions & 0 deletions cmd/ibc_denom/ibc_denom.go
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 52 additions & 0 deletions cmd/ibc_denom/ibc_denom_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
18 changes: 18 additions & 0 deletions cmd/ibc_denom/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 17a447e

Please sign in to comment.