From af658bc3e5df907b867a10fbe55cf149de81b16b Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 12 Oct 2022 12:07:44 +0300 Subject: [PATCH] cli: support Null as an argument for invocation-related commands --- cli/cmdargs/parser.go | 6 +++++- pkg/smartcontract/param_type.go | 6 ++++++ pkg/smartcontract/param_type_test.go | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cli/cmdargs/parser.go b/cli/cmdargs/parser.go index 3868466a2..13fccaf8a 100644 --- a/cli/cmdargs/parser.go +++ b/cli/cmdargs/parser.go @@ -32,7 +32,8 @@ const ( 'signature', 'bool', 'int', 'hash160', 'hash256', 'bytes', 'key' or 'string'. Array types are also supported: use special space-separated '[' and ']' symbols around array values to denote array bounds. Nested arrays are also - supported. + supported. Null parameter is supported via 'nil' keyword without additional + type specification. There is ability to provide an argument of 'bytearray' type via file. Use a special 'filebytes' argument type for this with a filepath specified after @@ -60,6 +61,8 @@ const ( following logic: - anything that can be interpreted as a decimal integer gets an 'int' type + - 'nil' string gets 'Any' NEP-14 parameter type and nil value which corresponds + to Null stackitem - 'true' and 'false' strings get 'bool' type - valid Neo addresses and 20 bytes long hex-encoded strings get 'hash160' type @@ -76,6 +79,7 @@ const ( Examples: * 'int:42' is an integer with a value of 42 * '42' is an integer with a value of 42 + * 'nil' is a parameter with Any NEP-14 type and nil value (corresponds to Null stackitem) * 'bad' is a string with a value of 'bad' * 'dead' is a byte array with a value of 'dead' * 'string:dead' is a string with a value of 'dead' diff --git a/pkg/smartcontract/param_type.go b/pkg/smartcontract/param_type.go index 59d44ab04..c6d640ae5 100644 --- a/pkg/smartcontract/param_type.go +++ b/pkg/smartcontract/param_type.go @@ -327,6 +327,8 @@ func adjustValToType(typ ParamType, val string) (interface{}, error) { return pub.Bytes(), nil case StringType: return val, nil + case AnyType: + return nil, nil default: return nil, errors.New("unsupported parameter type") } @@ -347,6 +349,10 @@ func inferParamType(val string) ParamType { return IntegerType } + if val == "nil" { + return AnyType + } + if val == "true" || val == "false" { return BoolType } diff --git a/pkg/smartcontract/param_type_test.go b/pkg/smartcontract/param_type_test.go index 24ffa69fe..af643b042 100644 --- a/pkg/smartcontract/param_type_test.go +++ b/pkg/smartcontract/param_type_test.go @@ -155,6 +155,9 @@ func TestInferParamType(t *testing.T) { }, { in: "dead", out: ByteArrayType, + }, { + in: "nil", + out: AnyType, }} for _, inout := range inouts { out := inferParamType(inout.in) @@ -323,6 +326,10 @@ func TestAdjustValToType(t *testing.T) { typ: InteropInterfaceType, val: "", err: true, + }, { + typ: AnyType, + val: "nil", + out: nil, }} for _, inout := range inouts {