Skip to content

Commit

Permalink
Merge pull request #20 from binance/rc-v0.3.7
Browse files Browse the repository at this point in the history
Release v0.3.7
  • Loading branch information
alplabin authored Apr 4, 2024
2 parents 9be009e + 8d32673 commit e2edefa
Show file tree
Hide file tree
Showing 24 changed files with 585 additions and 467 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 0.3.7 - 2024-04-04

### Changed
- Update types
- Update dependencies
- Add logger class
- Fix issue #14: Add new `WebsocketStream` type
- Fix issue #16: Update parameters of `GET /api/v3/ticker/tradingDay`
- Fix issue #18: Multiple websocket streams subscription are now handled

## 0.3.6 - 2024-01-24

### Added
Expand Down
3 changes: 2 additions & 1 deletion examples/restful/market/tradingDayTicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const baseURL = process.env.BINANCE_BASE_URL || '';
const client = new Spot('', '', { baseURL: baseURL });

const options: RestMarketTypes.tradingDayTickerOptions = {
symbol: 'BNBUSDT',
type: 'MINI',
};

client.tradingDayTicker('BNBUSDT', options).then((res: RestMarketTypes.tradingDayTickerResponse | RestMarketTypes.tradingDayTickerResponse[]) => {
client.tradingDayTicker(options).then((res: RestMarketTypes.tradingDayTickerResponse | RestMarketTypes.tradingDayTickerResponse[]) => {
console.log(res);
}).catch(err => { console.log(err); });
13 changes: 13 additions & 0 deletions examples/websocketStream/multiSubscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { WebsocketStream } from '../../src/index';

const callbacks = {
open: () => console.debug('Connected to WebSocket server'),
close: () => console.debug('Disconnected from WebSocket server'),
message: (data: string) => console.info(data)
};

const websocketStreamClient = new WebsocketStream({ callbacks });

websocketStreamClient.subscribe(['bnbusdt@depth', 'btcusdt@depth']);

setTimeout(() => websocketStreamClient.disconnect(), 6000);
572 changes: 254 additions & 318 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@binance/connector-typescript",
"version": "0.3.6",
"version": "0.3.7",
"description": "This is a lightweight library that works as a connector to the Binance public API.",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
74 changes: 74 additions & 0 deletions src/helpers/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
export enum LogLevel {
NONE = '',
DEBUG = 'debug',
INFO = 'info',
WARN = 'warn',
ERROR = 'error',
}

export class Logger {
private static instance: Logger;
private minLogLevel: LogLevel = LogLevel.INFO;
private readonly levelsOrder: LogLevel[] = [
LogLevel.NONE,
LogLevel.DEBUG,
LogLevel.INFO,
LogLevel.WARN,
LogLevel.ERROR,
];

constructor() { }

public static getInstance(): Logger {
if (!Logger.instance) {
Logger.instance = new Logger();
}
return Logger.instance;
}

public setMinLogLevel(level: LogLevel): void {
if (!this.isValidLogLevel(level)) {
throw new Error(`Invalid log level: ${level}`);
}
this.minLogLevel = level;
}

private isValidLogLevel(level: LogLevel): boolean {
return this.levelsOrder.includes(level);
}

private log(level: LogLevel, ...message: unknown[]): void {
if (level === LogLevel.NONE || !this.allowLevelLog(level)) {
return;
}

const timestamp = new Date().toISOString();
console[level](`[${timestamp}] [${level.toUpperCase()}]`, ...message);
}

private allowLevelLog(level: LogLevel): boolean {
if (!this.isValidLogLevel(level)) {
throw new Error(`Invalid log level: ${level}`);
}

const currentLevelIndex = this.levelsOrder.indexOf(level);
const minLevelIndex = this.levelsOrder.indexOf(this.minLogLevel);
return currentLevelIndex >= minLevelIndex;
}

public debug(...message: unknown[]): void {
this.log(LogLevel.DEBUG, ...message);
}

public info(...message: unknown[]): void {
this.log(LogLevel.INFO, ...message);
}

public warn(...message: unknown[]): void {
this.log(LogLevel.WARN, ...message);
}

public error(...message: unknown[]): void {
this.log(LogLevel.ERROR, ...message);
}
}
36 changes: 18 additions & 18 deletions src/modules/restful/margin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface marginAccountNewOrderResponse {
fills?: marginAccountNewOrderFills[];
}

interface marginAccountNewOrderFills {
export interface marginAccountNewOrderFills {
price: string;
qty: string;
commission: string;
Expand Down Expand Up @@ -165,7 +165,7 @@ export interface getCrossMarginTransferHistoryResponse {
total: number;
}

interface getCrossMarginTransferHistoryRows {
export interface getCrossMarginTransferHistoryRows {
amount: string;
asset: string;
status: MarginStatus;
Expand Down Expand Up @@ -195,7 +195,7 @@ export interface getInterestHistoryResponse {
total: number;
}

interface getInterestHistoryRows {
export interface getInterestHistoryRows {
txId: bigint;
interestAccuredTime: number;
asset: string;
Expand All @@ -221,7 +221,7 @@ export interface getForceLiquidationRecordResponse {
total: number;
}

interface getForceLiquidationRecordRows {
export interface getForceLiquidationRecordRows {
avgPrice: string;
executedQty: string;
orderId: number;
Expand Down Expand Up @@ -250,7 +250,7 @@ export interface getCrossMarginAccountDetailsResponse {
userAssets: getCrossMarginAccountDetailsUserassets[];
}

interface getCrossMarginAccountDetailsUserassets {
export interface getCrossMarginAccountDetailsUserassets {
asset: string;
borrowed: string;
free: string;
Expand Down Expand Up @@ -375,13 +375,13 @@ export interface marginAccountNewOcoResponse {
orderReports: marginAccountNewOcoOrderreports[];
}

interface marginAccountNewOcoOrders {
export interface marginAccountNewOcoOrders {
symbol: string;
orderId: number;
clientOrderId: string;
}

interface marginAccountNewOcoOrderreports {
export interface marginAccountNewOcoOrderreports {
symbol: string;
orderId: number;
orderListId: number;
Expand Down Expand Up @@ -420,13 +420,13 @@ export interface marginAccountCancelOcoResponse {
orderReports: marginAccountCancelOcoOrderReports[];
}

interface marginAccountCancelOcoOrders {
export interface marginAccountCancelOcoOrders {
symbol: string;
orderId: number;
clientOrderId: string;
}

interface marginAccountCancelOcoOrderReports {
export interface marginAccountCancelOcoOrderReports {
symbol: string;
origClientOrderId: string;
orderId: number;
Expand Down Expand Up @@ -464,7 +464,7 @@ export interface getMarginAccountOcoResponse {
orders: getMarginAccountOcoOrders[];
}

interface getMarginAccountOcoOrders {
export interface getMarginAccountOcoOrders {
symbol: string;
orderId: number;
clientOrderId: string;
Expand Down Expand Up @@ -492,7 +492,7 @@ export interface getMarginAccountAllOcoResponse {
orders: getMarginAccountAllOcoOrders[];
}

interface getMarginAccountAllOcoOrders {
export interface getMarginAccountAllOcoOrders {
symbol: string;
orderId: number;
clientOrderId: string;
Expand All @@ -517,7 +517,7 @@ export interface getMarginAccountOpenOcoResponse {
}


interface getMarginAccountOpenOcoOrders {
export interface getMarginAccountOpenOcoOrders {
symbol: string;
orderId: number;
clientOrderId: string;
Expand Down Expand Up @@ -589,7 +589,7 @@ export interface getIsolatedMarginAccountInfoResponse {
totalNetAssetOfBtc?: string;
}

interface getIsolatedMarginAccountInfoBaseasset {
export interface getIsolatedMarginAccountInfoBaseasset {
asset: string
borrowEnabled: boolean
borrowed: string
Expand All @@ -602,7 +602,7 @@ interface getIsolatedMarginAccountInfoBaseasset {
totalAsset: string
}

interface getIsolatedMarginAccountInfoQuoteasset {
export interface getIsolatedMarginAccountInfoQuoteasset {
asset: string
borrowEnabled: boolean
borrowed: string
Expand All @@ -615,7 +615,7 @@ interface getIsolatedMarginAccountInfoQuoteasset {
totalAsset: string
}

interface getIsolatedMarginAccountInfoAssets {
export interface getIsolatedMarginAccountInfoAssets {
baseAsset: getIsolatedMarginAccountInfoBaseasset;
quoteAsset: getIsolatedMarginAccountInfoQuoteasset;
symbol: string;
Expand Down Expand Up @@ -735,7 +735,7 @@ export interface getIsolatedMarginFeeDataResponse {
data: getIsolatedMarginFeeDataData[];
}

interface getIsolatedMarginFeeDataData {
export interface getIsolatedMarginFeeDataData {
coin: string;
dailyInterest: string;
borrowLimit: string;
Expand Down Expand Up @@ -775,7 +775,7 @@ export interface crossMarginCollateralRatioResponse {
assetNames: string[];
}

interface crossMarginCollateralRatioCollaterals {
export interface crossMarginCollateralRatioCollaterals {
minUsdValue: string;
maxUsdValue?: string;
discountRate: string;
Expand Down Expand Up @@ -817,7 +817,7 @@ export interface getSmallLiabilityExchangeHistoryResponse {
rows: getSmallLiabilityExchangeHistoryRows[];
}

interface getSmallLiabilityExchangeHistoryRows {
export interface getSmallLiabilityExchangeHistoryRows {
asset: string;
amount: string;
targetAsset: string;
Expand Down
17 changes: 10 additions & 7 deletions src/modules/restful/market/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,20 +286,23 @@ export function mixinMarket<T extends Constructor>(base: T): Constructor<MarketM
/**
* Trading Day Ticker {@link https://binance-docs.github.io/apidocs/spot/en/#trading-day-ticker}
*
* @param {string} symbol - Trading symbol, e.g. BNBUSDT
* @param {object} [options]
* @param {string} [options.symbols] - The maximum number of symbols allowed in a request is 100.
* @param {string} [options.symbol] - Trading symbol, e.g. BNBUSDT
* @param {string[]} [options.symbols] - The maximum number of symbols allowed in a request is 100.
* @param {string} [options.timeZone] - Default: 0 (UTC)
* @param {string} [options.type] - Supported values: FULL or MINI., If none provided, the default is FULL
*/
async tradingDayTicker(symbol: string, options?: tradingDayTickerOptions): Promise<tradingDayTickerResponse | tradingDayTickerResponse[]> {
validateRequiredParameters({ symbol });
async tradingDayTicker(options: tradingDayTickerOptions): Promise<tradingDayTickerResponse | tradingDayTickerResponse[]> {
if (options.symbol && options.symbols) throw new Error('Symbol and Symbols cannot be sent together.');
if (options && options.symbol && Object.prototype.hasOwnProperty.call(options, 'symbol')) {
options.symbol = options.symbol.toUpperCase();
}
if (options && options.symbols && Object.prototype.hasOwnProperty.call(options, 'symbols')) {
options.symbols = options.symbols.map(symbol => symbol.toUpperCase());
}
const url = this.preparePath('/api/v3/ticker/tradingDay',
Object.assign(
options ? options : {},
{
symbol: symbol.toUpperCase()
}
)
);
return await this.makeRequest('GET', url);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/restful/market/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ export interface MarketMethods {
symbolPriceTicker(options?: symbolPriceTickerOptions): Promise<symbolPriceTickerResponse | symbolPriceTickerResponse[]>;
symbolOrderBookTicker(options?: symbolOrderBookTickerOptions): Promise<symbolOrderBookTickerResponse | symbolOrderBookTickerResponse[]>;
rollingWindowPriceChangeStatistics(options?: rollingWindowPriceChangeStatisticsOptions): Promise<rollingWindowPriceChangeStatisticsResponse | rollingWindowPriceChangeStatisticsResponse[]>;
tradingDayTicker(symbol: string, options?: tradingDayTickerOptions): Promise<tradingDayTickerResponse | tradingDayTickerResponse[]>;
tradingDayTicker(options?: tradingDayTickerOptions): Promise<tradingDayTickerResponse | tradingDayTickerResponse[]>;
}
Loading

0 comments on commit e2edefa

Please sign in to comment.