-
Notifications
You must be signed in to change notification settings - Fork 250
/
filters.js
127 lines (115 loc) · 3.22 KB
/
filters.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
'use strict';
var etherUnits = require(__lib + "etherUnits.js")
var BigNumber = require('bignumber.js');
var RLP = require('rlp');
/*
Filter an array of TX
*/
function filterTX(txs, value) {
return txs.map(function(tx){
return [tx.hash, tx.blockNumber, tx.from, tx.to, etherUnits.toEther(new BigNumber(tx.value), 'ether'), tx.gas, tx.timestamp, tx.creates]
})
}
function filterTrace(txs, value) {
return txs.map(function(tx){
var t = tx;
if (t.type == "suicide") {
if (t.action.address)
t.from = t.action.address;
if (t.action.balance)
t.value = etherUnits.toEther( new BigNumber(t.action.balance), "wei");
if (t.action.refundAddress)
t.to = t.action.refundAddress;
} else {
if (t.action.to)
t.to = t.action.to;
t.from = t.action.from;
if (t.action.gas)
t.gas = new BigNumber(t.action.gas).toNumber();
if ((t.result) && (t.result.gasUsed))
t.gasUsed = new BigNumber(t.result.gasUsed).toNumber();
if ((t.result) && (t.result.address))
t.to = t.result.address;
t.value = etherUnits.toEther( new BigNumber(t.action.value), "wei");
}
return t;
})
}
function filterBlock(block, field, value) {
var tx = block.transactions.filter(function(obj) {
return obj[field]==value;
});
tx = tx[0];
if (typeof tx !== "undefined")
tx.timestamp = block.timestamp;
return tx;
}
/* make blocks human readable */
function filterBlocks(blocks) {
if (blocks.constructor !== Array) {
var b = blocks;
var ascii = hex2ascii(blocks.extraData);
b.extraDataHex = blocks.extraData;
b.extraData = ascii;
return b;
}
return blocks.map(function(block) {
var b = block;
var ascii = hex2ascii(block.extraData);
b.extraDataHex = block.extraData;
b.extraData = ascii;
return b;
})
}
/* stupid datatable format */
function datatableTX(txs) {
return txs.map(function(tx){
return [tx.hash, tx.blockNumber, tx.from, tx.to,
etherUnits.toEther(new BigNumber(tx.value), 'wei'), tx.gas, tx.timestamp]
})
}
function internalTX(txs) {
return txs.map(function(tx){
return [tx.transactionHash, tx.blockNumber, tx.action.from, tx.action.to,
etherUnits.toEther(new BigNumber(tx.action.value), 'wei'), tx.action.gas, tx.timestamp]
})
}
/* modified baToJSON() routine from rlp */
function baToString(ba) {
if (Buffer.isBuffer(ba)) {
return ba.toString('ascii');
} else if (ba instanceof Array) {
var array = [];
for (var i = 0; i < ba.length; i++) {
array.push(baToString(ba[i]));
}
return array.join('/');
} else {
return ba;
}
}
var hex2ascii = function (hexIn) {
var hex = hexIn.toString();
var str = '';
try {
var ba = RLP.decode(hex);
var test = ba[1].toString('ascii');
if (test == 'geth' || test == 'Parity') {
// FIXME
ba[0] = ba[0].toString('hex');
}
str = baToString(ba);
} catch(e) {
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
module.exports = {
filterBlock: filterBlock,
filterBlocks: filterBlocks,
filterTX: filterTX,
filterTrace: filterTrace,
datatableTX: datatableTX,
internalTX: internalTX
}