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

fix bcs args and add types #21

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 10 additions & 8 deletions x/move/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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<inner_type>
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:
Expand Down Expand Up @@ -148,15 +149,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
}
Expand Down Expand Up @@ -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<inner_type>
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:
Expand Down
57 changes: 53 additions & 4 deletions x/move/client/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -85,7 +86,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
Expand Down Expand Up @@ -165,9 +166,39 @@ 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 := getVectorType(argType)
vecType := getInnerType(argType)
items := strings.Split(arg, ",")
if err := s.SerializeLen(uint64(len(items))); err != nil {
return nil, err
Expand All @@ -178,6 +209,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")
Expand All @@ -186,10 +234,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) {
Expand Down
Loading