app.js 2.22 KB
const express = require('express');
const app = express();



const fetch = require('node-fetch');
const fs = require('fs');

const api_key = fs.readFileSync('apikey', 'ascii')
console.log('apikey:',api_key)
//const url = 'https://api.whale-alert.io/v1/status?api_key=' + api_key
const options = {method: 'GET', headers: {Accept: 'application/json'}};

var cursor = ''
url2 = 'https://api.whale-alert.io/v1/transactions?api_key=' + api_key + '&limit=1&cursor=' + cursor;

//Initialize timestamp

const fs2 = require('fs')
const fs3 = require('fs')
var TimeNow = parseInt((new Date()).getTime() / 1000)
console.log(TimeNow)
console.log('파일쓰기')
fetch(url2, options)
.then(res => res.json())
.then(json => {
    console.log(json)
    fs2.writeFileSync('timestamp', json.cursor, 'ascii')
})
// fetch(url, options)
//     .then(res => res.json())
//     .then(json => console.log(json))
//     .catch(err => console.error('error:' + 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 = '';

    for(var i = 0; i < data.count; i++){
        str = '#' + (i+1)+' ChainName: '+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('/', function (req, res) {
    cursor = fs3.readFileSync('timestamp', 'ascii')
    console.log('cursor', url2)
    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{
            fs2.writeFileSync('timestamp', json.cursor, 'ascii')
            res.send(chk_param(json, req.query.limit, data_collector))
        }
    })
    .catch(err => console.error('error:' + err))
})

var server = app.listen(23023)