forked from trufflesuite/truffle-artifactor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
459 lines (380 loc) · 13.1 KB
/
index.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
var Promise = require("bluebird");
var pkg = require("./package.json");
// fix this issue:
// https://github.com/ethereum/web3.js/issues/337
// XXX this should really be Pudding.toUTF8
Pudding.toAscii = function(hex) {
var str = '',
i = 0,
l = hex.length;
if (hex.substring(0, 2) === '0x') {
i = 2;
}
for (; i < l; i+=2) {
var code = parseInt(hex.substr(i, 2), 16);
if (code === 0) continue; // this is added
str += String.fromCharCode(code);
}
return str;
}
//
// This parses a multi-return values into a struct, giving
// use pseudo-capability of returning structs from solidity,
// as well as making it consistant with log events. Note the
// ABI produced by solidity supports this.
//
// it also properly converts bytes32 to usable string
//
Pudding.newReturn = function(rv, fnabi) {
newrv = [];
if (rv.constructor === Array) {
fnabi.outputs.forEach(function(x,i) {
if (x.type == 'bytes32') {
rv[i] = Pudding.toAscii(rv[i]);
}
newrv[x.name] = rv[i];
})
return newrv;
} else {
if (fnabi.outputs[0].type == 'bytes32') {
rv = Pudding.toAscii(rv);
}
return rv;
}
}
var SolidityEvent = require("web3/lib/web3/event.js");
Pudding.logParser = function (logs, abi) {
// pattern similar to lib/web3/contract.js: addEventsToContract()
var decoders = abi.filter(function (json) {
return json.type === 'event';
}).map(function(json) {
// note first and third params only required only by enocde and execute;
// so don't call those!
return new SolidityEvent(null, json, null);
});
return logs.map(function (log) {
var decoder = decoders.find(function(decoder) {
return (decoder.signature() == log.topics[0].replace("0x",""));
})
if (decoder) {
return decoder.decode(log);
} else {
return log;
}
}).map(function (log) {
abis = abi.find(function(json) {
return (json.type === 'event' && log.event === json.name);
});
if (abis && abis.inputs) {
abis.inputs.forEach(function (param, i) {
if (param.type == 'bytes32') {
log.args[param.name] = Pudding.toAscii(log.args[param.name]);
}
})
}
return log;
})
}
Pudding.newTx = function(tx, tx_info, logFunc, accept, reject, abi, eth) {
eth.getTransactionReceipt(tx, function(e, tx_receipt) {
if (e != null || tx_receipt == null) {
reject(new Error(e));
}
// make sure fields include all the fields from web3 events plus anything useful from tx_info
if (tx_receipt.logs.length > 0) {
var logs = Pudding.logParser(tx_receipt.logs, abi);
tx_receipt.type = tx_receipt.logs[0].type;
tx_receipt.logs = logs;
}
tx_receipt.gasSent = tx_info.gas;
logFunc(tx_receipt);
if (tx_receipt.gasUsed >= tx_info.gas) {
logFunc(tx_receipt);
reject(new Error("Transaction " + tx + " used up all gas " + tx_receipt.gasUsed));
} else {
accept(tx_receipt);
}
})
}
function Pudding(contract) {
if (!this.abi) {
throw new Error("Contract ABI not set. Please inherit Pudding and set static .abi variable with contract abi.");
}
this.contract = contract;
this.address = contract.address;
for (var i = 0; i < this.abi.length; i++) {
var fn = this.abi[i];
if (fn.type == "function") {
if (fn.constant == true) {
this[fn.name] = this.constructor.promisifyFunction(this.contract[fn.name],
Pudding.class_defaults.return_hook,
fn);
} else {
this[fn.name] = this.constructor.synchronizeFunction(this.contract[fn.name], this.contract);
}
this[fn.name].call = this.constructor.promisifyFunction(this.contract[fn.name].call,
Pudding.class_defaults.return_hook,
fn);
this[fn.name].sendTransaction = this.constructor.promisifyFunction(this.contract[fn.name].sendTransaction);
this[fn.name].request = this.contract[fn.name].request;
this[fn.name].estimateGas = this.constructor.promisifyFunction(this.contract[fn.name].estimateGas);
}
if (fn.type == "event") {
this[fn.name] = this.contract[fn.name];
}
}
this.allEvents = this.contract.allEvents;
};
Pudding.new = function() {
var args = Array.prototype.slice.call(arguments);
var web3 = this.web3;
if (web3 == undefined) {
console.log(this);
console.log(new Error().stack);
throw(Error("undefined web3, figure out the callstack issue"));
}
//console.log(this);
if (!this.prototype.binary) {
throw new Error("Contract binary not set. Please override Pudding and set .binary before calling new()");
}
var self = this;
return new Promise(function(accept, reject) {
var contract_class = web3.eth.contract(self.prototype.abi);
var tx_params = {};
var last_arg = args[args.length - 1];
// It's only tx_params if it's an object and not a BigNumber.
if (Pudding.is_object(last_arg) && last_arg instanceof Pudding.BigNumber == false) {
tx_params = args.pop();
}
tx_params = Pudding.merge(Pudding.class_defaults, self.prototype.class_defaults, tx_params);
if (tx_params.data == null) {
tx_params.data = self.prototype.binary;
}
// web3 0.9.0 and above calls new twice this callback twice.
// Why, I have no idea...
var intermediary = function(err, web3_instance) {
if (err != null) {
reject(err);
return;
}
if (err == null && web3_instance != null && web3_instance.address != null) {
accept(new self(web3_instance));
}
};
args.push(tx_params, intermediary);
contract_class.new.apply(contract_class, args);
});
};
Pudding.at = function(address) {
var web3 = this.web3;
var contract_class = web3.eth.contract(this.prototype.abi);
var contract = contract_class.at(address);
return new this(contract);
};
Pudding.deployed = function() {
if (!this.prototype.address) {
throw new Error("Contract address not set - deployed() relies on the contract class having a static 'address' value; please set that before using deployed().");
}
return this.at(this.prototype.address);
};
Pudding.extend = function() {
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < arguments.length; i++) {
var object = arguments[i];
var keys = Object.keys(object);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var value = object[key];
this.prototype[key] = value;
}
}
};
Pudding.whisk = function(data, constructor) {
/*
if (this.web3 == null) {
throw new Error("Please set data.web3 or call Pudding.setWeb3() before calling Pudding.whisk().");
}
*/
var Contract = constructor;
if (constructor == null) {
Contract = function(contract) {
Pudding.apply(this, arguments);
};
}
Contract.prototype = Object.create(Pudding.prototype);
Contract.abi = Contract.prototype.abi = data.abi;
Contract.binary = Contract.prototype.binary = data.binary;
Contract.unlinked_binary = Contract.prototype.unlinked_binary = data.unlinked_binary || data.binary;
Contract.prototype.class_defaults = data.defaults || {};
Contract.address = Contract.prototype.address = data.address;
Contract.deployed_address = Contract.prototype.deployed_address = data.address; // deprecated
Contract.generated_with = Contract.prototype.generated_with = data.generated_with;
Contract.contract_name = Contract.prototype.contract_name = data.contract_name;
Contract.web3 = data.web3 || Pudding.getWeb3();
// Post-whisked loads just return the contract.
Contract.load = function() {
return Contract;
};
Contract.new = Pudding.new.bind(Contract);
Contract.at = Pudding.at.bind(Contract);
Contract.deployed = Pudding.deployed.bind(Contract);
Contract.extend = Pudding.extend.bind(Contract);
return Contract;
}
Pudding.load = function(factories, scope) {
if (scope == null) {
scope = {};
}
if (!(factories instanceof Array)) {
factories = [factories];
}
var names = [];
for (var i = 0; i < factories.length; i++) {
var factory = factories[i];
var result = factory.load(this);
names.push(result.contract_name);
scope[result.contract_name] = result;
}
return names;
};
Pudding.defaults = function(class_defaults) {
if (this.class_defaults == null) {
this.class_defaults = {};
}
if (class_defaults == null) {
class_defaults = {};
}
var keys = Object.keys(class_defaults);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = class_defaults[key];
this.class_defaults[key] = value;
}
return this.class_defaults;
}
Pudding.setWeb3 = function(web3) {
if (web3.toBigNumber == null) {
throw new Error("Pudding.setWeb3() must be passed an instance of Web3 and not Web3 itself.");
}
this.BigNumber = web3.toBigNumber(0).constructor;
this.web3 = web3;
};
Pudding.getWeb3 = function() {
return this.web3 || Pudding.web3; // Note: Pudding often times === this;
}
Pudding.is_object = function(val) {
return typeof val == "object" && !(val instanceof Array);
};
Pudding.merge = function() {
var merged = {};
var args = Array.prototype.slice.call(arguments);
for (var i = 0; i < args.length; i++) {
var object = args[i];
var keys = Object.keys(object);
for (var j = 0; j < keys.length; j++) {
var key = keys[j];
var value = object[key];
merged[key] = value;
}
}
return merged;
};
Pudding.promisifyFunction = function(fn, hook, fnabi) {
var self = this;
return function() {
var instance = this;
var args = Array.prototype.slice.call(arguments);
var tx_params = {};
var last_arg = args[args.length - 1];
// It's only tx_params if it's an object and not a BigNumber.
if (Pudding.is_object(last_arg) && last_arg instanceof Pudding.BigNumber == false) {
tx_params = args.pop();
}
tx_params = Pudding.merge(Pudding.class_defaults, self.class_defaults, tx_params);
return new Promise(function(accept, reject) {
var callback = function(error, result) {
if (error != null) {
reject(error);
} else {
if (hook) {
result = hook(result, fnabi);
}
accept(result);
}
};
args.push(tx_params, callback);
fn.apply(instance.contract, args);
});
};
};
Pudding.synchronizeFunction = function(fn, contract) {
var self = this;
var thisContract = contract;
var abi = contract.abi;
return function() {
var args = Array.prototype.slice.call(arguments);
var tx_params = {};
var last_arg = args[args.length - 1];
var tx_hook;
var tx_log;
var tx_timeout = 0;
// It's only tx_params if it's an object and not a BigNumber.
if (Pudding.is_object(last_arg) && last_arg instanceof Pudding.BigNumber == false) {
tx_params = args.pop();
}
tx_params = Pudding.merge(Pudding.class_defaults, self.class_defaults, tx_params);
tx_hook = Pudding.class_defaults.tx_hook;
if (typeof tx_hook === 'undefined' || tx_hook === null || tx_hook === {}) {
tx_hook = function(tx, tx_info, logFunc, accept) {logFunc(tx_info); accept(tx)};
}
tx_log = Pudding.class_defaults.tx_log;
if (typeof tx_log === 'undefined' || tx_log === null || tx_log === {}) {
tx_log = function(tx){};
}
tx_timeout = Pudding.class_defaults.tx_timeout;
if (typeof tx_timeout === 'undefined' || tx_timeout === null || tx_timeout === 0) {
tx_timeout = 240000; // original default with this library
}
if(tx_params.abi) {
// XXX need to handle multiple ABIs in the future, via an {address,abi} pair
abi = tx_params.abi;
}
return new Promise(function(accept, reject) {
var callback = function(error, tx) {
var interval = null;
var max_attempts = tx_timeout / 1000;
var attempts = 0;
if (error != null) {
reject(error);
return;
}
var interval;
var make_attempt = function() {
//console.log "Interval check //{attempts}..."
thisContract._eth.getTransaction(tx, function(e, tx_info) {
// If there's an error ignore it. Sometimes tx_info is null, don't know why
if (e != null || tx_info == null) {
return;
}
if (tx_info.blockHash != null) {
clearInterval(interval);
tx_hook(tx, tx_info, tx_log, accept, reject, abi, thisContract._eth);
}
if (attempts >= max_attempts) {
clearInterval(interval);
reject(new Error("Transaction " + tx + " wasn't processed in " + attempts + " attempts!"));
}
attempts += 1;
});
};
interval = setInterval(make_attempt, 1000);
make_attempt();
};
args.push(tx_params, callback);
fn.apply(self, args);
});
};
};
Pudding.class_defaults = {};
Pudding.version = pkg.version;
module.exports = Pudding;