diff --git a/pkg/smartcontract/parameter.go b/pkg/smartcontract/parameter.go index 170a59028..e4496fd20 100644 --- a/pkg/smartcontract/parameter.go +++ b/pkg/smartcontract/parameter.go @@ -333,13 +333,9 @@ func NewParameterFromValue(value interface{}) (Parameter, error) { result.Type = ArrayType result.Value = arr case []interface{}: - arr := make([]Parameter, 0, len(v)) - for i := range v { - elem, err := NewParameterFromValue(v[i]) - if err != nil { - return result, err - } - arr = append(arr, elem) + arr, err := NewParametersFromValues(v...) + if err != nil { + return result, err } result.Type = ArrayType result.Value = arr @@ -350,6 +346,20 @@ func NewParameterFromValue(value interface{}) (Parameter, error) { return result, nil } +// NewParametersFromValues is similar to NewParameterFromValue except that it +// works with multiple values and returns a simple slice of Parameter. +func NewParametersFromValues(values ...interface{}) ([]Parameter, error) { + res := make([]Parameter, 0, len(values)) + for i := range values { + elem, err := NewParameterFromValue(values[i]) + if err != nil { + return nil, err + } + res = append(res, elem) + } + return res, nil +} + // ExpandParameterToEmitable converts a parameter to a type which can be handled as // an array item by emit.Array. It correlates with the way an RPC server handles // FuncParams for invoke* calls inside the request.ExpandArrayIntoScript function. diff --git a/pkg/smartcontract/parameter_test.go b/pkg/smartcontract/parameter_test.go index 281230ffd..181901876 100644 --- a/pkg/smartcontract/parameter_test.go +++ b/pkg/smartcontract/parameter_test.go @@ -692,3 +692,20 @@ func TestParameterFromValue(t *testing.T) { _, err = NewParameterFromValue([]interface{}{1, 2, make(map[string]int)}) require.Error(t, err) } + +func TestParametersFromValues(t *testing.T) { + res, err := NewParametersFromValues(42, "some", []byte{3, 2, 1}) + require.NoError(t, err) + require.Equal(t, []Parameter{{ + Type: IntegerType, + Value: big.NewInt(42), + }, { + Type: StringType, + Value: "some", + }, { + Type: ByteArrayType, + Value: []byte{3, 2, 1}, + }}, res) + _, err = NewParametersFromValues(42, make(map[int]int), []byte{3, 2, 1}) + require.Error(t, err) +}