@@ -5,7 +5,24 @@ import Binance from '../node-binance-api.js'
55import nock from 'nock' ;
66const assert = chai . assert ;
77
8- const binance = new Binance ( { } )
8+ const binance = new Binance ( {
9+ APIKEY : 'XXXXXXXXXXXXXXXXXXXXXXX' ,
10+ APISECRET : 'YYYYYYYYYYYYYYYYYYYYYY' ,
11+ } )
12+
13+ const debug = function ( x ) {
14+ if ( typeof ( process . env . node_binance_api ) === 'undefined' ) {
15+ return ;
16+ }
17+ logger . log ( typeof ( x ) ) ;
18+ logger . log ( util . inspect ( x ) ) ;
19+ }
20+
21+ function urlToObject ( queryString ) {
22+ const params = new URLSearchParams ( queryString ) ;
23+ const obj = Object . fromEntries ( params . entries ( ) ) ;
24+ return obj ;
25+ }
926
1027describe ( 'Static tests' , async function ( ) {
1128
@@ -23,6 +40,20 @@ describe( 'Static tests', async function () {
2340 interceptedBody = requestBody ; // Capture the request body
2441 return { success : true } ;
2542 } ) ;
43+ nock ( / .* / )
44+ . post ( / .* / )
45+ . reply ( 200 , function ( uri , requestBody ) {
46+ interceptedUrl = `${ this . req . options . proto } ://${ this . req . options . hostname } ${ uri } ` ;
47+ interceptedBody = requestBody ; // Capture the request body
48+ return { success : true } ;
49+ } ) ;
50+ nock ( / .* / )
51+ . delete ( / .* / )
52+ . reply ( 200 , function ( uri , requestBody ) {
53+ interceptedUrl = `${ this . req . options . proto } ://${ this . req . options . hostname } ${ uri } ` ;
54+ interceptedBody = requestBody ; // Capture the request body
55+ return { success : true } ;
56+ } ) ;
2657 } ) ;
2758
2859 it ( 'FetchTicker' , async function ( ) {
@@ -66,4 +97,112 @@ describe( 'Static tests', async function () {
6697 assert . equal ( interceptedUrl , 'https://fapi.binance.com/fapi/v1/trades?symbol=BTCUSDT' )
6798
6899 } )
100+
101+ it ( 'CancelOrder' , async function ( ) {
102+ await binance . cancel ( 'LTCUSDT' , '34234234' )
103+ assert ( interceptedUrl . startsWith ( 'https://api.binance.com/api/v3/order' ) )
104+ const obj = urlToObject ( interceptedUrl . replace ( 'https://api.binance.com/api/v3/order' , '' ) )
105+ assert . equal ( obj . symbol , 'LTCUSDT' )
106+ assert . equal ( obj . orderId , '34234234' )
107+ } )
108+
109+ it ( 'Futures CancelOrder' , async function ( ) {
110+ await binance . futuresCancel ( 'LTCUSDT' , { 'orderId' : '34234234' } )
111+ assert ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
112+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order' , '' ) )
113+ assert . equal ( obj . symbol , 'LTCUSDT' )
114+ assert . equal ( obj . orderId , '34234234' )
115+ } )
116+
117+ const SPOT_PREFIX = "x-HNA2TXFJ"
118+
119+ it ( 'MarketBuy' , async function ( ) {
120+ await binance . marketBuy ( 'LTCUSDT' , 0.5 )
121+ assert . equal ( interceptedUrl , 'https://api.binance.com/api/v3/order' )
122+ const obj = urlToObject ( interceptedBody )
123+ assert . equal ( obj . symbol , 'LTCUSDT' )
124+ assert . equal ( obj . side , 'BUY' )
125+ assert . equal ( obj . type , 'MARKET' )
126+ assert . equal ( obj . quantity , 0.5 )
127+ assert ( obj . newClientOrderId . startsWith ( SPOT_PREFIX ) )
128+ } )
129+
130+ it ( 'MarketSell' , async function ( ) {
131+ await binance . marketSell ( 'LTCUSDT' , 0.5 )
132+ assert . equal ( interceptedUrl , 'https://api.binance.com/api/v3/order' )
133+ const obj = urlToObject ( interceptedBody )
134+ assert . equal ( obj . symbol , 'LTCUSDT' )
135+ assert . equal ( obj . side , 'SELL' )
136+ assert . equal ( obj . type , 'MARKET' )
137+ assert . equal ( obj . quantity , 0.5 )
138+ assert ( obj . newClientOrderId . startsWith ( SPOT_PREFIX ) )
139+ } )
140+
141+ it ( 'LimitBuy' , async function ( ) {
142+ await binance . order ( 'BUY' , 'LTCUSDT' , 0.5 )
143+ assert . equal ( interceptedUrl , 'https://api.binance.com/api/v3/order' )
144+ const obj = urlToObject ( interceptedBody )
145+ assert . equal ( obj . symbol , 'LTCUSDT' )
146+ assert . equal ( obj . side , 'BUY' )
147+ assert . equal ( obj . type , 'LIMIT' )
148+ assert . equal ( obj . quantity , 0.5 )
149+ assert ( obj . newClientOrderId . startsWith ( SPOT_PREFIX ) )
150+ } )
151+
152+ it ( 'LimitSell' , async function ( ) {
153+ await binance . order ( 'SELL' , 'LTCUSDT' , 0.5 )
154+ assert . equal ( interceptedUrl , 'https://api.binance.com/api/v3/order' )
155+ const obj = urlToObject ( interceptedBody )
156+ assert . equal ( obj . symbol , 'LTCUSDT' )
157+ assert . equal ( obj . side , 'SELL' )
158+ assert . equal ( obj . type , 'LIMIT' )
159+ assert . equal ( obj . quantity , 0.5 )
160+ assert ( obj . newClientOrderId . startsWith ( SPOT_PREFIX ) )
161+ } )
162+
163+ const CONTRACT_PREFIX = "x-Cb7ytekJ"
164+
165+ it ( 'Futures MarketBuy' , async function ( ) {
166+ await binance . futuresMarketBuy ( 'LTCUSDT' , 0.5 )
167+ assert . isTrue ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
168+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
169+ assert . equal ( obj . symbol , 'LTCUSDT' )
170+ assert . equal ( obj . side , 'BUY' )
171+ assert . equal ( obj . type , 'MARKET' )
172+ assert . equal ( obj . quantity , 0.5 )
173+ assert ( obj . newClientOrderId . startsWith ( CONTRACT_PREFIX ) )
174+ } )
175+
176+ it ( 'Futures MarketSell' , async function ( ) {
177+ await binance . futuresMarketSell ( 'LTCUSDT' , 0.5 )
178+ assert . isTrue ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
179+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
180+ assert . equal ( obj . symbol , 'LTCUSDT' )
181+ assert . equal ( obj . side , 'SELL' )
182+ assert . equal ( obj . type , 'MARKET' )
183+ assert . equal ( obj . quantity , 0.5 )
184+ assert ( obj . newClientOrderId . startsWith ( CONTRACT_PREFIX ) )
185+ } )
186+
187+ it ( 'Futures LimitBuy' , async function ( ) {
188+ await binance . futuresOrder ( 'BUY' , 'LTCUSDT' , 0.5 , 100 )
189+ assert . isTrue ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
190+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
191+ assert . equal ( obj . symbol , 'LTCUSDT' )
192+ assert . equal ( obj . side , 'BUY' )
193+ assert . equal ( obj . type , 'LIMIT' )
194+ assert . equal ( obj . quantity , 0.5 )
195+ assert ( obj . newClientOrderId . startsWith ( CONTRACT_PREFIX ) )
196+ } )
197+
198+ it ( 'Futures LimitSell' , async function ( ) {
199+ await binance . futuresOrder ( 'SELL' , 'LTCUSDT' , 0.5 , 100 )
200+ assert . isTrue ( interceptedUrl . startsWith ( 'https://fapi.binance.com/fapi/v1/order' ) )
201+ const obj = urlToObject ( interceptedUrl . replace ( 'https://fapi.binance.com/fapi/v1/order?' , '' ) )
202+ assert . equal ( obj . symbol , 'LTCUSDT' )
203+ assert . equal ( obj . side , 'SELL' )
204+ assert . equal ( obj . type , 'LIMIT' )
205+ assert . equal ( obj . quantity , 0.5 )
206+ assert ( obj . newClientOrderId . startsWith ( CONTRACT_PREFIX ) )
207+ } )
69208} )
0 commit comments