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