1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Exchange; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class BtcE 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
|
7 |
|
public function send($uri, array $params = []) |
19
|
|
|
{ |
20
|
7 |
|
if(!$this->nonce) { |
21
|
|
|
|
22
|
7 |
|
$nonce = explode(' ', microtime()); |
23
|
7 |
|
$this->nonce = + $nonce[1].($nonce[0] * 1000000); |
|
|
|
|
24
|
7 |
|
$this->nonce = substr($this->nonce,5); |
|
|
|
|
25
|
|
|
|
26
|
7 |
|
$this->nonce = explode(' ', microtime())[1]; |
27
|
|
|
// $this->nonce = (int) 10000 * microtime(true); |
|
|
|
|
28
|
|
|
} else { |
29
|
|
|
$this->nonce ++ ; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
7 |
|
$params['nonce'] = $this->nonce; |
34
|
7 |
|
$params['method'] = $uri; |
35
|
|
|
|
36
|
|
|
// generate the POST data string |
37
|
7 |
|
$post_data = http_build_query($params, '', '&'); |
38
|
7 |
|
$sign = hash_hmac('sha512', $post_data, $this->config->getSecret()); |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
$headers = [ |
43
|
7 |
|
'Sign: '.$sign, |
44
|
7 |
|
'Key: '.$this->config->getKey(), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
|
48
|
7 |
|
static $ch = null; |
49
|
7 |
|
if (is_null($ch)) { |
50
|
1 |
|
$ch = curl_init(); |
51
|
1 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
52
|
1 |
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); |
53
|
|
|
} |
54
|
7 |
|
curl_setopt($ch, CURLOPT_URL, 'https://wex.nz/tapi/'); |
55
|
7 |
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
56
|
7 |
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
57
|
7 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
58
|
7 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
59
|
|
|
|
60
|
|
|
// run the query |
61
|
7 |
|
$res = curl_exec($ch); |
62
|
7 |
|
if ($res === false) throw new \Exception('Could not get reply: '.curl_error($ch)); |
63
|
7 |
|
$dec = json_decode($res, true); |
64
|
7 |
|
if (!$dec) throw new \Exception('Invalid data received, please make sure connection is working and requested API exists'); |
65
|
7 |
|
if($dec['success'] == 0) { |
66
|
7 |
|
throw new \Exception($dec['error']); |
67
|
|
|
} |
68
|
|
|
return $dec['return']; |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
*/ |
75
|
9 |
|
public function setClient() |
76
|
|
|
{ |
77
|
9 |
|
$this->client = new Client(['base_uri' => 'https://btc-e.com/tapi/']); |
78
|
9 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $btc |
82
|
|
|
* @param $price |
83
|
|
|
* @return mixed |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
1 |
View Code Duplication |
public function buyOrder($btc, $price) |
|
|
|
|
87
|
|
|
{ |
88
|
1 |
|
return $this->send('Trade',[ |
89
|
1 |
|
'pair' => 'btc_usd', |
90
|
1 |
|
'type' => 'buy', |
91
|
1 |
|
'rate' => $price, |
92
|
1 |
|
'amount' => $btc |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $btc |
98
|
|
|
* @param $price |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
1 |
View Code Duplication |
public function sellOrder($btc, $price) |
|
|
|
|
102
|
|
|
{ |
103
|
1 |
|
return $this->send('Trade',[ |
104
|
1 |
|
'pair' => 'btc_usd', |
105
|
1 |
|
'type' => 'sell', |
106
|
1 |
|
'rate' => $price, |
107
|
1 |
|
'amount' => $btc |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return mixed |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
1 |
|
public function getInfo() |
116
|
|
|
{ |
117
|
1 |
|
return $this->send('getInfo'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return mixed |
122
|
|
|
*/ |
123
|
1 |
|
public function getTicker() |
124
|
|
|
{ |
125
|
1 |
|
return json_decode($this->client->get('https://btc-e.com/api/3/ticker/btc_usd')->getBody()->getContents(), true); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
|
130
|
1 |
|
public function getOrders() |
131
|
|
|
{ |
132
|
1 |
|
return $this->send('ActiveOrders',[ |
133
|
1 |
|
'pair' => 'btc_usd' |
134
|
|
|
]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
1 |
|
public function getTransactionHistory() |
139
|
|
|
{ |
140
|
1 |
|
return $this->send('TransHistory',[]); |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
public function getTradeHistory() |
144
|
|
|
{ |
145
|
1 |
|
return $this->send('TradeHistory',[]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
|
149
|
|
|
|
150
|
1 |
|
public function cancelOrder($order_id) |
151
|
|
|
{ |
152
|
1 |
|
return $this->send('CancelOrder',[ |
153
|
1 |
|
'order_id' => $order_id |
154
|
|
|
]); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
} |
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.