Skip to content

Commit 6d61261

Browse files
committed
balances updated
1 parent 54daba7 commit 6d61261

File tree

1 file changed

+5
-72
lines changed

1 file changed

+5
-72
lines changed

php-binance-api.php

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ class API
6060
protected $requestCount = 0; // /< This stores the amount of API requests
6161
protected $httpDebug = false; // /< If you enable this, curl will output debugging information
6262
protected $subscriptions = []; // /< View all websocket subscriptions
63-
protected $btc_value = 0.00; // /< value of available spot assets
64-
protected $btc_total = 0.00;
65-
protected $futures_btc_value = 0.00; // /< value of available futures assets
66-
protected $futures_btc_total = 0.00;
6763

6864
// /< value of available onOrder assets
6965

@@ -1299,19 +1295,15 @@ public function depth(string $symbol, int $limit = 100)
12991295
*
13001296
* $balances = $api->balances();
13011297
*
1302-
* @param bool $priceData (optional) array of the symbols and their prices that balances are required for (supported only for spot)
13031298
* @param string $market_type (optional) market type - "spot" or "futures" (default is "spot")
13041299
* @param int $recvWindow (optional) the time in milliseconds to wait for the transfer to complete (not for spot)
13051300
* @param string $api_version (optional) not for spot - the api version to use (default is v2)
13061301
*
13071302
* @return array with error message or array of balances
13081303
* @throws \Exception
13091304
*/
1310-
public function balances($priceData = false, string $market_type = 'spot', $recvWindow = null, string $api_version = 'v2')
1305+
public function balances(string $market_type = 'spot', $recvWindow = null, string $api_version = 'v2')
13111306
{
1312-
if (is_array($priceData) === false) {
1313-
$priceData = false;
1314-
}
13151307
$is_spot = $market_type === 'spot';
13161308
$params = [];
13171309
if ($is_spot) {
@@ -1337,7 +1329,7 @@ public function balances($priceData = false, string $market_type = 'spot', $recv
13371329
echo "Error: your balances were empty or unset" . PHP_EOL;
13381330
return [];
13391331
}
1340-
return $this->balanceData($response, $priceData, $market_type);
1332+
return $this->balanceData($response, $market_type);
13411333
}
13421334

13431335
/**
@@ -1838,22 +1830,14 @@ public function candlesticks(string $symbol, string $interval = "5m", int $limit
18381830

18391831
/**
18401832
* balanceData Converts all your balances into a nice array
1841-
* If priceData is passed from $api->prices() it will add btcValue & btcTotal to each symbol
1842-
* This function sets $btc_value which is your estimated BTC value of all assets combined and $btc_total which includes amount on order
1843-
*
1844-
* $candles = $api->candlesticks("BNBBTC", "5m");
18451833
*
1846-
* @param $array array of your balances
18471834
* @param $priceData array of prices
18481835
* @return array containing the response
18491836
*/
1850-
protected function balanceData(array $array, $priceData, string $marketType = 'spot')
1837+
protected function balanceData(array $array, string $marketType = 'spot')
18511838
{
18521839
$balances = [];
18531840
$is_spot = $marketType === 'spot';
1854-
if (is_array($priceData)) {
1855-
$btc_value = $btc_total = 0.00;
1856-
}
18571841
if (empty($array) || ($is_spot && empty($array['balances']))) {
18581842
// WPCS: XSS OK.
18591843
echo "balanceData error: Please make sure your system time is synchronized: call \$api->useServerTime() before this function" . PHP_EOL;
@@ -1878,59 +1862,8 @@ protected function balanceData(array $array, $priceData, string $marketType = 's
18781862
"available" => $avaliable,
18791863
"onOrder" => $onOrder,
18801864
"total" => $total,
1865+
"info" => $obj,
18811866
];
1882-
1883-
if (is_array($priceData) === false) {
1884-
continue;
1885-
}
1886-
1887-
if ($total < 0.00000001) {
1888-
continue;
1889-
}
1890-
1891-
if ($is_spot) {
1892-
if ($asset === 'BTC') {
1893-
$balances[$asset]['btcValue'] = $avaliable;
1894-
$balances[$asset]['btcTotal'] = $total;
1895-
$btc_value += $avaliable;
1896-
$btc_total += $total;
1897-
continue;
1898-
} elseif ($asset === 'USDT' || $asset === 'USDC' || $asset === 'PAX' || $asset === 'BUSD') {
1899-
$btcValue = $avaliable / $priceData['BTCUSDT'];
1900-
$btcTotal = $total / $priceData['BTCUSDT'];
1901-
$balances[$asset]['btcValue'] = $btcValue;
1902-
$balances[$asset]['btcTotal'] = $btcTotal;
1903-
$btc_value += $btcValue;
1904-
$btc_total += $btcTotal;
1905-
continue;
1906-
}
1907-
1908-
$symbol = $asset . 'BTC';
1909-
1910-
if ($symbol === 'BTCUSDT') {
1911-
$btcValue = number_format($avaliable / $priceData['BTCUSDT'], 8, '.', '');
1912-
$btcTotal = number_format($total / $priceData['BTCUSDT'], 8, '.', '');
1913-
} elseif (isset($priceData[$symbol]) === false) {
1914-
$btcValue = $btcTotal = 0;
1915-
} else {
1916-
$btcValue = number_format($avaliable * $priceData[$symbol], 8, '.', '');
1917-
$btcTotal = number_format($total * $priceData[$symbol], 8, '.', '');
1918-
}
1919-
1920-
$balances[$asset]['btcValue'] = $btcValue;
1921-
$balances[$asset]['btcTotal'] = $btcTotal;
1922-
$btc_value += $btcValue;
1923-
$btc_total += $btcTotal;
1924-
}
1925-
}
1926-
if (is_array($priceData) && $is_spot) {
1927-
uasort($balances, function ($opA, $opB) {
1928-
if ($opA == $opB)
1929-
return 0;
1930-
return ($opA['btcValue'] < $opB['btcValue']) ? 1 : -1;
1931-
});
1932-
$this->btc_value = $btc_value;
1933-
$this->btc_total = $btc_total;
19341867
}
19351868
return $balances;
19361869
}
@@ -5501,7 +5434,7 @@ public function futuresBalances($recvWindow = null, string $api_version = 'v3')
55015434
if ($api_version !== 'v2' && $api_version !== 'v3') {
55025435
throw new \Exception('futuresBalances: api_version must be either v2 or v3');
55035436
}
5504-
return $this->balances(false, 'futures', $recvWindow, 'v3');
5437+
return $this->balances('futures', $recvWindow, 'v3');
55055438
}
55065439

55075440
/**

0 commit comments

Comments
 (0)