Skip to content

Commit

Permalink
Replaced got with axios due to possible interferences with https-prox…
Browse files Browse the repository at this point in the history
…y-agent
  • Loading branch information
skcxck committed Aug 29, 2021
1 parent 56ebb48 commit 74b91e4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 325 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"devDependencies": {
"@types/chai": "^4",
"@types/chai-as-promised": "^7.1.4",
"@types/got": "^9.6.12",
"@types/mocha": "^9",
"@types/nock": "^11.1.0",
"@types/node": "^16.7.2",
Expand Down Expand Up @@ -73,8 +72,8 @@
]
},
"dependencies": {
"axios": "^0.21.1",
"crypto": "^1.0.1",
"got": "^11.8.2",
"qs": "^6.10.1"
}
}
36 changes: 25 additions & 11 deletions src/api/krakenAPI.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import * as crypto from 'crypto';
import { BinaryToTextEncoding } from 'crypto';
import qs from 'qs';
import got, { Got, Response } from 'got';
import { StatusResponse, LunchMoneyKrakenConnectionConfig, KrakenResponse } from '../types/kraken.js';
import { OptionsOfJSONResponseBody } from 'got/dist/source/types';
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';

export default class KrakenAPI {
readonly client: Got;
readonly client: AxiosInstance;
readonly connection: LunchMoneyKrakenConnectionConfig;

config = {
Expand All @@ -20,21 +19,24 @@ export default class KrakenAPI {
};

constructor(connection: LunchMoneyKrakenConnectionConfig) {
this.client = got.extend({
prefixUrl: this.config.url + this.config.version,
this.client = axios.create({
baseURL: this.config.url + this.config.version,
headers: {
// If the Content-Type header is not present, it will be set to application/x-www-form-urlencoded.
'User-Agent': 'LunchMoney Kraken Client',
'API-Key': connection.apiKey,
'Content-Type': 'application/x-www-form-urlencoded',
},
// always resolve as errors are custom handled
validateStatus: () => {
return true;
},
throwHttpErrors: false,
});

this.connection = connection;
}

// Query Funds permission
async getAccountBalances(): Promise<Response<KrakenResponse<Map<string, string>>>> {
async getAccountBalances(): Promise<AxiosResponse<KrakenResponse<Map<string, string>>>> {
const nonce = KrakenAPI.genNonce();
let payload: Record<string, string | number> = { nonce: nonce };

Expand All @@ -44,23 +46,23 @@ export default class KrakenAPI {

return this.client.post<KrakenResponse<Map<string, string>>>(
this.routes.PRIVATE_BALANCE,
KrakenAPI.formUrlEncoded(payload),
this.options(this.routes.PRIVATE_BALANCE, payload, this.connection.apiSecret, nonce),
);
}

async getSystemStatus(): Promise<Response<KrakenResponse<StatusResponse>>> {
async getSystemStatus(): Promise<AxiosResponse<KrakenResponse<StatusResponse>>> {
return this.client.get<KrakenResponse<StatusResponse>>(this.routes.PUBLIC_SYSTEM_STATUS, {
responseType: 'json',
});
}

options(path: string, payload: Record<string, unknown>, secret: string, nonce: number): OptionsOfJSONResponseBody {
options(path: string, payload: Record<string, unknown>, secret: string, nonce: number): AxiosRequestConfig {
return {
headers: {
'API-Sign': KrakenAPI.getMessageSignature('/' + this.config.version + '/' + path, payload, secret, nonce),
},
responseType: 'json',
form: payload,
};
}

Expand All @@ -77,4 +79,16 @@ export default class KrakenAPI {
static genNonce(): number {
return Date.now() * 1000;
}

static formUrlEncoded(payload: Record<string, string | number>): string {
return Object.keys(payload).reduce(function (p, c) {
let prefix = '&';

if (!p) {
prefix = '';
}

return p + prefix + `${c}=${encodeURIComponent(payload[c])}`;
}, '');
}
}
16 changes: 8 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
LunchMoneyKrakenConnectionContext,
StatusResponse,
} from './types/kraken';
import { Response } from 'got';
import { AxiosResponse } from 'axios';

export { LunchMoneyCryptoConnection } from './types/lunchMoney';

Expand Down Expand Up @@ -89,16 +89,16 @@ export const LunchMoneyKrakenConnection: LunchMoneyCryptoConnection<
* - EGeneral:Permission denied (Wrong or none permission granted)
*/
export function validateResponse(
response: Response<KrakenResponse<unknown>>,
response: AxiosResponse<KrakenResponse<unknown>>,
): KrakenValidation<unknown, KrakenWarnings> {
let warnings: KrakenWarnings | null = null;

if (response.statusCode !== 200 || response.body.result === null) {
throw new Error(`Received unknown response from Kraken: ${response.statusCode} ${JSON.stringify(response.body)}`);
if (response.status !== 200 || response.data.result === null) {
throw new Error(`Received unknown response from Kraken: ${response.status} ${JSON.stringify(response.data)}`);
}

if (response.body.error && response.body.error.length !== 0) {
const error = response.body.error
if (response.data.error && response.data.error.length !== 0) {
const error = response.data.error
.filter((e) => e.startsWith('E'))
.map((e) => e.substr(1))
.join(', ');
Expand All @@ -107,8 +107,8 @@ export function validateResponse(
throw new Error(`Error receiving response from Kraken: ${error}`);
}

warnings = response.body.error.filter((e) => e.startsWith('W')).map((e) => e.substr(1));
warnings = response.data.error.filter((e) => e.startsWith('W')).map((e) => e.substr(1));
}

return { result: response.body.result, warnings: warnings };
return { result: response.data.result, warnings: warnings };
}
Loading

0 comments on commit 74b91e4

Please sign in to comment.