Skip to content

Commit 8987ccb

Browse files
committed
dynamic supported, qrcode, estimate, conversion
fetch list of supported coins add new qrcode, estimate and conversion endpoints
1 parent da40883 commit 8987ccb

File tree

2 files changed

+92
-20
lines changed

2 files changed

+92
-20
lines changed
Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
class CryptAPI {
77
private static $base_url = "https://api.cryptapi.io";
8-
private $valid_erc20_tokens = ['becaz', 'bnb', 'busd', 'cro', 'link', 'mkr', 'nexo', 'pax', 'tusd', 'usdc', 'usdt', ];
9-
private $valid_trc20_tokens = ['usdt', 'btc', 'eth', ];
10-
private $valid_coins = ['btc', 'bch', 'eth', 'ltc', 'xmr', 'iota', 'trx', ];
8+
private $valid_coins = [];
119
private $own_address = null;
10+
private $payment_address = null;
1211
private $callback_url = null;
1312
private $coin = null;
1413
private $ca_params = [];
@@ -25,14 +24,7 @@ class CryptAPI {
2524
];
2625

2726
public function __construct($coin, $own_address, $callback_url, $parameters=[], $ca_params=[]) {
28-
29-
foreach ($this->valid_erc20_tokens as $token) {
30-
$this->valid_coins[] = 'erc20_' . $token;
31-
}
32-
33-
foreach ($this->valid_trc20_tokens as $token) {
34-
$this->valid_coins[] = 'trc20_' . $token;
35-
}
27+
$this->valid_coins = CryptAPI::get_supported_coins();
3628

3729
if (!in_array($coin, $this->valid_coins)) {
3830
$vc = print_r($this->valid_coins, true);
@@ -44,11 +36,36 @@ public function __construct($coin, $own_address, $callback_url, $parameters=[],
4436
$this->coin = $coin;
4537
$this->ca_params = $ca_params;
4638
$this->parameters = $parameters;
39+
}
40+
41+
public static function get_supported_coins() {
42+
$info = CryptAPI::get_info(null, true);
4743

44+
if (empty($info)) {
45+
return null;
46+
}
47+
48+
unset($info['fee_tiers']);
49+
50+
$coins = [];
51+
52+
foreach ($info as $chain => $data) {
53+
$is_base_coin = in_array('ticker', array_keys($data));
54+
if ($is_base_coin) {
55+
$coins[] = $chain;
56+
continue;
57+
}
58+
59+
$base_ticker = "{$chain}_";
60+
foreach ($data as $token => $subdata) {
61+
$coins[] = $base_ticker . $token;
62+
}
63+
}
64+
65+
return $coins;
4866
}
4967

5068
public function get_address() {
51-
5269
if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) return null;
5370

5471
$callback_url = $this->callback_url;
@@ -65,14 +82,14 @@ public function get_address() {
6582
$response = CryptAPI::_request($this->coin, 'create', $ca_params);
6683

6784
if ($response->status == 'success') {
85+
$this->payment_address = $response->address_in;
6886
return $response->address_in;
6987
}
7088

7189
return null;
7290
}
7391

7492
public function check_logs() {
75-
7693
if (empty($this->coin) || empty($this->callback_url)) return null;
7794

7895
$params = [
@@ -88,8 +105,59 @@ public function check_logs() {
88105
return null;
89106
}
90107

91-
public static function get_info($coin) {
92-
$response = CryptAPI::_request($coin, 'info');
108+
public function get_qrcode($value = false, $size = false) {
109+
if (empty($this->coin)) return null;
110+
111+
$params = [
112+
'address' => $this->payment_address,
113+
];
114+
115+
if ($value) $params['value'] = $value;
116+
if ($size) $params['size'] = $size;
117+
118+
$response = CryptAPI::_request($this->coin, 'qrcode', $params);
119+
120+
if ($response->status == 'success') {
121+
return $response;
122+
}
123+
124+
return null;
125+
}
126+
127+
public static function get_info($coin = null, $assoc = false) {
128+
$params = [];
129+
130+
if (empty($coin)) {
131+
$params['prices'] = '0';
132+
}
133+
134+
$response = CryptAPI::_request($coin, 'info', $params, $assoc);
135+
136+
if (empty($coin) || $response->status == 'success') {
137+
return $response;
138+
}
139+
140+
return null;
141+
}
142+
143+
public static function get_estimate($coin, $addresses = 1, $priority = 'default') {
144+
$response = CryptAPI::_request($coin, 'estimate', [
145+
'addresses' => $addresses,
146+
'priority' => $priority
147+
]);
148+
149+
if ($response->status == 'success') {
150+
return $response;
151+
}
152+
153+
return null;
154+
}
155+
156+
public static function get_convert($coin, $value, $from) {
157+
$response = CryptAPI::_request($coin, 'convert', [
158+
'value' => $value,
159+
'from' => $from
160+
]);
93161

94162
if ($response->status == 'success') {
95163
return $response;
@@ -121,14 +189,18 @@ public static function process_callback($_get) {
121189
return $params;
122190
}
123191

124-
private static function _request($coin, $endpoint, $params=[]) {
125-
192+
private static function _request($coin, $endpoint, $params = [], $assoc = false) {
126193
$base_url = Cryptapi::$base_url;
127194
$coin = str_replace('_', '/', $coin);
128195

129196
if (!empty($params)) $data = http_build_query($params);
130197

131-
$url = "{$base_url}/{$coin}/{$endpoint}/";
198+
if (!empty($coin)) {
199+
$coin = str_replace('_', '/', $coin);
200+
$url = "{$base_url}/{$coin}/{$endpoint}/";
201+
} else {
202+
$url = "{$base_url}/{$endpoint}/";
203+
}
132204

133205
if (!empty($data)) $url .= "?{$data}";
134206

@@ -138,6 +210,6 @@ private static function _request($coin, $endpoint, $params=[]) {
138210
$response = curl_exec($ch);
139211
curl_close($ch);
140212

141-
return json_decode($response);
213+
return json_decode($response, $assoc);
142214
}
143215
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
},
2626
"autoload": {
2727
"psr-4": {
28-
"CryptAPI\\": "cryptapi/"
28+
"CryptAPI\\": "CryptAPI/"
2929
}
3030
},
3131
"version": "1.3.0"

0 commit comments

Comments
 (0)