-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add util cmd for create ibc denom by base denom and channel-id (…
…#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
Showing
3 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |