From ded915358a5f65c5c316364404b3537547d8944e Mon Sep 17 00:00:00 2001 From: ALPAC-4 Date: Wed, 29 Nov 2023 20:25:13 +0900 Subject: [PATCH 1/5] fix bcs args and add types --- x/move/client/cli/tx.go | 12 ++++++------ x/move/client/cli/utils.go | 26 ++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/x/move/client/cli/tx.go b/x/move/client/cli/tx.go index e66beb4b..367a2b4d 100644 --- a/x/move/client/cli/tx.go +++ b/x/move/client/cli/tx.go @@ -148,15 +148,15 @@ $ %s tx move execute \ return err } - argTypes, args := parseArguments(flagArgs) - if len(argTypes) != len(args) { - return fmt.Errorf("invalid argument format len(types) != len(args)") + moveArgTypes, moveArgs := parseArguments(flagArgs) + if len(moveArgTypes) != len(moveArgs) { + return fmt.Errorf("invalid argument format len(moveArgTypes) != len(moveArgs)") } - serializer := NewSerializer() bcsArgs := [][]byte{} - for i := range argTypes { - bcsArg, err := BcsSerializeArg(argTypes[i], args[i], serializer) + for i := range moveArgTypes { + serializer := NewSerializer() + bcsArg, err := BcsSerializeArg(moveArgTypes[i], moveArgs[i], serializer) if err != nil { return err } diff --git a/x/move/client/cli/utils.go b/x/move/client/cli/utils.go index e6052b29..7c97dc20 100644 --- a/x/move/client/cli/utils.go +++ b/x/move/client/cli/utils.go @@ -85,7 +85,7 @@ func BcsSerializeArg(argType string, arg string, s serde.Serializer) ([]byte, er return nil, err } return vmtypes.SerializeBytes(decoded) - case "address": + case "address", "object": accAddr, err := types.AccAddressFromString(arg) if err != nil { return nil, err @@ -167,7 +167,7 @@ func BcsSerializeArg(argType string, arg string, s serde.Serializer) ([]byte, er return s.GetBytes(), nil default: if vectorRegex.MatchString(argType) { - vecType := getVectorType(argType) + vecType := getInnerType(argType) items := strings.Split(arg, ",") if err := s.SerializeLen(uint64(len(items))); err != nil { return nil, err @@ -178,6 +178,23 @@ func BcsSerializeArg(argType string, arg string, s serde.Serializer) ([]byte, er return nil, err } } + return s.GetBytes(), nil + } else if optionRegex.MatchString(argType) { + optionType := getInnerType(argType) + if arg == "null" { + if err := s.SerializeLen(0); err != nil { + return nil, err + } + return s.GetBytes(), nil + } + if err := s.SerializeLen(1); err != nil { + return nil, err + } + _, err := BcsSerializeArg(optionType, arg, s) + if err != nil { + return nil, err + } + return s.GetBytes(), nil } else { return nil, errors.New("unsupported type arg") @@ -186,10 +203,11 @@ func BcsSerializeArg(argType string, arg string, s serde.Serializer) ([]byte, er } var vectorRegex = regexp.MustCompile(`^vector<(.*)>$`) +var optionRegex = regexp.MustCompile(`^option<(.*)>$`) -func getVectorType(vector string) string { +func getInnerType(arg string) string { re := regexp.MustCompile(`<(.*)>`) - return re.FindStringSubmatch(vector)[1] + return re.FindStringSubmatch(arg)[1] } func DivideUint128String(s string) (uint64, uint64, error) { From 6dae689105e62b82df135d08f5e698505a416e06 Mon Sep 17 00:00:00 2001 From: ALPAC-4 Date: Wed, 29 Nov 2023 20:54:50 +0900 Subject: [PATCH 2/5] add decimal types --- x/move/client/cli/utils.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/x/move/client/cli/utils.go b/x/move/client/cli/utils.go index 7c97dc20..bc191e61 100644 --- a/x/move/client/cli/utils.go +++ b/x/move/client/cli/utils.go @@ -10,6 +10,7 @@ import ( "strconv" "strings" + sdkmath "cosmossdk.io/math" "github.com/initia-labs/initia/x/move/types" vmtypes "github.com/initia-labs/initiavm/types" "github.com/novifinancial/serde-reflection/serde-generate/runtime/golang/bcs" @@ -165,6 +166,36 @@ func BcsSerializeArg(argType string, arg string, s serde.Serializer) ([]byte, er High: highHigh, }) return s.GetBytes(), nil + case "decimal128": + dec, err := sdkmath.LegacyNewDecFromStr(arg) + if err != nil { + return nil, err + } + decstr := dec.MulInt64(1000000000000000000).TruncateInt().String() + return BcsSerializeArg("u128", decstr, s) + case "decimal256": + dec, err := sdkmath.LegacyNewDecFromStr(arg) + if err != nil { + return nil, err + } + decstr := dec.MulInt64(1000000000000000000).TruncateInt().String() + return BcsSerializeArg("u256", decstr, s) + case "fixed_point32": + dec, err := sdkmath.LegacyNewDecFromStr(arg) + if err != nil { + return nil, err + } + decstr := dec.MulInt64(4294967296).TruncateInt().String() + return BcsSerializeArg("u64", decstr, s) + case "fixed_point64": + dec, err := sdkmath.LegacyNewDecFromStr(arg) + if err != nil { + return nil, err + } + denominator := new(big.Int); + denominator.SetString("18446744073709551616", 10) + decstr := dec.MulInt(sdkmath.NewIntFromBigInt(denominator)).TruncateInt().String() + return BcsSerializeArg("u128", decstr, s) default: if vectorRegex.MatchString(argType) { vecType := getInnerType(argType) From 9cd09ac7f49bef95dd1ff3572a29cc2b54c0005e Mon Sep 17 00:00:00 2001 From: ALPAC-4 Date: Wed, 29 Nov 2023 20:59:02 +0900 Subject: [PATCH 3/5] add support type comment --- x/move/client/cli/tx.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x/move/client/cli/tx.go b/x/move/client/cli/tx.go index 367a2b4d..bc2a352e 100644 --- a/x/move/client/cli/tx.go +++ b/x/move/client/cli/tx.go @@ -109,7 +109,8 @@ func ExecuteCmd() *cobra.Command { fmt.Sprintf(` Execute an entry function of a published module -Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw, vector +Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, + vector, option, decimal128, decimal256, fixed_point32, fixed_point64 Example of args: address:0x1 bool:true u8:0 string:hello vector:a,b,c,d Example: @@ -196,7 +197,8 @@ func ScriptCmd() *cobra.Command { fmt.Sprintf(` Execute a given script -Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw, vector +Supported types : u8, u16, u32, u64, u128, u256, bool, string, address, raw_hex, raw_base64, + vector, option, decimal128, decimal256, fixed_point32, fixed_point64 Example of args: address:0x1 bool:true u8:0 string:hello vector:a,b,c,d Example: From b1257285a8c8cc67c617ec23b4e5985af5b0cc5e Mon Sep 17 00:00:00 2001 From: ALPAC-4 Date: Wed, 29 Nov 2023 21:07:33 +0900 Subject: [PATCH 4/5] fix: use bcs serializer for addreess and object --- x/move/client/cli/utils.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/x/move/client/cli/utils.go b/x/move/client/cli/utils.go index bc191e61..8543a83a 100644 --- a/x/move/client/cli/utils.go +++ b/x/move/client/cli/utils.go @@ -92,10 +92,12 @@ func BcsSerializeArg(argType string, arg string, s serde.Serializer) ([]byte, er return nil, err } - if err := accAddr.Serialize(s); err != nil { + bcsBytes, err := accAddr.BcsSerialize(); + + if err != nil { return nil, err } - return s.GetBytes(), nil + return bcsBytes, nil case "string": if err := s.SerializeStr(arg); err != nil { From e630ee687ffd21f1d03c7985118335c7e83f3aac Mon Sep 17 00:00:00 2001 From: ALPAC-4 Date: Wed, 29 Nov 2023 21:08:39 +0900 Subject: [PATCH 5/5] fix: remove useless error handle --- x/move/client/cli/utils.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/x/move/client/cli/utils.go b/x/move/client/cli/utils.go index 8543a83a..defb7247 100644 --- a/x/move/client/cli/utils.go +++ b/x/move/client/cli/utils.go @@ -91,13 +91,7 @@ func BcsSerializeArg(argType string, arg string, s serde.Serializer) ([]byte, er if err != nil { return nil, err } - - bcsBytes, err := accAddr.BcsSerialize(); - - if err != nil { - return nil, err - } - return bcsBytes, nil + return accAddr.BcsSerialize(); case "string": if err := s.SerializeStr(arg); err != nil {