Skip to content

Commit

Permalink
Merge branch 'main' of github.com:jessy-bgl/swealy into dev/dca-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jessy-bgl committed Nov 11, 2022
2 parents e503a26 + cacce6b commit dd05590
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
IBinanceAuthHttpHeaders,
IBinanceAuthHttpParams,
IBinanceExchangeInfoResult,
IServerTimeResult,
} from './binance-api.types';

const BINANCE_API_BASE_URL = 'https://api.binance.com';
Expand All @@ -45,18 +46,18 @@ class BinanceApiRepository implements IExchangeApiRepository {
return { 'X-MBX-APIKEY': apiKey };
}

private getAuthRequestParams(
private async getAuthRequestParams(
apiSecret: string,
params?: any,
): IBinanceAuthHttpParams {
): Promise<IBinanceAuthHttpParams> {
let signature_payload = '';
if (params) {
for (const [key, value] of Object.entries(params)) {
if (!signature_payload) signature_payload = `${key}=${value}`;
else signature_payload += `&${key}=${value}`;
}
}
const timestamp = Date.now().toString();
const timestamp = await this.getServerTime();
signature_payload += params
? `&timestamp=${timestamp}`
: `timestamp=${timestamp}`;
Expand All @@ -67,6 +68,20 @@ class BinanceApiRepository implements IExchangeApiRepository {
return { timestamp, signature };
}

private async getServerTime(): Promise<string> {
try {
const endpoint = `/api/v3/time`;
const res = await this.httpService.get<IServerTimeResult>(
`${BINANCE_API_BASE_URL}${endpoint}`,
);
if (!res.data || !res.data.serverTime)
throw new Error('serverTime not found');
return res.data.serverTime.toString();
} catch (e) {
handleBinanceApiError(e);
}
}

async checkApiKeyValidity(exchange: Exchange): Promise<void> {
try {
const endpoint = '/sapi/v1/account/apiRestrictions';
Expand Down Expand Up @@ -94,7 +109,7 @@ class BinanceApiRepository implements IExchangeApiRepository {
newOrderRespType: BinanceOrderResponseTypesEnum.FULL,
};
const headers = this.getAuthRequestHeaders(dca.exchange.apiKey);
const { timestamp, signature } = this.getAuthRequestParams(
const { timestamp, signature } = await this.getAuthRequestParams(
dca.exchange.apiSecret,
requestParams,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,16 @@ interface IBinanceApiPlacerOrderResult {
// see more on https://binance-docs.github.io/apidocs/spot/en/#new-order-trade
}

interface IServerTimeResult {
serverTime: number;
}

export type {
IBinanceAuthHttpHeaders,
IBinanceAuthHttpParams,
IBinanceApiPlaceOrderParams,
IBinanceApiPlacerOrderResult,
IBinanceExchangeInfoResult,
IServerTimeResult,
};
export { BinanceOrderResponseTypesEnum };

0 comments on commit dd05590

Please sign in to comment.