Skip to content

Commit 0253e96

Browse files
committed
add changes
1 parent 2a68fb4 commit 0253e96

1 file changed

Lines changed: 41 additions & 6 deletions

File tree

src/node-binance-api.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,9 +2934,6 @@ export default class Binance {
29342934
* @return {undefined}
29352935
*/
29362936
userDataHandler(data: any) {
2937-
// Handle new WebSocket API format where events are wrapped
2938-
// New format: { subscriptionId: 0, event: { e: "eventType", ... } }
2939-
// Old format: { e: "eventType", ... }
29402937
let eventData = data;
29412938
if (data.subscriptionId !== undefined && data.event) {
29422939
eventData = data.event;
@@ -2975,9 +2972,6 @@ export default class Binance {
29752972
* @return {undefined}
29762973
*/
29772974
userMarginDataHandler(data: any) {
2978-
// Handle new WebSocket API format where events are wrapped
2979-
// New format: { subscriptionId: 0, event: { e: "eventType", ... } }
2980-
// Old format: { e: "eventType", ... }
29812975
let eventData = data;
29822976
if (data.subscriptionId !== undefined && data.event) {
29832977
eventData = data.event;
@@ -3847,6 +3841,47 @@ export default class Binance {
38473841
return this.priceData(data);
38483842
}
38493843

3844+
/**
3845+
* Gets the ticker price via WebSocket API (JSON-RPC)
3846+
* @param {string} symbol - single symbol
3847+
* @param {string[]} symbols - array of symbols
3848+
* @param {object} options - additional options (e.g. symbolStatus)
3849+
* @see https://developers.binance.com/docs/binance-spot-api-docs/web-socket-api/market-data-requests#symbol-price-ticker
3850+
* @return {promise} - resolves with ticker price data
3851+
*/
3852+
async tickerPrice(symbol?: string, symbols?: string[], options: Dict = {}): Promise<any> {
3853+
if (symbol && symbols) {
3854+
throw new Error('Cannot specify both symbol and symbols parameters');
3855+
}
3856+
3857+
const connectionId = 'marketData';
3858+
await this.ensureWsApiConnection(connectionId);
3859+
3860+
const params: Dict = { ...options };
3861+
if (symbol) params.symbol = symbol;
3862+
if (symbols) params.symbols = symbols;
3863+
3864+
return this.sendWsApiRequest(connectionId, 'ticker.price', params);
3865+
}
3866+
3867+
/**
3868+
* Ensures a WebSocket API connection is open for the given connectionId
3869+
* @param {string} connectionId - connection identifier
3870+
* @return {promise} - resolves when the connection is open
3871+
*/
3872+
private ensureWsApiConnection(connectionId: string): Promise<void> {
3873+
return new Promise((resolve, reject) => {
3874+
const existing = this.wsApiConnections[connectionId];
3875+
if (existing && existing.readyState === WebSocket.OPEN) {
3876+
resolve();
3877+
return;
3878+
}
3879+
const ws = this.connectWsApi(connectionId, () => {}, () => {});
3880+
ws.on('open', () => resolve());
3881+
ws.on('error', (err: Error) => reject(err));
3882+
});
3883+
}
3884+
38503885
/**
38513886
* Gets the book tickers of given symbol(s)
38523887
* @see https://developers.binance.com/docs/binance-spot-api-docs/testnet/rest-api/market-data-endpoints#symbol-order-book-ticker

0 commit comments

Comments
 (0)