-
Notifications
You must be signed in to change notification settings - Fork 14
/
parity.js
100 lines (91 loc) · 2.43 KB
/
parity.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
const async = require('async');
const fs = require('fs');
const BigNumber = require('bignumber.js');
class Parity {
constructor(web3) {
this.requestId = 1;
this.web3 = web3;
}
scanBlock(number, cb) {
this.web3.eth.getBlock(number, (err, block) => {
if (err) return cb(err);
if (!block) return cb(number, []);
let result = [];
async.each(block.transactions, (txHash, cb) => {
this.scanTransaction(txHash, (err, transactions) => {
if (err) return cb(err);
result = result.concat(transactions);
return cb();
});
}, (err) => cb(err, result));
});
}
scanTransaction(hash, cb) {
async.waterfall([
(cb) => {
this.web3.eth.getTransactionReceipt(hash, (err, result) => cb(err, result));
},
(receipt, cb) => {
this._replayTransaction(hash, (err, result) => cb(err, receipt, result));
},
(receipt, trace, cb) => {
if (trace.trace.some((row) => row.error)) {
return cb(null, []);
}
const txs = [];
trace.trace.forEach((callObject) => {
if (parseInt(callObject.action.value, 16) > 0) {
txs.push({
blockNumber: receipt.blockNumber,
blockHash: receipt.blockHash,
to: this._getAddress(callObject.action.to),
from: this._getAddress(callObject.action.from),
value: new BigNumber(callObject.action.value).toString(),
hash: hash,
type: callObject.type,
isSuicide: callObject.type == 'SELFDESTRUCT',
});
}
});
cb(null, txs);
},
], (err, transactions) => cb(err, transactions));
}
_replayTransaction(hash, cb) {
this.requestId++;
return this.web3.currentProvider.sendAsync({
method: "trace_replayTransaction",
params: [hash, ["trace"]],
jsonrpc: "2.0",
id: this.requestId.toString(),
}, (err, result) => {
if (err)
return cb(err);
if (result.error)
return cb(result.error.message);
return cb(null, result.result);
});
}
_getAddress(value) {
if (!value)
return value;
if (value.match(/^0x[a-zA-Z0-9]{40}/))
return value;
let address = this.web3.toHex(value);
while(address.length < 42)
address = address.replace(/^0x/, '0x0');
return address;
}
}
module.exports = Parity;
/**
* @typedef {object} etherscannerObj
* @property {String} hash
* @property {String} from
* @property {String} to
* @property {Number} value
* @property {Number} blockNumber
* @property {String} blockHash
* @property {Boolean} isInternal
* @property {String} type
*/