-
Notifications
You must be signed in to change notification settings - Fork 14
/
geth.js
136 lines (125 loc) · 3.32 KB
/
geth.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
const logger = require('log4js').getLogger('etherscanner');
const async = require('async');
const fs = require('fs');
const BigNumber = require('bignumber.js');
class Geth {
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, tx) => {
if(err) {
logger.error(`Get transaction ${hash} error`, err);
return cb(err)
}
if(!tx) {
return cb('Transaction not found');
}
// if(!tx.blockNumber) {
// return cb('Unconfirmed transaction');
// }
return cb(null, tx);
});
},
(tx, cb) => {
if (tx.status === '0x0') {
return cb(null, []);
}
return this._getTransactionCalls(tx, (err, result) => cb(err, result));
}
], (err, transactions) => cb(err, transactions));
}
_getTransactionCalls(tx, cb) {
this._getTransactionsFromTrace(tx.transactionHash, tx.blockNumber, (err, result) => {
if (err) {
return cb(err);
}
return cb(null, this._getTransactionsFromCall(tx, result));
});
}
_getTransactionsFromCall(tx, callObject) {
let txs = [];
if (parseInt(callObject.value, 16) > 0) {
txs.push({
blockNumber: tx.blockNumber,
blockHash: tx.blockHash,
to: this._getAddress(callObject.to),
from: this._getAddress(callObject.from),
value: new BigNumber(callObject.value).toString(),
hash: tx.transactionHash,
type: callObject.type,
isSuicide: callObject.type == 'SELFDESTRUCT',
});
}
if (!callObject.calls) {
return txs;
}
callObject.calls.forEach(_callObject => {
txs = txs.concat(this._getTransactionsFromCall(tx, _callObject));
});
return txs;
}
_getTransactionsFromTrace(txHash, txBlockNumber, cb) {
async.waterfall([
(cb) => {
this.web3.eth.getBlockNumber((err, blockNumber) => {
return cb(err, blockNumber);
});
},
(blockNumber, cb) => {
this.requestId++;
return this.web3.currentProvider.sendAsync({
method: "debug_traceTransaction",
params: [txHash, { tracer: "callTracer", reexec: txBlockNumber ? (blockNumber - txBlockNumber + 20) : 200, timeout: "60s"}],
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);
});
},
], cb);
}
_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 = Geth;
/**
* @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
*/