forked from fdbatista/bitso-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitso.php
More file actions
173 lines (135 loc) · 4.54 KB
/
Copy pathBitso.php
File metadata and controls
173 lines (135 loc) · 4.54 KB
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
<?php
declare(strict_types=1);
namespace BitsoAPI;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class Bitso
{
//constructor, default is dev url
public const URL = 'https://bitso.com';
public readonly HttpClientInterface $client;
private Client $bitsoClient;
public function __construct(public $key = '', public $secret = '', public $url = 'https://bitso.com')
{
$this->client = HttpClient::create(['base_uri' => $url]);
$this->bitsoClient = new Client($key, $secret, $url);
}
public function accounts(): Accounts
{
return new Accounts($this->bitsoClient, $this);
}
public function orders(): Orders
{
return new Orders($this->bitsoClient, $this);
}
public function booksAndTrades(): BooksAndTrades
{
return new BooksAndTrades($this->bitsoClient, $this);
}
public function contacts(): Contacts
{
return new Contacts($this);
}
public function clabes(): Clabes
{
return new Clabes($this);
}
//##### #######
//##### PUBLIC QUERIES #######
//##### #######
public function orderBook($params)
{
$parameters = http_build_query($params, '', '&');
$path = $this->url.'/api/v3/order_book/?'.$parameters;
$type = 'PUBLIC';
$HTTPMethod = 'GET';
$result = Client::urlRequest($this, $type, $path, $HTTPMethod, '', '');
return Client::checkAndDecode($result);
}
public function getTotalInMxn($accounts)
{
$sum_in_mxn = 0.0;
$sum_in_usd = 0.0;
foreach ($accounts as $account) {
$sum_in_mxn += $account['mxn'];
$sum_in_usd += $account['usd'];
}
// convert usd to mxn
$usd_to_mxn = (float)$this->getPriceForBook('usd_mxn');
return $sum_in_mxn + $sum_in_usd * $usd_to_mxn;
}
//##### #######
//##### PRIVATE QUERIES #######
//##### #######
public function getPriceForBook($book): string
{
$ticker = $this->booksAndTrades()->ticker($book);
return $ticker['last'];
}
public function fundings($params = []): array
{
if ($params === []) {
$params = ['limit' => 1];
}
if (in_array('fids', $params)) {
$ids = $params('fids');
unset($params['fids']);
}
$parameters = http_build_query($params, '', '&');
$path = $this->url.'/fundings/?'.$parameters;
$RequestPath = '/api/v3/fundings/?'.$parameters;
$HTTPMethod = 'GET';
return Client::getData($path, $RequestPath, $HTTPMethod);
}
public function open_orders($params): array
{
$parameters = http_build_query($params, '', '&');
$path = $this->url.'/open_orders/?'.$parameters;
$RequestPath = '/api/v3/open_orders/?'.$parameters;
$HTTPMethod = 'GET';
return Client::getData($path, $RequestPath, $HTTPMethod);
}
public function lookup_order($ids)
{
$parameters = implode(',', $ids);
$path = $this->url.'/orders/'.$parameters;
$RequestPath = '/api/v3/orders/'.$parameters;
$HTTPMethod = 'GET';
return Client::getData($path, $RequestPath, $HTTPMethod);
}
public function cancel_order($ids): array
{
if ($ids === 'all') {
$parameters = 'all';
} else {
$parameters = implode(',', $ids);
}
$path = $this->url.'/orders/'.$parameters;
$RequestPath = '/api/v3/orders/'.$parameters;
$HTTPMethod = 'DELETE';
return Client::getData($path, $RequestPath, $HTTPMethod);
}
public function placeOrder($params): array
{
$path = $this->url.'/api/v3/orders/';
$RequestPath = '/api/v3/orders/';
$HTTPMethod = 'POST';
$JSONPayload = json_encode($params, JSON_THROW_ON_ERROR);
return Client::getData($path, $RequestPath, $HTTPMethod);
}
public function fundingDestination($params): array
{
$parameters = http_build_query($params, '', '&');
$path = $this->url.'/funding_destination/?'.$parameters;
$RequestPath = '/api/v3/funding_destination/?'.$parameters;
$HTTPMethod = 'GET';
return Client::getData($path, $RequestPath, $HTTPMethod);
}
public function setCredentials($key, $secret): static
{
$this->key = $key;
$this->secret = $secret;
$this->bitsoClient = new Client($key, $secret, $this->url);
return $this;
}
}