-
Notifications
You must be signed in to change notification settings - Fork 38
/
util.go
87 lines (75 loc) · 2.58 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package client
import (
"math/big"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/pkg/errors"
)
/*
Use these function to parse stack parameters obtained from `TestInvoke`
function to native go types. You should know upfront return types of invoked
method.
*/
// BoolFromStackItem receives boolean value from the value of a smart contract parameter.
func BoolFromStackItem(param stackitem.Item) (bool, error) {
switch param.Type() {
case stackitem.BooleanT, stackitem.IntegerT, stackitem.ByteArrayT:
return param.TryBool()
default:
return false, errors.Errorf("chain/client: %s is not a bool type", param.Type())
}
}
// IntFromStackItem receives numerical value from the value of a smart contract parameter.
func IntFromStackItem(param stackitem.Item) (int64, error) {
switch param.Type() {
case stackitem.IntegerT, stackitem.ByteArrayT:
i, err := param.TryInteger()
if err != nil {
return 0, err
}
return i.Int64(), nil
default:
return 0, errors.Errorf("chain/client: %s is not an integer type", param.Type())
}
}
// 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() {
case stackitem.BufferT, stackitem.ByteArrayT:
return param.TryBytes()
case stackitem.AnyT:
if param.Value() == nil {
return nil, nil
}
fallthrough
default:
return nil, errors.Errorf("chain/client: %s is not a byte array type", param.Type())
}
}
// ArrayFromStackItem returns the slice contract parameters from passed parameter.
//
// If passed parameter carries boolean false value, (nil, nil) returns.
func ArrayFromStackItem(param stackitem.Item) ([]stackitem.Item, error) {
switch param.Type() {
case stackitem.AnyT:
return nil, nil
case stackitem.ArrayT, stackitem.StructT:
items, ok := param.Value().([]stackitem.Item)
if !ok {
return nil, errors.Errorf("chain/client: can't convert %T to parameter slice", param.Value())
}
return items, nil
default:
return nil, errors.Errorf("chain/client: %s is not an array type", param.Type())
}
}
// StringFromStackItem receives string value from the value of a smart contract parameter.
func StringFromStackItem(param stackitem.Item) (string, error) {
if param.Type() != stackitem.ByteArrayT {
return "", errors.Errorf("chain/client: %s is not an integer type", param.Type())
}
return stackitem.ToString(param)
}