Skip to content

Commit fa41f1f

Browse files
authored
Merge pull request #1 from Punksolid/better-separation-of-concerns
Bitso refactor better separation of concerns
2 parents 30f1ca8 + 862bd8a commit fa41f1f

File tree

5 files changed

+261
-243
lines changed

5 files changed

+261
-243
lines changed

BitsoAPI/Accounts.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace BitsoAPI;
4+
5+
class Accounts
6+
{
7+
public function __construct(
8+
private Bitso $bitso,
9+
)
10+
{}
11+
12+
public function balances(?string $asked_currency = null): array
13+
{
14+
/*
15+
Get a user's balance.
16+
Returns:
17+
A list of bitso.Balance instances.
18+
*/
19+
20+
$request_path = '/api/v3/balance/';
21+
22+
$nonce = Client::makeNonce();
23+
$HTTPMethod = 'GET';
24+
$JSONPayload = '';
25+
$message = $nonce.$HTTPMethod.$request_path.$JSONPayload;
26+
$format = 'Bitso %s:%s:%s';
27+
$signature = hash_hmac('sha256', $message, (string) $this->bitso->secret);
28+
$authHeader = sprintf($format, $this->bitso->key, $nonce, $signature);
29+
30+
$result = $this->bitso->client->request(
31+
'GET',
32+
$this->bitso->url.'/api/v3/balance/',
33+
[
34+
'headers' => ['Authorization' => $authHeader],
35+
]
36+
);
37+
38+
$balances_array = json_decode($result->getContent(), true, 512, JSON_THROW_ON_ERROR);
39+
$balances_array = $balances_array['payload']['balances'];
40+
41+
if ($asked_currency !== null) {
42+
$balances_array = array_filter($balances_array, fn ($balance) => $balance['currency'] === $asked_currency);
43+
}
44+
45+
return $balances_array;
46+
}
47+
48+
public function accountValue($in_currency = 'usd'): int
49+
{
50+
$fallback_currency_converted_to = 'mxn';
51+
$sub_accounts = $this->balances();
52+
53+
$accounts = [];
54+
foreach ($sub_accounts as $sub_account) {
55+
$currency = $sub_account['currency'];
56+
$total = $sub_account['total'];
57+
if ($total === 0) { // skip zero balances
58+
continue;
59+
}
60+
$accounts[$currency]['usd'] = 0;
61+
$accounts[$currency]['mxn'] = 0;
62+
63+
if ($currency === $in_currency) {
64+
$accounts[$currency][$in_currency] = $total;
65+
$account_value[$currency] = $total;
66+
67+
continue;
68+
}
69+
70+
if ($currency === $in_currency) {
71+
$accounts[$currency][$in_currency] = $total;
72+
$account_value[$currency] = $total;
73+
74+
continue;
75+
}
76+
77+
try {
78+
$book = $currency.'_'.$in_currency;
79+
$book_price = $this->bitso->getPriceForBook($book);
80+
$accounts[$currency][$in_currency] += $total * $book_price;
81+
} catch (\Throwable) {
82+
try {
83+
$book = $currency.'_'.$fallback_currency_converted_to;
84+
$book_price_in_fallback = $this->bitso->getPriceForBook($book);
85+
$accounts[$currency][$fallback_currency_converted_to] += $total * $book_price_in_fallback;
86+
} catch (\Throwable) {
87+
$book = 'usd'.'_'.$currency; // ars
88+
89+
$book_price_in_fallback = $this->bitso->getPriceForBook($book);
90+
91+
$accounts[$currency]['usd'] = $total / $book_price_in_fallback;
92+
}
93+
}
94+
}
95+
96+
return $this->bitso->getTotalInMxn($accounts);
97+
}
98+
}

0 commit comments

Comments
 (0)