forked from dbernar1/intersango
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwbx_api.php
286 lines (261 loc) · 9.16 KB
/
wbx_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
// Authentication is performed by signing each request using
// HMAC-SHA512. The request must contain an extra value "nonce" which
// must be an always incrementing numeric value. A reference
// implementation is provided here:
class WBX_API
{
function __construct($key, $secret)
{
$this->key = $key;
$this->secret = $secret;
$this->url = 'https://www.worldbitcoinexchange.com/api';
}
function query($path, array $req = array())
{
// generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
$mt = explode(' ', microtime());
$req['nonce'] = $mt[1].substr($mt[0], 2, 6);
// generate the POST data string
$post_data = http_build_query($req, '', '&');
// generate the extra headers
$headers = array('Rest-Key: ' . $this->key,
'Rest-Sign: '. base64_encode(hash_hmac('sha512', $post_data, $this->secret, true)));
// our curl handle (initialize if required)
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; WBX PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, "{$this->url}/$path");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// run the query
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: ' . curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception("Invalid data received, please make sure connection is working and requested API exists.\nresult: '$res'\n");
return $this->last = $dec;
}
function ok()
{
return (isset($this->last['status']) &&
$this->last['status'] == 'OK');
}
////////////////////////////////////////////////////////////////////////
// * add_order.php
//
// add an order to the orderbook
//
// needs permission: trade
//
// in:
// have_amount: decimal amount to offer
// have_currency: BTC or AUD to offer
// want_amount: decimal amount to request
// want_currency: BTC or AUD to request
//
// out:
// status: "OK" if successful
// orderid: order ID
////////////////////////////////////////////////////////////////////////
function add_order($have_amount, $have_currency,
$want_amount, $want_currency)
{
return self::query('addOrder.php',
array('have_amount' => $have_amount,
'have_currency' => $have_currency,
'want_amount' => $want_amount,
'want_currency' => $want_currency));
}
////////////////////////////////////////////////////////////////////////
// * cancel_order.php
//
// add an order to the orderbook
//
// needs permission: trade
//
// in:
// orderid: order ID
//
// out:
// status: "OK" if successful
////////////////////////////////////////////////////////////////////////
function cancel_order($orderid)
{
return self::query('cancelOrder.php',
array('orderid' => $orderid));
}
function cancel_all_orders()
{
$orders = self::get_orders();
if (!self::ok())
return $orders;
$ret = array();
foreach ($orders['orders'] as $order)
array_push($ret, self::cancel_order($order['orderid']));
return $ret;
}
////////////////////////////////////////////////////////////////////////
// * get_deposit_address.php
//
// get a Bitcoin address that can be used to deposit to your account
//
// needs permission: read
//
// in: (nothing)
//
// out:
// status: "OK" if successful
// address: an address you can use to send BTC to your exchange account
////////////////////////////////////////////////////////////////////////
function get_deposit_address()
{
return self::query('getDepositAddress.php');
}
////////////////////////////////////////////////////////////////////////
// * get_orders.php
//
// get a list of open orders in the orderbook; for partially
// matched orders, this reports only the remaining part of each
//
// needs permission: read
//
// in: (nothing)
//
// out:
// status: "OK" if successful
// list of:
// orderid: order ID
// text: a plain text description of the order
// have_amount: the offered amount, as a decimal
// have_currency: the offered currency
// want_amount: the requested amount, as a decimal
// want_currency: the requested currency
////////////////////////////////////////////////////////////////////////
function get_orders()
{
return self::query('getOrders.php');
}
////////////////////////////////////////////////////////////////////////
// * info.php
//
// get user information
//
// needs permission: read
//
// in: (nothing)
//
// out:
// status: "OK" if successful
// uid user id
// BTC: Bitcoin balance
// AUD: fiat balance
////////////////////////////////////////////////////////////////////////
function info()
{
return self::query('info.php');
}
////////////////////////////////////////////////////////////////////////
// * redeem_voucher.php
//
// redeem BTC or fiat voucher
//
// needs permission: deposit
//
// in:
// voucher: voucher string
//
// out:
// status: "OK" if successful
// currency: BTC or AUD
// amount: decimal amount credited to account
////////////////////////////////////////////////////////////////////////
function redeem_voucher($voucher)
{
return self::query('redeemVoucher.php',
array('voucher' => $voucher));
}
////////////////////////////////////////////////////////////////////////
// * withdraw_bitcoin.php
//
// withdraw BTC to a Bitcoin address
//
// needs permission: withdraw
//
// in:
// amount: decimal amount to withdraw
// address: Bitcoin address to withdraw to
//
// out:
// status: "OK" if successful
// reqid: withdrawal request ID
////////////////////////////////////////////////////////////////////////
function withdraw_bitcoin($amount, $address)
{
return self::query('withdrawBitcoin.php',
array('amount' => $amount,
'address' => $address));
}
////////////////////////////////////////////////////////////////////////
// * withdraw_fiat.php
//
// withdraw fiat to a bank account
//
// needs permission: withdraw
//
// in:
// amount: decimal amount to withdraw
// name_holder: name of account holder
// name_bank: name of the bank
// account_number: account number
// sort_code: bank branch identifier (BSB, sort code, etc.)
// ref: your reference (optional)
//
// out:
// status: "OK" if successful
// reqid: withdrawal request ID
////////////////////////////////////////////////////////////////////////
function withdraw_fiat($amount, $name_holder, $name_bank, $account_number, $sort_code, $ref = '')
{
return self::query('withdrawFiat.php',
array('amount' => $amount,
'name_holder' => $name_holder,
'name_bank' => $name_bank,
'account_number' => $account_number,
'sort_code' => $sort_code,
'ref' => $ref));
}
////////////////////////////////////////////////////////////////////////
// * withdraw_voucher.php
//
// withdraw BTC or fiat to a voucher
//
// needs permission: withdraw
//
// in:
// amount: decimal amount to withdraw
// currency: BTC or AUD
//
// out:
// status: "OK" if successful
// voucher: voucher string
// reqid: withdrawal request ID
////////////////////////////////////////////////////////////////////////
function withdraw_voucher($amount, $currency)
{
return self::query('withdrawVoucher.php',
array('currency' => $currency,
'amount' => $amount));
}
function withdraw_btc_voucher($amount)
{
return self::withdraw_voucher($amount, 'BTC');
}
function withdraw_fiat_voucher($amount)
{
return self::withdraw_voucher($amount, CURRENCY);
}
}
?>