Skip to content

Commit

Permalink
Merge pull request #378 from tiagosiebler/multisymtickers
Browse files Browse the repository at this point in the history
Add support for multi-symbol ticker param, with better return type signatures
  • Loading branch information
tiagosiebler authored Dec 7, 2023
2 parents 40f2463 + e88973b commit 67be225
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ jobs:
docker:
- image: cimg/node:15.1
# resource_class: tiagosiebler/localcirunner
# The resource_class feature allows configuring CPU and RAM resources for each job. Different resource classes are available for different executors. https://circleci.com/docs/2.0/configuration-reference/#resourceclass
resource_class: large
steps:
- checkout
- restore_cache:
Expand Down
22 changes: 20 additions & 2 deletions examples/rest-spot-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@ const client = new MainClient({

(async () => {
try {
console.log('getAvgPrice: ', await client.getAvgPrice({ symbol: 'BTCUSDT' }));
console.log('getExchangeInfo: ', JSON.stringify(await client.getExchangeInfo(), null, 2));
// console.log(
// 'getAvgPrice: ',
// await client.getAvgPrice({ symbol: 'BTCUSDT' }),
// );
// console.log(
// 'getExchangeInfo: ',
// JSON.stringify(await client.getExchangeInfo(), null, 2),
// );

const oneTicker = await client.get24hrChangeStatististics({
symbol: 'BTCUSDT',
});
console.log('getTickers', oneTicker);

const manyTickers = await client.get24hrChangeStatististics({
symbols: ['BTCUSDT', 'ETHUSDT'],
});
console.log('getTickers many', manyTickers);
const allTickers = await client.get24hrChangeStatististics();
console.log('getTickers all', allTickers);
} catch (e) {
console.error('request failed: ', e);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "binance",
"version": "2.8.13",
"version": "2.8.14",
"description": "Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & end-to-end tests.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
28 changes: 21 additions & 7 deletions src/main-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SymbolPrice,
RowsWithTotal,
CoinStartEndLimit,
SymbolArrayParam,
} from './types/shared';

import {
Expand Down Expand Up @@ -753,15 +754,28 @@ export class MainClient extends BaseRestClient {
}

get24hrChangeStatististics(
params?: Partial<BasicSymbolParam>,
params: BasicSymbolParam,
): Promise<DailyChangeStatistic>;

get24hrChangeStatististics(
params?: SymbolArrayParam,
): Promise<DailyChangeStatistic[]>;

get24hrChangeStatististics(
params?: Partial<BasicSymbolParam> | Partial<SymbolArrayParam>,
): Promise<DailyChangeStatistic | DailyChangeStatistic[]> {
if (!params?.symbol) {
return this.get('api/v3/ticker/24hr') as Promise<DailyChangeStatistic[]>;
if (params && typeof params['symbol'] === 'string') {
return this.get('api/v3/ticker/24hr', params);
}
return this.get(
'api/v3/ticker/24hr',
params,
) as Promise<DailyChangeStatistic>;

if (params && params['symbols'] && Array.isArray(params['symbols'])) {
const symbols = (params as SymbolArrayParam).symbols;
const symbolsQueryParam = JSON.stringify(symbols);

return this.get('api/v3/ticker/24hr?symbols=' + symbolsQueryParam);
}

return this.get('api/v3/ticker/24hr');
}

getSymbolPriceTicker(
Expand Down
4 changes: 4 additions & 0 deletions src/types/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export interface BasicSymbolParam {
isIsolated?: StringBoolean;
}

export interface SymbolArrayParam {
symbols: string[];
}

export interface BasicAssetPaginatedParams {
asset?: string;
startTime?: number;
Expand Down

0 comments on commit 67be225

Please sign in to comment.