diff --git a/pkg/morph/client/util.go b/pkg/morph/client/util.go index 59f608303..4066472e4 100644 --- a/pkg/morph/client/util.go +++ b/pkg/morph/client/util.go @@ -2,6 +2,7 @@ package client import ( "encoding/binary" + "math/big" sc "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" @@ -157,6 +158,11 @@ func IntFromStackItem(param stackitem.Item) (int64, error) { } } +// BigIntFromStackItem receives numerical value from the value of a smart contract parameter. +func BigIntFromStackItem(param stackitem.Item) (*big.Int, error) { + return param.TryInteger() +} + // BytesFromStackItem receives binary value from the value of a smart contract parameter. func BytesFromStackItem(param stackitem.Item) ([]byte, error) { switch param.Type() { diff --git a/pkg/morph/client/util_test.go b/pkg/morph/client/util_test.go index 8b0eff8f8..9d73376a8 100644 --- a/pkg/morph/client/util_test.go +++ b/pkg/morph/client/util_test.go @@ -1,9 +1,11 @@ package client import ( + "math" "math/big" "testing" + "github.com/nspcc-dev/neo-go/pkg/encoding/bigint" sc "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" @@ -50,9 +52,13 @@ var ( Value: []sc.Parameter{intParam, byteArrayParam}, } + bigIntValue = new(big.Int).Mul(big.NewInt(math.MaxInt64), big.NewInt(10)) + stringByteItem = stackitem.NewByteArray([]byte("Hello World")) intItem = stackitem.NewBigInteger(new(big.Int).SetInt64(1)) + bigIntItem = stackitem.NewBigInteger(bigIntValue) byteWithIntItem = stackitem.NewByteArray([]byte{0x0a}) + byteWithBigIntItem = stackitem.NewByteArray(bigint.ToBytes(bigIntValue)) emptyByteArrayItem = stackitem.NewByteArray([]byte{}) trueBoolItem = stackitem.NewBool(true) falseBoolItem = stackitem.NewBool(false) @@ -233,6 +239,27 @@ func TestIntFromStackItem(t *testing.T) { }) } +func TestBigIntFromStackItem(t *testing.T) { + t.Run("correct assert", func(t *testing.T) { + val, err := BigIntFromStackItem(bigIntItem) + require.NoError(t, err) + require.Equal(t, bigIntValue, val) + + val, err = BigIntFromStackItem(byteWithBigIntItem) + require.NoError(t, err) + require.Equal(t, bigIntValue, val) + + val, err = BigIntFromStackItem(emptyByteArrayItem) + require.NoError(t, err) + require.Equal(t, big.NewInt(0), val) + }) + + t.Run("incorrect assert", func(t *testing.T) { + _, err := BigIntFromStackItem(arrayItem) + require.Error(t, err) + }) +} + func TestStringFromStackItem(t *testing.T) { t.Run("correct assert", func(t *testing.T) { val, err := StringFromStackItem(stringByteItem)