1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Exchange; |
4
|
|
|
|
5
|
|
|
use Binance\API; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
class Binance extends ExchangeAbstract |
9
|
|
|
{ |
10
|
|
|
private $nonce = false; |
11
|
|
|
|
12
|
|
|
/** @var API $client */ |
13
|
|
|
protected $client; |
14
|
|
|
|
15
|
|
|
public function __construct(Config $config) |
16
|
|
|
{ |
17
|
|
|
parent::__construct($config); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param $uri |
22
|
|
|
* @param array $params |
23
|
|
|
* @return mixed |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
|
|
public function send($uri, array $params = []) |
27
|
|
|
{ |
28
|
|
|
if(!$this->nonce) { |
29
|
|
|
|
30
|
|
|
$nonce = explode(' ', microtime()); |
31
|
|
|
$this->nonce = + $nonce[1].($nonce[0] * 1000000); |
|
|
|
|
32
|
|
|
$this->nonce = substr($this->nonce,5); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->nonce = explode(' ', microtime())[1]; |
35
|
|
|
// $this->nonce = (int) 10000 * microtime(true); |
36
|
|
|
} else { |
37
|
|
|
$this->nonce ++ ; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
$params['nonce'] = $this->nonce; |
42
|
|
|
$params['method'] = $uri; |
43
|
|
|
|
44
|
|
|
// generate the POST data string |
45
|
|
|
$post_data = http_build_query($params, '', '&'); |
46
|
|
|
$sign = hash_hmac('sha512', $post_data, $this->config->getSecret()); |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|
50
|
|
|
$headers = [ |
51
|
|
|
'Sign: '.$sign, |
52
|
|
|
'Key: '.$this->config->getKey(), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
static $ch = null; |
57
|
|
|
if (is_null($ch)) { |
58
|
|
|
$ch = curl_init(); |
59
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
60
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); |
61
|
|
|
} |
62
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://wex.nz/tapi/'); |
63
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
64
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
65
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
66
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
67
|
|
|
|
68
|
|
|
// run the query |
69
|
|
|
$res = curl_exec($ch); |
70
|
|
|
if ($res === false) throw new \Exception('Could not get reply: '.curl_error($ch)); |
71
|
|
|
$dec = json_decode($res, true); |
72
|
|
|
if (!$dec) throw new \Exception('Invalid data received, please make sure connection is working and requested API exists'); |
73
|
|
|
if($dec['success'] == 0) { |
74
|
|
|
throw new \Exception($dec['error']); |
75
|
|
|
} |
76
|
|
|
return $dec['return']; |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* |
82
|
|
|
*/ |
83
|
|
|
public function setClient() |
84
|
|
|
{ |
85
|
|
|
$this->client = new API($this->config->getKey(), $this->config->getSecret()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $btc |
90
|
|
|
* @param $price |
91
|
|
|
* @return mixed |
92
|
|
|
* @throws \Exception |
93
|
|
|
*/ |
94
|
|
|
public function buyOrder($btc, $price) |
95
|
|
|
{ |
96
|
|
|
return $this->send('Trade',[ |
97
|
|
|
'pair' => 'btc_usd', |
98
|
|
|
'type' => 'buy', |
99
|
|
|
'rate' => $price, |
100
|
|
|
'amount' => $btc |
101
|
|
|
]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param $btc |
106
|
|
|
* @param $price |
107
|
|
|
* @return mixed |
108
|
|
|
* @throws \Exception |
109
|
|
|
*/ |
110
|
|
|
public function sellOrder($btc, $price) |
111
|
|
|
{ |
112
|
|
|
return $this->send('Trade',[ |
113
|
|
|
'pair' => 'btc_usd', |
114
|
|
|
'type' => 'sell', |
115
|
|
|
'rate' => $price, |
116
|
|
|
'amount' => $btc |
117
|
|
|
]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return mixed |
122
|
|
|
* @throws \Exception |
123
|
|
|
*/ |
124
|
|
|
public function getInfo() |
125
|
|
|
{ |
126
|
|
|
return $this->send('getInfo'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function getTicker() |
133
|
|
|
{ |
134
|
|
|
return json_decode($this->client->get('https://wex.nz/api/3/ticker/btc_usd')->getBody()->getContents(), true); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public function getOrderBook($pair = 'BTCUSD') |
142
|
|
|
{ |
143
|
|
|
$orders = $this->client->openOrders($pair); |
144
|
|
|
|
145
|
|
|
return json_decode($orders, true); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return mixed |
151
|
|
|
* @throws \Exception |
152
|
|
|
*/ |
153
|
|
|
public function getOrders() |
154
|
|
|
{ |
155
|
|
|
return $this->send('ActiveOrders',[ |
156
|
|
|
'pair' => 'btc_usd' |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return mixed |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
public function getTransactionHistory() |
165
|
|
|
{ |
166
|
|
|
return $this->send('TransHistory',[]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return mixed |
171
|
|
|
* @throws \Exception |
172
|
|
|
*/ |
173
|
|
|
public function getTradeHistory() |
174
|
|
|
{ |
175
|
|
|
return $this->send('TradeHistory',[]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $order_id |
181
|
|
|
* @return mixed |
182
|
|
|
* @throws \Exception |
183
|
|
|
*/ |
184
|
|
|
public function cancelOrder($order_id) |
185
|
|
|
{ |
186
|
|
|
return $this->send('CancelOrder',[ |
187
|
|
|
'order_id' => $order_id |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
} |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.