@@ -3,7 +3,20 @@ import nock from 'nock';
33import Binance , { ErrorCodes } from 'index'
44
55// Warning: For now these tests can't run in parallel due to nock interceptors
6- const binance = Binance ( )
6+ const binance = Binance ( {
7+ apiKey : 'testkey' ,
8+ apiSecret : 'test'
9+ } )
10+ const demoBinance = Binance ( {
11+ testnet : true ,
12+ } ) ;
13+
14+ function urlToObject ( queryString ) {
15+ const params = new URLSearchParams ( queryString ) ;
16+ const obj = Object . fromEntries ( params . entries ( ) ) ;
17+ return obj ;
18+ }
19+
720
821let interceptedUrl = null ;
922let interceptedBody = null ;
@@ -34,6 +47,16 @@ test.serial.beforeEach(t => {
3447 } ) ;
3548} ) ;
3649
50+ test . serial ( '[Rest] Spot demo url' , async t => {
51+ await demoBinance . time ( ) ;
52+ t . is ( interceptedUrl , 'https://demo-api.binance.com/api/v3/time' )
53+ } )
54+
55+ test . serial ( '[Rest] Futures demo url' , async t => {
56+ await demoBinance . futuresTime ( ) ;
57+ t . is ( interceptedUrl , 'https://demo-fapi.binance.com/fapi/v1/time' )
58+ } )
59+
3760test . serial ( '[REST] Prices no symbol' , async t => {
3861 await binance . prices ( ) ;
3962 t . is ( interceptedUrl , 'https://api.binance.com/api/v3/ticker/price' )
@@ -61,4 +84,183 @@ test.serial('[REST] Futures Orderbook', async t => {
6184 // it can throw an error because of the mocked response
6285 }
6386 t . is ( interceptedUrl , 'https://fapi.binance.com/fapi/v1/depth?symbol=BTCUSDT' )
64- } )
87+ } )
88+
89+ test . serial ( '[REST] OHLCVS' , async t => {
90+ try {
91+ await binance . candles ( { symbol : 'BTCUSDT' } )
92+ } catch ( e ) {
93+ // it can throw an error because of the mocked response
94+ }
95+ t . true ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/klines?interval=5m&symbol=BTCUSDT' ) )
96+ } )
97+
98+ test . serial ( '[REST] Futures OHLCVS' , async t => {
99+ try {
100+ await binance . futuresCandles ( { symbol : 'BTCUSDT' , interval : '30m' } )
101+ } catch ( e ) {
102+ // it can throw an error because of the mocked response
103+ }
104+ t . is ( interceptedUrl , 'https://fapi.binance.com/fapi/v1/klines?interval=30m&symbol=BTCUSDT' )
105+ } )
106+
107+ test . serial ( '[REST] Recent Trades' , async t => {
108+ await binance . trades ( { symbol : 'BTCUSDT' , limit : 500 } )
109+ t . is ( interceptedUrl , 'https://api.binance.com/api/v3/trades?symbol=BTCUSDT&limit=500' )
110+ } )
111+
112+ test . serial ( '[REST] Agg Trades' , async t => {
113+ try {
114+ await binance . aggTrades ( { symbol : 'BTCUSDT' } )
115+ } catch ( e ) {
116+ // it can throw an error because of the mocked response
117+ }
118+ t . is ( interceptedUrl , 'https://api.binance.com/api/v3/aggTrades?symbol=BTCUSDT' )
119+ } )
120+
121+ test . serial ( '[REST] FuturesTrades' , async t => {
122+ try {
123+ await binance . futuresTrades ( { symbol : 'BTCUSDT' } )
124+ } catch ( e ) {
125+ // it can throw an error because of the mocked response
126+ }
127+ t . is ( interceptedUrl , 'https://fapi.binance.com/fapi/v1/trades?symbol=BTCUSDT' )
128+ } )
129+
130+ test . serial ( '[REST] FuturesAggTrades' , async t => {
131+ try {
132+ await binance . futuresAggTrades ( { symbol : 'BTCUSDT' } )
133+ } catch ( e ) {
134+ // it can throw an error because of the mocked response
135+ }
136+ t . is ( interceptedUrl , 'https://fapi.binance.com/fapi/v1/aggTrades?symbol=BTCUSDT' )
137+ } )
138+
139+ test . serial ( '[REST] PositionRisk V2' , async t => {
140+ await binance . futuresPositionRisk ( )
141+ t . true ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v2/positionRisk' ) )
142+ } )
143+
144+
145+ test . serial ( '[REST] CancelOrder' , async t => {
146+ await binance . cancelOrder ( { symbol : 'LTCUSDT' , orderId : '34234234' } )
147+ t . true ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/order' ) )
148+ const obj = urlToObject ( interceptedUrl . replace ( 'https://api.binance.com/api/v3/order' , '' ) )
149+ t . is ( obj . symbol , 'LTCUSDT' )
150+ t . is ( obj . orderId , '34234234' )
151+ } )
152+
153+ test . serial ( '[REST] Futures CancelOrder' , async t => {
154+ await binance . futuresCancelOrder ( { symbol : 'LTCUSDT' , orderId : '34234234' } )
155+ t . true ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
156+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order' , '' ) )
157+ t . is ( obj . symbol , 'LTCUSDT' )
158+ t . is ( obj . orderId , '34234234' )
159+ } )
160+
161+
162+ test . serial ( '[REST] MarketBuy' , async t => {
163+ await binance . order ( { symbol : 'LTCUSDT' , side : 'BUY' , type : 'MARKET' , quantity : 0.5 } )
164+ t . true ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/order' ) )
165+ const body = interceptedUrl . replace ( 'https://api.binance.com/api/v3/order' , '' )
166+ const obj = urlToObject ( body )
167+ t . is ( obj . symbol , 'LTCUSDT' )
168+ t . is ( obj . side , 'BUY' )
169+ t . is ( obj . type , 'MARKET' )
170+ t . is ( obj . quantity , '0.5' )
171+ } )
172+
173+ test . serial ( '[REST] MarketSell' , async t => {
174+ await binance . order ( { symbol : 'LTCUSDT' , side : 'SELL' , type : 'MARKET' , quantity : 0.5 } )
175+ t . true ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/order' ) )
176+ const body = interceptedUrl . replace ( 'https://api.binance.com/api/v3/order' , '' )
177+ const obj = urlToObject ( body )
178+ t . is ( obj . symbol , 'LTCUSDT' )
179+ t . is ( obj . side , 'SELL' )
180+ t . is ( obj . type , 'MARKET' )
181+ t . is ( obj . quantity , '0.5' )
182+ } )
183+
184+ test . serial ( '[REST] LimitBuy' , async t => {
185+ await binance . order ( { symbol : 'LTCUSDT' , side : 'BUY' , type : 'LIMIT' , quantity : 0.5 , price : 100 } )
186+ t . true ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/order' ) )
187+ const body = interceptedUrl . replace ( 'https://api.binance.com/api/v3/order' , '' )
188+ const obj = urlToObject ( body )
189+ t . is ( obj . symbol , 'LTCUSDT' )
190+ t . is ( obj . side , 'BUY' )
191+ t . is ( obj . type , 'LIMIT' )
192+ t . is ( obj . quantity , '0.5' )
193+ } )
194+
195+ test . serial ( '[REST] LimitSell' , async t => {
196+ await binance . order ( { symbol : 'LTCUSDT' , side : 'SELL' , type : 'LIMIT' , quantity : 0.5 , price : 100 } )
197+ t . true ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/order' ) )
198+ const body = interceptedUrl . replace ( 'https://api.binance.com/api/v3/order' , '' )
199+ const obj = urlToObject ( body )
200+ t . is ( obj . symbol , 'LTCUSDT' )
201+ t . is ( obj . side , 'SELL' )
202+ t . is ( obj . type , 'LIMIT' )
203+ t . is ( obj . quantity , '0.5' )
204+ } )
205+
206+
207+ test . serial ( '[REST] Futures MarketBuy' , async t => {
208+ await binance . futuresOrder ( { symbol : 'LTCUSDT' , side : 'BUY' , type : 'MARKET' , quantity : 0.5 } )
209+ t . true ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
210+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
211+ t . is ( obj . symbol , 'LTCUSDT' )
212+ t . is ( obj . side , 'BUY' )
213+ t . is ( obj . type , 'MARKET' )
214+ t . is ( obj . quantity , '0.5' )
215+ } )
216+
217+ test . serial ( '[REST] Futures MarketSell' , async t => {
218+ await binance . futuresOrder ( { symbol : 'LTCUSDT' , side : 'SELL' , type : 'MARKET' , quantity : 0.5 } )
219+ t . true ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
220+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
221+ t . is ( obj . symbol , 'LTCUSDT' )
222+ t . is ( obj . side , 'SELL' )
223+ t . is ( obj . type , 'MARKET' )
224+ t . is ( obj . quantity , '0.5' )
225+ } )
226+
227+ test . serial ( '[REST] Futures LimitBuy' , async t => {
228+ await binance . futuresOrder ( { symbol : 'LTCUSDT' , side : 'BUY' , type : 'LIMIT' , quantity : 0.5 , price : 100 } )
229+ t . true ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
230+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
231+ t . is ( obj . symbol , 'LTCUSDT' )
232+ t . is ( obj . side , 'BUY' )
233+ t . is ( obj . type , 'LIMIT' )
234+ t . is ( obj . quantity , '0.5' )
235+ } )
236+
237+ test . serial ( '[REST] Futures LimitSell' , async t => {
238+ await binance . futuresOrder ( { symbol : 'LTCUSDT' , side : 'SELL' , type : 'LIMIT' , quantity : 0.5 , price : 100 } )
239+ t . true ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
240+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
241+ t . is ( obj . symbol , 'LTCUSDT' )
242+ t . is ( obj . side , 'SELL' )
243+ t . is ( obj . type , 'LIMIT' )
244+ t . is ( obj . quantity , '0.5' )
245+ } )
246+
247+
248+ test . serial ( '[REST] Futures cancel order' , async t => {
249+ await binance . futuresCancelOrder ( { symbol : 'LTCUSDT' , orderId : '34234234' } )
250+ const url = 'https://fapi.binance.com/fapi/v1/order'
251+ t . true ( interceptedUrl . startsWith ( url ) )
252+ const obj = urlToObject ( interceptedUrl . replace ( url , '' ) )
253+ t . is ( obj . orderId , '34234234' )
254+ t . is ( obj . symbol , 'LTCUSDT' )
255+ } )
256+
257+ test . serial ( '[REST] MarketBuy test' , async t => {
258+ await binance . orderTest ( { symbol : 'LTCUSDT' , side : 'BUY' , type : 'MARKET' , quantity : 0.5 } )
259+ t . true ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/order/test' ) )
260+ const body = interceptedUrl . replace ( 'https://api.binance.com/api/v3/order/test' , '' )
261+ const obj = urlToObject ( body )
262+ t . is ( obj . symbol , 'LTCUSDT' )
263+ t . is ( obj . side , 'BUY' )
264+ t . is ( obj . type , 'MARKET' )
265+ t . is ( obj . quantity , '0.5' )
266+ } )
0 commit comments