-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathschema-abi-json-bridge.js
206 lines (185 loc) · 5.21 KB
/
schema-abi-json-bridge.js
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
import Func from './ethabi-js/spec/function';
import EthAbi from "./ethabi-js";
export default AbiJsonBribge = {
schema: (abi, name, { fields, except, custom, constant = 0, func = 0}={}) => {
let keys, conten;
const abiParsed = new EthAbi(abi);
// Get field definitions for the all types
keys = getFields({abiParsed, fields, except, constant, func});
content = keys.map(item => {
if(item.name == "constructor" && _.isEmpty(item.inputs)) {
return null;
}
return getFieldSchema(item, custom);
});
return new SimpleSchema(content);
},
events: (abi) => {
const abiParsed = new EthAbi(abi);
return abiParsed.events;
},
payable: (abi, name) => {
let keys;
const abiParsed = new EthAbi(abi);
let payable = false;
// Get field definitions for the all types
keys = getFields({abiParsed});
keys.map(item => {
if(item.name == "constructor" && _.isEmpty(item.inputs)) {
payable = true;
}
});
return payable;
},
};
// Get field key definition
const getFieldSchema = (item, custom = {}) => {
let value = {};
if(custom[item.name]) {
value = custom[item.name];
value.abiInterface = item.abi;
return value;
}
if(!_.isEmpty(item.inputs)) {
_.map(item.inputs, function(param) {
const format = getFieldFormat(param.name, param._kind, item.name);
value[item.name + "." + param.name] = format;
})
}
if(_.isEmpty(value)) {
const attrClass = item.constant ? "": "hidden";
value[item.name] = {"type": String,
"label": item.name,
"autoform": {
"group": item.name,
"afFieldInput": {
"readonly": true,
"class": attrClass
},
},
"optional": true,
"abiInterface": item.abi
}
return value;
}
let out = {};
out[item.name] = {"type": Object,
"label": item.name,
"autoform": {
"group": item.name
},
"optional": true,
"abiInterface": item.abi
};
return _.extend(out, value);
};
const getFieldFormat = (name, kind, parent) => {
let value = '';
switch(kind._type) {
case 'address':
value = {type: String,
label: name + " (" + kind._type + ")",
optional: true,
autoform: {
group: parent
}
};
break;
case 'fixedBytes':
case 'bytes':
value = {type: String,
label: name + " (" + kind._type + ")",
optional: true,
autoform: {
afFieldInput: {
type: "textarea",
rows: 5
},
group: parent
}
};
break;
case 'int':
case 'uint':
value = {type: String,
label: name + " (" + kind._type + ")",
optional: true,
autoform: {
group: parent,
placeholder: kind._type
}
};
break;
case 'bool':
value = {type: Boolean,
label: name + " (" + kind._type + ")",
optional: true,
autoform: {
group: parent
}
};
break;
case 'string':
value = {type: String,
label: name + " (" + kind._type + ")",
optional: true,
autoform: {
group: parent,
}
};
break;
case 'array':
case 'fixedArray':
value = {type: String,
label: name + "(" + kind._subtype._type + "[] in JSON format)",
optional: true,
autoform: {
group: parent,
afFieldInput: {
type: "textarea",
rows: 10
}
}
};
break;
}
return value;
};
const getFields = ({abiParsed, fields, except=[], constant = 0, func = 0}) => {
let keys, objectKeys;
if(fields && !fields.length) {
fields = null;
}
if(except && !except.length) {
except = null;
}
// Get firstLevelKeys
preparedFields = abiParsed._interface.filter(k => {
if(fields) {
return fields.indexOf(k._name) > -1;
}
if(except) {
return except.indexOf(k._name) == -1;
}
let result_func = true;
if(func == -1) {
result_func = !(k instanceof Func);
}
if(func == 1) {
result_func = k instanceof Func;
}
let result_constant = true;
if(constant == -1) {
result_constant = new Boolean(k._constant) == false;
}
if(constant == 1) {
result_constant = new Boolean(k._constant) == true;
}
return result_func && result_constant;
}).map(k => {
k._name = _.isUndefined(k._name) ? 'constructor': k._name;
k._name = k._name == 'undefined' ? 'fallback': k._name;
return {"name": k._name, "inputs": k._inputs, "abi": k}
});
return preparedFields;
};