-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockscout.js
277 lines (250 loc) · 10.5 KB
/
blockscout.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
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
//https://blockscout.com/poa/core/api
var request = require('request');
// poa/core = POA
// poa/sokol = POA testnet
// eth/mainnet = ETH mainnet
// eth/ropsten = ETH testnet
// etc/mainnet = ETC mainnet
// https://blockscout.com/poa/core/api?module=account&action=tokenbalance&contractaddress=0x11c4469d974f8af5ba9ec99f3c42c07c848c861c&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
// https://blockscout.com/poa/core/api?module=account&action=balance&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
// https://blockscout.com/poa/core/api?module=account&action=tokentx&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
// https://blockscout.com/poa/core/api?module=account&action=txlist&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
module.exports = class blockScout {
constructor(host, version, debug = false) {
this.host = host;
this.version = version + "/api?";
this.debug = debug;
this.versions = ["poa/core",
"poa/sokol",
"eth/mainnet",
"eth/ropsten",
"etc/mainnet"];
console.log("Host: " + this.host + "\n" + "Version: " + this.version + "\n" + "Debug: " + this.debug);
}
//HTTP GET REQUEST
GETit(action, callback){
request(this.host + this.version + action, function (error, response, body) {
if(error){
return callback({success:false, error:error.message})
}else{
if(this.debug == true){
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
}
if(response.statusCode == 200){
return callback({success:true, data:JSON.parse(body)});
}else{
return callback({success:false, error:"Bad response code: " + response.statusCode.toString()});
}
}
});
}
//module = account
//balance
//https://blockscout.com/poa/core/api?module=account&action=balance&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
balance(address, callback){ //0x11c4469d974f8af5ba9ec99f3c42c07c848c861c 0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
this.GETit("module=account&action=balance&address=" + address, function(r){
if(r.success){
var bal = (parseInt(r.data["result"]) / 1000000000000000000).toString()
return callback({success:true, data:parseFloat(bal)});
}else{
return callback({success:false, error:r.error});
}
});
}
//balancemulti
//https://blockscout.com/poa/core/api?module=account&action=balancemulti&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692,0x0Fe18f369c7F34208922cAEBbd5d21E131E44692,0x11c4469d974f8af5ba9ec99f3c42c07c848c861c
balanceMulti(address, callback){
this.GETit("module=account&action=balancemulti&address=" + address, function(r){
if(r.success){
return callback({success:true, data:r.data["result"]});
}else{
return callback({success:false, error:r.error});
}
});
}
//txlist
//https://blockscout.com/poa/core/api?module=account&action=txlist&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
txList(address, callback){
this.GETit("module=account&action=txlist&address=" + address, function(r){
if(r.success){
console.log("txList");
console.log(r.data);
console.log("preparing to parse");
// var jsontest = JSON.parse(r.data);
// console.log(jsontest);
return callback({success:true, data:JSON.parse(r.data)});
}else{
console.log("oh no")
return callback({success:false, error:r.error});
}
});
}
//txlistinternal
//https://blockscout.com/poa/core/api?module=account&action=txlistinternal&txhash=0x759630ce113d098fccf211da4a7c1ce79ffe2c76b477eaf053351b3c356d1a00
txListInternal(txhash, callback){
this.GETit("module=account&action=txlistinternal&txhash=" + txhash, function(r){
if(r.success){
return callback({success:true, data:r.data["result"]});
}else{
return callback({success:false, error:r.error});
}
});
}
//tokentx
//https://blockscout.com/poa/core/api?module=account&action=tokentx&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
tokenTxList(address, toAddress, callback){ //0x11c4469d974f8af5ba9ec99f3c42c07c848c861c 0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
this.GETit("module=account&action=tokentx&address=" + address, function(r){
if(r.success){
//console.log("txList");
var x = 0;
const timeStamps = [];
for (var i in r.data.result) {
if (r.data.result[i].tokenSymbol == 'BUFF') {
//console.log(r.data.result[i].to);
//console.log(r.data.result[i].value);
if (r.data.result[i].to == toAddress) {
//console.log(r.data.result[i].value);
// console.log(r.data.result[i].timeStamp);
// timeStamps.push(r.data.result[i].timeStamp);
var dateTime = new Date(r.data.result[i].timeStamp*1000);
var dateTimeMST = new Date(r.data.result[i].timeStamp*1000).toLocaleString('en-US', {timeZone: 'America/Denver'})
// console.log(dateTimeMST);
timeStamps.push(dateTimeMST);
x += Number(r.data.result[i].value);
};
};
};
var y = x / (10 ** 18);
// console.log("X is " + x);
//console.log("Y is " + y);
// console.log(r.data.result[0]);
// console.log("preparing to parse");
// return callback({success:true, data:JSON.parse(r.data.result)});
return callback({success:true, data:y, timeStamps:timeStamps});
}else{
return callback({success:false, error:r.error});
}
});
}
//tokenbalance
//https://blockscout.com/poa/core/api?module=account&action=tokenbalance&contractaddress=0x11c4469d974f8af5ba9ec99f3c42c07c848c861c&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
tokenBalance(contractAddress, address, callback){ //0x11c4469d974f8af5ba9ec99f3c42c07c848c861c 0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
this.GETit("module=account&action=tokenbalance&contractaddress=" + contractAddress+ "&address=" + address, function(r){
if(r.success){
return callback({success:true, data:r.data["result"]});
}else{
return callback({success:false, error:r.error});
}
});
}
//tokenlist
//https://blockscout.com/poa/core/api?module=account&action=tokenlist&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
tokenList(address, callback){ //0x11c4469d974f8af5ba9ec99f3c42c07c848c861c 0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
this.GETit("module=account&action=tokenlist&address=" + address, function(r){
if(r.success){
return callback({success:true, data:r.data["result"]});
}else{
return callback({success:false, error:r.error});
}
});
}
//getminedblocks
//https://blockscout.com/poa/core/api?module=account&action=getminedblocks&address=0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
getMinedBlocks(address, callback){ //0x11c4469d974f8af5ba9ec99f3c42c07c848c861c 0x0Fe18f369c7F34208922cAEBbd5d21E131E44692
this.GETit("module=account&action=getminedblocks&address=" + address, function(r){
if(r.success){
if(r.data["message"] == "No blocks found"){
return callback({success:false, error:"No blocks found"});
}
return callback({success:true, data:r.data["result"]});
}else{
return callback({success:false, error:r.error});
}
});
}
}
//const bs = new blockScout("https://blockscout.com/", "poa/core"); //default, debug = false, can pass true
//example
/*
console.log(bs.versions)
console.log(bs.host)
console.log(bs.version)
*/
/*
bs.balance("0x0Fe18f369c7F34208922cAEBbd5d21E131E44692", function(r){
if(r.success){
console.log("Balance: " + r.data)
}else{
console.log(r.error);
}
});
*/
/*
bs.balanceMulti("0x0Fe18f369c7F34208922cAEBbd5d21E131E44692,0x11c4469d974f8af5ba9ec99f3c42c07c848c861c", function(r){
if(r.success){
console.log("Multi Balance: ")
console.log(JSON.stringify(r.data))
}else{
console.log(r.error);
}
});
*/
/*
bs.txList("0x0Fe18f369c7F34208922cAEBbd5d21E131E44692", function(r){
if(r.success){
console.log("Token Transfers: ")
console.log(JSON.stringify(r.data))
}else{
console.log(r.error);
}
});
*/
/*
bs.txListInternal("0x759630ce113d098fccf211da4a7c1ce79ffe2c76b477eaf053351b3c356d1a00", function(r){
if(r.success){
console.log("TX Internal: ")
console.log(JSON.stringify(r.data))
}else{
console.log(r.error);
}
});
*/
/*
bs.tokenTxList("0x0Fe18f369c7F34208922cAEBbd5d21E131E44692", function(r){
if(r.success){
console.log("Token Transfers: ")
console.log(JSON.stringify(r.data))
}else{
console.log(r.error);
}
});
*/
/*
bs.tokenBalance("0x11c4469d974f8af5ba9ec99f3c42c07c848c861c&address", "0x0Fe18f369c7F34208922cAEBbd5d21E131E44692", function(r){
if(r.success){
console.log("Token Balance: " + r.data)
}else{
console.log(r.error);
}
});
*/
/*
bs.tokenList("0x11c4469d974f8af5ba9ec99f3c42c07c848c861c", function(r){
if(r.success){
console.log("Token List: " + JSON.stringify(r.data))
}else{
console.log(r.error);
}
});
*/
/*
bs.getMinedBlocks("0x11c4469d974f8af5ba9ec99f3c42c07c848c861c", function(r){
if(r.success){
console.log("Mined Blocks: " + JSON.stringify(r.data))
}else{
console.log(r.error);
}
});
*/