-
Notifications
You must be signed in to change notification settings - Fork 5
/
interface.go
64 lines (56 loc) · 1.52 KB
/
interface.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
package interfaces
import (
stdJSON "encoding/json"
"github.com/dipdup-net/go-lib/tools/consts"
jsoniter "github.com/json-iterator/go"
"github.com/pkg/errors"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
var all = map[string]Contract{
consts.ViewAddressTag: &ViewAddress{},
consts.ViewBalanceOfTag: &ViewBalanceOf{},
consts.ViewNatTag: &ViewNat{},
consts.FA1Tag: &Fa1{},
consts.FA12Tag: &Fa1_2{},
consts.FA2Tag: &Fa2{},
}
// Contract -
type Contract interface {
GetName() string
GetContractInterface() string
}
// ContractInterface -
type ContractInterface struct {
IsRoot bool `json:"is_root,omitempty"`
Entrypoints map[string]stdJSON.RawMessage
}
// GetAll - receives all contract interfaces
func GetAll() (map[string]ContractInterface, error) {
res := make(map[string]ContractInterface)
for _, i := range all {
name := i.GetName()
str := i.GetContractInterface()
var ci ContractInterface
if err := json.UnmarshalFromString(str, &ci); err != nil {
return nil, err
}
res[name] = ci
}
return res, nil
}
// GetMethods - returns list of interface methods
func GetMethods(name string) ([]string, error) {
i, ok := all[name]
if !ok {
return nil, errors.Errorf("Unknown interface name: %s", name)
}
var ci ContractInterface
if err := json.UnmarshalFromString(i.GetContractInterface(), &ci); err != nil {
return nil, err
}
methods := make([]string, 0)
for entrypoint := range ci.Entrypoints {
methods = append(methods, entrypoint)
}
return methods, nil
}