|
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
|
|
|
/** |
|
18
|
|
|
* Class PoloniexManager |
|
19
|
|
|
* |
|
20
|
|
|
* @author Grisha Chasovskih <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
final class PoloniexManager |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var PublicApi |
|
26
|
|
|
*/ |
|
27
|
|
|
private $publicApi; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var TradingApi |
|
31
|
|
|
*/ |
|
32
|
|
|
private $tradingApi; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* PoloniexManager constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param PublicApi $publicApi |
|
38
|
|
|
* @param TradingApi $tradingApi |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(PublicApi $publicApi, TradingApi $tradingApi) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->publicApi = $publicApi; |
|
43
|
|
|
$this->tradingApi = $tradingApi; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get total balance for given currency |
|
48
|
|
|
* |
|
49
|
|
|
* @param ApiKey $apiKey User credentials |
|
50
|
|
|
* @param string $currency Any available currency at market (DOGE, LTC, ETH, USDT, XMR etc..) |
|
51
|
|
|
* |
|
52
|
|
|
* @return float |
|
53
|
|
|
* @throws PoloniexException |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getBalance(ApiKey $apiKey, string $currency = 'BTC'): float |
|
56
|
|
|
{ |
|
57
|
|
|
$currency = strtoupper($currency); |
|
58
|
|
|
$this->tradingApi->setApiKey($apiKey); |
|
59
|
|
|
$completeBalances = $this->tradingApi->returnCompleteBalances(); |
|
60
|
|
|
$balance = 0; |
|
61
|
|
|
|
|
62
|
|
|
foreach ($completeBalances as $coin => $completeBalance) { |
|
63
|
|
|
$balance += $completeBalance->btcValue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
if ($currency !== 'BTC') { |
|
67
|
|
|
$ticker = $this->publicApi->returnTicker(); |
|
68
|
|
|
$btcCoin = 'BTC_' . $currency; |
|
69
|
|
|
$coinBtc = $currency . '_BTC'; |
|
70
|
|
|
|
|
71
|
|
|
if (isset($ticker[$coinBtc])) { |
|
72
|
|
|
$balance *= $ticker[$coinBtc]->last; |
|
73
|
|
|
} elseif (isset($ticker[$btcCoin])) { |
|
74
|
|
|
$balance /= $ticker[$btcCoin]->last; |
|
75
|
|
|
} else { |
|
76
|
|
|
throw new PoloniexException(sprintf('Invalid currency given: %s', $currency)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $balance; |
|
81
|
|
|
} |
|
82
|
|
|
} |