@@ -692,9 +692,6 @@ public function exchangeInfo($symbols = null)
692
692
{
693
693
if (!$ this ->exchangeInfo ) {
694
694
$ arr = array ();
695
- $ arr ['symbols ' ] = array ();
696
- $ parameters = [];
697
-
698
695
if ($ symbols ) {
699
696
if (gettype ($ symbols ) == "string " ) {
700
697
$ parameters ["symbol " ] = $ symbols ;
@@ -706,7 +703,11 @@ public function exchangeInfo($symbols = null)
706
703
} else {
707
704
$ arr = $ this ->httpRequest ("v3/exchangeInfo " );
708
705
}
709
-
706
+ if ((is_array ($ arr ) === false ) || empty ($ arr )) {
707
+ echo "Error: unable to fetch spot exchange info " . PHP_EOL ;
708
+ $ arr = array ();
709
+ $ arr ['symbols ' ] = array ();
710
+ }
710
711
$ this ->exchangeInfo = $ arr ;
711
712
$ this ->exchangeInfo ['symbols ' ] = null ;
712
713
@@ -921,13 +922,16 @@ public function depositAddress(string $asset, $network = null)
921
922
$ return = $ this ->httpRequest ("v1/capital/deposit/address " , "GET " , $ params , true );
922
923
923
924
// Adding for backwards compatibility with wapi
924
- $ return ['asset ' ] = $ return ['coin ' ];
925
- $ return ['addressTag ' ] = $ return ['tag ' ];
926
-
927
- if (!empty ($ return ['address ' ])) {
928
- $ return ['success ' ] = 1 ;
925
+ if (is_array ($ return ) && !empty ($ return )) {
926
+ $ return ['asset ' ] = $ return ['coin ' ];
927
+ $ return ['addressTag ' ] = $ return ['tag ' ];
928
+ if (!empty ($ return ['address ' ])) {
929
+ $ return ['success ' ] = 1 ;
930
+ } else {
931
+ $ return ['success ' ] = 0 ;
932
+ }
929
933
} else {
930
- $ return [ ' success ' ] = 0 ;
934
+ echo " Error: no deposit address found " . PHP_EOL ;
931
935
}
932
936
933
937
return $ return ;
@@ -955,8 +959,12 @@ public function depositHistory(string $asset = null, array $params = [])
955
959
$ return = $ this ->httpRequest ("v1/capital/deposit/hisrec " , "GET " , $ params , true );
956
960
957
961
// Adding for backwards compatibility with wapi
958
- foreach ($ return as $ key =>$ item ) {
959
- $ return [$ key ]['asset ' ] = $ item ['coin ' ];
962
+ if (is_array ($ return ) && !empty ($ return )) {
963
+ foreach ($ return as $ key =>$ item ) {
964
+ $ return [$ key ]['asset ' ] = $ item ['coin ' ];
965
+ }
966
+ } else {
967
+ echo "Error: no deposit history found " . PHP_EOL ;
960
968
}
961
969
962
970
return $ return ;
@@ -1091,7 +1099,7 @@ public function transfer(string $type, string $asset, string $amount, $fromSymbo
1091
1099
*
1092
1100
* @property int $weight 1
1093
1101
*
1094
- * @param string $type (optional ) type of transfer, e.g. MAIN_MARGIN (@see transfer())
1102
+ * @param string $type (mandatory ) type of transfer, e.g. MAIN_MARGIN (@see transfer())
1095
1103
* @param string $startTime (optional) start time in milliseconds
1096
1104
* @param string $endTime (optional) end time in milliseconds
1097
1105
* @param int $limit (optional) the number of records to return (default 10, max 100)
@@ -1158,7 +1166,10 @@ public function prices()
1158
1166
public function price (string $ symbol )
1159
1167
{
1160
1168
$ ticker = $ this ->httpRequest ("v3/ticker/price " , "GET " , ["symbol " => $ symbol ]);
1161
-
1169
+ if (!isset ($ ticker ['price ' ])) {
1170
+ echo "Error: unable to fetch price for $ symbol " . PHP_EOL ;
1171
+ return null ;
1172
+ }
1162
1173
return $ ticker ['price ' ];
1163
1174
}
1164
1175
@@ -1283,6 +1294,14 @@ public function depth(string $symbol, int $limit = 100)
1283
1294
"symbol " => $ symbol ,
1284
1295
"limit " => $ limit ,
1285
1296
]);
1297
+ if (is_array ($ json ) === false ) {
1298
+ echo "Error: unable to fetch depth " . PHP_EOL ;
1299
+ $ json = [];
1300
+ }
1301
+ if (empty ($ json )) {
1302
+ echo "Error: depth were empty " . PHP_EOL ;
1303
+ return $ json ;
1304
+ }
1286
1305
if (isset ($ this ->info [$ symbol ]) === false ) {
1287
1306
$ this ->info [$ symbol ] = [];
1288
1307
}
@@ -3112,7 +3131,7 @@ public function accountSnapshot($type, $nbrDays = 5, $startTime = 0, $endTime =
3112
3131
if ($ startTime > 0 )
3113
3132
$ params ['startTime ' ] = $ startTime ;
3114
3133
if ($ endTime > 0 )
3115
- $ params ['endTime ' ] = $ startTime ;
3134
+ $ params ['endTime ' ] = $ endTime ;
3116
3135
if ($ nbrDays != 5 )
3117
3136
$ params ['limit ' ] = $ nbrDays ;
3118
3137
@@ -3249,6 +3268,14 @@ public function ocoOrder(string $side, string $symbol, $quantity, $price, $stopp
3249
3268
public function avgPrice (string $ symbol )
3250
3269
{
3251
3270
$ ticker = $ this ->httpRequest ("v3/avgPrice " , "GET " , ["symbol " => $ symbol ]);
3271
+ if (is_array ($ ticker ) === false ) {
3272
+ echo "Error: unable to fetch avg price " . PHP_EOL ;
3273
+ $ ticker = [];
3274
+ }
3275
+ if (empty ($ ticker )) {
3276
+ echo "Error: avg price was empty " . PHP_EOL ;
3277
+ return null ;
3278
+ }
3252
3279
return $ ticker ['price ' ];
3253
3280
}
3254
3281
@@ -3320,11 +3347,12 @@ public function futuresTime()
3320
3347
public function futuresExchangeInfo ()
3321
3348
{
3322
3349
if (!$ this ->futuresExchangeInfo ) {
3323
- $ arr = array ();
3324
- $ arr ['symbols ' ] = array ();
3325
-
3326
3350
$ arr = $ this ->httpRequest ("v1/exchangeInfo " , "GET " , [ 'fapi ' => true ]);
3327
-
3351
+ if ((is_array ($ arr ) === false ) || empty ($ arr )) {
3352
+ echo "Error: unable to fetch futures exchange info " . PHP_EOL ;
3353
+ $ arr = array ();
3354
+ $ arr ['symbols ' ] = array ();
3355
+ }
3328
3356
$ this ->futuresExchangeInfo = $ arr ;
3329
3357
$ this ->futuresExchangeInfo ['symbols ' ] = null ;
3330
3358
@@ -3369,6 +3397,14 @@ public function futuresDepth(string $symbol, int $limit = null)
3369
3397
$ params ['limit ' ] = $ limit ;
3370
3398
}
3371
3399
$ json = $ this ->httpRequest ("v1/depth " , "GET " , $ params );
3400
+ if (is_array ($ json ) === false ) {
3401
+ echo "Error: unable to fetch futures depth " . PHP_EOL ;
3402
+ $ json = [];
3403
+ }
3404
+ if (empty ($ json )) {
3405
+ echo "Error: futures depth were empty " . PHP_EOL ;
3406
+ return $ json ;
3407
+ }
3372
3408
if (isset ($ this ->info [$ symbol ]) === false ) {
3373
3409
$ this ->info [$ symbol ] = [];
3374
3410
$ this ->info [$ symbol ]['futures ' ] = [];
@@ -3588,12 +3624,12 @@ public function futuresMarkPriceCandlesticks(string $symbol, string $interval =
3588
3624
}
3589
3625
3590
3626
/**
3591
- * futuresPremiumIndexKlines get the candles for the given intervals
3627
+ * futuresPremiumIndexCandlesticks get the candles for the given intervals
3592
3628
* 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
3593
3629
*
3594
3630
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Premium-Index-Kline-Data
3595
3631
*
3596
- * $candles = $api->futuresPremiumIndexKlines ("ETHBTC", "5m");
3632
+ * $candles = $api->futuresPremiumIndexCandlesticks ("ETHBTC", "5m");
3597
3633
*
3598
3634
* @property int $weight 5
3599
3635
* for limit < 100 - weight 1
@@ -3610,7 +3646,7 @@ public function futuresMarkPriceCandlesticks(string $symbol, string $interval =
3610
3646
* @return array containing the response
3611
3647
* @throws \Exception
3612
3648
*/
3613
- public function futuresPremiumIndexKlines (string $ symbol , string $ interval = '5m ' , int $ limit = null , $ startTime = null , $ endTime = null )
3649
+ public function futuresPremiumIndexCandlesticks (string $ symbol , string $ interval = '5m ' , int $ limit = null , $ startTime = null , $ endTime = null )
3614
3650
{
3615
3651
return $ this ->futuresCandlesticksHelper ($ symbol , $ interval , $ limit , $ startTime , $ endTime , 'premiumIndexKlines ' );
3616
3652
}
@@ -3627,11 +3663,11 @@ private function futuresCandlesticksHelper($symbol, $interval, $limit, $startTim
3627
3663
if (!isset ($ this ->charts ['futures ' ][$ symbol ])) {
3628
3664
$ this ->charts ['futures ' ][$ symbol ] = [];
3629
3665
}
3630
- if (!isset ($ this ->charts ['futures ' ][$ symbol ][$ type ])) {
3631
- $ this ->charts ['futures ' ][$ symbol ][$ type ] = [];
3666
+ if (!isset ($ this ->charts ['futures ' ][$ symbol ][$ contractType ])) {
3667
+ $ this ->charts ['futures ' ][$ symbol ][$ contractType ] = [];
3632
3668
}
3633
- if (!isset ($ this ->charts ['futures ' ][$ symbol ][$ type ][$ interval ])) {
3634
- $ this ->charts ['futures ' ][$ symbol ][$ type ][$ interval ] = [];
3669
+ if (!isset ($ this ->charts ['futures ' ][$ symbol ][$ contractType ][$ interval ])) {
3670
+ $ this ->charts ['futures ' ][$ symbol ][$ contractType ][$ interval ] = [];
3635
3671
}
3636
3672
$ params = [
3637
3673
'interval ' => $ interval ,
@@ -3800,6 +3836,10 @@ public function futuresPrice(string $symbol)
3800
3836
'fapi ' => true ,
3801
3837
];
3802
3838
$ ticker = $ this ->httpRequest ("v1/ticker/price " , "GET " , $ parameters );
3839
+ if (!isset ($ ticker ['price ' ])) {
3840
+ echo "Error: unable to fetch futures price for $ symbol " . PHP_EOL ;
3841
+ return null ;
3842
+ }
3803
3843
return $ ticker ['price ' ];
3804
3844
}
3805
3845
@@ -3844,6 +3884,10 @@ public function futuresPriceV2(string $symbol)
3844
3884
'fapi ' => true ,
3845
3885
];
3846
3886
$ ticker = $ this ->httpRequest ("v2/ticker/price " , "GET " , $ parameters );
3887
+ if (!isset ($ ticker ['price ' ])) {
3888
+ echo "Error: unable to fetch futures price for $ symbol " . PHP_EOL ;
3889
+ return null ;
3890
+ }
3847
3891
return $ ticker ['price ' ];
3848
3892
}
3849
3893
@@ -3943,7 +3987,7 @@ public function futuresOpenInterest(string $symbol): array
3943
3987
* symbolPeriodLimitStartEndRequest
3944
3988
* helper for routing GET methods that require symbol, period, limit, startTime and endTime
3945
3989
*/
3946
- private function symbolPeriodLimitStartEndContractTypeRequest ($ symbol , $ period , $ limit , $ startTime , $ endTime , $ url , $ base = 'fapi ' , $ contractType = null )
3990
+ private function symbolPeriodLimitStartEndRequest ($ symbol , $ period , $ limit , $ startTime , $ endTime , $ url , $ base = 'fapi ' , $ contractType = null )
3947
3991
{
3948
3992
$ parameters = [
3949
3993
'symbol ' => $ symbol ,
@@ -4477,7 +4521,7 @@ public function futuresSellTest(string $symbol, $quantity = null, $price = null,
4477
4521
* @return array containing the request
4478
4522
* @throws \Exception
4479
4523
*/
4480
- protected function createBatchOrdersRequest (array $ orders )
4524
+ protected function createBatchOrdersRequest (array $ orders, bool $ edit = false )
4481
4525
{
4482
4526
$ formatedOrders = [];
4483
4527
for ($ index = 0 ; $ index < count ($ orders ); $ index ++) {
@@ -4491,6 +4535,9 @@ protected function createBatchOrdersRequest(array $orders)
4491
4535
if (!isset ($ order ['flags ' ])) {
4492
4536
$ order ['flags ' ] = [];
4493
4537
}
4538
+ if (!isset ($ order ['type ' ])) {
4539
+ $ order ['type ' ] = 'LIMIT ' ;
4540
+ }
4494
4541
$ formatedOrder = $ this ->createFuturesOrderRequest (
4495
4542
$ order ['side ' ],
4496
4543
$ order ['symbol ' ],
@@ -4503,11 +4550,15 @@ protected function createBatchOrdersRequest(array $orders)
4503
4550
// remove recvWindow from the order
4504
4551
unset($ formatedOrder ['recvWindow ' ]);
4505
4552
}
4506
- if (isset ($ order ['orderId ' ])) {
4507
- $ formatedOrder ['orderId ' ] = $ order ['orderId ' ];
4508
- }
4509
- if (isset ($ order ['origClientOrderId ' ])) {
4510
- $ formatedOrder ['origClientOrderId ' ] = $ order ['origClientOrderId ' ];
4553
+ if ($ edit ) {
4554
+ if (isset ($ order ['orderId ' ])) {
4555
+ $ formatedOrder ['orderId ' ] = $ order ['orderId ' ];
4556
+ }
4557
+ if (isset ($ order ['origClientOrderId ' ])) {
4558
+ $ formatedOrder ['origClientOrderId ' ] = $ order ['origClientOrderId ' ];
4559
+ }
4560
+ unset($ formatedOrder ['type ' ]);
4561
+ unset($ formatedOrder ['newClientOrderId ' ]);
4511
4562
}
4512
4563
$ formatedOrders [$ index ] = $ formatedOrder ;
4513
4564
}
@@ -4580,6 +4631,7 @@ public function futuresEditOrder(string $symbol, string $side, string $quantity,
4580
4631
$ opt ['orderId ' ] = $ orderId ;
4581
4632
}
4582
4633
unset($ opt ['type ' ]);
4634
+ unset($ opt ['newClientOrderId ' ]);
4583
4635
$ opt ['fapi ' ] = true ;
4584
4636
return $ this ->httpRequest ("v1/order " , 'PUT ' , $ opt , true );
4585
4637
}
@@ -4601,14 +4653,13 @@ public function futuresEditOrders(array $orders, $recvWindow = null)
4601
4653
$ params = [
4602
4654
'fapi ' => true ,
4603
4655
];
4604
- $ formatedOrders = $ this ->createBatchOrdersRequest ($ orders );
4656
+ $ formatedOrders = $ this ->createBatchOrdersRequest ($ orders, true );
4605
4657
if ($ recvWindow ) {
4606
4658
$ params ['recvWindow ' ] = $ recvWindow ;
4607
4659
}
4608
4660
// current endpoint accepts orders list as a json string in the query string
4609
- $ encodedOrders = json_encode ($ formatedOrders );
4610
- $ url = 'v1/batchOrders?batchOrders= ' . $ encodedOrders ;
4611
- return $ this ->httpRequest ($ url , 'PUT ' , $ params , true );
4661
+ $ params ['batchOrders ' ] = json_encode ($ formatedOrders );
4662
+ return $ this ->httpRequest ("v1/batchOrders " , 'PUT ' , $ params , true );
4612
4663
}
4613
4664
4614
4665
/**
@@ -4710,8 +4761,8 @@ public function futuresCancelBatchOrders(string $symbol, $orderIdList = null, $o
4710
4761
// remove quotes and spaces
4711
4762
$ params ['orderIdList ' ] = str_replace (' ' , '' , str_replace ('" ' , '' , str_replace ("' " , '' , $ idsString )));
4712
4763
} else if ($ origClientOrderIdList ) {
4713
- // remove spaces
4714
- $ params ['origClientOrderIdList ' ] = str_replace (' ' , '' , json_encode ($ origClientOrderIdList ));
4764
+ // remove spaces between the ids
4765
+ $ params ['origClientOrderIdList ' ] = str_replace (', ' , ', ' , json_encode ($ origClientOrderIdList ));
4715
4766
} else {
4716
4767
throw new \Exception ('futuresCancelBatchOrders: either orderIdList or origClientOrderIdList must be set ' );
4717
4768
}
@@ -6054,7 +6105,7 @@ public function convertAccept(string $quoteId, int $recvWindow = null, array $pa
6054
6105
$ request = [
6055
6106
'quoteId ' => $ quoteId ,
6056
6107
];
6057
- return $ this ->fapiRequest ("v1/cconvert /acceptQuote " , 'POST ' , array_merge ($ request , $ params ), true , $ recvWindow );
6108
+ return $ this ->fapiRequest ("v1/convert /acceptQuote " , 'POST ' , array_merge ($ request , $ params ), true , $ recvWindow );
6058
6109
}
6059
6110
6060
6111
/**
@@ -6098,4 +6149,4 @@ public function convertStatus($orderId = null, $quoteId = null)
6098
6149
}
6099
6150
return $ this ->httpRequest ("v1/convert/orderStatus " , 'GET ' , $ params , true );
6100
6151
}
6101
- }
6152
+ }
0 commit comments