app.js 4.47 KB
var express = require('express');
var app = express();
var fs = require('fs');
const fetch = require('node-fetch');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

var sortingArr = function(data, ratio) 
{
    return data.sort(function(first, second)
    {
        var a = first[ratio];
        var b = second[ratio];
        
        return a > b ? -1 : a < b ? 1 : 0;
    });
};



app.get('/pumping', function(req, res) {
    fs.readFile('coin_name.txt', 'utf8', function(err,data){
        coinNameArr = data.split(",");
        

        var volumePerWeekArr = new Array();
        function getWeekVolume()
        {
            var promise = new Promise(function(resolve, reject)
            {
                for (i = 0; i < coinNameArr.length; ++i)
                {
                    (function (i)
                    {
                        setTimeout(function () {
                            const url = 'https://api.upbit.com/v1/candles/weeks/?market='+coinNameArr[i]+'&count=1';
                            const options = {method: 'GET', headers: {Accept: 'application/json'}};
                            var coinJson = new Object();
                            fetch(url, options)
                            .then(res => res.json())
                            .then(json => {
                                coinJson.coin = coinNameArr[i];
                                coinJson.tradeVolumePerWeek = json[0].candle_acc_trade_volume;
                                volumePerWeekArr.push(coinJson);
                                if (volumePerWeekArr.length >= coinNameArr.length)
                                    resolve(volumePerWeekArr);
                            })
                            .catch(err => console.error('error:' + err));
                        }, i*300);    
                    })(i);
                }
            });
            return promise;
        }


        var volumePerThreeMinArr = new Array();
        function getThreeMinVolume()
        {
            var promise = new Promise(function(resolve, reject)
            {
                for (i = 0; i < coinNameArr.length; ++i)
                {
                    (function (i)
                    {
                        setTimeout(function () {
                            const url = 'https://api.upbit.com/v1/candles/minutes/3?market='+coinNameArr[i]+'&count=1';
                            const options = {method: 'GET', headers: {Accept: 'application/json'}};
                            var coinJson = new Object();
                            fetch(url, options)
                            .then(res => res.json())
                            .then(json => {
                                coinJson.coin = coinNameArr[i];
                                coinJson.tradeVolumePerThreeMin = json[0].candle_acc_trade_volume;
                                volumePerThreeMinArr.push(coinJson);
                                if (volumePerThreeMinArr.length >= coinNameArr.length)
                                    resolve(volumePerThreeMinArr);
                            })
                            .catch(err => console.error('error:' + err));
                        }, i*300);    
                    })(i);
                }

            });
            return promise;
        }


        var pumpingArr = new Array();
        function getResultArr(volArr, minArr)
        {   
            var promise = new Promise(function(resolve, reject)
            {
                for (var i = 0; i < coinNameArr.length; i++)
                {
                    var coinJson = new Object();
                    coinJson.name = coinNameArr[i];
                    coinJson.pumpingRatio = minArr[i].tradeVolumePerThreeMin / volArr[i].tradeVolumePerWeek * 1000000
                    pumpingArr.push(coinJson);
                    if (pumpingArr.length >= coinNameArr.length)
                        resolve(pumpingArr);
                }    
            })
        return promise;
        }

          

        // get
        getWeekVolume().then(function(volArr){
            getThreeMinVolume().then(function(minArr){
                getResultArr(volArr, minArr).then(function(result){
                    
                    //console.log(sortingArr(result, "pumpingRatio"));
                    res.send(sortingArr(result, "pumpingRatio"));

                })
            })
        })



    })
})
var server = app.listen(8082);
console.log("Server Created..");