app.js
2.07 KB
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
const express = require('express');
const app = express();
const fetch = require('node-fetch');
const fs_apikey = require('fs');
const api_key = fs_apikey.readFileSync('apikey', 'ascii')
var cursor = ''
url2 = 'https://api.whale-alert.io/v1/transactions?api_key=' + api_key + '&cursor=' + cursor;
const options = {method: 'GET', headers: {Accept: 'application/json'}};
//Initialize cursor
var TimeNow = parseInt((new Date()).getTime() / 1000)
var uts2time = new Date()
console.log(TimeNow)
fetch(url2, options)
.then(res => res.json())
.then(json => {
console.log(json)
cursor = json.cursor
})
.catch((err) => {
console.log('error : inital_fetch:'+err)
})
function chk_param(data, query_limit, callback){
//query_limit (non_int) => return 0;
//query_limit (int) => return query_limit;
query_limit = parseInt(query_limit)
if(isNaN(query_limit)){ return callback(data, 0)}
else {return callback(data, query_limit)}
}
function data_collector(data, limit){
var ret = '';
var str = '';
ret += '# of Transactions : ' + data.count + '\n'
for(var i = 0; i < data.count; i++){
uts2time.setTime(data.transactions[i].timestamp * 1000)
str = '#' + (i+1)+' \tTime: ' + uts2time.toLocaleString() + '\tChainName: '+data.transactions[i].blockchain+'\tAmount: '+data.transactions[i].amount+' USD'
if(data.transactions[i].amount > limit){
//console.log(str)
ret += '</br>' + str;
}
else{
//console.log(str)
}
}
return ret;
}
app.get('/whale', function (req, res) {
console.log('cursor', cursor)
fu = fetch(url2+cursor, options);
fu.then(res => res.json())
.then(json => {
//API usage limit
console.log(json)
if(json.result == 'error'){
res.send("Please try later")
}
else{
cursor = json.cursor
res.send(chk_param(json, req.query.limit, data_collector))
}
})
.catch(err => {
console.error('error: var:fu:' + err)
res.send('ERROR')
})
})
var server = app.listen(23023)