nothingisdead / npm-kraken-api

[DEPRECATED] NodeJS Client Library for the Kraken (kraken.com) API
Other
412 stars 188 forks source link

Running alongside node-binance-api gives ECONNREFUSED 127.0.0.1:443 #78

Open alimohyudin opened 2 years ago

alimohyudin commented 2 years ago

Everything works best until I added Binance Api support for my application.

Error I get:

RequestError: connect ECONNREFUSED 127.0.0.1:443
    at ClientRequest.<anonymous> (/server-bot/node_modules/got/dist/source/core/index.js:956:111)
    at Object.onceWrapper (node:events:510:26)
    at ClientRequest.emit (node:events:402:35)
    at ClientRequest.origin.emit (/server-bot/node_modules/@szmarczak/http-timer/dist/source/index.js:43:20)
    at TLSSocket.socketErrorListener (node:_http_client:447:9)
    at TLSSocket.emit (node:events:390:28)
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1146:16) {
  code: 'ECONNREFUSED',
  timings: {
    start: 1646464300686,
    socket: 1646464300686,
    lookup: 1646464300686,
    connect: undefined,
    secureConnect: undefined,
    upload: undefined,
    response: undefined,
    end: undefined,
    error: 1646464300687,
    abort: undefined,
    phases: {
      wait: 0,
      dns: 0,
      tcp: undefined,
      tls: undefined,
      request: undefined,
      firstByte: undefined,
      download: undefined,
      total: 1
    }
  }
}

If I remove node-binance-api from code it works just fine.

What should be the patch I need to do to make it work?

Thanks

BobWassermann commented 2 years ago

I'm running into the same issue. Seems to work locally, but break once I deploy.

alimohyudin commented 2 years ago

I'm running into the same issue. Seems to work locally, but break once I deploy.

The issue was with got module kraken.js is using. I solved this by replacing got with axios inside kraken.js of this module.

//const got    = require('got');
const axios = require('axios');

...

const rawRequest = async(url, headers, data, timeout) => {
    // Set custom User-Agent string
    headers['User-Agent'] = 'Kraken Javascript API Client';

    const options = { headers, timeout };

    Object.assign(options, {
        method: 'POST',
        body: qs.stringify(data),
    });

    // const { body } = await got(url, options);
    // const response = JSON.parse(body);
    let body = await axios.post(url, qs.stringify(data), options);

    let myresponse = JSON.stringify(body.data);
    // console.log("start");
    // console.log(myresponse);
    // console.log("end");

    const response = JSON.parse(myresponse);

    if (response.error && response.error.length) {
        const error = response.error
            .filter((e) => e.startsWith('E'))
            .map((e) => e.substr(1));

        if (!error.length) {
            throw new Error("Kraken API returned an unknown error");
        }

        throw new Error(error.join(', '));
    }

    return response;
};

You can ask me if you get any error, will be happy to help you. :)