-
Notifications
You must be signed in to change notification settings - Fork 1
/
constructor.go
85 lines (75 loc) · 2.46 KB
/
constructor.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
package abi
// Constructor represents a constructor in an Contract. The constructor can be used to
// encode arguments for a constructor call.
type Constructor struct {
inputs *TupleType
config *ABI
}
// NewConstructor creates a new Constructor instance.
func NewConstructor(inputs *TupleType) *Constructor {
return Default.NewConstructor(inputs)
}
// ParseConstructor parses a constructor signature and returns a new Constructor.
//
// A constructor signature is similar to a method signature, but it does not
// have a name and returns no values. It can be optionally prefixed with the
// "constructor" keyword.
//
// The following examples are valid signatures:
//
// ((uint256,bytes32)[])
// ((uint256 a, bytes32 b)[] c)
// constructor(tuple(uint256 a, bytes32 b)[] memory c)
//
// This function is equivalent to calling Parser.ParseConstructor with the
// default configuration.
func ParseConstructor(signature string) (*Constructor, error) {
return Default.ParseConstructor(signature)
}
// MustParseConstructor is like ParseConstructor but panics on error.
func MustParseConstructor(signature string) *Constructor {
c, err := ParseConstructor(signature)
if err != nil {
panic(err)
}
return c
}
// NewConstructor creates a new Constructor instance.
func (a *ABI) NewConstructor(inputs *TupleType) *Constructor {
return &Constructor{
inputs: inputs,
config: a,
}
}
// ParseConstructor parses a constructor signature and returns a new Constructor.
//
// See ParseConstructor for more information.
func (a *ABI) ParseConstructor(signature string) (*Constructor, error) {
return parseConstructor(a, signature)
}
// Inputs returns the input arguments of the constructor as a tuple type.
func (m *Constructor) Inputs() *TupleType {
return m.inputs
}
// EncodeArg encodes arguments for a constructor call using a provided map or
// structure. The map or structure must have fields with the same names as
// the constructor arguments.
func (m *Constructor) EncodeArg(arg any) ([]byte, error) {
encoded, err := m.config.EncodeValue(m.inputs, arg)
if err != nil {
return nil, err
}
return encoded, nil
}
// EncodeArgs encodes arguments for a constructor call.
func (m *Constructor) EncodeArgs(args ...any) ([]byte, error) {
encoded, err := m.config.EncodeValues(m.inputs, args...)
if err != nil {
return nil, err
}
return encoded, nil
}
// String returns the human-readable signature of the constructor.
func (m *Constructor) String() string {
return "constructor" + m.inputs.String()
}