Skip to content

Commit

Permalink
add execute & query view json function to cli cmd (#232)
Browse files Browse the repository at this point in the history
* add execute & query view json function to cli cmd

* add warning msg

* simplify parsing logics

* change test to use BCSEncode

---------

Co-authored-by: beer-1 <147697694+beer-1@users.noreply.github.com>
  • Loading branch information
sh-cha and beer-1 authored Jul 19, 2024
1 parent 2b4b1ea commit 3434001
Show file tree
Hide file tree
Showing 6 changed files with 461 additions and 197 deletions.
27 changes: 14 additions & 13 deletions cmd/move/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ func moveTestCmd() *cobra.Command {

func moveEncodeCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "encode '[arg arg arg...]'",
Short: "encode a move arguments",
Use: "encode [flags]",
Short: "encode a move arguments in BCS format",
Long: fmt.Sprintf(`
Provide BCS encoding for move arguments.
Expand All @@ -208,30 +208,31 @@ func moveEncodeCmd(ac address.Codec) *cobra.Command {
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"'
$ %s move encode --args '["address:0x1", "bool:true", "u8:0x01", "u128:1234", "vector<u32>:a,b,c,d", "string:hello world"]'
`, version.AppName,
),
Args: cobra.ExactArgs(1),
Args: cobra.ExactArgs(0),
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)")
flagArgs, err := movecli.ReadAndDecodeJSONStringArray[string](cmd, movecli.FlagArgs)
if err != nil {
return errorsmod.Wrap(err, "failed to read move args")
}

for i := range moveArgTypes {
serializer := movecli.NewSerializer()
bcsArg, err := movecli.BcsSerializeArg(moveArgTypes[i], moveArgs[i], serializer, ac)
if err != nil {
return err
}
bcsArgs, err := movecli.BCSEncode(ac, flagArgs)
if err != nil {
return errorsmod.Wrap(err, "failed to encode move args")
}

for _, bcsArg := range bcsArgs {
fmt.Println("0x" + hex.EncodeToString(bcsArg))
}

return nil
},
}

cmd.Flags().AddFlagSet(movecli.FlagSetArgs())

return cmd
}

Expand Down
16 changes: 12 additions & 4 deletions x/move/client/cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@ func FlagSetUpgradePolicy() *flag.FlagSet {
// FlagSetArgs Returns the FlagSet for args related operations.
func FlagSetArgs() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(FlagArgs, "", `The arguments for move functions.
ex) address:0x1 bool:true u8:0x01 u128:1234 'vector<u32>:a,b,c,d'`)
fs.String(FlagArgs, "", `The array of BCS arguments for the move function.
Example: '["address:0x1", "bool:true", "u8:0x01", "u128:1234", "vector<u32>:a,b,c,d"]'`)
return fs
}

// FlagSetArgs Returns the FlagSet for args related operations.
func FlagSetJSONArgs() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(FlagArgs, "", `The array of JSON arguments for the move function.
Example: '[0, true, "0x1", "1234", ["a","b","c","d"]]'`)
return fs
}

// FlagSetTypeArgs Returns the FlagSet for type args related operations.
func FlagSetTypeArgs() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(FlagTypeArgs, "", `The type arguments for move functions.
ex) 0x1::BasicCoin::getBalance<u8>`)
fs.String(FlagTypeArgs, "", `The array of type arguments for the move function.
ex) '["0x1::BasicCoin::getBalance<u8>", "0x1::BasicCoin::getBalance<u64>"]'`)
return fs
}
118 changes: 90 additions & 28 deletions x/move/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"fmt"
"strings"

"cosmossdk.io/core/address"
"github.com/spf13/cobra"

"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -32,7 +33,8 @@ func GetQueryCmd(ac address.Codec) *cobra.Command {
GetCmdResources(ac),
GetCmdTableEntry(ac),
GetCmdTableEntries(ac),
GetCmdQueryEntryFunction(ac),
GetCmdQueryViewFunction(ac),
GetCmdQueryViewJSONFunction(ac),
GetCmdQueryParams(),
)
return queryCmd
Expand Down Expand Up @@ -271,7 +273,7 @@ func GetCmdTableEntries(ac address.Codec) *cobra.Command {
return cmd
}

func GetCmdQueryEntryFunction(ac address.Codec) *cobra.Command {
func GetCmdQueryViewFunction(ac address.Codec) *cobra.Command {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()
cmd := &cobra.Command{
Use: "view [module owner] [module name] [function name]",
Expand All @@ -289,8 +291,8 @@ $ %s query move view \
%s1lwjmdnks33xwnmfayc64ycprww49n33mtm92ne \
ManagedCoin \
get_balance \
--type-args '0x1::native_uinit::Coin 0x1::native_uusdc::Coin' \
--args 'u8:0 address:0x1 string:"hello world"'
--type-args '["0x1::BasicCoin::getBalance<u8>", "0x1::BasicCoin::getBalance<u64>"]' \
--args '["address:0x1", "bool:true", "u8:0x01", "u128:1234", "vector<u32>:a,b,c,d", "string:hello world"]'
`, version.AppName, bech32PrefixAccAddr,
),
),
Expand All @@ -307,45 +309,31 @@ $ %s query move view \
return err
}

var typeArgs []string
flagTypeArgs, err := cmd.Flags().GetString(FlagTypeArgs)
tyArgs, err := ReadAndDecodeJSONStringArray[string](cmd, FlagTypeArgs)
if err != nil {
return err
}
if flagTypeArgs != "" {
typeArgs = strings.Split(flagTypeArgs, " ")
return errorsmod.Wrap(err, "failed to read type args")
}

flagArgs, err := cmd.Flags().GetString(FlagArgs)
flagArgs, err := ReadAndDecodeJSONStringArray[string](cmd, FlagArgs)
if err != nil {
return err
return errorsmod.Wrap(err, "failed to read move args")
}

moveArgTypes, moveArgs := ParseArguments(flagArgs)
if len(moveArgTypes) != len(moveArgs) {
return fmt.Errorf("invalid argument format len(types) != len(args)")
}

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

bcsArgs = append(bcsArgs, bcsArg)
bcsArgs, err := BCSEncode(ac, flagArgs)
if err != nil {
return errorsmod.Wrap(err, "failed to encode move args")
}

queryClient := types.NewQueryClient(clientCtx)

//nolint
res, err := queryClient.View(
context.Background(),
&types.QueryViewRequest{
Address: args[0],
ModuleName: args[1],
FunctionName: args[2],
TypeArgs: typeArgs,
TypeArgs: tyArgs,
Args: bcsArgs,
},
)
Expand All @@ -362,6 +350,80 @@ $ %s query move view \
return cmd
}

func GetCmdQueryViewJSONFunction(ac address.Codec) *cobra.Command {
bech32PrefixAccAddr := sdk.GetConfig().GetBech32AccountAddrPrefix()
cmd := &cobra.Command{
Use: "view-json [module owner] [module name] [function name]",
Short: "Get view json function execution result",
Long: strings.TrimSpace(
fmt.Sprintf(`
Get an view function execution result
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: "0x1" "true" "0" "hello vector" ["a","b","c","d"]
Example:
$ %s query move view_json \
%s1lwjmdnks33xwnmfayc64ycprww49n33mtm92ne \
ManagedCoin \
get_balance \
--type-args '["0x1::BasicCoin::getBalance<u8>", "0x1::BasicCoin::getBalance<u64>"]' \
--args '[0, true, "0x1", "1234", ["a","b","c","d"]]'
Note that there should be no spaces within the arguments, since each argument is separated by a space.
`, version.AppName, bech32PrefixAccAddr,
),
),
Aliases: []string{"ej"},
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

_, err = types.AccAddressFromString(ac, args[0])
if err != nil {
return err
}

tyArgs, err := ReadAndDecodeJSONStringArray[string](cmd, FlagTypeArgs)
if err != nil {
return errorsmod.Wrap(err, "failed to read type args")
}

moveArgs, err := ReadJSONStringArray(cmd, FlagArgs)
if err != nil {
return errorsmod.Wrap(err, "failed to read move args")
}

queryClient := types.NewQueryClient(clientCtx)

//nolint
res, err := queryClient.ViewJSON(
context.Background(),
&types.QueryViewJSONRequest{
Address: args[0],
ModuleName: args[1],
FunctionName: args[2],
TypeArgs: tyArgs,
Args: moveArgs,
},
)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
cmd.Flags().AddFlagSet(FlagSetTypeArgs())
cmd.Flags().AddFlagSet(FlagSetJSONArgs())
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

// GetCmdQueryParams implements the params query command.
func GetCmdQueryParams() *cobra.Command {
cmd := &cobra.Command{
Expand Down
Loading

0 comments on commit 3434001

Please sign in to comment.