| 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486 |
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
12×
1×
4×
1×
6×
1×
8×
1×
6×
1×
318×
318×
317×
317×
10461×
10461×
317×
317×
317×
317×
317×
317×
317×
317×
1×
1×
6×
1×
1×
1×
317×
317×
317×
317×
317×
317×
317×
317×
317×
317×
317×
317×
12×
317×
317×
317×
317×
317×
317×
317×
317×
317×
317×
317×
8×
8×
317×
317×
317×
317×
317×
317×
317×
317×
317×
317×
4×
1×
3×
4×
4×
2×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
317×
1×
| /*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file index.js
* @author Fabian Vogelsteller <fabian@ethereum.org>
* @date 2017
*/
"use strict";
var _ = require('underscore');
var core = require('web3-core');
var helpers = require('web3-core-helpers');
var Subscriptions = require('web3-core-subscriptions').subscriptions;
var Method = require('web3-core-method');
var utils = require('web3-utils');
var Net = require('web3-net');
var Personal = require('web3-eth-personal');
var Contract = require('web3-eth-contract');
var Iban = require('web3-eth-iban');
var Accounts = require('web3-eth-accounts');
var abi = require('web3-eth-abi');
var getNetworkType = require('./getNetworkType.js');
var formatters = helpers.formatters;
var blockCall = function (args) {
return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber";
};
var transactionFromBlockCall = function (args) {
return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex';
};
var uncleCall = function (args) {
return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex';
};
var getBlockTransactionCountCall = function (args) {
return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber';
};
var uncleCountCall = function (args) {
return (_.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber';
};
var Eth = function Eth() {
var _this = this;
// sets _requestmanager
core.packageInit(this, arguments);
this.clearSubscriptions = _this._requestManager.clearSubscriptions;
methods().forEach(function(method) {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager, _this); // second param means is Eth (necessary for promiEvent)
});
// add net
this.net = new Net(this.currentProvider);
// add guess chain
this.net.getNetworkType = getNetworkType.bind(this);
// add accounts
this.accounts = new Accounts(this);
// add personal
this.personal = new Personal(this.currentProvider);
// add contract
this.Contract = Contract;
this.Contract.prototype._eth = this;
// add IBAN
this.Iban = Iban;
// add ABI
this.abi = abi;
};
core.addProviders(Eth);
Object.defineProperty(Eth.prototype, 'defaultBlock', {
get: function () {
return helpers.config.defaultBlock;
},
set: function (val) {
helpers.config.defaultBlock = val;
return val;
},
enumerable: true
});
Object.defineProperty(Eth.prototype, 'defaultAccount', {
get: function () {
return helpers.config.defaultAccount;
},
set: function (val) {
helpers.config.defaultAccount = val;
return val;
},
enumerable: true
});
var methods = function () {
var getVersion = new Method({
name: 'getProtocolVersion',
call: 'eth_protocolVersion',
params: 0
});
var getCoinbase = new Method({
name: 'getCoinbase',
call: 'eth_coinbase',
params: 0
});
var getMining = new Method({
name: 'isMining',
call: 'eth_mining',
params: 0
});
var getHashrate = new Method({
name: 'getHashrate',
call: 'eth_hashrate',
params: 0,
outputFormatter: utils.hexToNumber
});
var isSyncing = new Method({
name: 'isSyncing',
call: 'eth_syncing',
params: 0,
outputFormatter: formatters.outputSyncingFormatter
});
var getGasPrice = new Method({
name: 'getGasPrice',
call: 'eth_gasPrice',
params: 0,
outputFormatter: formatters.outputBigNumberFormatter
});
var getAccounts = new Method({
name: 'getAccounts',
call: 'eth_accounts',
params: 0,
outputFormatter: utils.toChecksumAddress
});
var getBlockNumber = new Method({
name: 'getBlockNumber',
call: 'eth_blockNumber',
params: 0,
outputFormatter: utils.hexToNumber
});
var getBalance = new Method({
name: 'getBalance',
call: 'eth_getBalance',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: formatters.outputBigNumberFormatter
});
var getStorageAt = new Method({
name: 'getStorageAt',
call: 'eth_getStorageAt',
params: 3,
inputFormatter: [formatters.inputAddressFormatter, utils.numberToHex, formatters.inputDefaultBlockNumberFormatter]
});
var getCode = new Method({
name: 'getCode',
call: 'eth_getCode',
params: 2,
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
});
var getBlock = new Method({
name: 'getBlock',
call: blockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }],
outputFormatter: formatters.outputBlockFormatter
});
var getUncle = new Method({
name: 'getUncle',
call: uncleCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatters.outputBlockFormatter,
});
var getBlockTransactionCount = new Method({
name: 'getBlockTransactionCount',
call: getBlockTransactionCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
});
var getBlockUncleCount = new Method({
name: 'getBlockUncleCount',
call: uncleCountCall,
params: 1,
inputFormatter: [formatters.inputBlockNumberFormatter],
outputFormatter: utils.hexToNumber
});
var getTransaction = new Method({
name: 'getTransaction',
call: 'eth_getTransactionByHash',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionFormatter
});
var getTransactionFromBlock = new Method({
name: 'getTransactionFromBlock',
call: transactionFromBlockCall,
params: 2,
inputFormatter: [formatters.inputBlockNumberFormatter, utils.numberToHex],
outputFormatter: formatters.outputTransactionFormatter
});
var getTransactionReceipt = new Method({
name: 'getTransactionReceipt',
call: 'eth_getTransactionReceipt',
params: 1,
inputFormatter: [null],
outputFormatter: formatters.outputTransactionReceiptFormatter
});
var getTransactionCount = new Method({
name: 'getTransactionCount',
call: 'eth_getTransactionCount',
params: 2,
inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter],
outputFormatter: utils.hexToNumber
});
var sendSignedTransaction = new Method({
name: 'sendSignedTransaction',
call: 'eth_sendRawTransaction',
params: 1,
inputFormatter: [null]
});
var signTransaction = new Method({
name: 'signTransaction',
call: 'eth_signTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
var sendTransaction = new Method({
name: 'sendTransaction',
call: 'eth_sendTransaction',
params: 1,
inputFormatter: [formatters.inputTransactionFormatter]
});
var sign = new Method({
name: 'sign',
call: 'eth_sign',
params: 2,
inputFormatter: [formatters.inputSignFormatter, formatters.inputAddressFormatter],
transformPayload: function (payload) {
payload.params.reverse();
return payload;
}
});
var call = new Method({
name: 'call',
call: 'eth_call',
params: 2,
inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter]
});
var estimateGas = new Method({
name: 'estimateGas',
call: 'eth_estimateGas',
params: 1,
inputFormatter: [formatters.inputCallFormatter],
outputFormatter: utils.hexToNumber
});
var getCompilers = new Method({
name: 'getCompilers',
call: 'eth_getCompilers',
params: 0
});
var compileSolidity = new Method({
name: 'compile.solidity',
call: 'eth_compileSolidity',
params: 1
});
var compileLLL = new Method({
name: 'compile.lll',
call: 'eth_compileLLL',
params: 1
});
var compileSerpent = new Method({
name: 'compile.serpent',
call: 'eth_compileSerpent',
params: 1
});
var submitWork = new Method({
name: 'submitWork',
call: 'eth_submitWork',
params: 3
});
var getWork = new Method({
name: 'getWork',
call: 'eth_getWork',
params: 0
});
var getPastLogs = new Method({
name: 'getPastLogs',
call: 'eth_getLogs',
params: 1,
inputFormatter: [formatters.inputLogFormatter],
outputFormatter: formatters.outputLogFormatter
});
// subscriptions
var subscribe = new Subscriptions({
name: 'subscribe',
type: 'eth',
subscriptions: {
'newBlockHeaders': {
// TODO change name on RPC side?
subscriptionName: 'newHeads', // replace subscription with this name
params: 0,
outputFormatter: formatters.outputBlockFormatter
},
'pendingTransactions': {
params: 0
// outputFormatter: formatters.outputTransactionFormatter // returns only hash???
},
'logs': {
params: 1,
inputFormatter: [formatters.inputLogFormatter],
outputFormatter: formatters.outputLogFormatter,
// DUBLICATE, also in web3-eth-contract
subscriptionHandler: function (output) {
if(output.removed) {
this.emit('changed', output);
} else {
this.emit('data', output);
}
Eif (_.isFunction(this.callback)) {
this.callback(null, output, this);
}
}
},
'syncing': {
params: 0,
outputFormatter: formatters.outputSyncingFormatter,
subscriptionHandler: function (output) {
var _this = this;
// fire TRUE at start
if(this._isSyncing !== true) {
this._isSyncing = true;
this.emit('changed', _this._isSyncing);
Eif (_.isFunction(this.callback)) {
this.callback(null, _this._isSyncing, this);
}
setTimeout(function () {
_this.emit('data', output);
Eif (_.isFunction(_this.callback)) {
_this.callback(null, output, _this);
}
}, 0);
// fire sync status
} else {
this.emit('data', output);
Eif (_.isFunction(_this.callback)) {
this.callback(null, output, this);
}
// wait for some time before fireing the FALSE
clearTimeout(this._isSyncingTimeout);
this._isSyncingTimeout = setTimeout(function () {
Eif(output.currentBlock > output.highestBlock - 200) {
_this._isSyncing = false;
_this.emit('changed', _this._isSyncing);
Eif (_.isFunction(_this.callback)) {
_this.callback(null, _this._isSyncing, _this);
}
}
}, 500);
}
}
}
}
});
return [
getVersion,
getCoinbase,
getMining,
getHashrate,
isSyncing,
getGasPrice,
getAccounts,
getBlockNumber,
getBalance,
getStorageAt,
getCode,
getBlock,
getUncle,
getCompilers,
getBlockTransactionCount,
getBlockUncleCount,
getTransaction,
getTransactionFromBlock,
getTransactionReceipt,
getTransactionCount,
call,
estimateGas,
sendSignedTransaction,
signTransaction,
sendTransaction,
sign,
compileSolidity,
compileLLL,
compileSerpent,
submitWork,
getWork,
getPastLogs,
subscribe
];
};
module.exports = Eth;
|