This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
interfaces.go
441 lines (413 loc) · 9.46 KB
/
interfaces.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) 2020-2021 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package micheline
import (
"encoding/json"
"strings"
)
func (s *Script) Implements(i Interface) bool {
eps, _ := s.Entrypoints(true)
if len(eps) == 0 {
return false
}
return i.Matches(eps)
}
func (s *Script) ImplementsStrict(i Interface) bool {
eps, _ := s.Entrypoints(true)
if len(eps) == 0 {
return false
}
return i.MatchesStrict(eps)
}
func (s *Script) Interfaces() Interfaces {
eps, _ := s.Entrypoints(true)
if len(eps) == 0 {
return nil
}
iv := make(Interfaces, 0)
for _, i := range WellKnownInterfaces {
if !i.Matches(eps) {
continue
}
iv = append(iv, i)
}
return iv
}
func (s *Script) InterfacesStrict() Interfaces {
eps, _ := s.Entrypoints(true)
if len(eps) == 0 {
return nil
}
iv := make(Interfaces, 0)
for _, i := range WellKnownInterfaces {
if !i.MatchesStrict(eps) {
continue
}
iv = append(iv, i)
}
return iv
}
type Interface string
func (m Interface) String() string {
return string(m)
}
// Checks if a contract implements all entrypoints required by a standard
// interface without requiring argument labels to match. This is a looser
// definition of interface compliance, but in line with the Michelson
// type system which ignores annotation labels for type equality.
//
// This check uses extracted Typedefs to avoid issues when the Micheline
// primitive structure diverges from the defined interface (e.g. due to
// comb type unfolding).
func (m Interface) Matches(e Entrypoints) bool {
for _, spec := range InterfaceSpecs[m] {
// use Typedef to avoid differences in data encoding with comb pairs
specType := NewType(spec).Typedef("")
var matched bool
for _, ep := range e {
// check entrypoint name
if ep.Name != spec.GetVarAnnoAny() {
continue
}
// check entrypoint type
epType := NewType(*ep.Prim).Typedef("")
if specType.Equal(epType) {
matched = true
break
}
}
if !matched {
return false
}
}
return true
}
// Checks if a contract strictly implements all standard interface
// entrypoints including argument types and argument names (annotations).
//
// This check uses extracted Typedefs to avoid issues when the Micheline
// primitive structure diverges from the defined interface (e.g. due to
// comb type unfolding).
func (m Interface) MatchesStrict(e Entrypoints) bool {
for _, spec := range InterfaceSpecs[m] {
// use Typedef to avoid differences in data encoding with comb pairs
specType := NewType(spec).Typedef("")
var matched bool
for _, ep := range e {
// check entrypoint name
if ep.Name != spec.GetVarAnnoAny() {
continue
}
// check entrypoint type
epType := NewType(*ep.Prim).Typedef("")
if specType.StrictEqual(epType) {
matched = true
break
}
}
if !matched {
return false
}
}
return true
}
func (m Interface) Contains(e Entrypoint) bool {
epType := NewType(*e.Prim).Typedef("")
for _, spec := range InterfaceSpecs[m] {
// check entrypoint name
if e.Name != spec.GetVarAnnoAny() {
continue
}
// check entrypoint type
specType := NewType(spec).Typedef("")
if specType.Equal(epType) {
return true
}
}
return false
}
func (m Interface) ContainsStrict(e Entrypoint) bool {
epType := NewType(*e.Prim).Typedef("")
for _, spec := range InterfaceSpecs[m] {
// check entrypoint name
if e.Name != spec.GetVarAnnoAny() {
continue
}
// check entrypoint type
specType := NewType(spec).Typedef("")
if specType.StrictEqual(epType) {
return true
}
}
return false
}
func (m Interface) FuncType(name string) Type {
for _, v := range InterfaceSpecs[m] {
if v.GetVarAnnoAny() == name {
return NewType(v)
}
}
return Type{}
}
func (m Interface) FuncPrim(name string) Prim {
for _, v := range InterfaceSpecs[m] {
if v.GetVarAnnoAny() == name {
return v
}
}
return Prim{}
}
type Interfaces []Interface
func (i Interfaces) Contains(x Interface) bool {
for _, v := range i {
if v == x {
return true
}
}
return false
}
func (i Interfaces) String() string {
if len(i) == 0 {
return ""
}
var b strings.Builder
for k, v := range i {
if k > 0 {
b.WriteRune(',')
}
b.WriteString(string(v))
}
return b.String()
}
func (i *Interfaces) Parse(s string) error {
if len(s) == 0 {
return nil
}
split := strings.Split(s, ",")
if cap(*i) < len(split) {
*i = make([]Interface, len(split))
}
(*i) = (*i)[:len(split)]
for k := range split {
(*i)[k] = Interface(split[k])
}
return nil
}
func (i Interfaces) MarshalText() ([]byte, error) {
return []byte(i.String()), nil
}
func (i *Interfaces) UnmarshalText(b []byte) error {
return i.Parse(string(b))
}
func (i Interfaces) MarshalJSON() ([]byte, error) {
x := make([]string, len(i))
for k, v := range i {
x[k] = string(v)
}
return json.Marshal(x)
}
var (
IManager = Interface("MANAGER")
ISetDelegate = Interface("SET_DELEGATE")
ITzip5 = Interface("TZIP-005")
ITzip7 = Interface("TZIP-007")
ITzip12 = Interface("TZIP-012")
WellKnownInterfaces = []Interface{
IManager,
ISetDelegate,
ITzip5,
ITzip7,
ITzip12,
}
)
// WellKnownInterfaces contains entrypoint types for standard call interfaces and other
// known contracts.
var InterfaceSpecs = map[Interface][]Prim{
// manager.tz
IManager: {
// 1 (lambda %do unit (list operation))
NewCodeAnno(T_LAMBDA, "%do", NewCode(T_UNIT), NewCode(T_LIST, NewCode(T_OPERATION))),
// 2 (unit %default)
NewCodeAnno(T_UNIT, "%default"),
},
// generic set delegate interface
ISetDelegate: {
// option %setDelegate key_hash
NewCodeAnno(T_OPTION, "%setDelegate", NewCode(T_KEY_HASH)),
},
// Tzip 5 a.k.a FA1
// https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-5/tzip-5.md
ITzip5: {
// (address :from, (address :to, nat :value)) %transfer
NewPairType(
NewCodeAnno(T_ADDRESS, ":from"),
NewPairType(
NewCodeAnno(T_ADDRESS, ":to"),
NewCodeAnno(T_NAT, ":value"),
),
"%transfer",
),
// view (address :owner) nat %getBalance
NewPairType(
NewCodeAnno(T_ADDRESS, ":owner"),
NewCode(T_CONTRACT, NewCode(T_NAT)),
"%getBalance",
),
// view unit nat %getTotalSupply
NewPairType(
NewCode(T_UNIT),
NewCode(T_CONTRACT, NewCode(T_NAT)),
"%getTotalSupply",
),
},
// Tzip 7 a.k.a FA1.2
// https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-7/tzip-7.md
ITzip7: {
// (address :from, (address :to, nat :value)) %transfer
NewPairType(
NewCodeAnno(T_ADDRESS, ":from"),
NewPairType(
NewCodeAnno(T_ADDRESS, ":to"),
NewCodeAnno(T_NAT, ":value"),
),
"%transfer",
),
// (address :spender, nat :value) %approve
NewPairType(
NewCodeAnno(T_ADDRESS, ":spender"),
NewCodeAnno(T_NAT, ":value"),
"%approve",
),
// (view (address :owner, address :spender) nat) %getAllowance
NewPairType(
NewPairType(
NewCodeAnno(T_ADDRESS, ":owner"),
NewCodeAnno(T_ADDRESS, ":spender"),
),
NewCode(T_CONTRACT, NewCode(T_NAT)),
"%getAllowance",
),
// (view (address :owner) nat) %getBalance
NewPairType(
NewCodeAnno(T_ADDRESS, ":owner"),
NewCode(T_CONTRACT, NewCode(T_NAT)),
"%getBalance",
),
// (view unit nat) %getTotalSupply
NewPairType(
NewCode(T_UNIT),
NewCode(T_CONTRACT, NewCode(T_NAT)),
"%getTotalSupply",
),
},
// Tzip 12 a.k.a. FA2
// https://gitlab.com/tzip/tzip/-/blob/master/proposals/tzip-12/tzip-12.md
ITzip12: {
// (list %transfer
// (pair
// (address %from_)
// (list %txs
// (pair
// (address %to_)
// (pair
// (nat %token_id)
// (nat %amount)
// )
// )
// )
// )
// )
NewCodeAnno(T_LIST, "%transfer",
NewPairType(
NewCodeAnno(T_ADDRESS, "%from_"),
NewCodeAnno(T_LIST, "%txs",
NewPairType(
NewCodeAnno(T_ADDRESS, "%to_"),
NewPairType(
NewCodeAnno(T_NAT, "%token_id"),
NewCodeAnno(T_NAT, "%amount"),
),
),
),
),
),
// (pair %balance_of
// (list %requests
// (pair
// (address %owner)
// (nat %token_id)
// )
// )
// (contract %callback
// (list
// (pair
// (pair %request
// (address %owner)
// (nat %token_id)
// )
// (nat %balance)
// )
// )
// )
// )
NewPairType(
NewCodeAnno(T_LIST, "%requests",
NewPairType(
NewCodeAnno(T_ADDRESS, "%owner"),
NewCodeAnno(T_NAT, "%token_id"),
),
),
NewCodeAnno(T_CONTRACT, "%callback",
NewCode(T_LIST,
NewPairType(
NewPairType(
NewCodeAnno(T_ADDRESS, "%owner"),
NewCodeAnno(T_NAT, "%token_id"),
"%request",
),
NewCodeAnno(T_NAT, "%balance"),
),
),
),
"%balance_of",
),
// (list %update_operators
// (or
// (pair %add_operator
// (address %owner)
// (pair
// (address %operator)
// (nat %token_id)
// )
// )
// (pair %remove_operator
// (address %owner)
// (pair
// (address %operator)
// (nat %token_id)
// )
// )
// )
// )
NewCodeAnno(T_LIST, "%update_operators",
NewCode(T_OR,
NewPairType(
NewCodeAnno(T_ADDRESS, "%owner"),
NewPairType(
NewCodeAnno(T_ADDRESS, "%operator"),
NewCodeAnno(T_NAT, "%token_id"),
),
"%add_operator",
),
NewPairType(
NewCodeAnno(T_ADDRESS, "%owner"),
NewPairType(
NewCodeAnno(T_ADDRESS, "%operator"),
NewCodeAnno(T_NAT, "%token_id"),
),
"%remove_operator",
),
),
),
},
}