Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add move encode cmd #230

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions cmd/move/move.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package movecmd

import (
"encoding/hex"
"fmt"
"os"
"path"
"strings"

"cosmossdk.io/core/address"
"cosmossdk.io/errors"
errorsmod "cosmossdk.io/errors"

"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/version"

movecli "github.com/initia-labs/initia/x/move/client/cli"
movetypes "github.com/initia-labs/initia/x/move/types"
Expand Down Expand Up @@ -108,6 +110,7 @@ func MoveCommand(ac address.Codec) *cobra.Command {
cmd.AddCommand(
moveBuildCmd(),
moveTestCmd(),
moveEncodeCmd(ac),
moveNewCmd(),
moveCleanCmd(),
moveDeployCmd(ac),
Expand Down Expand Up @@ -193,6 +196,45 @@ func moveTestCmd() *cobra.Command {
return cmd
}

func moveEncodeCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "encode '[arg arg arg...]'",
Short: "encode a move arguments",
Long: fmt.Sprintf(`
Provide BCS encoding for move arguments.

Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64,
vector<inner_type>, option<inner_type>, decimal128, decimal256, fixed_point32, fixed_point64
Example of args: address:0x1 bool:true u8:0 string:hello vector<u32>:a,b,c,d

Example:
$ %s move encode 'u8:0 address:0x1 string:"hello world"'
`, version.AppName,
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
moveArgTypes, moveArgs := movecli.ParseArguments(args[0])
if len(moveArgTypes) != len(moveArgs) {
return fmt.Errorf("invalid argument format len(moveArgTypes) != len(moveArgs)")
}

for i := range moveArgTypes {
serializer := movecli.NewSerializer()
bcsArg, err := movecli.BcsSerializeArg(moveArgTypes[i], moveArgs[i], serializer, ac)
if err != nil {
return err
}

fmt.Println("0x" + hex.EncodeToString(bcsArg))
}

return nil
},
}

return cmd
}

func moveCoverageSummaryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "summary [flags]",
Expand Down Expand Up @@ -446,7 +488,7 @@ func moveDeployCmd(ac address.Codec) *cobra.Command {
// request contract verify
if flagVerify {
if err := verifyContract(*vc); err != nil {
return errors.Wrap(err, "failed to verify published package")
return errorsmod.Wrap(err, "failed to verify published package")
}
}

Expand Down Expand Up @@ -510,7 +552,7 @@ func moveVerifyCmd() *cobra.Command {

err = verifyContract(*uc)
if err != nil {
return errors.Wrap(err, "failed to verify")
return errorsmod.Wrap(err, "failed to verify")
}

fmt.Println("Verification done.")
Expand Down
2 changes: 1 addition & 1 deletion x/move/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
return err
}

moveArgTypes, moveArgs := parseArguments(flagArgs)
moveArgTypes, moveArgs := ParseArguments(flagArgs)

Check warning on line 324 in x/move/client/cli/query.go

View check run for this annotation

Codecov / codecov/patch

x/move/client/cli/query.go#L324

Added line #L324 was not covered by tests
if len(moveArgTypes) != len(moveArgs) {
return fmt.Errorf("invalid argument format len(types) != len(args)")
}
Expand Down
2 changes: 1 addition & 1 deletion x/move/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@
return err
}

moveArgTypes, moveArgs := parseArguments(flagArgs)
moveArgTypes, moveArgs := ParseArguments(flagArgs)

Check warning on line 158 in x/move/client/cli/tx.go

View check run for this annotation

Codecov / codecov/patch

x/move/client/cli/tx.go#L158

Added line #L158 was not covered by tests
if len(moveArgTypes) != len(moveArgs) {
return fmt.Errorf("invalid argument format len(moveArgTypes) != len(moveArgs)")
}
Expand Down
2 changes: 1 addition & 1 deletion x/move/client/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func DivideUint256String(s string) (uint64, uint64, uint64, uint64, error) {
return highHigh, highLow, high, low, nil
}

func parseArguments(s string) (tt []string, args []string) {
func ParseArguments(s string) (tt []string, args []string) {
if len(s) == 0 {
return
}
Expand Down
2 changes: 1 addition & 1 deletion x/move/client/cli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func Test_parseArguments(t *testing.T) {
argTypes, args := parseArguments("u8:1 u16:256 string:\"hello world\" string:\"hello\" vector<string>:\"hello world\",\"hello world\" vector<bool>:true,false,true")
argTypes, args := ParseArguments("u8:1 u16:256 string:\"hello world\" string:\"hello\" vector<string>:\"hello world\",\"hello world\" vector<bool>:true,false,true")
require.Equal(t, []string{"u8", "u16", "string", "string", "vector<string>", "vector<bool>"}, argTypes)
require.Equal(t, []string{"1", "256", "hello world", "hello", "hello world,hello world", "true,false,true"}, args)
}
Expand Down
Loading