|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Poloniex PHP SDK. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright 2017-2018 Chasovskih Grisha <[email protected]> |
|
9
|
|
|
* @license https://github.com/signulls/poloniex-php-sdk/blob/master/LICENSE MIT |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Poloniex; |
|
13
|
|
|
|
|
14
|
|
|
use Poloniex\Api\{PublicApi, TradingApi}; |
|
15
|
|
|
use Poloniex\Exception\PoloniexException; |
|
16
|
|
|
|
|
17
|
|
|
final class PoloniexManager |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var PublicApi |
|
21
|
|
|
*/ |
|
22
|
|
|
private $publicApi; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var TradingApi |
|
26
|
|
|
*/ |
|
27
|
|
|
private $tradingApi; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* PoloniexManager constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param PublicApi $publicApi |
|
33
|
|
|
* @param TradingApi $tradingApi |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(PublicApi $publicApi, TradingApi $tradingApi) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->publicApi = $publicApi; |
|
38
|
|
|
$this->tradingApi = $tradingApi; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get total balance for given currency |
|
43
|
|
|
* |
|
44
|
|
|
* @param ApiKey $apiKey |
|
45
|
|
|
* @param string $currency |
|
46
|
|
|
* |
|
47
|
|
|
* @return float |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getBalance(ApiKey $apiKey, string $currency = 'BTC'): float |
|
50
|
|
|
{ |
|
51
|
|
|
$currency = strtoupper($currency); |
|
52
|
|
|
$this->tradingApi->setApiKey($apiKey); |
|
53
|
|
|
$completeBalances = $this->tradingApi->returnCompleteBalances(); |
|
54
|
|
|
$ticker = $currency !== 'BTC' ? $this->publicApi->returnTicker() : null; |
|
55
|
|
|
|
|
56
|
|
|
$balance = 0; |
|
57
|
|
|
|
|
58
|
|
|
foreach ($completeBalances as $coin => $completeBalance) { |
|
59
|
|
|
if ($currency === 'BTC') { |
|
60
|
|
|
$balance += $completeBalance->btcValue; |
|
61
|
|
|
|
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$pair = $currency . '_' . strtoupper($coin); |
|
66
|
|
|
|
|
67
|
|
|
if (!isset($ticker[$pair])) { |
|
68
|
|
|
throw new PoloniexException(sprintf('Invalid currency given: %s', $currency)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if ($completeBalance->available > 0) { |
|
72
|
|
|
$balance += $completeBalance->available * $ticker[$pair]->last; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($completeBalance->onOrders > 0) { |
|
76
|
|
|
$balance += $completeBalance->onOrders * $ticker[$pair]->last; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $balance; |
|
81
|
|
|
} |
|
82
|
|
|
} |