|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaravelCryptoStats\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
|
|
7
|
|
|
class ChainsoConnector extends AbstractConnector |
|
8
|
|
|
{ |
|
9
|
|
|
public $supported_currencies = ['BTC', 'LTC', 'DOGE', 'ZEC', 'DOGE']; |
|
10
|
|
|
protected $api_url_prefix = 'https://chain.so//api/v2/'; |
|
11
|
|
|
protected $api_description = 'https://chain.so/api'; |
|
12
|
|
|
protected $block_link_prefix = 'https://chain.so/address/'; |
|
13
|
|
|
|
|
14
|
|
|
public function validateAddress(string $address): bool |
|
15
|
|
|
{ |
|
16
|
|
|
if ($address) { |
|
17
|
|
|
$url = $this->api_url_prefix.'is_address_valid/'.$this->currency.'/'.$address; |
|
18
|
|
|
$response = $this->apiCall($url); |
|
19
|
|
|
|
|
20
|
|
|
if (isset($response['is_valid'])) { |
|
21
|
|
|
return $response['is_valid']; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
throw new Exception('Wallet address can not be empty!'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getBalance(string $address): float |
|
29
|
|
|
{ |
|
30
|
|
|
if ($address) { |
|
31
|
|
|
$url = $this->api_url_prefix.'get_address_balance/'.$this->currency.'/'.$address; |
|
32
|
|
|
$response = $this->apiCall($url); |
|
33
|
|
|
|
|
34
|
|
|
if (isset($response['confirmed_balance'])) { |
|
35
|
|
|
return $this->roundBalance($response['confirmed_balance']); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
throw new Exception('Wallet address can not be empty!'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getBlockExplorerLink(string $address): string |
|
43
|
|
|
{ |
|
44
|
|
|
if ($address) { |
|
45
|
|
|
return $this->block_link_prefix.$this->currency.'/'.$address; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
throw new Exception('Wallet address can not be empty!'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function apiCall(string $url) |
|
52
|
|
|
{ |
|
53
|
|
|
$response = $this->sendApiRequest($url); |
|
54
|
|
|
if ($response && isset($response['data']) && isset($response['status']) && $response['status'] == 'success') { |
|
55
|
|
|
return $response['data']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
throw new Exception('Output data is not correct. Check the API description - '.$this->api_description.'!'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|