forked from lljxx1/EOS-Voter-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 4
/
recorder.js
146 lines (115 loc) · 3.24 KB
/
recorder.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
var EosApi = require('eosjs-api');
var fs = require('fs');
var stream = require('stream');
var liner = new stream.Transform( { objectMode: true } )
liner._transform = function (chunk, encoding, done) {
var data = chunk.toString()
if (this._lastLineData) data = this._lastLineData + data
var lines = data.split('\n')
this._lastLineData = lines.splice(lines.length-1,1)[0]
lines.forEach(this.push.bind(this))
done()
}
liner._flush = function (done) {
if (this._lastLineData) this.push(this._lastLineData)
this._lastLineData = null
done()
}
function loadFromFile(){
FILE_PATH = "./block.json";
var source = fs.createReadStream(FILE_PATH)
source.pipe(liner)
liner.on('readable', function () {
var line
while (line = liner.read()) {
parseBlock(line);
}
})
}
var API_ENDPOINT = "http://127.0.0.1:8888";
eos = EosApi({
httpEndpoint: API_ENDPOINT,
logger: {
}
})
var current = parseInt(fs.readFileSync("./fetched", "utf-8"));
console.log("start fetch block from", current);
function fetchBlock(){
eos.getBlock(current, (error, result) => {
if(!error){
current++;
try{
parseBlock(result, true);
fs.writeFileSync("./fetched", current);
}catch(e){
console.log(e, JSON.stringify(result));
throw e;
}
}else{}
fetchBlock();
})
}
function listenBlock(){
fetchBlock();
}
function parseBlock(line, json){
if(!json){
try{
line = JSON.parse(line);
}catch(e){
}
}else{ }
line.transactions.forEach(function(transaction){
if(transaction.status != "hard_fail" && typeof transaction.trx != "string"){
transaction.trx.transaction.actions.forEach(function(action){
handleAction(action, line);
})
}
})
}
function handleAction(action, block){
var actionName = action.name;
try{
actionHanddler[actionName] && actionHanddler[actionName](action['data'], block);
}catch(e){
console.error("parseActionError", e);
}
}
function getAccountData(account){
return new Promise(function(resolve, reject){
Promise.all([
eos.getAccount({
account_name: account
}),
eos.getCurrencyBalance({
code: "eosio.token",
account: account
})
]).then(function(res){
res[0].blance = res[1];
resolve(res[0])
}, function(err){
reject(error);
console.log("error", err);
}).catch(function(err){
reject(error);
console.log("error", err);
})
})
}
var actionHanddler = {};
actionHanddler['voteproducer'] = function(data, block){
var voter = data.voter;
var producers = data.producers;
data.block_num = block.block_num;
data.timestamp = block.timestamp;
getAccountData(voter).then(function(voterData){
data.voterData = voterData;
fs.appendFileSync("./voter.log", JSON.stringify(data)+"\n");
}, function(err){
console.log(err);
}).catch(function(err){
console.log(err);
})
}
listenBlock();