Skip to content

Commit 2b69dd5

Browse files
authored
Merge pull request #510 from yzh-pelle/static-tests
Static tests
2 parents 2523de1 + 25775ae commit 2b69dd5

File tree

2 files changed

+2904
-53
lines changed

2 files changed

+2904
-53
lines changed

php-binance-api.php

Lines changed: 92 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,6 @@ public function exchangeInfo($symbols = null)
692692
{
693693
if (!$this->exchangeInfo) {
694694
$arr = array();
695-
$arr['symbols'] = array();
696-
$parameters = [];
697-
698695
if ($symbols) {
699696
if (gettype($symbols) == "string") {
700697
$parameters["symbol"] = $symbols;
@@ -706,7 +703,11 @@ public function exchangeInfo($symbols = null)
706703
} else {
707704
$arr = $this->httpRequest("v3/exchangeInfo");
708705
}
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+
}
710711
$this->exchangeInfo = $arr;
711712
$this->exchangeInfo['symbols'] = null;
712713

@@ -921,13 +922,16 @@ public function depositAddress(string $asset, $network = null)
921922
$return = $this->httpRequest("v1/capital/deposit/address", "GET", $params, true);
922923

923924
// 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+
}
929933
} else {
930-
$return['success'] = 0;
934+
echo "Error: no deposit address found" . PHP_EOL;
931935
}
932936

933937
return $return;
@@ -955,8 +959,12 @@ public function depositHistory(string $asset = null, array $params = [])
955959
$return = $this->httpRequest("v1/capital/deposit/hisrec", "GET", $params, true);
956960

957961
// 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;
960968
}
961969

962970
return $return;
@@ -1091,7 +1099,7 @@ public function transfer(string $type, string $asset, string $amount, $fromSymbo
10911099
*
10921100
* @property int $weight 1
10931101
*
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())
10951103
* @param string $startTime (optional) start time in milliseconds
10961104
* @param string $endTime (optional) end time in milliseconds
10971105
* @param int $limit (optional) the number of records to return (default 10, max 100)
@@ -1158,7 +1166,10 @@ public function prices()
11581166
public function price(string $symbol)
11591167
{
11601168
$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+
}
11621173
return $ticker['price'];
11631174
}
11641175

@@ -1283,6 +1294,14 @@ public function depth(string $symbol, int $limit = 100)
12831294
"symbol" => $symbol,
12841295
"limit" => $limit,
12851296
]);
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+
}
12861305
if (isset($this->info[$symbol]) === false) {
12871306
$this->info[$symbol] = [];
12881307
}
@@ -3112,7 +3131,7 @@ public function accountSnapshot($type, $nbrDays = 5, $startTime = 0, $endTime =
31123131
if ($startTime > 0)
31133132
$params['startTime'] = $startTime;
31143133
if ($endTime > 0)
3115-
$params['endTime'] = $startTime;
3134+
$params['endTime'] = $endTime;
31163135
if ($nbrDays != 5)
31173136
$params['limit'] = $nbrDays;
31183137

@@ -3249,6 +3268,14 @@ public function ocoOrder(string $side, string $symbol, $quantity, $price, $stopp
32493268
public function avgPrice(string $symbol)
32503269
{
32513270
$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+
}
32523279
return $ticker['price'];
32533280
}
32543281

@@ -3320,11 +3347,12 @@ public function futuresTime()
33203347
public function futuresExchangeInfo()
33213348
{
33223349
if (!$this->futuresExchangeInfo) {
3323-
$arr = array();
3324-
$arr['symbols'] = array();
3325-
33263350
$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+
}
33283356
$this->futuresExchangeInfo = $arr;
33293357
$this->futuresExchangeInfo['symbols'] = null;
33303358

@@ -3369,6 +3397,14 @@ public function futuresDepth(string $symbol, int $limit = null)
33693397
$params['limit'] = $limit;
33703398
}
33713399
$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+
}
33723408
if (isset($this->info[$symbol]) === false) {
33733409
$this->info[$symbol] = [];
33743410
$this->info[$symbol]['futures'] = [];
@@ -3588,12 +3624,12 @@ public function futuresMarkPriceCandlesticks(string $symbol, string $interval =
35883624
}
35893625

35903626
/**
3591-
* futuresPremiumIndexKlines get the candles for the given intervals
3627+
* futuresPremiumIndexCandlesticks get the candles for the given intervals
35923628
* 1m,3m,5m,15m,30m,1h,2h,4h,6h,8h,12h,1d,3d,1w,1M
35933629
*
35943630
* @link https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Premium-Index-Kline-Data
35953631
*
3596-
* $candles = $api->futuresPremiumIndexKlines("ETHBTC", "5m");
3632+
* $candles = $api->futuresPremiumIndexCandlesticks("ETHBTC", "5m");
35973633
*
35983634
* @property int $weight 5
35993635
* for limit < 100 - weight 1
@@ -3610,7 +3646,7 @@ public function futuresMarkPriceCandlesticks(string $symbol, string $interval =
36103646
* @return array containing the response
36113647
* @throws \Exception
36123648
*/
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)
36143650
{
36153651
return $this->futuresCandlesticksHelper($symbol, $interval, $limit, $startTime, $endTime, 'premiumIndexKlines');
36163652
}
@@ -3627,11 +3663,11 @@ private function futuresCandlesticksHelper($symbol, $interval, $limit, $startTim
36273663
if (!isset($this->charts['futures'][$symbol])) {
36283664
$this->charts['futures'][$symbol] = [];
36293665
}
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] = [];
36323668
}
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] = [];
36353671
}
36363672
$params = [
36373673
'interval' => $interval,
@@ -3800,6 +3836,10 @@ public function futuresPrice(string $symbol)
38003836
'fapi' => true,
38013837
];
38023838
$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+
}
38033843
return $ticker['price'];
38043844
}
38053845

@@ -3844,6 +3884,10 @@ public function futuresPriceV2(string $symbol)
38443884
'fapi' => true,
38453885
];
38463886
$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+
}
38473891
return $ticker['price'];
38483892
}
38493893

@@ -3943,7 +3987,7 @@ public function futuresOpenInterest(string $symbol): array
39433987
* symbolPeriodLimitStartEndRequest
39443988
* helper for routing GET methods that require symbol, period, limit, startTime and endTime
39453989
*/
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)
39473991
{
39483992
$parameters = [
39493993
'symbol' => $symbol,
@@ -4477,7 +4521,7 @@ public function futuresSellTest(string $symbol, $quantity = null, $price = null,
44774521
* @return array containing the request
44784522
* @throws \Exception
44794523
*/
4480-
protected function createBatchOrdersRequest(array $orders)
4524+
protected function createBatchOrdersRequest(array $orders, bool $edit = false)
44814525
{
44824526
$formatedOrders = [];
44834527
for ($index = 0; $index < count($orders); $index++) {
@@ -4491,6 +4535,9 @@ protected function createBatchOrdersRequest(array $orders)
44914535
if (!isset($order['flags'])) {
44924536
$order['flags'] = [];
44934537
}
4538+
if (!isset($order['type'])) {
4539+
$order['type'] = 'LIMIT';
4540+
}
44944541
$formatedOrder = $this->createFuturesOrderRequest(
44954542
$order['side'],
44964543
$order['symbol'],
@@ -4503,11 +4550,15 @@ protected function createBatchOrdersRequest(array $orders)
45034550
// remove recvWindow from the order
45044551
unset($formatedOrder['recvWindow']);
45054552
}
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']);
45114562
}
45124563
$formatedOrders[$index] = $formatedOrder;
45134564
}
@@ -4580,6 +4631,7 @@ public function futuresEditOrder(string $symbol, string $side, string $quantity,
45804631
$opt['orderId'] = $orderId;
45814632
}
45824633
unset($opt['type']);
4634+
unset($opt['newClientOrderId']);
45834635
$opt['fapi'] = true;
45844636
return $this->httpRequest("v1/order", 'PUT', $opt, true);
45854637
}
@@ -4601,14 +4653,13 @@ public function futuresEditOrders(array $orders, $recvWindow = null)
46014653
$params = [
46024654
'fapi' => true,
46034655
];
4604-
$formatedOrders = $this->createBatchOrdersRequest($orders);
4656+
$formatedOrders = $this->createBatchOrdersRequest($orders, true);
46054657
if ($recvWindow) {
46064658
$params['recvWindow'] = $recvWindow;
46074659
}
46084660
// 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);
46124663
}
46134664

46144665
/**
@@ -4710,8 +4761,8 @@ public function futuresCancelBatchOrders(string $symbol, $orderIdList = null, $o
47104761
// remove quotes and spaces
47114762
$params['orderIdList'] = str_replace(' ', '', str_replace('"', '', str_replace("'", '', $idsString)));
47124763
} 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));
47154766
} else {
47164767
throw new \Exception('futuresCancelBatchOrders: either orderIdList or origClientOrderIdList must be set');
47174768
}
@@ -6054,7 +6105,7 @@ public function convertAccept(string $quoteId, int $recvWindow = null, array $pa
60546105
$request = [
60556106
'quoteId' => $quoteId,
60566107
];
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);
60586109
}
60596110

60606111
/**
@@ -6098,4 +6149,4 @@ public function convertStatus($orderId = null, $quoteId = null)
60986149
}
60996150
return $this->httpRequest("v1/convert/orderStatus", 'GET', $params, true);
61006151
}
6101-
}
6152+
}

0 commit comments

Comments
 (0)