-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquickAbiForm.js
258 lines (238 loc) · 9.76 KB
/
quickAbiForm.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
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
import EthAbi from "./ethabi-js";
import Constructor from './ethabi-js/spec/constructor';
import Decoder from './ethabi-js/decoder';
import Token from './ethabi-js/token';
const executeFunctionByName = function(functionName, context /*, args */) {
var args = [].slice.call(arguments).splice(2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for(var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
};
Template.quickAbiForm.onRendered(function () {
var context = Template.instance().data;
var adjustedData = AutoForm.parseData(context);
var simpleSchema = adjustedData._resolvedSchema;
var resultCallback = !_.isUndefined(context.resultCallback) ? context.resultCallback : function(error, result) {console.log(error, result)};
var parents = _.filter(Object.keys(simpleSchema._schema),
function(item) {
return !_.isUndefined(simpleSchema._schema[item].abiInterface) &&
simpleSchema._schema[item].abiInterface._inputs.length == 0 &&
simpleSchema._schema[item].abiInterface._constant
item.indexOf('.') == -1;
}
);
var sourceId = !_.isUndefined(context.id) ? context.id : "commonAbiJson";
var address = context.address;
const updateSC = function () {
_.map(parents, function(id) {
var localSourceId = sourceId + "" + id;
var abiInterface = simpleSchema._schema[id].abiInterface;
var cr = new Constructor(abiInterface);
let outParams = [];
let outParamsName = [];
_.map(abiInterface._outputs, function(data, i) {
outParamsName.push(abiInterface._outputs[i].name);
outParams.push(abiInterface._outputs[i]._kind);
});
const encodedData = "0x" + abiInterface._signature;
try {
web3.eth.call({
to: address,
data: encodedData
}, function(error, result) {
if(error) {
console.log(error);
return false;
} else {
const outDecoded = Decoder.decode(outParams, result);
let namedOut = [];
namedOut = _.map(outParamsName, function(name, i) {
return {"name": name, "value": outDecoded[i].value, "type": outDecoded[i].type};
});
namedOut = executeFunctionByName(resultCallback, window, null, namedOut, id, context);
let finishOut = [];
finishOut = _.map(namedOut, function(item) {
if(_.isArray(item.value)) {
return _.map(item.value, function(item2) {
return item2.name ? item2.name + ": " + item2.value: item2.value;
}).join(', ');
} else {
return item.name ? item.name + ": " + item.value: item.value;
}
});
$('#' + localSourceId + ' input[data-schema-key="' + id + '"]').val(finishOut.join(', '));
}
});
} catch(e) {
executeFunctionByName(resultCallback, window, e);
}
});
};
setTimeout(function() {
updateSC();
}, 3000);
this.updateInterval = setInterval(function() {
updateSC();
}, 30000);
});
Template.quickAbiForm.onDestroyed(function () {
Meteor.clearInterval(this.updateInterval);
});
Template.quickAbiForm.helpers({
schema: function() {
var context = _.clone(this);
var adjustedData = AutoForm.parseData(_.clone(this));
var simpleSchema = adjustedData._resolvedSchema;
var resultCallback = !_.isUndefined(context.resultCallback) ? context.resultCallback : function(error, result) {console.log(error, result)};
var parents = _.filter(Object.keys(simpleSchema._schema), function(item) {return item.indexOf('.') == -1;});
var ids = {};
var sourceId = !_.isUndefined(context.id) ? context.id : "commonAbiJson";
var address = context.address;
_.map(parents, function(id) {
var localSourceId = sourceId + "" + id;
ids[localSourceId] = {
onSubmit: function(insertDoc, updateDoc, currentDoc) {
var interContext = this;
var fieldName = interContext.ss._firstLevelSchemaKeys[0];
var abiInterface = interContext.ss._schema[fieldName].abiInterface;
var cr = new Constructor(abiInterface);
var params= [];
let index = 0;
let breakProcess = false;
_.map(insertDoc[fieldName], function(data) {
if(_.indexOf(['fixedArray', 'array'], abiInterface._inputs[index]._kind._type) != -1) {
try {
parsedData = JSON.parse(data);
data = parsedData.map((value) => new Token(abiInterface._inputs[index]._kind._subtype._type, value));
} catch(e) {
executeFunctionByName(resultCallback, window, "Wrong JSON format");
breakProcess = true;
return false;
}
}
params.push(new Token(abiInterface._inputs[index]._kind._type, data));
index++;
});
if(breakProcess) {
interContext.done(new Error("Wrong JSON format"));
return false;
}
var outParams= [];
var outParamsName = [];
_.map(abiInterface._outputs, function(data, i) {
outParamsName.push(abiInterface._outputs[i].name);
outParams.push(abiInterface._outputs[i]._kind);
});
const encodedData = "0x" + abiInterface._signature + cr.encodeCall(params);
if(abiInterface._constant) {
try {
web3.eth.call({
to: address,
data: encodedData
}, function(error, result) {
if(error) {
try {
executeFunctionByName(resultCallback, window, error);
} catch(e) {
console.log(e);
}
return interContext.done(error);
} else {
let outDecoded = Decoder.decode(outParams, result);
let index = 0;
let namedOut = [];
namedOut = _.map(outParamsName, function(name, i) {
return {"name": name, "value": outDecoded[i].value, "type": outDecoded[i].type};
});
try {
namedOut = executeFunctionByName(resultCallback, window, null, namedOut, fieldName, context);
} catch(e) {
console.log(e);
}
interContext.done(null, namedOut);
return false;
}
});
} catch(e) {
executeFunctionByName(resultCallback, window, e);
interContext.done(e);
return false;
}
} else {
try {
web3.eth.sendTransaction({
to: address,
data: encodedData
}, function(error, result) {
if(error) {
try {
executeFunctionByName(resultCallback, window, error);
} catch(e) {
console.log(e);
}
interContext.done(error);
return false;
} else {
var outDecoded = Decoder.decode(outParams, result);
var namedOut = [];
namedOut = _.map(outParamsName, function(name, i) {
return {"name": name, "value": outDecoded[i].value, "type": outDecoded[i].type};
});
try {
namedOut = executeFunctionByName(resultCallback, window, null, namedOut, fieldName, context);
} catch(e) {
console.log(e);
}
return interContext.done(null, namedOut);
}
});
} catch(e) {
executeFunctionByName(resultCallback, window, e);
return interContext.done(e);
}
}
return false;
},
onError: function (formType, error) {
try {
executeFunctionByName(resultCallback, window, error);
} catch(e) {
console.log(e);
}
return false;
}
}
});
AutoForm.hooks(ids);
return parents;
},
innerContext: function() {
var context = _.clone(Template.parentData(1));
var sourceField = Template.currentData();
context.id = !_.isUndefined(context.id) ? context.id : "commonAbiJson";
context.id = context.id + "" + sourceField;
context.buttonContent = !_.isUndefined(context.buttonContent) ? context.buttonContent : "Send";
var keys = Object.keys(context.schema._schema);
if(!_.isUndefined(context.schema._schema[sourceField].abiInterface) &&
_.isEmpty(context.schema._schema[sourceField].abiInterface._inputs) &&
context.schema._schema[sourceField].abiInterface.constant
) {
context.buttonContent = false;
}
if(!_.isUndefined(context.schema._schema[sourceField].abiInterface) &&
context.schema._schema[sourceField].abiInterface.payable
) {
context.class = "abiPayable";
}
var fields = [sourceField];
var additionalFields = _.filter(keys, function(field) {
return field.indexOf(sourceField + ".") != -1;
});
fields = _.union(fields, additionalFields);
context.schema = context.schema.pick(fields);
return context;
}
});