binance 3.0.6 → 3.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -0
- package/lib/types/websockets/ws-general.d.ts +7 -0
- package/lib/util/requestUtils.d.ts +7 -0
- package/lib/util/requestUtils.js +30 -3
- package/lib/util/requestUtils.js.map +1 -1
- package/lib/util/websockets/websocket-util.d.ts +1 -0
- package/lib/util/websockets/websocket-util.js +38 -3
- package/lib/util/websockets/websocket-util.js.map +1 -1
- package/llms.txt +1249 -1141
- package/package.json +1 -1
package/llms.txt
CHANGED
|
@@ -1091,6 +1091,63 @@ function onUserDataEvent(data: WsUserDataEvents)
|
|
|
1091
1091
|
// wsClient.subscribeMarginUserDataStream();
|
|
1092
1092
|
// wsClient.subscribeIsolatedMarginUserDataStream('BTCUSDT');
|
|
1093
1093
|
|
|
1094
|
+
================
|
|
1095
|
+
File: examples/WebSockets/ws-userdata-listenKey-testnet.ts
|
|
1096
|
+
================
|
|
1097
|
+
import {
|
|
1098
|
+
DefaultLogger,
|
|
1099
|
+
isWsFormattedFuturesUserDataEvent,
|
|
1100
|
+
isWsFormattedSpotUserDataEvent,
|
|
1101
|
+
isWsFormattedSpotUserDataExecutionReport,
|
|
1102
|
+
isWsFormattedUserDataEvent,
|
|
1103
|
+
WebsocketClient,
|
|
1104
|
+
WsUserDataEvents,
|
|
1105
|
+
} from '../../src/index';
|
|
1106
|
+
⋮----
|
|
1107
|
+
// or
|
|
1108
|
+
// import { DefaultLogger, WebsocketClient } from 'binance';
|
|
1109
|
+
⋮----
|
|
1110
|
+
// console.log('using api credentials: ', { key, secret });
|
|
1111
|
+
⋮----
|
|
1112
|
+
// Optional, hook and customise logging behavior
|
|
1113
|
+
⋮----
|
|
1114
|
+
// If you prefer, you can receive raw unprocessed data without the "beautifier":
|
|
1115
|
+
// wsClient.on('message', (data) => {
|
|
1116
|
+
// console.log('raw message received ', JSON.stringify(data, null, 2));
|
|
1117
|
+
// });
|
|
1118
|
+
⋮----
|
|
1119
|
+
function onUserDataEvent(data: WsUserDataEvents)
|
|
1120
|
+
⋮----
|
|
1121
|
+
// the market denotes which API category it came from
|
|
1122
|
+
// if (data.wsMarket.includes('spot')) {
|
|
1123
|
+
⋮----
|
|
1124
|
+
// or use a type guard, if one exists (PRs welcome)
|
|
1125
|
+
⋮----
|
|
1126
|
+
// Beautified/formatted events from binance:
|
|
1127
|
+
⋮----
|
|
1128
|
+
// The wsKey can be parsed to determine the type of message (what websocket it came from)
|
|
1129
|
+
// if (!Array.isArray(data) && data.wsKey.includes('userData')) {
|
|
1130
|
+
// return onUserDataEvent(data);
|
|
1131
|
+
// }
|
|
1132
|
+
⋮----
|
|
1133
|
+
// or use a type guard if available
|
|
1134
|
+
⋮----
|
|
1135
|
+
// response to command sent via WS stream (e.g LIST_SUBSCRIPTIONS)
|
|
1136
|
+
⋮----
|
|
1137
|
+
// This is a good place to check your own state is still in sync with the account state on the exchange, in case any events were missed while the library was reconnecting:
|
|
1138
|
+
// - fetch balances
|
|
1139
|
+
// - fetch positions
|
|
1140
|
+
// - fetch orders
|
|
1141
|
+
⋮----
|
|
1142
|
+
// wsClient.subscribeMarginUserDataStream();
|
|
1143
|
+
// wsClient.subscribeIsolatedMarginUserDataStream('BTCUSDT');
|
|
1144
|
+
// wsClient.subscribeUsdFuturesUserDataStream();
|
|
1145
|
+
⋮----
|
|
1146
|
+
// setTimeout(() => {
|
|
1147
|
+
// console.log('killing all connections');
|
|
1148
|
+
// wsClient.closeAll();
|
|
1149
|
+
// }, 1000 * 15);
|
|
1150
|
+
|
|
1094
1151
|
================
|
|
1095
1152
|
File: src/types/websockets/ws-events-formatted.ts
|
|
1096
1153
|
================
|
|
@@ -1647,337 +1704,122 @@ export type WsFormattedMessage =
|
|
|
1647
1704
|
| WsMessageMarkPriceUpdateEventFormatted[];
|
|
1648
1705
|
|
|
1649
1706
|
================
|
|
1650
|
-
File: src/types/
|
|
1707
|
+
File: src/types/coin.ts
|
|
1651
1708
|
================
|
|
1652
|
-
import {
|
|
1653
|
-
import
|
|
1654
|
-
⋮----
|
|
1655
|
-
import { RestClientOptions } from '../../util/requestUtils';
|
|
1656
|
-
import { WsKey } from '../../util/websockets/websocket-util';
|
|
1709
|
+
import { FuturesContractType, PositionSide } from './futures';
|
|
1710
|
+
import { numberInString, OrderSide } from './shared';
|
|
1657
1711
|
⋮----
|
|
1658
|
-
export interface
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1712
|
+
export interface PositionRisk {
|
|
1713
|
+
symbol: string;
|
|
1714
|
+
positionAmt: numberInString;
|
|
1715
|
+
entryPrice: numberInString;
|
|
1716
|
+
markPrice: numberInString;
|
|
1717
|
+
unRealizedProfit: numberInString;
|
|
1718
|
+
liquidationPrice: numberInString;
|
|
1719
|
+
leverage: numberInString;
|
|
1720
|
+
maxQty: numberInString;
|
|
1721
|
+
marginType: string;
|
|
1722
|
+
isolatedMargin: numberInString;
|
|
1723
|
+
isAutoAddMargin: boolean;
|
|
1724
|
+
positionSide: PositionSide;
|
|
1725
|
+
updateTime: number;
|
|
1662
1726
|
}
|
|
1663
1727
|
⋮----
|
|
1664
|
-
export
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
| 'isolatedMargin'
|
|
1671
|
-
| 'riskDataMargin'
|
|
1672
|
-
| 'usdm'
|
|
1673
|
-
| 'usdmTestnet'
|
|
1674
|
-
| 'coinm'
|
|
1675
|
-
| 'coinmTestnet'
|
|
1676
|
-
| 'options'
|
|
1677
|
-
| 'optionsTestnet'
|
|
1678
|
-
| 'portfoliom';
|
|
1679
|
-
⋮----
|
|
1680
|
-
export interface WsSharedBase {
|
|
1681
|
-
wsMarket: WsMarket;
|
|
1682
|
-
wsKey: WsKey;
|
|
1683
|
-
streamName: string;
|
|
1728
|
+
export interface CoinMOpenInterest {
|
|
1729
|
+
symbol: string;
|
|
1730
|
+
pair: string;
|
|
1731
|
+
openInterest: numberInString;
|
|
1732
|
+
contractType: FuturesContractType;
|
|
1733
|
+
time: number;
|
|
1684
1734
|
}
|
|
1735
|
+
export type SymbolOrPair =
|
|
1736
|
+
| { pair: string; symbol?: never }
|
|
1737
|
+
| { pair?: never; symbol: string };
|
|
1685
1738
|
⋮----
|
|
1686
|
-
export interface
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1739
|
+
export interface CoinMSymbolOrderBookTicker {
|
|
1740
|
+
symbol: string;
|
|
1741
|
+
pair: string;
|
|
1742
|
+
bidPrice: numberInString;
|
|
1743
|
+
bidQty: numberInString;
|
|
1744
|
+
askPrice: numberInString;
|
|
1745
|
+
askQty: numberInString;
|
|
1746
|
+
time: number;
|
|
1694
1747
|
}
|
|
1695
1748
|
⋮----
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
| 'instrument_info'
|
|
1703
|
-
| 'klineV2';
|
|
1749
|
+
export interface CoinMPaginatedRequest {
|
|
1750
|
+
fromId?: number;
|
|
1751
|
+
startTime?: number;
|
|
1752
|
+
endTime?: number;
|
|
1753
|
+
limit?: number;
|
|
1754
|
+
}
|
|
1704
1755
|
⋮----
|
|
1705
|
-
export
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
| 'instrument_info'
|
|
1711
|
-
| 'kline';
|
|
1756
|
+
export interface CoinMAccountTradeParamsWithPair extends CoinMPaginatedRequest {
|
|
1757
|
+
pair: string;
|
|
1758
|
+
symbol?: never;
|
|
1759
|
+
fromId?: never;
|
|
1760
|
+
}
|
|
1712
1761
|
⋮----
|
|
1713
|
-
export
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
| 'mergedDepth'
|
|
1719
|
-
| 'diffDepth';
|
|
1762
|
+
export interface CoinMAccountTradeParamsWithSymbol
|
|
1763
|
+
extends CoinMPaginatedRequest {
|
|
1764
|
+
symbol: string;
|
|
1765
|
+
pair?: never;
|
|
1766
|
+
}
|
|
1720
1767
|
⋮----
|
|
1721
|
-
export
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1768
|
+
export interface CoinMAccountTradeParamsWithFromId
|
|
1769
|
+
extends CoinMPaginatedRequest {
|
|
1770
|
+
fromId: number;
|
|
1771
|
+
startTime?: never;
|
|
1772
|
+
endTime?: never;
|
|
1773
|
+
}
|
|
1727
1774
|
⋮----
|
|
1728
|
-
export type
|
|
1729
|
-
|
|
|
1730
|
-
|
|
|
1731
|
-
|
|
|
1732
|
-
| WsPublicSpotV2Topic
|
|
1733
|
-
| string;
|
|
1775
|
+
export type CoinMAccountTradeParams =
|
|
1776
|
+
| CoinMAccountTradeParamsWithSymbol
|
|
1777
|
+
| CoinMAccountTradeParamsWithPair
|
|
1778
|
+
| CoinMAccountTradeParamsWithFromId;
|
|
1734
1779
|
⋮----
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1780
|
+
export interface CoinMPositionTrade {
|
|
1781
|
+
symbol: string;
|
|
1782
|
+
id: number;
|
|
1783
|
+
orderId: number;
|
|
1784
|
+
pair: string;
|
|
1785
|
+
side: OrderSide;
|
|
1786
|
+
price: numberInString;
|
|
1787
|
+
qty: numberInString;
|
|
1788
|
+
realizedPnl: numberInString;
|
|
1789
|
+
marginAsset: string;
|
|
1790
|
+
baseQty: numberInString;
|
|
1791
|
+
commission: numberInString;
|
|
1792
|
+
commissionAsset: string;
|
|
1793
|
+
time: number;
|
|
1794
|
+
positionSide: PositionSide;
|
|
1795
|
+
buyer: boolean;
|
|
1796
|
+
maker: boolean;
|
|
1797
|
+
}
|
|
1741
1798
|
⋮----
|
|
1742
|
-
export
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1799
|
+
export interface FundingRate {
|
|
1800
|
+
symbol: string;
|
|
1801
|
+
adjustedFundingRateCap: string;
|
|
1802
|
+
adjustedFundingRateFloor: string;
|
|
1803
|
+
fundingIntervalHours: number;
|
|
1804
|
+
disclaimer: boolean;
|
|
1805
|
+
}
|
|
1748
1806
|
⋮----
|
|
1749
|
-
export
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1807
|
+
export interface GetClassicPortfolioMarginNotionalLimitParams {
|
|
1808
|
+
symbol?: string;
|
|
1809
|
+
pair?: string;
|
|
1810
|
+
}
|
|
1753
1811
|
⋮----
|
|
1754
|
-
export
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1812
|
+
export interface ClassicPortfolioMarginNotionalLimit {
|
|
1813
|
+
symbol: string;
|
|
1814
|
+
pair: string;
|
|
1815
|
+
notionalLimit: string;
|
|
1816
|
+
}
|
|
1759
1817
|
⋮----
|
|
1760
|
-
export
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
/** Your API secret */
|
|
1767
|
-
api_secret?: string;
|
|
1768
|
-
|
|
1769
|
-
beautify?: boolean;
|
|
1770
|
-
|
|
1771
|
-
/**
|
|
1772
|
-
* If true, log a warning if the beautifier is missing anything for an event
|
|
1773
|
-
*/
|
|
1774
|
-
beautifyWarnIfMissing?: boolean;
|
|
1775
|
-
|
|
1776
|
-
/**
|
|
1777
|
-
* Set to `true` to connect to Binance's testnet environment.
|
|
1778
|
-
*
|
|
1779
|
-
* Notes:
|
|
1780
|
-
* - Not all WebSocket categories support testnet.
|
|
1781
|
-
* - If testing a strategy, this is not recommended. Testnet market data is very different from real market conditions. More guidance here: https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
1782
|
-
*/
|
|
1783
|
-
testnet?: boolean;
|
|
1784
|
-
|
|
1785
|
-
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
1786
|
-
recvWindow?: number;
|
|
1787
|
-
|
|
1788
|
-
// Disable ping/pong ws heartbeat mechanism (not recommended)
|
|
1789
|
-
disableHeartbeat?: boolean;
|
|
1790
|
-
|
|
1791
|
-
/** How often to check if the connection is alive */
|
|
1792
|
-
pingInterval?: number;
|
|
1793
|
-
|
|
1794
|
-
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
1795
|
-
pongTimeout?: number;
|
|
1796
|
-
|
|
1797
|
-
/** Delay in milliseconds before respawning the connection */
|
|
1798
|
-
reconnectTimeout?: number;
|
|
1799
|
-
|
|
1800
|
-
restOptions?: RestClientOptions;
|
|
1801
|
-
|
|
1802
|
-
requestOptions?: AxiosRequestConfig;
|
|
1803
|
-
|
|
1804
|
-
wsOptions?: {
|
|
1805
|
-
protocols?: string[];
|
|
1806
|
-
agent?: any;
|
|
1807
|
-
};
|
|
1808
|
-
|
|
1809
|
-
wsUrl?: string;
|
|
1810
|
-
|
|
1811
|
-
/**
|
|
1812
|
-
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
1813
|
-
*
|
|
1814
|
-
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
1815
|
-
*/
|
|
1816
|
-
customSignMessageFn?: (message: string, secret: string) => Promise<string>;
|
|
1817
|
-
}
|
|
1818
|
-
⋮----
|
|
1819
|
-
/** Your API key */
|
|
1820
|
-
⋮----
|
|
1821
|
-
/** Your API secret */
|
|
1822
|
-
⋮----
|
|
1823
|
-
/**
|
|
1824
|
-
* If true, log a warning if the beautifier is missing anything for an event
|
|
1825
|
-
*/
|
|
1826
|
-
⋮----
|
|
1827
|
-
/**
|
|
1828
|
-
* Set to `true` to connect to Binance's testnet environment.
|
|
1829
|
-
*
|
|
1830
|
-
* Notes:
|
|
1831
|
-
* - Not all WebSocket categories support testnet.
|
|
1832
|
-
* - If testing a strategy, this is not recommended. Testnet market data is very different from real market conditions. More guidance here: https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
1833
|
-
*/
|
|
1834
|
-
⋮----
|
|
1835
|
-
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
1836
|
-
⋮----
|
|
1837
|
-
// Disable ping/pong ws heartbeat mechanism (not recommended)
|
|
1838
|
-
⋮----
|
|
1839
|
-
/** How often to check if the connection is alive */
|
|
1840
|
-
⋮----
|
|
1841
|
-
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
1842
|
-
⋮----
|
|
1843
|
-
/** Delay in milliseconds before respawning the connection */
|
|
1844
|
-
⋮----
|
|
1845
|
-
/**
|
|
1846
|
-
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
1847
|
-
*
|
|
1848
|
-
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
1849
|
-
*/
|
|
1850
|
-
⋮----
|
|
1851
|
-
/**
|
|
1852
|
-
* WS configuration that's always defined, regardless of user configuration
|
|
1853
|
-
* (usually comes from defaults if there's no user-provided values)
|
|
1854
|
-
*/
|
|
1855
|
-
export interface WebsocketClientOptions extends WSClientConfigurableOptions {
|
|
1856
|
-
pongTimeout: number;
|
|
1857
|
-
pingInterval: number;
|
|
1858
|
-
reconnectTimeout: number;
|
|
1859
|
-
recvWindow: number;
|
|
1860
|
-
authPrivateConnectionsOnConnect: boolean;
|
|
1861
|
-
authPrivateRequests: boolean;
|
|
1862
|
-
}
|
|
1863
|
-
|
|
1864
|
-
================
|
|
1865
|
-
File: src/types/coin.ts
|
|
1866
|
-
================
|
|
1867
|
-
import { FuturesContractType, PositionSide } from './futures';
|
|
1868
|
-
import { numberInString, OrderSide } from './shared';
|
|
1869
|
-
⋮----
|
|
1870
|
-
export interface PositionRisk {
|
|
1871
|
-
symbol: string;
|
|
1872
|
-
positionAmt: numberInString;
|
|
1873
|
-
entryPrice: numberInString;
|
|
1874
|
-
markPrice: numberInString;
|
|
1875
|
-
unRealizedProfit: numberInString;
|
|
1876
|
-
liquidationPrice: numberInString;
|
|
1877
|
-
leverage: numberInString;
|
|
1878
|
-
maxQty: numberInString;
|
|
1879
|
-
marginType: string;
|
|
1880
|
-
isolatedMargin: numberInString;
|
|
1881
|
-
isAutoAddMargin: boolean;
|
|
1882
|
-
positionSide: PositionSide;
|
|
1883
|
-
updateTime: number;
|
|
1884
|
-
}
|
|
1885
|
-
⋮----
|
|
1886
|
-
export interface CoinMOpenInterest {
|
|
1887
|
-
symbol: string;
|
|
1888
|
-
pair: string;
|
|
1889
|
-
openInterest: numberInString;
|
|
1890
|
-
contractType: FuturesContractType;
|
|
1891
|
-
time: number;
|
|
1892
|
-
}
|
|
1893
|
-
export type SymbolOrPair =
|
|
1894
|
-
| { pair: string; symbol?: never }
|
|
1895
|
-
| { pair?: never; symbol: string };
|
|
1896
|
-
⋮----
|
|
1897
|
-
export interface CoinMSymbolOrderBookTicker {
|
|
1898
|
-
symbol: string;
|
|
1899
|
-
pair: string;
|
|
1900
|
-
bidPrice: numberInString;
|
|
1901
|
-
bidQty: numberInString;
|
|
1902
|
-
askPrice: numberInString;
|
|
1903
|
-
askQty: numberInString;
|
|
1904
|
-
time: number;
|
|
1905
|
-
}
|
|
1906
|
-
⋮----
|
|
1907
|
-
export interface CoinMPaginatedRequest {
|
|
1908
|
-
fromId?: number;
|
|
1909
|
-
startTime?: number;
|
|
1910
|
-
endTime?: number;
|
|
1911
|
-
limit?: number;
|
|
1912
|
-
}
|
|
1913
|
-
⋮----
|
|
1914
|
-
export interface CoinMAccountTradeParamsWithPair extends CoinMPaginatedRequest {
|
|
1915
|
-
pair: string;
|
|
1916
|
-
symbol?: never;
|
|
1917
|
-
fromId?: never;
|
|
1918
|
-
}
|
|
1919
|
-
⋮----
|
|
1920
|
-
export interface CoinMAccountTradeParamsWithSymbol
|
|
1921
|
-
extends CoinMPaginatedRequest {
|
|
1922
|
-
symbol: string;
|
|
1923
|
-
pair?: never;
|
|
1924
|
-
}
|
|
1925
|
-
⋮----
|
|
1926
|
-
export interface CoinMAccountTradeParamsWithFromId
|
|
1927
|
-
extends CoinMPaginatedRequest {
|
|
1928
|
-
fromId: number;
|
|
1929
|
-
startTime?: never;
|
|
1930
|
-
endTime?: never;
|
|
1931
|
-
}
|
|
1932
|
-
⋮----
|
|
1933
|
-
export type CoinMAccountTradeParams =
|
|
1934
|
-
| CoinMAccountTradeParamsWithSymbol
|
|
1935
|
-
| CoinMAccountTradeParamsWithPair
|
|
1936
|
-
| CoinMAccountTradeParamsWithFromId;
|
|
1937
|
-
⋮----
|
|
1938
|
-
export interface CoinMPositionTrade {
|
|
1939
|
-
symbol: string;
|
|
1940
|
-
id: number;
|
|
1941
|
-
orderId: number;
|
|
1942
|
-
pair: string;
|
|
1943
|
-
side: OrderSide;
|
|
1944
|
-
price: numberInString;
|
|
1945
|
-
qty: numberInString;
|
|
1946
|
-
realizedPnl: numberInString;
|
|
1947
|
-
marginAsset: string;
|
|
1948
|
-
baseQty: numberInString;
|
|
1949
|
-
commission: numberInString;
|
|
1950
|
-
commissionAsset: string;
|
|
1951
|
-
time: number;
|
|
1952
|
-
positionSide: PositionSide;
|
|
1953
|
-
buyer: boolean;
|
|
1954
|
-
maker: boolean;
|
|
1955
|
-
}
|
|
1956
|
-
⋮----
|
|
1957
|
-
export interface FundingRate {
|
|
1958
|
-
symbol: string;
|
|
1959
|
-
adjustedFundingRateCap: string;
|
|
1960
|
-
adjustedFundingRateFloor: string;
|
|
1961
|
-
fundingIntervalHours: number;
|
|
1962
|
-
disclaimer: boolean;
|
|
1963
|
-
}
|
|
1964
|
-
⋮----
|
|
1965
|
-
export interface GetClassicPortfolioMarginNotionalLimitParams {
|
|
1966
|
-
symbol?: string;
|
|
1967
|
-
pair?: string;
|
|
1968
|
-
}
|
|
1969
|
-
⋮----
|
|
1970
|
-
export interface ClassicPortfolioMarginNotionalLimit {
|
|
1971
|
-
symbol: string;
|
|
1972
|
-
pair: string;
|
|
1973
|
-
notionalLimit: string;
|
|
1974
|
-
}
|
|
1975
|
-
⋮----
|
|
1976
|
-
export interface ClassicPortfolioMarginAccount {
|
|
1977
|
-
maxWithdrawAmountUSD: string;
|
|
1978
|
-
asset: string;
|
|
1979
|
-
maxWithdrawAmount: string;
|
|
1980
|
-
}
|
|
1818
|
+
export interface ClassicPortfolioMarginAccount {
|
|
1819
|
+
maxWithdrawAmountUSD: string;
|
|
1820
|
+
asset: string;
|
|
1821
|
+
maxWithdrawAmount: string;
|
|
1822
|
+
}
|
|
1981
1823
|
⋮----
|
|
1982
1824
|
export interface FuturesTransactionHistoryDownloadLink {
|
|
1983
1825
|
downloadId: string;
|
|
@@ -5706,443 +5548,248 @@ import { DefaultLogger, WebsocketClient } from '../../src/index';
|
|
|
5706
5548
|
// arrays also supported:
|
|
5707
5549
|
|
|
5708
5550
|
================
|
|
5709
|
-
File: examples/WebSockets/ws-
|
|
5551
|
+
File: examples/WebSockets/ws-userdata-README.MD
|
|
5710
5552
|
================
|
|
5711
|
-
|
|
5712
|
-
|
|
5713
|
-
|
|
5714
|
-
|
|
5715
|
-
|
|
5716
|
-
|
|
5717
|
-
|
|
5718
|
-
|
|
5719
|
-
|
|
5720
|
-
|
|
5721
|
-
|
|
5722
|
-
|
|
5723
|
-
|
|
5724
|
-
|
|
5725
|
-
|
|
5726
|
-
|
|
5727
|
-
|
|
5728
|
-
|
|
5729
|
-
|
|
5730
|
-
|
|
5731
|
-
|
|
5732
|
-
|
|
5733
|
-
|
|
5734
|
-
|
|
5735
|
-
|
|
5736
|
-
|
|
5737
|
-
|
|
5738
|
-
|
|
5739
|
-
|
|
5740
|
-
|
|
5741
|
-
|
|
5742
|
-
|
|
5743
|
-
|
|
5744
|
-
|
|
5745
|
-
|
|
5746
|
-
|
|
5747
|
-
|
|
5748
|
-
|
|
5749
|
-
|
|
5750
|
-
|
|
5751
|
-
|
|
5752
|
-
|
|
5753
|
-
|
|
5754
|
-
//
|
|
5755
|
-
//
|
|
5756
|
-
//
|
|
5757
|
-
// }
|
|
5758
|
-
// // For one symbol
|
|
5759
|
-
// if (isWsFormattedMarkPriceUpdateEvent(data)) {
|
|
5760
|
-
// console.log('log mark price: ', data);
|
|
5761
|
-
// return;
|
|
5762
|
-
// }
|
|
5763
|
-
// // for many symbols
|
|
5764
|
-
// if (isWsFormattedMarkPriceUpdateArray(data)) {
|
|
5765
|
-
// console.log('log mark prices: ', data);
|
|
5766
|
-
// return;
|
|
5767
|
-
// }
|
|
5768
|
-
// if (isWsFormattedKline(data)) {
|
|
5769
|
-
// console.log('log kline: ', data);
|
|
5770
|
-
// return;
|
|
5771
|
-
// }
|
|
5772
|
-
// if (isWsFormattedTrade(data)) {
|
|
5773
|
-
// return console.log('log trade: ', data);
|
|
5774
|
-
// }
|
|
5775
|
-
// if (isWsFormattedForceOrder(data)) {
|
|
5776
|
-
// return console.log('log force order: ', data);
|
|
5777
|
-
// }
|
|
5778
|
-
// if (isWsFormatted24hrTickerArray(data)) {
|
|
5779
|
-
// return console.log('log 24hr ticker array: ', data);
|
|
5780
|
-
// }
|
|
5781
|
-
// if (isWsFormattedRollingWindowTickerArray(data)) {
|
|
5782
|
-
// return console.log('log rolling window ticker array: ', data);
|
|
5783
|
-
// }
|
|
5784
|
-
// if (isWsFormatted24hrTicker(data)) {
|
|
5785
|
-
// return console.log('log 24hr ticker: ', data);
|
|
5786
|
-
// }
|
|
5787
|
-
// if (isWsPartialBookDepthEventFormatted(data)) {
|
|
5788
|
-
// return console.log('log partial book depth event: ', data);
|
|
5789
|
-
// }
|
|
5790
|
-
⋮----
|
|
5791
|
-
//console.log('log formattedUserDataMessage: ', data);
|
|
5792
|
-
⋮----
|
|
5793
|
-
// response to command sent via WS stream (e.g LIST_SUBSCRIPTIONS)
|
|
5553
|
+
# User Data Streams - Binance
|
|
5554
|
+
|
|
5555
|
+
The user data streams are the WebSocket method for asynchronously receiving events when any changes happen on your account. Including but not limited to:
|
|
5556
|
+
- order events (filled/cancelled/updated/etc)
|
|
5557
|
+
- position events
|
|
5558
|
+
- balance events (deposit/withdraw/etc)
|
|
5559
|
+
- misc events (leverage changed, position mode changed, etc)
|
|
5560
|
+
|
|
5561
|
+
## Mechanisms
|
|
5562
|
+
|
|
5563
|
+
There are currently two key mechanisms for subscribing to user data events
|
|
5564
|
+
|
|
5565
|
+
### Mechanisms - Listen Key
|
|
5566
|
+
|
|
5567
|
+
The older and original mechanism involves a listen key. This is requested via the REST API and then used when opening a connection to the user data stream.
|
|
5568
|
+
|
|
5569
|
+
The listen key can expire and needs a regular "ping" (a REST API call) to keep it alive.
|
|
5570
|
+
|
|
5571
|
+
With the "binance" Node.js & JavaScript SDK, you can easily subscribe to the user data stream via the listen key workflow, through just one function call.
|
|
5572
|
+
|
|
5573
|
+
The SDK will automatically:
|
|
5574
|
+
- fetch a listen key
|
|
5575
|
+
- perform regular keep alive requests on the listen key
|
|
5576
|
+
- handle listen key expiry/refresh
|
|
5577
|
+
- handle reconnects, if a connection is temporarily lost
|
|
5578
|
+
|
|
5579
|
+
All you have to do is ask for the user data stream and process incoming events - the SDK will handle the rest!
|
|
5580
|
+
|
|
5581
|
+
For a working example, refer to the [ws-userdata-listenkey](./ws-userdata-listenkey.ts) example.
|
|
5582
|
+
|
|
5583
|
+
**Deprecation warning: As of April 2025, the listen key workflow is also deprecated for Spot markets (no known ETA for removal).** Refer to the changelog for details: https://developers.binance.com/docs/binance-spot-api-docs/CHANGELOG#2025-04-07
|
|
5584
|
+
|
|
5585
|
+
### Mechanisms - WS API User Data Stream
|
|
5586
|
+
|
|
5587
|
+
The newer mechanism for the user data stream is via an active WebSocket API connection. This is a simpler mechanism in that it does not require a listen key. You can easily use this new mechanism via the WebsocketClient (or WebsocketAPIClient) within this SDK.
|
|
5588
|
+
|
|
5589
|
+
For a working example, refer to the [ws-userdata-wsapi](./ws-userdata-wsapi.ts) example.
|
|
5590
|
+
|
|
5591
|
+
================
|
|
5592
|
+
File: examples/WebSockets/ws-userdata-wsapi.ts
|
|
5593
|
+
================
|
|
5594
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5595
|
+
// or
|
|
5596
|
+
// import { WebsocketAPIClient, WebsocketClient, WS_KEY_MAP } from 'binance';
|
|
5597
|
+
// or
|
|
5598
|
+
// const { WebsocketAPIClient, WebsocketClient, WS_KEY_MAP } = require('binance');
|
|
5794
5599
|
⋮----
|
|
5795
|
-
|
|
5600
|
+
import {
|
|
5601
|
+
isWsFormattedFuturesUserDataEvent,
|
|
5602
|
+
isWsFormattedSpotBalanceUpdate,
|
|
5603
|
+
isWsFormattedSpotOutboundAccountPosition,
|
|
5604
|
+
isWsFormattedSpotUserDataEvent,
|
|
5605
|
+
isWsFormattedUserDataEvent,
|
|
5606
|
+
WebsocketAPIClient,
|
|
5607
|
+
WebsocketClient,
|
|
5608
|
+
WS_KEY_MAP,
|
|
5609
|
+
} from '../../src';
|
|
5796
5610
|
⋮----
|
|
5797
5611
|
/**
|
|
5798
|
-
|
|
5799
|
-
|
|
5800
|
-
|
|
5801
|
-
|
|
5612
|
+
* The WS API only works with an Ed25519 API key.
|
|
5613
|
+
*
|
|
5614
|
+
* Check the rest-private-ed25519.md in this folder for more guidance
|
|
5615
|
+
* on preparing this Ed25519 API key.
|
|
5616
|
+
*/
|
|
5802
5617
|
⋮----
|
|
5803
|
-
//
|
|
5804
|
-
//
|
|
5805
|
-
//
|
|
5618
|
+
// returned by binance, generated using the publicKey (above)
|
|
5619
|
+
// const key = 'BVv39ATnIme5TTZRcC3I04C3FqLVM7vCw3Hf7mMT7uu61nEZK8xV1V5dmhf9kifm';
|
|
5620
|
+
// Your Ed25519 private key is passed as the "secret"
|
|
5621
|
+
// const secret = privateKey;
|
|
5806
5622
|
⋮----
|
|
5807
|
-
|
|
5808
|
-
|
|
5809
|
-
|
|
5810
|
-
// 'btcusdt@aggTrade',
|
|
5811
|
-
// 'btcusdt@markPrice',
|
|
5812
|
-
// '!ticker@arr',
|
|
5813
|
-
// '!miniTicker@arr',
|
|
5814
|
-
// ],
|
|
5815
|
-
// 'usdm',
|
|
5816
|
-
// );
|
|
5623
|
+
function attachEventHandlers<TWSClient extends WebsocketClient>(
|
|
5624
|
+
wsClient: TWSClient,
|
|
5625
|
+
): void
|
|
5817
5626
|
⋮----
|
|
5818
5627
|
/**
|
|
5819
|
-
|
|
5820
|
-
|
|
5821
|
-
⋮----
|
|
5822
|
-
// Aggregate Trade Streams
|
|
5823
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#aggregate-trade-streams
|
|
5824
|
-
⋮----
|
|
5825
|
-
// Trade Streams
|
|
5826
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#trade-streams
|
|
5628
|
+
* General event handlers for monitoring the WebsocketClient
|
|
5629
|
+
*/
|
|
5827
5630
|
⋮----
|
|
5828
|
-
//
|
|
5829
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#klinecandlestick-streams-for-utc
|
|
5631
|
+
// Raw events received from binance, as is:
|
|
5830
5632
|
⋮----
|
|
5831
|
-
//
|
|
5832
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#klinecandlestick-streams-with-timezone-offset
|
|
5633
|
+
// console.log('raw message received ', JSON.stringify(data));
|
|
5833
5634
|
⋮----
|
|
5834
|
-
//
|
|
5835
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-mini-ticker-stream
|
|
5635
|
+
// Formatted events from the built-in beautifier, with fully readable property names and parsed floats:
|
|
5836
5636
|
⋮----
|
|
5837
|
-
//
|
|
5838
|
-
//
|
|
5637
|
+
// We've included type guards for many events, especially on the user data stream, to help easily
|
|
5638
|
+
// identify events using simple `if` checks.
|
|
5639
|
+
//
|
|
5640
|
+
// Use `if` checks to narrow down specific events from the user data stream
|
|
5839
5641
|
⋮----
|
|
5840
|
-
|
|
5841
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-ticker-streams
|
|
5642
|
+
//// More general handlers, if you prefer:
|
|
5842
5643
|
⋮----
|
|
5843
|
-
//
|
|
5844
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-tickers-stream
|
|
5644
|
+
// Any user data event in spot:
|
|
5845
5645
|
⋮----
|
|
5846
|
-
//
|
|
5847
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-rolling-window-statistics-streams
|
|
5646
|
+
// Any user data event in futures:
|
|
5848
5647
|
⋮----
|
|
5849
|
-
//
|
|
5850
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-rolling-window-statistics-streams
|
|
5648
|
+
// Any user data event on any market (spot + futures)
|
|
5851
5649
|
⋮----
|
|
5852
|
-
//
|
|
5853
|
-
//
|
|
5650
|
+
// Formatted user data events also have a dedicated event handler, but that's optional and no different to the above
|
|
5651
|
+
// wsClient.on('formattedUserDataMessage', (data) => {
|
|
5652
|
+
// if (isWsFormattedSpotOutboundAccountPosition(data)) {
|
|
5653
|
+
// return;
|
|
5654
|
+
// // console.log(
|
|
5655
|
+
// // 'formattedUserDataMessage->isWsFormattedSpotOutboundAccountPosition: ',
|
|
5656
|
+
// // data,
|
|
5657
|
+
// // );
|
|
5658
|
+
// }
|
|
5659
|
+
// if (isWsFormattedSpotBalanceUpdate(data)) {
|
|
5660
|
+
// return console.log(
|
|
5661
|
+
// 'formattedUserDataMessage->isWsFormattedSpotBalanceUpdate: ',
|
|
5662
|
+
// data,
|
|
5663
|
+
// );
|
|
5664
|
+
// }
|
|
5665
|
+
// console.log('formattedUserDataMessage: ', data);
|
|
5666
|
+
// });
|
|
5854
5667
|
⋮----
|
|
5855
|
-
//
|
|
5856
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#average-price
|
|
5668
|
+
// console.log('ws response: ', JSON.stringify(data));
|
|
5857
5669
|
⋮----
|
|
5858
|
-
|
|
5859
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams
|
|
5670
|
+
async function main()
|
|
5860
5671
|
⋮----
|
|
5861
|
-
//
|
|
5862
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream
|
|
5672
|
+
// Enforce testnet ws connections, regardless of supplied wsKey:
|
|
5863
5673
|
⋮----
|
|
5864
|
-
//
|
|
5865
|
-
//
|
|
5866
|
-
//
|
|
5867
|
-
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams
|
|
5674
|
+
// Note: unless you set this to false, the SDK will automatically call
|
|
5675
|
+
// the `subscribeUserDataStream()` method again if reconnected (if you called it before):
|
|
5676
|
+
// resubscribeUserDataStreamAfterReconnect: true,
|
|
5868
5677
|
⋮----
|
|
5869
|
-
//
|
|
5870
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams
|
|
5678
|
+
// If you want your own event handlers instead of the default ones with logs, disable this setting and see the `attachEventHandlers` example below:
|
|
5871
5679
|
⋮----
|
|
5872
|
-
//
|
|
5873
|
-
//
|
|
5680
|
+
// Attach your own event handlers to process incoming events
|
|
5681
|
+
// You may want to disable the default ones to avoid unnecessary logs (via attachEventListeners:false, above)
|
|
5874
5682
|
⋮----
|
|
5875
|
-
//
|
|
5876
|
-
//
|
|
5683
|
+
// Optional, if you see RECV Window errors, you can use this to manage time issues.
|
|
5684
|
+
// ! However, make sure you sync your system clock first!
|
|
5685
|
+
// https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow
|
|
5686
|
+
// wsClient.setTimeOffsetMs(-5000);
|
|
5877
5687
|
⋮----
|
|
5878
|
-
//
|
|
5879
|
-
//
|
|
5880
|
-
// 'btcusdt@kline_1m',
|
|
5881
|
-
// Continuous Contract Kline/Candlestick Streams
|
|
5882
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Continuous-Contract-Kline-Candlestick-Streams
|
|
5883
|
-
// 'btcusdt_perpetual@continuousKline_1m', // DOESNT EXIST AS TYPE GUARD, ONLY IN BEAUTIFIER
|
|
5884
|
-
// Individual Symbol Mini Ticker Stream
|
|
5885
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Mini-Ticker-Stream
|
|
5886
|
-
// 'btcusdt@miniTicker', // DOESNT EXIST AS TYPE GUARD, ONLY FOR RAW MESSAGE
|
|
5887
|
-
// All Market Mini Tickers Stream
|
|
5888
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
|
|
5889
|
-
// '!miniTicker@arr', // DOESNT EXIST AS TYPE GUARD
|
|
5890
|
-
// Individual Symbol Ticker Streams
|
|
5891
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
|
|
5892
|
-
//'btcusdt@ticker',
|
|
5893
|
-
// All Market Tickers Stream
|
|
5894
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Tickers-Stream
|
|
5895
|
-
// '!ticker@arr', // DOESNT EXIST AS TYPE GUARD
|
|
5896
|
-
// Individual Symbol Book Ticker Streams
|
|
5897
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Book-Ticker-Streams
|
|
5898
|
-
//'btcusdt@bookTicker', // DOESNT EXIST AS TYPE GUARD
|
|
5899
|
-
// All Book Tickers Stream
|
|
5900
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Book-Tickers-Stream
|
|
5901
|
-
// '!bookTicker', // DOESNT EXIST AS TYPE GUARD
|
|
5902
|
-
// Liquidation Order Stream
|
|
5903
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Liquidation-Order-Streams
|
|
5904
|
-
// 'btcusdt@forceOrder',
|
|
5905
|
-
// Liquidation Order Stream for All market
|
|
5906
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Liquidation-Order-Streams
|
|
5907
|
-
//'!forceOrder@arr',
|
|
5908
|
-
// Partial Book Depth Streams
|
|
5909
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
|
|
5910
|
-
//'btcusdt@depth5',
|
|
5911
|
-
// 'btcusdt@depth10@100ms'
|
|
5912
|
-
// Diff. Book Depth Stream
|
|
5913
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
|
|
5688
|
+
// Note: unless you set resubscribeUserDataStreamAfterReconnect to false, the SDK will
|
|
5689
|
+
// automatically call this method again if reconnected,
|
|
5914
5690
|
⋮----
|
|
5915
|
-
//
|
|
5916
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Composite-Index-Symbol-Information-Streams
|
|
5917
|
-
// 'btcusdt@compositeIndex' // DOESNT EXIST AS TYPE GUARD
|
|
5918
|
-
// Contract Info Stream
|
|
5919
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Contract-Info-Stream
|
|
5920
|
-
// '!contractInfo' // DOESNT EXIST AS TYPE GUARD
|
|
5921
|
-
// Multi-Assets Mode Asset Index Stream
|
|
5922
|
-
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Multi-Assets-Mode-Asset-Index
|
|
5923
|
-
// '!assetIndex@arr' // DOESNT EXIST AS TYPE GUARD
|
|
5924
|
-
// 'btcusdt@assetIndex'
|
|
5691
|
+
WS_KEY_MAP.mainWSAPI, // The `mainWSAPI` wsKey will connect to the "spot" Websocket API on Binance.
|
|
5925
5692
|
⋮----
|
|
5926
|
-
|
|
5927
|
-
* Subscribe to each available european options market data websocket topic, the new way:
|
|
5928
|
-
*
|
|
5929
|
-
* https://developers.binance.com/docs/derivatives/option/websocket-market-streams/New-Symbol-Info
|
|
5930
|
-
*
|
|
5931
|
-
* https://eapi.binance.com/eapi/v1/exchangeInfo
|
|
5932
|
-
*/
|
|
5933
|
-
⋮----
|
|
5934
|
-
// /**
|
|
5935
|
-
// *
|
|
5936
|
-
// * For those that used the Node.js Binance SDK before the v3 release, you can
|
|
5937
|
-
// * still subscribe to available market topics the "old" way, for convenience
|
|
5938
|
-
// * when migrating from the old WebsocketClient to the new multiplex client):
|
|
5939
|
-
// *
|
|
5940
|
-
// */
|
|
5941
|
-
⋮----
|
|
5942
|
-
// wsClient.subscribeAggregateTrades(symbol, 'usdm');
|
|
5943
|
-
// wsClient.subscribeTrades(symbol, 'spot');
|
|
5944
|
-
// wsClient.subscribeTrades(symbol, 'usdm');
|
|
5945
|
-
// wsClient.subscribeTrades(coinMSymbol, 'coinm');
|
|
5946
|
-
// wsClient.subscribeCoinIndexPrice(coinMSymbol2);
|
|
5947
|
-
// wsClient.subscribeAllBookTickers('usdm');
|
|
5948
|
-
// wsClient.subscribeSpotKline(symbol, '1m');
|
|
5949
|
-
// wsClient.subscribeMarkPrice(symbol, 'usdm');
|
|
5950
|
-
// wsClient.subscribeMarkPrice(coinMSymbol, 'coinm');
|
|
5951
|
-
// wsClient.subscribeAllMarketMarkPrice('usdm');
|
|
5952
|
-
// wsClient.subscribeAllMarketMarkPrice('coinm');
|
|
5953
|
-
// wsClient.subscribeKlines(symbol, '1m', 'usdm');
|
|
5954
|
-
// wsClient.subscribeContinuousContractKlines(
|
|
5955
|
-
// symbol,
|
|
5956
|
-
// 'perpetual',
|
|
5957
|
-
// '1m',
|
|
5958
|
-
// 'usdm',
|
|
5959
|
-
// );
|
|
5960
|
-
// wsClient.subscribeIndexKlines(coinMSymbol2, '1m');
|
|
5961
|
-
// wsClient.subscribeMarkPriceKlines(coinMSymbol, '1m');
|
|
5962
|
-
// wsClient.subscribeSymbolMini24hrTicker(symbol, 'spot'); // 0116 265 5309, opt 1
|
|
5963
|
-
// wsClient.subscribeSymbolMini24hrTicker(symbol, 'usdm');
|
|
5964
|
-
// wsClient.subscribeSymbolMini24hrTicker(coinMSymbol, 'coinm');
|
|
5965
|
-
// wsClient.subscribeSymbol24hrTicker(symbol, 'spot');
|
|
5966
|
-
// wsClient.subscribeSymbol24hrTicker(symbol, 'usdm');
|
|
5967
|
-
// wsClient.subscribeSymbol24hrTicker(coinMSymbol, 'coinm');
|
|
5968
|
-
// wsClient.subscribeAllMini24hrTickers('spot');
|
|
5969
|
-
// wsClient.subscribeAllMini24hrTickers('usdm');
|
|
5970
|
-
// wsClient.subscribeAllMini24hrTickers('coinm');
|
|
5971
|
-
// wsClient.subscribeAll24hrTickers('spot');
|
|
5972
|
-
// wsClient.subscribeAll24hrTickers('usdm');
|
|
5973
|
-
// wsClient.subscribeAll24hrTickers('coinm');
|
|
5974
|
-
// wsClient.subscribeSymbolLiquidationOrders(symbol, 'usdm');
|
|
5975
|
-
// wsClient.subscribeAllLiquidationOrders('usdm');
|
|
5976
|
-
// wsClient.subscribeAllLiquidationOrders('coinm');
|
|
5977
|
-
// wsClient.subscribeSpotSymbol24hrTicker(symbol);
|
|
5978
|
-
// wsClient.subscribeSpotPartialBookDepth('ETHBTC', 5, 1000);
|
|
5979
|
-
// wsClient.subscribeAllRollingWindowTickers('spot', '1d');
|
|
5980
|
-
// wsClient.subscribeSymbolBookTicker(symbol, 'spot');
|
|
5981
|
-
// wsClient.subscribePartialBookDepths(symbol, 5, 100, 'spot');
|
|
5982
|
-
// wsClient.subscribeDiffBookDepth(symbol, 100, 'spot');
|
|
5983
|
-
// wsClient.subscribeContractInfoStream('usdm');
|
|
5984
|
-
// wsClient.subscribeContractInfoStream('coinm');
|
|
5693
|
+
// Start executing the example workflow
|
|
5985
5694
|
|
|
5986
5695
|
================
|
|
5987
|
-
File: examples/
|
|
5696
|
+
File: examples/README.md
|
|
5988
5697
|
================
|
|
5989
|
-
|
|
5990
|
-
DefaultLogger,
|
|
5991
|
-
isWsFormattedFuturesUserDataEvent,
|
|
5992
|
-
isWsFormattedSpotUserDataEvent,
|
|
5993
|
-
isWsFormattedSpotUserDataExecutionReport,
|
|
5994
|
-
isWsFormattedUserDataEvent,
|
|
5995
|
-
WebsocketClient,
|
|
5996
|
-
WsUserDataEvents,
|
|
5997
|
-
} from '../../src/index';
|
|
5998
|
-
⋮----
|
|
5999
|
-
// or
|
|
6000
|
-
// import { DefaultLogger, WebsocketClient } from 'binance';
|
|
6001
|
-
⋮----
|
|
6002
|
-
// console.log('using api credentials: ', { key, secret });
|
|
6003
|
-
⋮----
|
|
6004
|
-
// Optional, hook and customise logging behavior
|
|
6005
|
-
⋮----
|
|
6006
|
-
// If you prefer, you can receive raw unprocessed data without the "beautifier":
|
|
6007
|
-
// wsClient.on('message', (data) => {
|
|
6008
|
-
// console.log('raw message received ', JSON.stringify(data, null, 2));
|
|
6009
|
-
// });
|
|
6010
|
-
⋮----
|
|
6011
|
-
function onUserDataEvent(data: WsUserDataEvents)
|
|
6012
|
-
⋮----
|
|
6013
|
-
// the market denotes which API category it came from
|
|
6014
|
-
// if (data.wsMarket.includes('spot')) {
|
|
6015
|
-
⋮----
|
|
6016
|
-
// or use a type guard, if one exists (PRs welcome)
|
|
6017
|
-
⋮----
|
|
6018
|
-
// Beautified/formatted events from binance:
|
|
6019
|
-
⋮----
|
|
6020
|
-
// The wsKey can be parsed to determine the type of message (what websocket it came from)
|
|
6021
|
-
// if (!Array.isArray(data) && data.wsKey.includes('userData')) {
|
|
6022
|
-
// return onUserDataEvent(data);
|
|
6023
|
-
// }
|
|
6024
|
-
⋮----
|
|
6025
|
-
// or use a type guard if available
|
|
6026
|
-
⋮----
|
|
6027
|
-
// response to command sent via WS stream (e.g LIST_SUBSCRIPTIONS)
|
|
6028
|
-
⋮----
|
|
6029
|
-
// This is a good place to check your own state is still in sync with the account state on the exchange, in case any events were missed while the library was reconnecting:
|
|
6030
|
-
// - fetch balances
|
|
6031
|
-
// - fetch positions
|
|
6032
|
-
// - fetch orders
|
|
6033
|
-
⋮----
|
|
6034
|
-
// wsClient.subscribeMarginUserDataStream();
|
|
6035
|
-
// wsClient.subscribeIsolatedMarginUserDataStream('BTCUSDT');
|
|
6036
|
-
// wsClient.subscribeUsdFuturesUserDataStream();
|
|
6037
|
-
⋮----
|
|
6038
|
-
// setTimeout(() => {
|
|
6039
|
-
// console.log('killing all connections');
|
|
6040
|
-
// wsClient.closeAll();
|
|
6041
|
-
// }, 1000 * 15);
|
|
5698
|
+
# Binance API - Examples
|
|
6042
5699
|
|
|
6043
|
-
|
|
6044
|
-
|
|
6045
|
-
|
|
6046
|
-
|
|
6047
|
-
|
|
6048
|
-
|
|
6049
|
-
|
|
6050
|
-
|
|
6051
|
-
|
|
6052
|
-
|
|
6053
|
-
|
|
6054
|
-
|
|
6055
|
-
|
|
6056
|
-
|
|
6057
|
-
|
|
6058
|
-
|
|
6059
|
-
|
|
6060
|
-
|
|
6061
|
-
|
|
6062
|
-
|
|
6063
|
-
|
|
6064
|
-
|
|
6065
|
-
|
|
6066
|
-
|
|
6067
|
-
|
|
6068
|
-
|
|
6069
|
-
|
|
6070
|
-
|
|
6071
|
-
|
|
6072
|
-
|
|
6073
|
-
|
|
6074
|
-
|
|
6075
|
-
|
|
6076
|
-
|
|
6077
|
-
|
|
6078
|
-
|
|
6079
|
-
|
|
6080
|
-
|
|
6081
|
-
|
|
6082
|
-
|
|
6083
|
-
|
|
6084
|
-
|
|
6085
|
-
|
|
6086
|
-
|
|
6087
|
-
|
|
6088
|
-
|
|
6089
|
-
|
|
6090
|
-
|
|
6091
|
-
|
|
6092
|
-
|
|
6093
|
-
|
|
6094
|
-
|
|
6095
|
-
|
|
6096
|
-
|
|
6097
|
-
|
|
6098
|
-
|
|
6099
|
-
|
|
6100
|
-
|
|
6101
|
-
|
|
6102
|
-
|
|
6103
|
-
|
|
6104
|
-
|
|
6105
|
-
|
|
6106
|
-
|
|
6107
|
-
|
|
6108
|
-
|
|
6109
|
-
|
|
6110
|
-
|
|
6111
|
-
|
|
6112
|
-
|
|
6113
|
-
|
|
6114
|
-
|
|
6115
|
-
|
|
6116
|
-
|
|
6117
|
-
|
|
6118
|
-
|
|
6119
|
-
|
|
6120
|
-
|
|
6121
|
-
|
|
6122
|
-
|
|
6123
|
-
|
|
6124
|
-
|
|
6125
|
-
|
|
6126
|
-
|
|
6127
|
-
|
|
6128
|
-
|
|
6129
|
-
|
|
6130
|
-
|
|
6131
|
-
|
|
6132
|
-
|
|
6133
|
-
|
|
6134
|
-
|
|
6135
|
-
|
|
6136
|
-
// ! However, make sure you sync your system clock first!
|
|
6137
|
-
// https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow
|
|
6138
|
-
// wsClient.setTimeOffsetMs(-5000);
|
|
6139
|
-
⋮----
|
|
6140
|
-
// Note: unless you set resubscribeUserDataStreamAfterReconnect to false, the SDK will
|
|
6141
|
-
// automatically call this method again if reconnected,
|
|
6142
|
-
⋮----
|
|
6143
|
-
WS_KEY_MAP.mainWSAPI, // The `mainWSAPI` wsKey will connect to the "spot" Websocket API on Binance.
|
|
6144
|
-
⋮----
|
|
6145
|
-
// Start executing the example workflow
|
|
5700
|
+
This folder contains ready to go examples demonstrating various aspects of this API implementation, written in TypeScript (but they are compatible with pure JavaScript projects).
|
|
5701
|
+
|
|
5702
|
+
Found something difficult to implement? Contribute to these examples and help others!
|
|
5703
|
+
|
|
5704
|
+
## Getting started
|
|
5705
|
+
|
|
5706
|
+
- Clone the project (or download it as a zip, or install the module in your own project `npm install binance`).
|
|
5707
|
+
- Edit the sample as needed (some samples require edits, e.g API keys or import statements to import from npm, not src).
|
|
5708
|
+
- Execute the sample using tsx: `tsx examples/REST/rest-spot-public.ts`.
|
|
5709
|
+
|
|
5710
|
+
Samples that refer to API credentials using `process.env.API_KEY_COM` can be spawned with environment variables. Unix/macOS example:
|
|
5711
|
+
```
|
|
5712
|
+
APIKEY='apikeypastedhere' APISECRET='apisecretpastedhere' tsx examples/WebSockets/ws-userdata-listenkey.ts
|
|
5713
|
+
```
|
|
5714
|
+
|
|
5715
|
+
Or edit the example directly to hardcode your API keys.
|
|
5716
|
+
|
|
5717
|
+
### WebSockets
|
|
5718
|
+
|
|
5719
|
+
All examples relating to WebSockets can be found in the [examples/WebSockets](./WebSockets/) folder. High level summary of available examples:
|
|
5720
|
+
|
|
5721
|
+
#### Consumers
|
|
5722
|
+
|
|
5723
|
+
These are purely for receiving data from Binance's WebSockets (market data, account updates, etc).
|
|
5724
|
+
|
|
5725
|
+
##### Market Data
|
|
5726
|
+
|
|
5727
|
+
These examples demonstrate subscribing to & receiving market data from Binance's WebSockets:
|
|
5728
|
+
|
|
5729
|
+
- ws-public.ts
|
|
5730
|
+
- Demonstration on general usage of the WebSocket client to subscribe to / unsubscribe from one or more market data topics.
|
|
5731
|
+
- ws-public-spot-orderbook.ts
|
|
5732
|
+
- Subscribing to orderbook events for multiple symbols in spot markets.
|
|
5733
|
+
- ws-public-spot-trades.ts
|
|
5734
|
+
- Subscribing to raw trades for multiple symbols in spot markets.
|
|
5735
|
+
- ws-unsubscribe.ts
|
|
5736
|
+
- Subscribing to a list of topics, and then unsubscribing from a few topics in that list.
|
|
5737
|
+
- ws-public-usdm-funding.ts
|
|
5738
|
+
- Simple example subscribing to a general topic, and how to process incoming events to only extract funding rates from those events.
|
|
5739
|
+
|
|
5740
|
+
##### Account Data
|
|
5741
|
+
|
|
5742
|
+
These examples demonstrate receiving account update events from Binance's WebSockets:
|
|
5743
|
+
|
|
5744
|
+
- ws-userdata-listenkey.ts
|
|
5745
|
+
- Demonstration on subscribing to various user data streams (spot, margin, futures),
|
|
5746
|
+
- Handling incoming user data events
|
|
5747
|
+
- Using provided type guards to determine which product group the user data event is for (spot, margin, futures, etc).
|
|
5748
|
+
- ws-userdata-listenKey-testnet.ts
|
|
5749
|
+
- Similar to above, but on testnet.
|
|
5750
|
+
- ws-userdata-connection-safety.ts
|
|
5751
|
+
- Demonstration on extra safety around the first user data stream connection.
|
|
5752
|
+
- Note: this is overkill in most situations...
|
|
5753
|
+
|
|
5754
|
+
##### WebSocket API
|
|
5755
|
+
|
|
5756
|
+
These examples demonstrate how to send commands using Binance's WebSocket API (e.g. submitting orders). Very similar to the REST API, but using a persisted WebSocket connection instead of HTTP requests.
|
|
5757
|
+
|
|
5758
|
+
- ws-api-client.ts
|
|
5759
|
+
- Demonstration of using Binance's WebSocket API in Node.js/JavaScript/TypeScript, using the WebsocketAPIClient.
|
|
5760
|
+
- This WebsocketAPIClient is very similar to a REST client, with one method per available command (endpoint) and fully typed requests & responses.
|
|
5761
|
+
- Routing is automatically handled via the WebsocketClient, including authentication and connection persistence. Just call the functions you need - the SDK does the rest.
|
|
5762
|
+
- From a usage perspective, it feels like a REST API - you can await responses just like a HTTP request.
|
|
5763
|
+
- ws-api-raw-promises.ts
|
|
5764
|
+
- More verbose usage of the WebSocket API using the `sendWSAPIRequest()` method.
|
|
5765
|
+
- The `WebsocketAPIClient` uses this method too, so in most cases it is simple to just use the `WebsocketAPIClient` instead.
|
|
5766
|
+
- ws-userdata-wsapi.ts
|
|
5767
|
+
- The listenKey workflow for the user data stream is deprecated (in spot markets).
|
|
5768
|
+
- This example demonstrates how to subscribe to the user data stream in spot markets, without a listen key, using the WebSocket API.
|
|
5769
|
+
|
|
5770
|
+
##### Misc Workflows
|
|
5771
|
+
|
|
5772
|
+
These are miscellaneous examples that cover one or more of the above categories:
|
|
5773
|
+
|
|
5774
|
+
- ws-close.ts
|
|
5775
|
+
- Closing the (old listen-key driven) user data stream.
|
|
5776
|
+
- Unsubscribing from various topics.
|
|
5777
|
+
- ws-proxy-socks.ts
|
|
5778
|
+
- Using WebSockets over a SOCKS proxy.
|
|
5779
|
+
- deprecated-ws-public.ts
|
|
5780
|
+
|
|
5781
|
+
|
|
5782
|
+
### REST APIs
|
|
5783
|
+
|
|
5784
|
+
All examples relating to REST APIs can be found in the [examples/REST](./REST/) folder. Most examples are named around functionality & product group. Any examples with "private" involve API calls relating to your account (such as changing settings or submitting orders, etc),
|
|
5785
|
+
|
|
5786
|
+
High level summary for some of the available examples, but check the folder for a complete list:
|
|
5787
|
+
|
|
5788
|
+
#### REST USDM Examples
|
|
5789
|
+
|
|
5790
|
+
- `rest-future-bracket-order.ts` Creates three order, entry, TP, SL and submit them all at once using `submitMultipleOrders`
|
|
5791
|
+
- `rest-usdm-order.ts` Creates single entry, using `submitNewOrder`
|
|
5792
|
+
- `rest-usdm-order-sl.ts` Modify current Stop Loss order(HedgeMode only)
|
|
6146
5793
|
|
|
6147
5794
|
================
|
|
6148
5795
|
File: src/types/websockets/ws-api-responses.ts
|
|
@@ -7840,225 +7487,455 @@ export interface WsMessageFuturesUserDataOrderTradeUpdateEventRaw
|
|
|
7840
7487
|
};
|
|
7841
7488
|
}
|
|
7842
7489
|
⋮----
|
|
7843
|
-
pP: boolean; // ignore
|
|
7844
|
-
si: numberInString; // ignore
|
|
7845
|
-
ss: numberInString; // ignore
|
|
7490
|
+
pP: boolean; // ignore
|
|
7491
|
+
si: numberInString; // ignore
|
|
7492
|
+
ss: numberInString; // ignore
|
|
7493
|
+
⋮----
|
|
7494
|
+
export interface WsMessageFuturesUserDataTradeLiteEventRaw
|
|
7495
|
+
extends WsSharedBase {
|
|
7496
|
+
e: 'TRADE_LITE'; // Event Type
|
|
7497
|
+
E: number; // Event Time
|
|
7498
|
+
T: number; // Transaction Time
|
|
7499
|
+
s: string; // Symbol
|
|
7500
|
+
q: string; // Original Quantity
|
|
7501
|
+
p: string; // Original Price
|
|
7502
|
+
m: boolean; // Is this trade the maker side?
|
|
7503
|
+
c: string; // Client Order Id
|
|
7504
|
+
S: 'BUY' | 'SELL'; // Side
|
|
7505
|
+
L: string; // Last Filled Price
|
|
7506
|
+
l: string; // Order Last Filled Quantity
|
|
7507
|
+
t: number; // Trade Id
|
|
7508
|
+
i: number; // Order Id
|
|
7509
|
+
}
|
|
7510
|
+
⋮----
|
|
7511
|
+
e: 'TRADE_LITE'; // Event Type
|
|
7512
|
+
E: number; // Event Time
|
|
7513
|
+
T: number; // Transaction Time
|
|
7514
|
+
s: string; // Symbol
|
|
7515
|
+
q: string; // Original Quantity
|
|
7516
|
+
p: string; // Original Price
|
|
7517
|
+
m: boolean; // Is this trade the maker side?
|
|
7518
|
+
c: string; // Client Order Id
|
|
7519
|
+
S: 'BUY' | 'SELL'; // Side
|
|
7520
|
+
L: string; // Last Filled Price
|
|
7521
|
+
l: string; // Order Last Filled Quantity
|
|
7522
|
+
t: number; // Trade Id
|
|
7523
|
+
i: number; // Order Id
|
|
7524
|
+
⋮----
|
|
7525
|
+
export interface WsMessageFuturesUserDataAccountConfigUpdateEventRaw
|
|
7526
|
+
extends WsSharedBase {
|
|
7527
|
+
e: 'ACCOUNT_CONFIG_UPDATE';
|
|
7528
|
+
E: number;
|
|
7529
|
+
T: number;
|
|
7530
|
+
ac?: {
|
|
7531
|
+
s: string;
|
|
7532
|
+
l: number;
|
|
7533
|
+
};
|
|
7534
|
+
ai?: {
|
|
7535
|
+
j: boolean;
|
|
7536
|
+
};
|
|
7537
|
+
}
|
|
7538
|
+
⋮----
|
|
7539
|
+
export interface WsMessageIndexPriceUpdateEventRaw extends WsSharedBase {
|
|
7540
|
+
e: 'indexPriceUpdate';
|
|
7541
|
+
E: number;
|
|
7542
|
+
i: string;
|
|
7543
|
+
p: numberInString;
|
|
7544
|
+
}
|
|
7545
|
+
⋮----
|
|
7546
|
+
export interface WsMessageMarkPriceUpdateEventRaw extends WsSharedBase {
|
|
7547
|
+
e: 'markPriceUpdate';
|
|
7548
|
+
E: number;
|
|
7549
|
+
s: string;
|
|
7550
|
+
p: string;
|
|
7551
|
+
P: string;
|
|
7552
|
+
i: string;
|
|
7553
|
+
r: string;
|
|
7554
|
+
T: number;
|
|
7555
|
+
}
|
|
7556
|
+
⋮----
|
|
7557
|
+
export interface WsMessageForceOrderRaw extends WsSharedBase {
|
|
7558
|
+
e: 'forceOrder';
|
|
7559
|
+
E: number;
|
|
7560
|
+
o: {
|
|
7561
|
+
s: string;
|
|
7562
|
+
S: string;
|
|
7563
|
+
o: string;
|
|
7564
|
+
f: string;
|
|
7565
|
+
q: string;
|
|
7566
|
+
p: string;
|
|
7567
|
+
ap: string;
|
|
7568
|
+
X: string;
|
|
7569
|
+
l: string;
|
|
7570
|
+
z: string;
|
|
7571
|
+
T: number;
|
|
7572
|
+
};
|
|
7573
|
+
}
|
|
7574
|
+
⋮----
|
|
7575
|
+
export interface WsMessageFuturesUserDataStrategyUpdateRaw
|
|
7576
|
+
extends WsSharedBase {
|
|
7577
|
+
e: 'STRATEGY_UPDATE'; // Event Type
|
|
7578
|
+
T: number; // Transaction Time
|
|
7579
|
+
E: number; // Event Time
|
|
7580
|
+
su: {
|
|
7581
|
+
si: number; // Strategy ID
|
|
7582
|
+
st: string; // Strategy Type
|
|
7583
|
+
ss: string; // Strategy Status
|
|
7584
|
+
s: string; // Symbol
|
|
7585
|
+
ut: number; // Update Time
|
|
7586
|
+
c: number; // opCode
|
|
7587
|
+
};
|
|
7588
|
+
}
|
|
7589
|
+
⋮----
|
|
7590
|
+
e: 'STRATEGY_UPDATE'; // Event Type
|
|
7591
|
+
T: number; // Transaction Time
|
|
7592
|
+
E: number; // Event Time
|
|
7593
|
+
⋮----
|
|
7594
|
+
si: number; // Strategy ID
|
|
7595
|
+
st: string; // Strategy Type
|
|
7596
|
+
ss: string; // Strategy Status
|
|
7597
|
+
s: string; // Symbol
|
|
7598
|
+
ut: number; // Update Time
|
|
7599
|
+
c: number; // opCode
|
|
7600
|
+
⋮----
|
|
7601
|
+
export interface WsMessageFuturesUserDataGridUpdateRaw extends WsSharedBase {
|
|
7602
|
+
e: 'GRID_UPDATE'; // Event Type
|
|
7603
|
+
T: number; // Transaction Time
|
|
7604
|
+
E: number; // Event Time
|
|
7605
|
+
gu: {
|
|
7606
|
+
si: number; // Strategy ID
|
|
7607
|
+
st: string; // Strategy Type
|
|
7608
|
+
ss: string; // Strategy Status
|
|
7609
|
+
s: string; // Symbol
|
|
7610
|
+
r: numberInString; // Realized PNL
|
|
7611
|
+
up: numberInString; // Unmatched Average Price
|
|
7612
|
+
uq: numberInString; // Unmatched Qty
|
|
7613
|
+
uf: numberInString; // Unmatched Fee
|
|
7614
|
+
mp: numberInString; // Matched PNL
|
|
7615
|
+
ut: number; // Update Time
|
|
7616
|
+
};
|
|
7617
|
+
}
|
|
7618
|
+
⋮----
|
|
7619
|
+
e: 'GRID_UPDATE'; // Event Type
|
|
7620
|
+
T: number; // Transaction Time
|
|
7621
|
+
E: number; // Event Time
|
|
7622
|
+
⋮----
|
|
7623
|
+
si: number; // Strategy ID
|
|
7624
|
+
st: string; // Strategy Type
|
|
7625
|
+
ss: string; // Strategy Status
|
|
7626
|
+
s: string; // Symbol
|
|
7627
|
+
r: numberInString; // Realized PNL
|
|
7628
|
+
up: numberInString; // Unmatched Average Price
|
|
7629
|
+
uq: numberInString; // Unmatched Qty
|
|
7630
|
+
uf: numberInString; // Unmatched Fee
|
|
7631
|
+
mp: numberInString; // Matched PNL
|
|
7632
|
+
ut: number; // Update Time
|
|
7633
|
+
⋮----
|
|
7634
|
+
export interface WsMessageFuturesUserDataContractInfoRaw extends WsSharedBase {
|
|
7635
|
+
e: 'contractInfo'; // Event Type
|
|
7636
|
+
E: number; // Event Time
|
|
7637
|
+
s: string; // Symbol
|
|
7638
|
+
ps: string; // Pair
|
|
7639
|
+
ct: string; // Contract type
|
|
7640
|
+
dt: number; // Delivery date time
|
|
7641
|
+
ot: number; // onboard date time
|
|
7642
|
+
cs: string; // Contract status
|
|
7643
|
+
bks: {
|
|
7644
|
+
bs: number; // Notional bracket
|
|
7645
|
+
bnf: number; // Floor notional of this bracket
|
|
7646
|
+
bnc: number; // Cap notional of this bracket
|
|
7647
|
+
mmr: number; // Maintenance ratio for this bracket
|
|
7648
|
+
cf: number; // Auxiliary number for quick calculation
|
|
7649
|
+
mi: number; // Min leverage for this bracket
|
|
7650
|
+
ma: number; // Max leverage for this bracket
|
|
7651
|
+
}[];
|
|
7652
|
+
}
|
|
7653
|
+
⋮----
|
|
7654
|
+
e: 'contractInfo'; // Event Type
|
|
7655
|
+
E: number; // Event Time
|
|
7656
|
+
s: string; // Symbol
|
|
7657
|
+
ps: string; // Pair
|
|
7658
|
+
ct: string; // Contract type
|
|
7659
|
+
dt: number; // Delivery date time
|
|
7660
|
+
ot: number; // onboard date time
|
|
7661
|
+
cs: string; // Contract status
|
|
7662
|
+
⋮----
|
|
7663
|
+
bs: number; // Notional bracket
|
|
7664
|
+
bnf: number; // Floor notional of this bracket
|
|
7665
|
+
bnc: number; // Cap notional of this bracket
|
|
7666
|
+
mmr: number; // Maintenance ratio for this bracket
|
|
7667
|
+
cf: number; // Auxiliary number for quick calculation
|
|
7668
|
+
mi: number; // Min leverage for this bracket
|
|
7669
|
+
ma: number; // Max leverage for this bracket
|
|
7670
|
+
⋮----
|
|
7671
|
+
export type WsRawSpotUserDataEventRaw =
|
|
7672
|
+
| WsMessageSpotUserDataExecutionReportEventRaw
|
|
7673
|
+
| WsMessageSpotOutboundAccountPositionRaw
|
|
7674
|
+
| WsMessageSpotBalanceUpdateRaw
|
|
7675
|
+
| WsMessageSpotUserDataListStatusEventRaw;
|
|
7676
|
+
⋮----
|
|
7677
|
+
export type WsMessageFuturesUserDataEventRaw =
|
|
7678
|
+
| WsMessageFuturesUserDataAccountUpdateRaw
|
|
7679
|
+
| WsMessageFuturesUserDataListenKeyExpiredRaw
|
|
7680
|
+
| WsMessageFuturesUserDataMarginCallRaw
|
|
7681
|
+
| WsMessageFuturesUserDataOrderTradeUpdateEventRaw
|
|
7682
|
+
| WsMessageFuturesUserDataAccountConfigUpdateEventRaw
|
|
7683
|
+
| WsMessageFuturesUserDataCondOrderTriggerRejectEventRaw
|
|
7684
|
+
| WsMessageFuturesUserDataTradeLiteEventRaw
|
|
7685
|
+
| WsMessageFuturesUserDataStrategyUpdateRaw
|
|
7686
|
+
| WsMessageFuturesUserDataGridUpdateRaw
|
|
7687
|
+
| WsMessageFuturesUserDataContractInfoRaw;
|
|
7688
|
+
⋮----
|
|
7689
|
+
export type WsRawMessage =
|
|
7690
|
+
| WsMessageKlineRaw
|
|
7691
|
+
| WsMessageAggTradeRaw
|
|
7692
|
+
| WsMessageTradeRaw
|
|
7693
|
+
| WsMessage24hrMiniTickerRaw
|
|
7694
|
+
| WsMessage24hrMiniTickerRaw[]
|
|
7695
|
+
| WsMessage24hrTickerRaw
|
|
7696
|
+
| WsMessage24hrTickerRaw[]
|
|
7697
|
+
| WsMessageRollingWindowTickerRaw[]
|
|
7698
|
+
| WsMessageBookTickerEventRaw
|
|
7699
|
+
| WsMessagePartialBookDepthEventRaw
|
|
7700
|
+
| WsMessageForceOrderRaw
|
|
7701
|
+
| WsRawSpotUserDataEventRaw
|
|
7702
|
+
| WsMessageIndexPriceUpdateEventRaw
|
|
7703
|
+
| WsMessageFuturesUserDataAccountUpdateRaw
|
|
7704
|
+
| WsMessageFuturesUserDataListenKeyExpiredRaw
|
|
7705
|
+
| WsMessageFuturesUserDataMarginCallRaw
|
|
7706
|
+
| WsMessageFuturesUserDataOrderTradeUpdateEventRaw
|
|
7707
|
+
| WsMessageFuturesUserDataAccountConfigUpdateEventRaw
|
|
7708
|
+
| WsMessageFuturesUserDataCondOrderTriggerRejectEventRaw;
|
|
7709
|
+
|
|
7710
|
+
================
|
|
7711
|
+
File: src/types/websockets/ws-general.ts
|
|
7712
|
+
================
|
|
7713
|
+
import { AxiosRequestConfig } from 'axios';
|
|
7714
|
+
import WebSocket from 'isomorphic-ws';
|
|
7715
|
+
⋮----
|
|
7716
|
+
import { RestClientOptions } from '../../util/requestUtils';
|
|
7717
|
+
import { WsKey } from '../../util/websockets/websocket-util';
|
|
7718
|
+
⋮----
|
|
7719
|
+
export interface MessageEventLike {
|
|
7720
|
+
target: WebSocket;
|
|
7721
|
+
type: 'message';
|
|
7722
|
+
data: string;
|
|
7723
|
+
}
|
|
7724
|
+
⋮----
|
|
7725
|
+
export function isMessageEvent(msg: unknown): msg is MessageEventLike
|
|
7846
7726
|
⋮----
|
|
7847
|
-
export
|
|
7848
|
-
|
|
7849
|
-
|
|
7850
|
-
|
|
7851
|
-
|
|
7852
|
-
|
|
7853
|
-
|
|
7854
|
-
|
|
7855
|
-
|
|
7856
|
-
|
|
7857
|
-
|
|
7858
|
-
|
|
7859
|
-
|
|
7860
|
-
t: number; // Trade Id
|
|
7861
|
-
i: number; // Order Id
|
|
7862
|
-
}
|
|
7727
|
+
export type WsMarket =
|
|
7728
|
+
| 'spot'
|
|
7729
|
+
| 'spotTestnet'
|
|
7730
|
+
| 'crossMargin'
|
|
7731
|
+
| 'isolatedMargin'
|
|
7732
|
+
| 'riskDataMargin'
|
|
7733
|
+
| 'usdm'
|
|
7734
|
+
| 'usdmTestnet'
|
|
7735
|
+
| 'coinm'
|
|
7736
|
+
| 'coinmTestnet'
|
|
7737
|
+
| 'options'
|
|
7738
|
+
| 'optionsTestnet'
|
|
7739
|
+
| 'portfoliom';
|
|
7863
7740
|
⋮----
|
|
7864
|
-
|
|
7865
|
-
|
|
7866
|
-
|
|
7867
|
-
|
|
7868
|
-
|
|
7869
|
-
p: string; // Original Price
|
|
7870
|
-
m: boolean; // Is this trade the maker side?
|
|
7871
|
-
c: string; // Client Order Id
|
|
7872
|
-
S: 'BUY' | 'SELL'; // Side
|
|
7873
|
-
L: string; // Last Filled Price
|
|
7874
|
-
l: string; // Order Last Filled Quantity
|
|
7875
|
-
t: number; // Trade Id
|
|
7876
|
-
i: number; // Order Id
|
|
7741
|
+
export interface WsSharedBase {
|
|
7742
|
+
wsMarket: WsMarket;
|
|
7743
|
+
wsKey: WsKey;
|
|
7744
|
+
streamName: string;
|
|
7745
|
+
}
|
|
7877
7746
|
⋮----
|
|
7878
|
-
export interface
|
|
7879
|
-
|
|
7880
|
-
|
|
7881
|
-
|
|
7882
|
-
|
|
7883
|
-
|
|
7884
|
-
|
|
7885
|
-
l: number;
|
|
7886
|
-
};
|
|
7887
|
-
ai?: {
|
|
7888
|
-
j: boolean;
|
|
7747
|
+
export interface WsResponse {
|
|
7748
|
+
type: 'message';
|
|
7749
|
+
data: {
|
|
7750
|
+
result: boolean | string[] | null;
|
|
7751
|
+
id: number;
|
|
7752
|
+
isWSAPIResponse: boolean;
|
|
7753
|
+
wsKey: WsKey;
|
|
7889
7754
|
};
|
|
7890
7755
|
}
|
|
7891
7756
|
⋮----
|
|
7892
|
-
|
|
7893
|
-
|
|
7894
|
-
|
|
7895
|
-
|
|
7896
|
-
|
|
7897
|
-
|
|
7757
|
+
// Same as inverse futures
|
|
7758
|
+
export type WsPublicInverseTopic =
|
|
7759
|
+
| 'orderBookL2_25'
|
|
7760
|
+
| 'orderBookL2_200'
|
|
7761
|
+
| 'trade'
|
|
7762
|
+
| 'insurance'
|
|
7763
|
+
| 'instrument_info'
|
|
7764
|
+
| 'klineV2';
|
|
7898
7765
|
⋮----
|
|
7899
|
-
export
|
|
7900
|
-
|
|
7901
|
-
|
|
7902
|
-
|
|
7903
|
-
|
|
7904
|
-
|
|
7905
|
-
|
|
7906
|
-
r: string;
|
|
7907
|
-
T: number;
|
|
7908
|
-
}
|
|
7766
|
+
export type WsPublicUSDTPerpTopic =
|
|
7767
|
+
| 'orderBookL2_25'
|
|
7768
|
+
| 'orderBookL2_200'
|
|
7769
|
+
| 'trade'
|
|
7770
|
+
| 'insurance'
|
|
7771
|
+
| 'instrument_info'
|
|
7772
|
+
| 'kline';
|
|
7909
7773
|
⋮----
|
|
7910
|
-
export
|
|
7911
|
-
|
|
7912
|
-
|
|
7913
|
-
|
|
7914
|
-
|
|
7915
|
-
|
|
7916
|
-
|
|
7917
|
-
f: string;
|
|
7918
|
-
q: string;
|
|
7919
|
-
p: string;
|
|
7920
|
-
ap: string;
|
|
7921
|
-
X: string;
|
|
7922
|
-
l: string;
|
|
7923
|
-
z: string;
|
|
7924
|
-
T: number;
|
|
7925
|
-
};
|
|
7926
|
-
}
|
|
7774
|
+
export type WsPublicSpotV1Topic =
|
|
7775
|
+
| 'trade'
|
|
7776
|
+
| 'realtimes'
|
|
7777
|
+
| 'kline'
|
|
7778
|
+
| 'depth'
|
|
7779
|
+
| 'mergedDepth'
|
|
7780
|
+
| 'diffDepth';
|
|
7927
7781
|
⋮----
|
|
7928
|
-
export
|
|
7929
|
-
|
|
7930
|
-
|
|
7931
|
-
|
|
7932
|
-
|
|
7933
|
-
|
|
7934
|
-
si: number; // Strategy ID
|
|
7935
|
-
st: string; // Strategy Type
|
|
7936
|
-
ss: string; // Strategy Status
|
|
7937
|
-
s: string; // Symbol
|
|
7938
|
-
ut: number; // Update Time
|
|
7939
|
-
c: number; // opCode
|
|
7940
|
-
};
|
|
7941
|
-
}
|
|
7782
|
+
export type WsPublicSpotV2Topic =
|
|
7783
|
+
| 'depth'
|
|
7784
|
+
| 'kline'
|
|
7785
|
+
| 'trade'
|
|
7786
|
+
| 'bookTicker'
|
|
7787
|
+
| 'realtimes';
|
|
7942
7788
|
⋮----
|
|
7943
|
-
|
|
7944
|
-
|
|
7945
|
-
|
|
7789
|
+
export type WsPublicTopics =
|
|
7790
|
+
| WsPublicInverseTopic
|
|
7791
|
+
| WsPublicUSDTPerpTopic
|
|
7792
|
+
| WsPublicSpotV1Topic
|
|
7793
|
+
| WsPublicSpotV2Topic
|
|
7794
|
+
| string;
|
|
7946
7795
|
⋮----
|
|
7947
|
-
|
|
7948
|
-
|
|
7949
|
-
|
|
7950
|
-
|
|
7951
|
-
|
|
7952
|
-
|
|
7796
|
+
// Same as inverse futures
|
|
7797
|
+
export type WsPrivateInverseTopic =
|
|
7798
|
+
| 'position'
|
|
7799
|
+
| 'execution'
|
|
7800
|
+
| 'order'
|
|
7801
|
+
| 'stop_order';
|
|
7953
7802
|
⋮----
|
|
7954
|
-
export
|
|
7955
|
-
|
|
7956
|
-
|
|
7957
|
-
|
|
7958
|
-
|
|
7959
|
-
|
|
7960
|
-
|
|
7961
|
-
|
|
7962
|
-
|
|
7963
|
-
|
|
7964
|
-
|
|
7965
|
-
|
|
7966
|
-
|
|
7967
|
-
|
|
7968
|
-
|
|
7803
|
+
export type WsPrivateUSDTPerpTopic =
|
|
7804
|
+
| 'position'
|
|
7805
|
+
| 'execution'
|
|
7806
|
+
| 'order'
|
|
7807
|
+
| 'stop_order'
|
|
7808
|
+
| 'wallet';
|
|
7809
|
+
⋮----
|
|
7810
|
+
export type WsPrivateSpotTopic =
|
|
7811
|
+
| 'outboundAccountInfo'
|
|
7812
|
+
| 'executionReport'
|
|
7813
|
+
| 'ticketInfo';
|
|
7814
|
+
⋮----
|
|
7815
|
+
export type WsPrivateTopic =
|
|
7816
|
+
| WsPrivateInverseTopic
|
|
7817
|
+
| WsPrivateUSDTPerpTopic
|
|
7818
|
+
| WsPrivateSpotTopic
|
|
7819
|
+
| string;
|
|
7820
|
+
⋮----
|
|
7821
|
+
export type WsTopic = WsPublicTopics | WsPrivateTopic;
|
|
7822
|
+
⋮----
|
|
7823
|
+
export interface WSClientConfigurableOptions {
|
|
7824
|
+
/** Your API key */
|
|
7825
|
+
api_key?: string;
|
|
7826
|
+
|
|
7827
|
+
/** Your API secret */
|
|
7828
|
+
api_secret?: string;
|
|
7829
|
+
|
|
7830
|
+
beautify?: boolean;
|
|
7831
|
+
|
|
7832
|
+
/**
|
|
7833
|
+
* If true, log a warning if the beautifier is missing anything for an event
|
|
7834
|
+
*/
|
|
7835
|
+
beautifyWarnIfMissing?: boolean;
|
|
7836
|
+
|
|
7837
|
+
/**
|
|
7838
|
+
* Set to `true` to connect to Binance's testnet environment.
|
|
7839
|
+
*
|
|
7840
|
+
* Notes:
|
|
7841
|
+
* - Not all WebSocket categories support testnet.
|
|
7842
|
+
* - If testing a strategy, this is not recommended. Testnet market data is very different from real market conditions. More guidance here: https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
7843
|
+
*/
|
|
7844
|
+
testnet?: boolean;
|
|
7845
|
+
|
|
7846
|
+
/**
|
|
7847
|
+
* Default: false. If true, use market maker endpoints when available.
|
|
7848
|
+
* Eligible for high-frequency trading users who have enrolled and qualified
|
|
7849
|
+
* in at least one of the Futures Liquidity Provider Programs.
|
|
7850
|
+
* More info: https://www.binance.com/en/support/faq/detail/7df7f3838c3b49e692d175374c3a3283
|
|
7851
|
+
*/
|
|
7852
|
+
useMMEndpoints?: boolean;
|
|
7853
|
+
|
|
7854
|
+
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
7855
|
+
recvWindow?: number;
|
|
7856
|
+
|
|
7857
|
+
// Disable ping/pong ws heartbeat mechanism (not recommended)
|
|
7858
|
+
disableHeartbeat?: boolean;
|
|
7859
|
+
|
|
7860
|
+
/** How often to check if the connection is alive */
|
|
7861
|
+
pingInterval?: number;
|
|
7862
|
+
|
|
7863
|
+
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
7864
|
+
pongTimeout?: number;
|
|
7865
|
+
|
|
7866
|
+
/** Delay in milliseconds before respawning the connection */
|
|
7867
|
+
reconnectTimeout?: number;
|
|
7868
|
+
|
|
7869
|
+
restOptions?: RestClientOptions;
|
|
7870
|
+
|
|
7871
|
+
requestOptions?: AxiosRequestConfig;
|
|
7872
|
+
|
|
7873
|
+
wsOptions?: {
|
|
7874
|
+
protocols?: string[];
|
|
7875
|
+
agent?: any;
|
|
7969
7876
|
};
|
|
7877
|
+
|
|
7878
|
+
wsUrl?: string;
|
|
7879
|
+
|
|
7880
|
+
/**
|
|
7881
|
+
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
7882
|
+
*
|
|
7883
|
+
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
7884
|
+
*/
|
|
7885
|
+
customSignMessageFn?: (message: string, secret: string) => Promise<string>;
|
|
7970
7886
|
}
|
|
7971
7887
|
⋮----
|
|
7972
|
-
|
|
7973
|
-
T: number; // Transaction Time
|
|
7974
|
-
E: number; // Event Time
|
|
7888
|
+
/** Your API key */
|
|
7975
7889
|
⋮----
|
|
7976
|
-
|
|
7977
|
-
st: string; // Strategy Type
|
|
7978
|
-
ss: string; // Strategy Status
|
|
7979
|
-
s: string; // Symbol
|
|
7980
|
-
r: numberInString; // Realized PNL
|
|
7981
|
-
up: numberInString; // Unmatched Average Price
|
|
7982
|
-
uq: numberInString; // Unmatched Qty
|
|
7983
|
-
uf: numberInString; // Unmatched Fee
|
|
7984
|
-
mp: numberInString; // Matched PNL
|
|
7985
|
-
ut: number; // Update Time
|
|
7890
|
+
/** Your API secret */
|
|
7986
7891
|
⋮----
|
|
7987
|
-
|
|
7988
|
-
|
|
7989
|
-
|
|
7990
|
-
s: string; // Symbol
|
|
7991
|
-
ps: string; // Pair
|
|
7992
|
-
ct: string; // Contract type
|
|
7993
|
-
dt: number; // Delivery date time
|
|
7994
|
-
ot: number; // onboard date time
|
|
7995
|
-
cs: string; // Contract status
|
|
7996
|
-
bks: {
|
|
7997
|
-
bs: number; // Notional bracket
|
|
7998
|
-
bnf: number; // Floor notional of this bracket
|
|
7999
|
-
bnc: number; // Cap notional of this bracket
|
|
8000
|
-
mmr: number; // Maintenance ratio for this bracket
|
|
8001
|
-
cf: number; // Auxiliary number for quick calculation
|
|
8002
|
-
mi: number; // Min leverage for this bracket
|
|
8003
|
-
ma: number; // Max leverage for this bracket
|
|
8004
|
-
}[];
|
|
8005
|
-
}
|
|
7892
|
+
/**
|
|
7893
|
+
* If true, log a warning if the beautifier is missing anything for an event
|
|
7894
|
+
*/
|
|
8006
7895
|
⋮----
|
|
8007
|
-
|
|
8008
|
-
|
|
8009
|
-
|
|
8010
|
-
|
|
8011
|
-
|
|
8012
|
-
|
|
8013
|
-
|
|
8014
|
-
cs: string; // Contract status
|
|
7896
|
+
/**
|
|
7897
|
+
* Set to `true` to connect to Binance's testnet environment.
|
|
7898
|
+
*
|
|
7899
|
+
* Notes:
|
|
7900
|
+
* - Not all WebSocket categories support testnet.
|
|
7901
|
+
* - If testing a strategy, this is not recommended. Testnet market data is very different from real market conditions. More guidance here: https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
7902
|
+
*/
|
|
8015
7903
|
⋮----
|
|
8016
|
-
|
|
8017
|
-
|
|
8018
|
-
|
|
8019
|
-
|
|
8020
|
-
|
|
8021
|
-
|
|
8022
|
-
ma: number; // Max leverage for this bracket
|
|
7904
|
+
/**
|
|
7905
|
+
* Default: false. If true, use market maker endpoints when available.
|
|
7906
|
+
* Eligible for high-frequency trading users who have enrolled and qualified
|
|
7907
|
+
* in at least one of the Futures Liquidity Provider Programs.
|
|
7908
|
+
* More info: https://www.binance.com/en/support/faq/detail/7df7f3838c3b49e692d175374c3a3283
|
|
7909
|
+
*/
|
|
8023
7910
|
⋮----
|
|
8024
|
-
|
|
8025
|
-
| WsMessageSpotUserDataExecutionReportEventRaw
|
|
8026
|
-
| WsMessageSpotOutboundAccountPositionRaw
|
|
8027
|
-
| WsMessageSpotBalanceUpdateRaw
|
|
8028
|
-
| WsMessageSpotUserDataListStatusEventRaw;
|
|
7911
|
+
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
8029
7912
|
⋮----
|
|
8030
|
-
|
|
8031
|
-
| WsMessageFuturesUserDataAccountUpdateRaw
|
|
8032
|
-
| WsMessageFuturesUserDataListenKeyExpiredRaw
|
|
8033
|
-
| WsMessageFuturesUserDataMarginCallRaw
|
|
8034
|
-
| WsMessageFuturesUserDataOrderTradeUpdateEventRaw
|
|
8035
|
-
| WsMessageFuturesUserDataAccountConfigUpdateEventRaw
|
|
8036
|
-
| WsMessageFuturesUserDataCondOrderTriggerRejectEventRaw
|
|
8037
|
-
| WsMessageFuturesUserDataTradeLiteEventRaw
|
|
8038
|
-
| WsMessageFuturesUserDataStrategyUpdateRaw
|
|
8039
|
-
| WsMessageFuturesUserDataGridUpdateRaw
|
|
8040
|
-
| WsMessageFuturesUserDataContractInfoRaw;
|
|
7913
|
+
// Disable ping/pong ws heartbeat mechanism (not recommended)
|
|
8041
7914
|
⋮----
|
|
8042
|
-
|
|
8043
|
-
|
|
8044
|
-
|
|
8045
|
-
|
|
8046
|
-
|
|
8047
|
-
|
|
8048
|
-
|
|
8049
|
-
|
|
8050
|
-
|
|
8051
|
-
|
|
8052
|
-
|
|
8053
|
-
|
|
8054
|
-
|
|
8055
|
-
|
|
8056
|
-
|
|
8057
|
-
|
|
8058
|
-
|
|
8059
|
-
|
|
8060
|
-
|
|
8061
|
-
|
|
7915
|
+
/** How often to check if the connection is alive */
|
|
7916
|
+
⋮----
|
|
7917
|
+
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
7918
|
+
⋮----
|
|
7919
|
+
/** Delay in milliseconds before respawning the connection */
|
|
7920
|
+
⋮----
|
|
7921
|
+
/**
|
|
7922
|
+
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
7923
|
+
*
|
|
7924
|
+
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
7925
|
+
*/
|
|
7926
|
+
⋮----
|
|
7927
|
+
/**
|
|
7928
|
+
* WS configuration that's always defined, regardless of user configuration
|
|
7929
|
+
* (usually comes from defaults if there's no user-provided values)
|
|
7930
|
+
*/
|
|
7931
|
+
export interface WebsocketClientOptions extends WSClientConfigurableOptions {
|
|
7932
|
+
pongTimeout: number;
|
|
7933
|
+
pingInterval: number;
|
|
7934
|
+
reconnectTimeout: number;
|
|
7935
|
+
recvWindow: number;
|
|
7936
|
+
authPrivateConnectionsOnConnect: boolean;
|
|
7937
|
+
authPrivateRequests: boolean;
|
|
7938
|
+
}
|
|
8062
7939
|
|
|
8063
7940
|
================
|
|
8064
7941
|
File: src/types/futures.ts
|
|
@@ -10349,133 +10226,34 @@ async function main()
|
|
|
10349
10226
|
// the `subscribeUserDataStream()` method again if reconnected (if you called it before):
|
|
10350
10227
|
// resubscribeUserDataStreamAfterReconnect: true,
|
|
10351
10228
|
⋮----
|
|
10352
|
-
// If you want your own event handlers instead of the default ones with logs, disable this setting and see the `attachEventHandlers` example below:
|
|
10353
|
-
// attachEventListeners: false
|
|
10354
|
-
⋮----
|
|
10355
|
-
// Optional, attach basic event handlers, so nothing is left unhandled
|
|
10356
|
-
// attachEventHandlers(wsClient.getWSClient());
|
|
10357
|
-
⋮----
|
|
10358
|
-
// Optional, if you see RECV Window errors, you can use this to manage time issues.
|
|
10359
|
-
// ! However, make sure you sync your system clock first!
|
|
10360
|
-
// https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow
|
|
10361
|
-
// wsClient.setTimeOffsetMs(-5000);
|
|
10362
|
-
⋮----
|
|
10363
|
-
// Optional. Can be used to prepare a connection before sending commands.
|
|
10364
|
-
// Can be done as part of a bootstrapping workflow, to reduce initial latency when sending the first command
|
|
10365
|
-
// await wsClient.getWSClient().connectWSAPI(WS_KEY_MAP.mainWSAPI);
|
|
10366
|
-
⋮----
|
|
10367
|
-
// SPOT - Market data requests
|
|
10368
|
-
⋮----
|
|
10369
|
-
// SPOT - Trading requests
|
|
10370
|
-
⋮----
|
|
10371
|
-
// SPOT - Account requests
|
|
10372
|
-
⋮----
|
|
10373
|
-
// FUTURES - Market data requests
|
|
10374
|
-
⋮----
|
|
10375
|
-
// FUTURES - Trading requests
|
|
10376
|
-
⋮----
|
|
10377
|
-
// FUTURES - Account requests
|
|
10378
|
-
⋮----
|
|
10379
|
-
// Start executing the example workflow
|
|
10380
|
-
|
|
10381
|
-
================
|
|
10382
|
-
File: examples/README.md
|
|
10383
|
-
================
|
|
10384
|
-
# Binance API - Examples
|
|
10385
|
-
|
|
10386
|
-
This folder contains ready to go examples demonstrating various aspects of this API implementation, written in TypeScript (but they are compatible with pure JavaScript projects).
|
|
10387
|
-
|
|
10388
|
-
Found something difficult to implement? Contribute to these examples and help others!
|
|
10389
|
-
|
|
10390
|
-
## Getting started
|
|
10391
|
-
|
|
10392
|
-
- Clone the project (or download it as a zip, or install the module in your own project `npm install binance`).
|
|
10393
|
-
- Edit the sample as needed (some samples require edits, e.g API keys or import statements to import from npm, not src).
|
|
10394
|
-
- Execute the sample using tsx: `tsx examples/REST/rest-spot-public.ts`.
|
|
10395
|
-
|
|
10396
|
-
Samples that refer to API credentials using `process.env.API_KEY_COM` can be spawned with environment variables. Unix/macOS example:
|
|
10397
|
-
```
|
|
10398
|
-
APIKEY='apikeypastedhere' APISECRET='apisecretpastedhere' tsx examples/WebSockets/ws-userdata-listenkey.ts
|
|
10399
|
-
```
|
|
10400
|
-
|
|
10401
|
-
Or edit the example directly to hardcode your API keys.
|
|
10402
|
-
|
|
10403
|
-
### WebSockets
|
|
10404
|
-
|
|
10405
|
-
All examples relating to WebSockets can be found in the [examples/WebSockets](./WebSockets/) folder. High level summary of available examples:
|
|
10406
|
-
|
|
10407
|
-
#### Consumers
|
|
10408
|
-
|
|
10409
|
-
These are purely for receiving data from Binance's WebSockets (market data, account updates, etc).
|
|
10410
|
-
|
|
10411
|
-
##### Market Data
|
|
10412
|
-
|
|
10413
|
-
These examples demonstrate subscribing to & receiving market data from Binance's WebSockets:
|
|
10414
|
-
|
|
10415
|
-
- ws-public.ts
|
|
10416
|
-
- Demonstration on general usage of the WebSocket client to subscribe to / unsubscribe from one or more market data topics.
|
|
10417
|
-
- ws-public-spot-orderbook.ts
|
|
10418
|
-
- Subscribing to orderbook events for multiple symbols in spot markets.
|
|
10419
|
-
- ws-public-spot-trades.ts
|
|
10420
|
-
- Subscribing to raw trades for multiple symbols in spot markets.
|
|
10421
|
-
- ws-unsubscribe.ts
|
|
10422
|
-
- Subscribing to a list of topics, and then unsubscribing from a few topics in that list.
|
|
10423
|
-
- ws-public-usdm-funding.ts
|
|
10424
|
-
- Simple example subscribing to a general topic, and how to process incoming events to only extract funding rates from those events.
|
|
10425
|
-
|
|
10426
|
-
##### Account Data
|
|
10427
|
-
|
|
10428
|
-
These examples demonstrate receiving account update events from Binance's WebSockets:
|
|
10429
|
-
|
|
10430
|
-
- ws-userdata-listenkey.ts
|
|
10431
|
-
- Demonstration on subscribing to various user data streams (spot, margin, futures),
|
|
10432
|
-
- Handling incoming user data events
|
|
10433
|
-
- Using provided type guards to determine which product group the user data event is for (spot, margin, futures, etc).
|
|
10434
|
-
- ws-userdata-listenKey-testnet.ts
|
|
10435
|
-
- Similar to above, but on testnet.
|
|
10436
|
-
- ws-userdata-connection-safety.ts
|
|
10437
|
-
- Demonstration on extra safety around the first user data stream connection.
|
|
10438
|
-
- Note: this is overkill in most situations...
|
|
10439
|
-
|
|
10440
|
-
##### WebSocket API
|
|
10441
|
-
|
|
10442
|
-
These examples demonstrate how to send commands using Binance's WebSocket API (e.g. submitting orders). Very similar to the REST API, but using a persisted WebSocket connection instead of HTTP requests.
|
|
10443
|
-
|
|
10444
|
-
- ws-api-client.ts
|
|
10445
|
-
- Demonstration of using Binance's WebSocket API in Node.js/JavaScript/TypeScript, using the WebsocketAPIClient.
|
|
10446
|
-
- This WebsocketAPIClient is very similar to a REST client, with one method per available command (endpoint) and fully typed requests & responses.
|
|
10447
|
-
- Routing is automatically handled via the WebsocketClient, including authentication and connection persistence. Just call the functions you need - the SDK does the rest.
|
|
10448
|
-
- From a usage perspective, it feels like a REST API - you can await responses just like a HTTP request.
|
|
10449
|
-
- ws-api-raw-promises.ts
|
|
10450
|
-
- More verbose usage of the WebSocket API using the `sendWSAPIRequest()` method.
|
|
10451
|
-
- The `WebsocketAPIClient` uses this method too, so in most cases it is simple to just use the `WebsocketAPIClient` instead.
|
|
10452
|
-
- ws-userdata-wsapi.ts
|
|
10453
|
-
- The listenKey workflow for the user data stream is deprecated (in spot markets).
|
|
10454
|
-
- This example demonstrates how to subscribe to the user data stream in spot markets, without a listen key, using the WebSocket API.
|
|
10455
|
-
|
|
10456
|
-
##### Misc Workflows
|
|
10457
|
-
|
|
10458
|
-
These are miscellaneous examples that cover one or more of the above categories:
|
|
10459
|
-
|
|
10460
|
-
- ws-close.ts
|
|
10461
|
-
- Closing the (old listen-key driven) user data stream.
|
|
10462
|
-
- Unsubscribing from various topics.
|
|
10463
|
-
- ws-proxy-socks.ts
|
|
10464
|
-
- Using WebSockets over a SOCKS proxy.
|
|
10465
|
-
- deprecated-ws-public.ts
|
|
10466
|
-
|
|
10467
|
-
|
|
10468
|
-
### REST APIs
|
|
10469
|
-
|
|
10470
|
-
All examples relating to REST APIs can be found in the [examples/REST](./REST/) folder. Most examples are named around functionality & product group. Any examples with "private" involve API calls relating to your account (such as changing settings or submitting orders, etc),
|
|
10471
|
-
|
|
10472
|
-
High level summary for some of the available examples, but check the folder for a complete list:
|
|
10473
|
-
|
|
10474
|
-
#### REST USDM Examples
|
|
10475
|
-
|
|
10476
|
-
- `rest-future-bracket-order.ts` Creates three order, entry, TP, SL and submit them all at once using `submitMultipleOrders`
|
|
10477
|
-
- `rest-usdm-order.ts` Creates single entry, using `submitNewOrder`
|
|
10478
|
-
- `rest-usdm-order-sl.ts` Modify current Stop Loss order(HedgeMode only)
|
|
10229
|
+
// If you want your own event handlers instead of the default ones with logs, disable this setting and see the `attachEventHandlers` example below:
|
|
10230
|
+
// attachEventListeners: false
|
|
10231
|
+
⋮----
|
|
10232
|
+
// Optional, attach basic event handlers, so nothing is left unhandled
|
|
10233
|
+
// attachEventHandlers(wsClient.getWSClient());
|
|
10234
|
+
⋮----
|
|
10235
|
+
// Optional, if you see RECV Window errors, you can use this to manage time issues.
|
|
10236
|
+
// ! However, make sure you sync your system clock first!
|
|
10237
|
+
// https://github.com/tiagosiebler/awesome-crypto-examples/wiki/Timestamp-for-this-request-is-outside-of-the-recvWindow
|
|
10238
|
+
// wsClient.setTimeOffsetMs(-5000);
|
|
10239
|
+
⋮----
|
|
10240
|
+
// Optional. Can be used to prepare a connection before sending commands.
|
|
10241
|
+
// Can be done as part of a bootstrapping workflow, to reduce initial latency when sending the first command
|
|
10242
|
+
// await wsClient.getWSClient().connectWSAPI(WS_KEY_MAP.mainWSAPI);
|
|
10243
|
+
⋮----
|
|
10244
|
+
// SPOT - Market data requests
|
|
10245
|
+
⋮----
|
|
10246
|
+
// SPOT - Trading requests
|
|
10247
|
+
⋮----
|
|
10248
|
+
// SPOT - Account requests
|
|
10249
|
+
⋮----
|
|
10250
|
+
// FUTURES - Market data requests
|
|
10251
|
+
⋮----
|
|
10252
|
+
// FUTURES - Trading requests
|
|
10253
|
+
⋮----
|
|
10254
|
+
// FUTURES - Account requests
|
|
10255
|
+
⋮----
|
|
10256
|
+
// Start executing the example workflow
|
|
10479
10257
|
|
|
10480
10258
|
================
|
|
10481
10259
|
File: examples/WebSockets/ws-api-raw-promises.ts
|
|
@@ -17826,107 +17604,362 @@ l: number; // last trade ID
|
|
|
17826
17604
|
T: number; // timestamp
|
|
17827
17605
|
m: boolean; // is buyer market maker
|
|
17828
17606
|
⋮----
|
|
17829
|
-
export interface AlphaKlinesParams {
|
|
17830
|
-
symbol: string;
|
|
17831
|
-
interval: string; // 1s, 15s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
|
|
17832
|
-
limit?: number;
|
|
17833
|
-
startTime?: number;
|
|
17834
|
-
endTime?: number;
|
|
17835
|
-
}
|
|
17607
|
+
export interface AlphaKlinesParams {
|
|
17608
|
+
symbol: string;
|
|
17609
|
+
interval: string; // 1s, 15s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
|
|
17610
|
+
limit?: number;
|
|
17611
|
+
startTime?: number;
|
|
17612
|
+
endTime?: number;
|
|
17613
|
+
}
|
|
17614
|
+
⋮----
|
|
17615
|
+
interval: string; // 1s, 15s, 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M
|
|
17616
|
+
⋮----
|
|
17617
|
+
export type AlphaKline = [
|
|
17618
|
+
string, // Open time
|
|
17619
|
+
string, // Open price
|
|
17620
|
+
string, // High price
|
|
17621
|
+
string, // Low price
|
|
17622
|
+
string, // Close price
|
|
17623
|
+
string, // Volume
|
|
17624
|
+
string, // Close time
|
|
17625
|
+
string, // Quote asset volume
|
|
17626
|
+
string, // Number of trades
|
|
17627
|
+
string, // Taker buy base asset volume
|
|
17628
|
+
string, // Taker buy quote asset volume
|
|
17629
|
+
string, // Ignore (always "0")
|
|
17630
|
+
];
|
|
17631
|
+
⋮----
|
|
17632
|
+
string, // Open time
|
|
17633
|
+
string, // Open price
|
|
17634
|
+
string, // High price
|
|
17635
|
+
string, // Low price
|
|
17636
|
+
string, // Close price
|
|
17637
|
+
string, // Volume
|
|
17638
|
+
string, // Close time
|
|
17639
|
+
string, // Quote asset volume
|
|
17640
|
+
string, // Number of trades
|
|
17641
|
+
string, // Taker buy base asset volume
|
|
17642
|
+
string, // Taker buy quote asset volume
|
|
17643
|
+
string, // Ignore (always "0")
|
|
17644
|
+
⋮----
|
|
17645
|
+
export interface AlphaTickerParams {
|
|
17646
|
+
symbol: string;
|
|
17647
|
+
}
|
|
17648
|
+
⋮----
|
|
17649
|
+
export interface AlphaTicker {
|
|
17650
|
+
symbol: string;
|
|
17651
|
+
priceChange: string;
|
|
17652
|
+
priceChangePercent: string;
|
|
17653
|
+
weightedAvgPrice: string;
|
|
17654
|
+
lastPrice: string;
|
|
17655
|
+
lastQty: string;
|
|
17656
|
+
openPrice: string;
|
|
17657
|
+
highPrice: string;
|
|
17658
|
+
lowPrice: string;
|
|
17659
|
+
volume: string;
|
|
17660
|
+
quoteVolume: string;
|
|
17661
|
+
openTime: number;
|
|
17662
|
+
closeTime: number;
|
|
17663
|
+
firstId: number;
|
|
17664
|
+
lastId: number;
|
|
17665
|
+
count: number;
|
|
17666
|
+
}
|
|
17667
|
+
|
|
17668
|
+
================
|
|
17669
|
+
File: examples/WebSockets/ws-public.ts
|
|
17670
|
+
================
|
|
17671
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
17672
|
+
import {
|
|
17673
|
+
DefaultLogger,
|
|
17674
|
+
isWsAggTradeFormatted,
|
|
17675
|
+
isWsFormatted24hrTicker,
|
|
17676
|
+
isWsFormatted24hrTickerArray,
|
|
17677
|
+
isWsFormattedForceOrder,
|
|
17678
|
+
isWsFormattedKline,
|
|
17679
|
+
isWsFormattedMarkPriceUpdateArray,
|
|
17680
|
+
isWsFormattedMarkPriceUpdateEvent,
|
|
17681
|
+
isWsFormattedRollingWindowTickerArray,
|
|
17682
|
+
isWsFormattedTrade,
|
|
17683
|
+
isWsPartialBookDepthEventFormatted,
|
|
17684
|
+
WebsocketClient,
|
|
17685
|
+
} from '../../src';
|
|
17686
|
+
⋮----
|
|
17687
|
+
// or, with the npm package
|
|
17688
|
+
/*
|
|
17689
|
+
import {
|
|
17690
|
+
WebsocketClient,
|
|
17691
|
+
DefaultLogger,
|
|
17692
|
+
isWsFormatted24hrTicker,
|
|
17693
|
+
isWsFormattedKline,
|
|
17694
|
+
} from 'binance';
|
|
17695
|
+
*/
|
|
17696
|
+
⋮----
|
|
17697
|
+
// Without typescript:
|
|
17698
|
+
// const logger = {
|
|
17699
|
+
⋮----
|
|
17700
|
+
// A simple way to suppress heartbeats but receive all other traces
|
|
17701
|
+
// if (params[0].includes('ping') || params[0].includes('pong')) {
|
|
17702
|
+
// return;
|
|
17703
|
+
// }
|
|
17704
|
+
⋮----
|
|
17705
|
+
// Optional: when enabled, the SDK will try to format incoming data into more readable objects.
|
|
17706
|
+
// Beautified data is emitted via the "formattedMessage" event
|
|
17707
|
+
⋮----
|
|
17708
|
+
logger, // Optional: customise logging behaviour by extending or overwriting the default logger implementation
|
|
17709
|
+
⋮----
|
|
17710
|
+
// Raw unprocessed incoming data, e.g. if you have the beautifier disabled
|
|
17711
|
+
⋮----
|
|
17712
|
+
// console.log('raw message received ', JSON.stringify(data, null, 2));
|
|
17713
|
+
// console.log('raw message received ', JSON.stringify(data));
|
|
17714
|
+
⋮----
|
|
17715
|
+
// Formatted data that has gone through the beautifier
|
|
17716
|
+
⋮----
|
|
17717
|
+
/**
|
|
17718
|
+
* Optional: we've included type-guards for many formatted websocket topics.
|
|
17719
|
+
*
|
|
17720
|
+
* These can be used within `if` blocks to narrow down specific event types (even for non-typescript users).
|
|
17721
|
+
*/
|
|
17722
|
+
// if (isWsAggTradeFormatted(data)) {
|
|
17723
|
+
// console.log('log agg trade: ', data);
|
|
17724
|
+
// return;
|
|
17725
|
+
// }
|
|
17726
|
+
⋮----
|
|
17727
|
+
// // For one symbol
|
|
17728
|
+
// if (isWsFormattedMarkPriceUpdateEvent(data)) {
|
|
17729
|
+
// console.log('log mark price: ', data);
|
|
17730
|
+
// return;
|
|
17731
|
+
// }
|
|
17732
|
+
⋮----
|
|
17733
|
+
// // for many symbols
|
|
17734
|
+
// if (isWsFormattedMarkPriceUpdateArray(data)) {
|
|
17735
|
+
// console.log('log mark prices: ', data);
|
|
17736
|
+
// return;
|
|
17737
|
+
// }
|
|
17738
|
+
⋮----
|
|
17739
|
+
// if (isWsFormattedKline(data)) {
|
|
17740
|
+
// console.log('log kline: ', data);
|
|
17741
|
+
// return;
|
|
17742
|
+
// }
|
|
17743
|
+
⋮----
|
|
17744
|
+
// if (isWsFormattedTrade(data)) {
|
|
17745
|
+
// return console.log('log trade: ', data);
|
|
17746
|
+
// }
|
|
17747
|
+
⋮----
|
|
17748
|
+
// if (isWsFormattedForceOrder(data)) {
|
|
17749
|
+
// return console.log('log force order: ', data);
|
|
17750
|
+
// }
|
|
17751
|
+
⋮----
|
|
17752
|
+
// if (isWsFormatted24hrTickerArray(data)) {
|
|
17753
|
+
// return console.log('log 24hr ticker array: ', data);
|
|
17754
|
+
// }
|
|
17755
|
+
⋮----
|
|
17756
|
+
// if (isWsFormattedRollingWindowTickerArray(data)) {
|
|
17757
|
+
// return console.log('log rolling window ticker array: ', data);
|
|
17758
|
+
// }
|
|
17759
|
+
⋮----
|
|
17760
|
+
// if (isWsFormatted24hrTicker(data)) {
|
|
17761
|
+
// return console.log('log 24hr ticker: ', data);
|
|
17762
|
+
// }
|
|
17763
|
+
⋮----
|
|
17764
|
+
// if (isWsPartialBookDepthEventFormatted(data)) {
|
|
17765
|
+
// return console.log('log partial book depth event: ', data);
|
|
17766
|
+
// }
|
|
17767
|
+
⋮----
|
|
17768
|
+
// response to command sent via WS stream (e.g LIST_SUBSCRIPTIONS)
|
|
17769
|
+
⋮----
|
|
17770
|
+
// No action needed here, unless you need to query the REST API after being reconnected.
|
|
17771
|
+
⋮----
|
|
17772
|
+
/**
|
|
17773
|
+
* The Websocket Client will automatically manage connectivity and active topics/subscriptions for you.
|
|
17774
|
+
*
|
|
17775
|
+
* Simply call wsClient.subscribe(topic, wsKey) as many times as you want, with or without an array.
|
|
17776
|
+
*/
|
|
17777
|
+
⋮----
|
|
17778
|
+
// // E.g. one at a time, routed to the coinm futures websockets:
|
|
17779
|
+
// wsClient.subscribe('btcusd@indexPrice', 'coinm');
|
|
17780
|
+
// wsClient.subscribe('btcusd@miniTicker', 'coinm');
|
|
17781
|
+
⋮----
|
|
17782
|
+
// // Or send many topics at once to a stream, e.g. the usdm futures stream:
|
|
17783
|
+
// wsClient.subscribe(
|
|
17784
|
+
// [
|
|
17785
|
+
// 'btcusdt@aggTrade',
|
|
17786
|
+
// 'btcusdt@markPrice',
|
|
17787
|
+
// '!ticker@arr',
|
|
17788
|
+
// '!miniTicker@arr',
|
|
17789
|
+
// ],
|
|
17790
|
+
// 'usdm',
|
|
17791
|
+
// );
|
|
17792
|
+
⋮----
|
|
17793
|
+
/**
|
|
17794
|
+
* Subscribe to each available type of spot market topic, the new way
|
|
17795
|
+
*/
|
|
17796
|
+
⋮----
|
|
17797
|
+
// Aggregate Trade Streams
|
|
17798
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#aggregate-trade-streams
|
|
17799
|
+
⋮----
|
|
17800
|
+
// Trade Streams
|
|
17801
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#trade-streams
|
|
17802
|
+
⋮----
|
|
17803
|
+
// Kline/Candlestick Streams for UTC
|
|
17804
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#klinecandlestick-streams-for-utc
|
|
17805
|
+
⋮----
|
|
17806
|
+
// Kline/Candlestick Streams with timezone offset
|
|
17807
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#klinecandlestick-streams-with-timezone-offset
|
|
17808
|
+
⋮----
|
|
17809
|
+
// Individual Symbol Mini Ticker Stream
|
|
17810
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-mini-ticker-stream
|
|
17811
|
+
⋮----
|
|
17812
|
+
// All Market Mini Tickers Stream
|
|
17813
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-mini-tickers-stream
|
|
17814
|
+
⋮----
|
|
17815
|
+
// Individual Symbol Ticker Streams
|
|
17816
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-ticker-streams
|
|
17817
|
+
⋮----
|
|
17818
|
+
// All Market Tickers Stream
|
|
17819
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-tickers-stream
|
|
17820
|
+
⋮----
|
|
17821
|
+
// Individual Symbol Rolling Window Statistics Streams
|
|
17822
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-rolling-window-statistics-streams
|
|
17823
|
+
⋮----
|
|
17824
|
+
// All Market Rolling Window Statistics Streams
|
|
17825
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#all-market-rolling-window-statistics-streams
|
|
17826
|
+
⋮----
|
|
17827
|
+
// Individual Symbol Book Ticker Streams
|
|
17828
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#individual-symbol-book-ticker-streams
|
|
17829
|
+
⋮----
|
|
17830
|
+
// Average Price
|
|
17831
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#average-price
|
|
17832
|
+
⋮----
|
|
17833
|
+
// Partial Book Depth Streams
|
|
17834
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#partial-book-depth-streams
|
|
17835
|
+
⋮----
|
|
17836
|
+
// Diff. Depth Stream
|
|
17837
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams#diff-depth-stream
|
|
17838
|
+
⋮----
|
|
17839
|
+
// Look at the `WS_KEY_URL_MAP` for a list of values here:
|
|
17840
|
+
// https://github.com/tiagosiebler/binance/blob/master/src/util/websockets/websocket-util.ts
|
|
17841
|
+
// "main" connects to wss://stream.binance.com:9443/stream
|
|
17842
|
+
// https://developers.binance.com/docs/binance-spot-api-docs/web-socket-streams
|
|
17843
|
+
⋮----
|
|
17844
|
+
// Aggregate Trade Stream
|
|
17845
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Aggregate-Trade-Streams
|
|
17836
17846
|
⋮----
|
|
17837
|
-
|
|
17847
|
+
// Mark Price Stream
|
|
17848
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Mark-Price-Stream
|
|
17838
17849
|
⋮----
|
|
17839
|
-
|
|
17840
|
-
|
|
17841
|
-
string, // Open price
|
|
17842
|
-
string, // High price
|
|
17843
|
-
string, // Low price
|
|
17844
|
-
string, // Close price
|
|
17845
|
-
string, // Volume
|
|
17846
|
-
string, // Close time
|
|
17847
|
-
string, // Quote asset volume
|
|
17848
|
-
string, // Number of trades
|
|
17849
|
-
string, // Taker buy base asset volume
|
|
17850
|
-
string, // Taker buy quote asset volume
|
|
17851
|
-
string, // Ignore (always "0")
|
|
17852
|
-
];
|
|
17850
|
+
// Mark Price Stream for All market
|
|
17851
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Mark-Price-Stream-for-All-market
|
|
17853
17852
|
⋮----
|
|
17854
|
-
|
|
17855
|
-
|
|
17856
|
-
|
|
17857
|
-
|
|
17858
|
-
|
|
17859
|
-
|
|
17860
|
-
|
|
17861
|
-
|
|
17862
|
-
|
|
17863
|
-
|
|
17864
|
-
|
|
17865
|
-
|
|
17853
|
+
// Kline/Candlestick Streams
|
|
17854
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Kline-Candlestick-Streams
|
|
17855
|
+
// 'btcusdt@kline_1m',
|
|
17856
|
+
// Continuous Contract Kline/Candlestick Streams
|
|
17857
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Continuous-Contract-Kline-Candlestick-Streams
|
|
17858
|
+
// 'btcusdt_perpetual@continuousKline_1m', // DOESNT EXIST AS TYPE GUARD, ONLY IN BEAUTIFIER
|
|
17859
|
+
// Individual Symbol Mini Ticker Stream
|
|
17860
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Mini-Ticker-Stream
|
|
17861
|
+
// 'btcusdt@miniTicker', // DOESNT EXIST AS TYPE GUARD, ONLY FOR RAW MESSAGE
|
|
17862
|
+
// All Market Mini Tickers Stream
|
|
17863
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Mini-Tickers-Stream
|
|
17864
|
+
// '!miniTicker@arr', // DOESNT EXIST AS TYPE GUARD
|
|
17865
|
+
// Individual Symbol Ticker Streams
|
|
17866
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Ticker-Streams
|
|
17867
|
+
//'btcusdt@ticker',
|
|
17868
|
+
// All Market Tickers Stream
|
|
17869
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Tickers-Stream
|
|
17870
|
+
// '!ticker@arr', // DOESNT EXIST AS TYPE GUARD
|
|
17871
|
+
// Individual Symbol Book Ticker Streams
|
|
17872
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Individual-Symbol-Book-Ticker-Streams
|
|
17873
|
+
//'btcusdt@bookTicker', // DOESNT EXIST AS TYPE GUARD
|
|
17874
|
+
// All Book Tickers Stream
|
|
17875
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Book-Tickers-Stream
|
|
17876
|
+
// '!bookTicker', // DOESNT EXIST AS TYPE GUARD
|
|
17877
|
+
// Liquidation Order Stream
|
|
17878
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Liquidation-Order-Streams
|
|
17879
|
+
// 'btcusdt@forceOrder',
|
|
17880
|
+
// Liquidation Order Stream for All market
|
|
17881
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/All-Market-Liquidation-Order-Streams
|
|
17882
|
+
//'!forceOrder@arr',
|
|
17883
|
+
// Partial Book Depth Streams
|
|
17884
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Partial-Book-Depth-Streams
|
|
17885
|
+
//'btcusdt@depth5',
|
|
17886
|
+
// 'btcusdt@depth10@100ms'
|
|
17887
|
+
// Diff. Book Depth Stream
|
|
17888
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Diff-Book-Depth-Streams
|
|
17889
|
+
// 'btcusdt@depth',
|
|
17890
|
+
// 'btcusdt@depth@100ms',
|
|
17891
|
+
// 'btcusdt@depth@500ms',
|
|
17892
|
+
// 'btcusdt@depth@1000ms'
|
|
17893
|
+
// Composite Index Symbol Information Streams
|
|
17894
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Composite-Index-Symbol-Information-Streams
|
|
17895
|
+
// 'btcusdt@compositeIndex' // DOESNT EXIST AS TYPE GUARD
|
|
17896
|
+
// Contract Info Stream
|
|
17897
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Contract-Info-Stream
|
|
17898
|
+
// '!contractInfo' // DOESNT EXIST AS TYPE GUARD
|
|
17899
|
+
// Multi-Assets Mode Asset Index Stream
|
|
17900
|
+
// https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams/Multi-Assets-Mode-Asset-Index
|
|
17901
|
+
// '!assetIndex@arr' // DOESNT EXIST AS TYPE GUARD
|
|
17902
|
+
// 'btcusdt@assetIndex'
|
|
17866
17903
|
⋮----
|
|
17867
|
-
|
|
17868
|
-
|
|
17869
|
-
|
|
17904
|
+
/**
|
|
17905
|
+
* Subscribe to each available european options market data websocket topic, the new way:
|
|
17906
|
+
*
|
|
17907
|
+
* https://developers.binance.com/docs/derivatives/option/websocket-market-streams/New-Symbol-Info
|
|
17908
|
+
*
|
|
17909
|
+
* https://eapi.binance.com/eapi/v1/exchangeInfo
|
|
17910
|
+
*/
|
|
17870
17911
|
⋮----
|
|
17871
|
-
|
|
17872
|
-
|
|
17873
|
-
|
|
17874
|
-
|
|
17875
|
-
|
|
17876
|
-
|
|
17877
|
-
|
|
17878
|
-
|
|
17879
|
-
|
|
17880
|
-
|
|
17881
|
-
|
|
17882
|
-
|
|
17883
|
-
|
|
17884
|
-
|
|
17885
|
-
|
|
17886
|
-
|
|
17887
|
-
|
|
17888
|
-
|
|
17889
|
-
|
|
17890
|
-
|
|
17891
|
-
|
|
17892
|
-
|
|
17893
|
-
|
|
17894
|
-
|
|
17895
|
-
|
|
17896
|
-
|
|
17897
|
-
|
|
17898
|
-
|
|
17899
|
-
|
|
17900
|
-
|
|
17901
|
-
|
|
17902
|
-
|
|
17903
|
-
|
|
17904
|
-
|
|
17905
|
-
|
|
17906
|
-
|
|
17907
|
-
|
|
17908
|
-
|
|
17909
|
-
|
|
17910
|
-
|
|
17911
|
-
|
|
17912
|
-
|
|
17913
|
-
|
|
17914
|
-
|
|
17915
|
-
|
|
17916
|
-
|
|
17917
|
-
|
|
17918
|
-
|
|
17919
|
-
|
|
17920
|
-
|
|
17921
|
-
|
|
17922
|
-
|
|
17923
|
-
**Deprecation warning: As of April 2025, the listen key workflow is also deprecated for Spot markets (no known ETA for removal).** Refer to the changelog for details: https://developers.binance.com/docs/binance-spot-api-docs/CHANGELOG#2025-04-07
|
|
17924
|
-
|
|
17925
|
-
### Mechanisms - WS API User Data Stream
|
|
17926
|
-
|
|
17927
|
-
The newer mechanism for the user data stream is via an active WebSocket API connection. This is a simpler mechanism in that it does not require a listen key. You can easily use this new mechanism via the WebsocketClient (or WebsocketAPIClient) within this SDK.
|
|
17928
|
-
|
|
17929
|
-
For a working example, refer to the [ws-userdata-wsapi](./ws-userdata-wsapi.ts) example.
|
|
17912
|
+
// /**
|
|
17913
|
+
// *
|
|
17914
|
+
// * For those that used the Node.js Binance SDK before the v3 release, you can
|
|
17915
|
+
// * still subscribe to available market topics the "old" way, for convenience
|
|
17916
|
+
// * when migrating from the old WebsocketClient to the new multiplex client):
|
|
17917
|
+
// *
|
|
17918
|
+
// */
|
|
17919
|
+
⋮----
|
|
17920
|
+
// wsClient.subscribeAggregateTrades(symbol, 'usdm');
|
|
17921
|
+
// wsClient.subscribeTrades(symbol, 'spot');
|
|
17922
|
+
// wsClient.subscribeTrades(symbol, 'usdm');
|
|
17923
|
+
// wsClient.subscribeTrades(coinMSymbol, 'coinm');
|
|
17924
|
+
// wsClient.subscribeCoinIndexPrice(coinMSymbol2);
|
|
17925
|
+
// wsClient.subscribeAllBookTickers('usdm');
|
|
17926
|
+
// wsClient.subscribeSpotKline(symbol, '1m');
|
|
17927
|
+
// wsClient.subscribeMarkPrice(symbol, 'usdm');
|
|
17928
|
+
// wsClient.subscribeMarkPrice(coinMSymbol, 'coinm');
|
|
17929
|
+
// wsClient.subscribeAllMarketMarkPrice('usdm');
|
|
17930
|
+
// wsClient.subscribeAllMarketMarkPrice('coinm');
|
|
17931
|
+
// wsClient.subscribeKlines(symbol, '1m', 'usdm');
|
|
17932
|
+
// wsClient.subscribeContinuousContractKlines(
|
|
17933
|
+
// symbol,
|
|
17934
|
+
// 'perpetual',
|
|
17935
|
+
// '1m',
|
|
17936
|
+
// 'usdm',
|
|
17937
|
+
// );
|
|
17938
|
+
// wsClient.subscribeIndexKlines(coinMSymbol2, '1m');
|
|
17939
|
+
// wsClient.subscribeMarkPriceKlines(coinMSymbol, '1m');
|
|
17940
|
+
// wsClient.subscribeSymbolMini24hrTicker(symbol, 'spot'); // 0116 265 5309, opt 1
|
|
17941
|
+
// wsClient.subscribeSymbolMini24hrTicker(symbol, 'usdm');
|
|
17942
|
+
// wsClient.subscribeSymbolMini24hrTicker(coinMSymbol, 'coinm');
|
|
17943
|
+
// wsClient.subscribeSymbol24hrTicker(symbol, 'spot');
|
|
17944
|
+
// wsClient.subscribeSymbol24hrTicker(symbol, 'usdm');
|
|
17945
|
+
// wsClient.subscribeSymbol24hrTicker(coinMSymbol, 'coinm');
|
|
17946
|
+
// wsClient.subscribeAllMini24hrTickers('spot');
|
|
17947
|
+
// wsClient.subscribeAllMini24hrTickers('usdm');
|
|
17948
|
+
// wsClient.subscribeAllMini24hrTickers('coinm');
|
|
17949
|
+
// wsClient.subscribeAll24hrTickers('spot');
|
|
17950
|
+
// wsClient.subscribeAll24hrTickers('usdm');
|
|
17951
|
+
// wsClient.subscribeAll24hrTickers('coinm');
|
|
17952
|
+
// wsClient.subscribeSymbolLiquidationOrders(symbol, 'usdm');
|
|
17953
|
+
// wsClient.subscribeAllLiquidationOrders('usdm');
|
|
17954
|
+
// wsClient.subscribeAllLiquidationOrders('coinm');
|
|
17955
|
+
// wsClient.subscribeSpotSymbol24hrTicker(symbol);
|
|
17956
|
+
// wsClient.subscribeSpotPartialBookDepth('ETHBTC', 5, 1000);
|
|
17957
|
+
// wsClient.subscribeAllRollingWindowTickers('spot', '1d');
|
|
17958
|
+
// wsClient.subscribeSymbolBookTicker(symbol, 'spot');
|
|
17959
|
+
// wsClient.subscribePartialBookDepths(symbol, 5, 100, 'spot');
|
|
17960
|
+
// wsClient.subscribeDiffBookDepth(symbol, 100, 'spot');
|
|
17961
|
+
// wsClient.subscribeContractInfoStream('usdm');
|
|
17962
|
+
// wsClient.subscribeContractInfoStream('coinm');
|
|
17930
17963
|
|
|
17931
17964
|
================
|
|
17932
17965
|
File: src/main-client.ts
|
|
@@ -22147,6 +22180,80 @@ client
|
|
|
22147
22180
|
|
|
22148
22181
|
See [coinm-client.ts](./src/coinm-client.ts) for further information.
|
|
22149
22182
|
|
|
22183
|
+
## Market Maker Endpoints
|
|
22184
|
+
|
|
22185
|
+
Binance provides specialized market maker endpoints for qualified high-frequency trading users who have enrolled in at least one of the Futures Liquidity Provider Programs, including the USDⓈ-M Futures Maker Program, COIN-M Futures Maker Program, and USDⓈ-M Futures Taker Program.
|
|
22186
|
+
|
|
22187
|
+
These endpoints provide the same functionality as regular endpoints but with optimized routing for market makers. For more information about eligibility and enrollment, visit: https://www.binance.com/en/support/faq/detail/7df7f3838c3b49e692d175374c3a3283
|
|
22188
|
+
|
|
22189
|
+
### Using Market Maker Endpoints
|
|
22190
|
+
|
|
22191
|
+
To use market maker endpoints, simply add the `useMMEndpoints: true` option when initializing any client (REST API clients, WebSocket clients, or WebSocket API clients):
|
|
22192
|
+
|
|
22193
|
+
#### REST API Clients
|
|
22194
|
+
|
|
22195
|
+
```javascript
|
|
22196
|
+
import { USDMClient, CoinMClient } from 'binance';
|
|
22197
|
+
|
|
22198
|
+
// USD-M Futures with MM endpoints
|
|
22199
|
+
const usdmClient = new USDMClient({
|
|
22200
|
+
api_key: API_KEY,
|
|
22201
|
+
api_secret: API_SECRET,
|
|
22202
|
+
useMMEndpoints: true, // Enable market maker endpoints
|
|
22203
|
+
});
|
|
22204
|
+
|
|
22205
|
+
// COIN-M Futures with MM endpoints
|
|
22206
|
+
const coinmClient = new CoinMClient({
|
|
22207
|
+
api_key: API_KEY,
|
|
22208
|
+
api_secret: API_SECRET,
|
|
22209
|
+
useMMEndpoints: true, // Enable market maker endpoints
|
|
22210
|
+
});
|
|
22211
|
+
```
|
|
22212
|
+
|
|
22213
|
+
#### WebSocket Clients
|
|
22214
|
+
|
|
22215
|
+
```javascript
|
|
22216
|
+
import { WebsocketClient, WebsocketAPIClient } from 'binance';
|
|
22217
|
+
|
|
22218
|
+
// WebSocket consumer with MM endpoints
|
|
22219
|
+
const wsClient = new WebsocketClient({
|
|
22220
|
+
api_key: API_KEY,
|
|
22221
|
+
api_secret: API_SECRET,
|
|
22222
|
+
useMMEndpoints: true, // Enable market maker endpoints
|
|
22223
|
+
});
|
|
22224
|
+
|
|
22225
|
+
// WebSocket API client with MM endpoints
|
|
22226
|
+
const wsApiClient = new WebsocketAPIClient({
|
|
22227
|
+
api_key: API_KEY,
|
|
22228
|
+
api_secret: API_SECRET,
|
|
22229
|
+
useMMEndpoints: true, // Enable market maker endpoints
|
|
22230
|
+
});
|
|
22231
|
+
```
|
|
22232
|
+
|
|
22233
|
+
**Note:** Market maker endpoints are only available for futures products (USD-M and COIN-M). Spot, margin, and other product groups use the regular endpoints regardless of the `useMMEndpoints` setting. Market maker endpoints are also not available on testnet environments.
|
|
22234
|
+
|
|
22235
|
+
### Best practice
|
|
22236
|
+
|
|
22237
|
+
Since market maker endpoints are only available for some of the futures endpoints, you may need to use multiple client instances if your algorithm needs to use both regular and MM endpoints.
|
|
22238
|
+
|
|
22239
|
+
```javascript
|
|
22240
|
+
import { USDMClient } from 'binance';
|
|
22241
|
+
|
|
22242
|
+
// MM client for USD-M futures
|
|
22243
|
+
const futuresMMClient = new USDMClient({
|
|
22244
|
+
api_key: API_KEY,
|
|
22245
|
+
api_secret: API_SECRET,
|
|
22246
|
+
useMMEndpoints: true, // Use MM endpoints for futures
|
|
22247
|
+
});
|
|
22248
|
+
|
|
22249
|
+
// Regular client for USD-M futures
|
|
22250
|
+
const futuresRegularClient = new USDMClient({
|
|
22251
|
+
api_key: API_KEY,
|
|
22252
|
+
api_secret: API_SECRET,
|
|
22253
|
+
useMMEndpoints: false, // Use regular endpoints for futures
|
|
22254
|
+
});
|
|
22255
|
+
```
|
|
22256
|
+
|
|
22150
22257
|
## WebSockets
|
|
22151
22258
|
|
|
22152
22259
|
### WebSocket Consumers
|
|
@@ -23529,7 +23636,7 @@ File: package.json
|
|
|
23529
23636
|
================
|
|
23530
23637
|
{
|
|
23531
23638
|
"name": "binance",
|
|
23532
|
-
"version": "3.0.
|
|
23639
|
+
"version": "3.0.7",
|
|
23533
23640
|
"description": "Professional Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & end-to-end tests.",
|
|
23534
23641
|
"main": "lib/index.js",
|
|
23535
23642
|
"types": "lib/index.d.ts",
|
|
@@ -23627,6 +23734,7 @@ File: package.json
|
|
|
23627
23734
|
|
|
23628
23735
|
|
|
23629
23736
|
|
|
23737
|
+
|
|
23630
23738
|
================================================================
|
|
23631
23739
|
End of Codebase
|
|
23632
23740
|
================================================================
|