-
Notifications
You must be signed in to change notification settings - Fork 217
/
abi.go
145 lines (125 loc) · 3.44 KB
/
abi.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
package eos
import (
"encoding/json"
"fmt"
"io"
)
// see: libraries/chain/contracts/abi_serializer.cpp:53...
// see: libraries/chain/include/eosio/chain/contracts/types.hpp:100
type ABI struct {
fitNodeos bool
Version string `json:"version"`
Types []ABIType `json:"types,omitempty"`
Structs []StructDef `json:"structs,omitempty"`
Actions []ActionDef `json:"actions,omitempty"`
Tables []TableDef `json:"tables,omitempty"`
RicardianClauses []ClausePair `json:"ricardian_clauses,omitempty"`
ErrorMessages []ABIErrorMessage `json:"error_messages,omitempty"`
Extensions []*Extension `json:"abi_extensions,omitempty"`
Variants []VariantDef `json:"variants,omitempty" eos:"binary_extension"`
ActionResults []ActionResultDef `json:"action_results,omitempty" eos:"binary_extension"`
}
func NewABI(r io.Reader) (*ABI, error) {
abi := &ABI{}
abiDecoder := json.NewDecoder(r)
err := abiDecoder.Decode(abi)
if err != nil {
return nil, fmt.Errorf("read abi: %w", err)
}
return abi, nil
}
func (a *ABI) SetFitNodeos(v bool) {
a.fitNodeos = v
}
func (a *ABI) ActionForName(name ActionName) *ActionDef {
for _, a := range a.Actions {
if a.Name == name {
return &a
}
}
return nil
}
func (a *ABI) StructForName(name string) *StructDef {
for _, s := range a.Structs {
if s.Name == name {
return &s
}
}
return nil
}
func (a *ABI) TableForName(name TableName) *TableDef {
for _, s := range a.Tables {
if s.Name == name {
return &s
}
}
return nil
}
func (a *ABI) VariantForName(name string) *VariantDef {
for _, s := range a.Variants {
if s.Name == name {
return &s
}
}
return nil
}
func (a *ABI) ActionResultForName(name ActionName) *ActionResultDef {
for _, s := range a.ActionResults {
if s.Name == name {
return &s
}
}
return nil
}
func (a *ABI) TypeNameForNewTypeName(typeName string) (resolvedTypeName string, isAlias bool) {
for _, t := range a.Types {
if t.NewTypeName == typeName {
return t.Type, true
}
}
return typeName, false
}
type ABIType struct {
NewTypeName string `json:"new_type_name"`
Type string `json:"type"`
}
type StructDef struct {
Name string `json:"name"`
Base string `json:"base"`
Fields []FieldDef `json:"fields,omitempty"`
}
type FieldDef struct {
Name string `json:"name"`
Type string `json:"type"`
}
type ActionDef struct {
Name ActionName `json:"name"`
Type string `json:"type"`
RicardianContract string `json:"ricardian_contract"`
}
// TableDef defines a table. See libraries/chain/include/eosio/chain/contracts/types.hpp:78
type TableDef struct {
Name TableName `json:"name"`
IndexType string `json:"index_type"`
KeyNames []string `json:"key_names,omitempty"`
KeyTypes []string `json:"key_types,omitempty"`
Type string `json:"type"`
}
// VariantDef defines a variant type. See libraries/chain/include/eosio/chain/contracts/types.hpp:78
type VariantDef struct {
Name string `json:"name"`
Types []string `json:"types,omitempty"`
}
// ClausePair represents clauses, related to Ricardian Contracts.
type ClausePair struct {
ID string `json:"id"`
Body string `json:"body"`
}
type ABIErrorMessage struct {
Code Uint64 `json:"error_code"`
Message string `json:"error_msg"`
}
type ActionResultDef struct {
Name ActionName `json:"name"`
ResultType string `json:"result_type"`
}