forked from NethermindEth/starknet.go
/
contract.go
200 lines (162 loc) · 4.2 KB
/
contract.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package types
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"fmt"
)
type EntryPoint struct {
// The offset of the entry point in the program
Offset NumAsHex `json:"offset"`
// A unique identifier of the entry point (function) in the program
Selector string `json:"selector"`
}
type ABI []ABIEntry
type EntryPointsByType struct {
Constructor []EntryPoint `json:"CONSTRUCTOR"`
External []EntryPoint `json:"EXTERNAL"`
L1Handler []EntryPoint `json:"L1_HANDLER"`
}
type ContractClass struct {
// Program A base64 representation of the compressed program code
Program string `json:"program"`
EntryPointsByType EntryPointsByType `json:"entry_points_by_type"`
ABI *ABI `json:"abi,omitempty"`
}
func (c *ContractClass) UnmarshalJSON(content []byte) error {
v := map[string]json.RawMessage{}
if err := json.Unmarshal(content, &v); err != nil {
return err
}
// process 'program'. If it is a string, keep it, otherwise encode it.
data, ok := v["program"]
if !ok {
return fmt.Errorf("missing program in json object")
}
program := ""
if err := json.Unmarshal(data, &program); err != nil {
if program, err = encodeProgram(data); err != nil {
return err
}
}
c.Program = program
// process 'entry_points_by_type'
data, ok = v["entry_points_by_type"]
if !ok {
return fmt.Errorf("missing entry_points_by_type in json object")
}
entryPointsByType := EntryPointsByType{}
if err := json.Unmarshal(data, &entryPointsByType); err != nil {
return err
}
c.EntryPointsByType = entryPointsByType
// process 'abi'
data, ok = v["abi"]
if !ok {
// contractClass can have an empty ABI for instance with ClassAt
return nil
}
abis := []interface{}{}
if err := json.Unmarshal(data, &abis); err != nil {
return err
}
abiPointer := ABI{}
for _, abi := range abis {
if checkABI, ok := abi.(map[string]interface{}); ok {
var ab ABIEntry
abiType, ok := checkABI["type"].(string)
if !ok {
return fmt.Errorf("unknown abi type %v", checkABI["type"])
}
switch abiType {
case string(ABITypeConstructor), string(ABITypeFunction), string(ABITypeL1Handler):
ab = &FunctionABIEntry{}
case string(ABITypeStruct):
ab = &StructABIEntry{}
case string(ABITypeEvent):
ab = &EventABIEntry{}
default:
return fmt.Errorf("unknown ABI type %v", checkABI["type"])
}
data, err := json.Marshal(checkABI)
if err != nil {
return err
}
err = json.Unmarshal(data, ab)
if err != nil {
return err
}
abiPointer = append(abiPointer, ab)
}
}
c.ABI = &abiPointer
return nil
}
type ABIEntry interface {
IsType() ABIType
}
type ABIType string
const (
ABITypeConstructor ABIType = "constructor"
ABITypeFunction ABIType = "function"
ABITypeL1Handler ABIType = "l1_handler"
ABITypeEvent ABIType = "event"
ABITypeStruct ABIType = "struct"
)
type StructABIEntry struct {
// The event type
Type ABIType `json:"type"`
// The event name
Name string `json:"name"`
Size uint64 `json:"size"`
Members []Member `json:"members"`
}
type Member struct {
TypedParameter
Offset uint64 `json:"offset"`
}
type EventABIEntry struct {
// The event type
Type ABIType `json:"type"`
// The event name
Name string `json:"name"`
Keys []TypedParameter `json:"keys"`
Data []TypedParameter `json:"data"`
}
type FunctionABIEntry struct {
// The function type
Type ABIType `json:"type"`
// The function name
Name string `json:"name"`
StateMutability *string `json:"stateMutability,omitempty"`
Inputs []TypedParameter `json:"inputs"`
Outputs []TypedParameter `json:"outputs"`
}
func (s *StructABIEntry) IsType() ABIType {
return s.Type
}
func (e *EventABIEntry) IsType() ABIType {
return e.Type
}
func (f *FunctionABIEntry) IsType() ABIType {
return f.Type
}
type TypedParameter struct {
// The parameter's name
Name string `json:"name"`
// The parameter's type
Type string `json:"type"`
}
// encodeProgram compress a program to send it to the API
func encodeProgram(content []byte) (string, error) {
buf := bytes.NewBuffer(nil)
gzipContent := gzip.NewWriter(buf)
_, err := gzipContent.Write(content)
if err != nil {
return "", err
}
gzipContent.Close()
program := base64.StdEncoding.EncodeToString(buf.Bytes())
return program, nil
}