Skip to content

Commit

Permalink
add move encode cmd (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Jul 18, 2024
1 parent c95783c commit afa0faa
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
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 @@ $ %s query move view \
return err
}

moveArgTypes, moveArgs := parseArguments(flagArgs)
moveArgTypes, moveArgs := ParseArguments(flagArgs)
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 @@ $ %s tx move execute \
return err
}

moveArgTypes, moveArgs := parseArguments(flagArgs)
moveArgTypes, moveArgs := ParseArguments(flagArgs)
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

0 comments on commit afa0faa

Please sign in to comment.