|
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
|
|
|
$completeBalances = $this->tradingApi->setApiKey($apiKey)->returnCompleteBalances(); |
|
59
|
|
|
$balance = 0; |
|
60
|
|
|
|
|
61
|
|
|
foreach ($completeBalances as $coin => $completeBalance) { |
|
62
|
|
|
$balance += $completeBalance->btcValue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if ($currency !== 'BTC') { |
|
66
|
|
|
$ticker = $this->publicApi->returnTicker(); |
|
67
|
|
|
$btcCoin = 'BTC_' . $currency; |
|
68
|
|
|
$coinBtc = $currency . '_BTC'; |
|
69
|
|
|
|
|
70
|
|
|
if (isset($ticker[$coinBtc])) { |
|
71
|
|
|
$balance *= $ticker[$coinBtc]->last; |
|
72
|
|
|
} elseif (isset($ticker[$btcCoin])) { |
|
73
|
|
|
$balance /= $ticker[$btcCoin]->last; |
|
74
|
|
|
} else { |
|
75
|
|
|
throw new PoloniexException(sprintf('Invalid currency given: %s', $currency)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $balance; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get available balance for given coin. |
|
84
|
|
|
* You can use available balance for trades. |
|
85
|
|
|
* |
|
86
|
|
|
* @param ApiKey $apiKey |
|
87
|
|
|
* @param string $coin |
|
88
|
|
|
* |
|
89
|
|
|
* @return float|null |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getAvailableBalance(ApiKey $apiKey, string $coin):? float |
|
92
|
|
|
{ |
|
93
|
|
|
$balances = $this->tradingApi->setApiKey($apiKey)->returnCompleteBalances(); |
|
94
|
|
|
|
|
95
|
|
|
if (!isset($balances[$coin])) { |
|
96
|
|
|
throw new PoloniexException(sprintf('Invalid coin given: %s', $coin)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $balances[$coin]->available; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get asks for given trade pair |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $pair |
|
106
|
|
|
* |
|
107
|
|
|
* @return float[] Keys are prices and values are amount of trades |
|
108
|
|
|
*/ |
|
109
|
|
|
public function asks(string $pair): array |
|
110
|
|
|
{ |
|
111
|
|
|
$asks = []; |
|
112
|
|
|
foreach ($this->publicApi->returnOrderBook($pair)->asks as $ask) { |
|
113
|
|
|
$asks[$ask[0]] = $ask[1]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
ksort($asks); |
|
117
|
|
|
|
|
118
|
|
|
return $asks; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get bids |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $pair |
|
125
|
|
|
* |
|
126
|
|
|
* @return float[] Keys are prices and values are amount of trades |
|
127
|
|
|
*/ |
|
128
|
|
|
public function bids(string $pair): array |
|
129
|
|
|
{ |
|
130
|
|
|
$bids = []; |
|
131
|
|
|
foreach ($this->publicApi->returnOrderBook($pair)->bids as $bid) { |
|
132
|
|
|
$bids[$bid[0]] = $bid[1]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
ksort($bids); |
|
136
|
|
|
|
|
137
|
|
|
return $bids; |
|
138
|
|
|
} |
|
139
|
|
|
} |