Skip to content

Commit

Permalink
Merge pull request #2 from benphelps/pull-request
Browse files Browse the repository at this point in the history
dynamic supported, qrcode, estimate, conversion
  • Loading branch information
cryptapi authored Oct 26, 2021
2 parents da40883 + 276867a commit eb1a192
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 20 deletions.
110 changes: 91 additions & 19 deletions cryptapi/cryptapi.php → CryptAPI/CryptAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

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

public function __construct($coin, $own_address, $callback_url, $parameters=[], $ca_params=[]) {

foreach ($this->valid_erc20_tokens as $token) {
$this->valid_coins[] = 'erc20_' . $token;
}

foreach ($this->valid_trc20_tokens as $token) {
$this->valid_coins[] = 'trc20_' . $token;
}
$this->valid_coins = CryptAPI::get_supported_coins();

if (!in_array($coin, $this->valid_coins)) {
$vc = print_r($this->valid_coins, true);
Expand All @@ -44,11 +36,36 @@ public function __construct($coin, $own_address, $callback_url, $parameters=[],
$this->coin = $coin;
$this->ca_params = $ca_params;
$this->parameters = $parameters;
}

public static function get_supported_coins() {
$info = CryptAPI::get_info(null, true);

if (empty($info)) {
return null;
}

unset($info['fee_tiers']);

$coins = [];

foreach ($info as $chain => $data) {
$is_base_coin = in_array('ticker', array_keys($data));
if ($is_base_coin) {
$coins[] = $chain;
continue;
}

$base_ticker = "{$chain}_";
foreach ($data as $token => $subdata) {
$coins[] = $base_ticker . $token;
}
}

return $coins;
}

public function get_address() {

if (empty($this->own_address) || empty($this->coin) || empty($this->callback_url)) return null;

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

if ($response->status == 'success') {
$this->payment_address = $response->address_in;
return $response->address_in;
}

return null;
}

public function check_logs() {

if (empty($this->coin) || empty($this->callback_url)) return null;

$params = [
Expand All @@ -88,8 +105,59 @@ public function check_logs() {
return null;
}

public static function get_info($coin) {
$response = CryptAPI::_request($coin, 'info');
public function get_qrcode($value = false, $size = false) {
if (empty($this->coin)) return null;

$params = [
'address' => $this->payment_address,
];

if ($value) $params['value'] = $value;
if ($size) $params['size'] = $size;

$response = CryptAPI::_request($this->coin, 'qrcode', $params);

if ($response->status == 'success') {
return $response;
}

return null;
}

public static function get_info($coin = null, $assoc = false) {
$params = [];

if (empty($coin)) {
$params['prices'] = '0';
}

$response = CryptAPI::_request($coin, 'info', $params, $assoc);

if (empty($coin) || $response->status == 'success') {
return $response;
}

return null;
}

public static function get_estimate($coin, $addresses = 1, $priority = 'default') {
$response = CryptAPI::_request($coin, 'estimate', [
'addresses' => $addresses,
'priority' => $priority
]);

if ($response->status == 'success') {
return $response;
}

return null;
}

public static function get_convert($coin, $value, $from) {
$response = CryptAPI::_request($coin, 'convert', [
'value' => $value,
'from' => $from
]);

if ($response->status == 'success') {
return $response;
Expand Down Expand Up @@ -121,14 +189,18 @@ public static function process_callback($_get) {
return $params;
}

private static function _request($coin, $endpoint, $params=[]) {

private static function _request($coin, $endpoint, $params = [], $assoc = false) {
$base_url = Cryptapi::$base_url;
$coin = str_replace('_', '/', $coin);

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

$url = "{$base_url}/{$coin}/{$endpoint}/";
if (!empty($coin)) {
$coin = str_replace('_', '/', $coin);
$url = "{$base_url}/{$coin}/{$endpoint}/";
} else {
$url = "{$base_url}/{$endpoint}/";
}

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

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

return json_decode($response);
return json_decode($response, $assoc);
}
}
75 changes: 75 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,81 @@ $data = $ca->check_logs();
Same parameters as before, the `$data` returned can be checked here: https://cryptapi.io/docs/#/Bitcoin/btclogs


### Generating a QR code

```php
<?php
require 'vendor/autoload.php'; // Where your vendor directory is

$ca = new CryptAPI\CryptAPI($coin, $my_address, $callback_url, $parameters, $cryptapi_params);
$payment_address = $ca->get_address();

$qrcode = $ca->get_qrcode($value, $size);
```

For object creation, same parameters as before. You must first call `get_address` as this method requires the payment address to have been created.

For QR code generation:

``$value`` Value to request the user, in the main coin (BTC, ETH, etc). Optional, pass `false` to not add a value to the QR.

``$size`` Size of the QR Code image in pixels. Optional, pass `false` to use the default size of 512.

Response is an object with `qr_code` (base64 encoded image data) and `payment_uri` (the value encoded in the QR), see https://cryptapi.io/docs/#operation/btcqrcode for more information.


### Estimating transaction fees

```php
<?php
require 'vendor/autoload.php'; // Where your vendor directory is

$fees = CryptAPI\CryptAPI::get_estimate($coin, $addresses, $priority);
```

Where:

``$coin`` is the coin you wish to check, from CryptAPI's supported currencies (e.g `'btc', 'eth', 'erc20_usdt', ...`)

``$addresses`` The number of addresses to forward the funds to. Optional, defaults to 1.

``$priority`` Confirmation priority, needs to be one of `['fast', 'default', 'economic']`. Optional, defaults to `default`.

Response is an object with `estimated_cost` and `estimated_cost_usd`, see https://cryptapi.io/docs/#operation/btcestimate for more information.


### Converting between coins and fiat

```php
<?php
require 'vendor/autoload.php'; // Where your vendor directory is

$conversion = CryptAPI\CryptAPI::get_convert($coin, $value, $from);
```

Where:

``$coin`` the target currency to convert to, from CryptAPI's supported currencies (e.g `'btc', 'eth', 'erc20_usdt', ...`)

``$value`` Value to convert in `from`.

``$from`` Currency to convert from, FIAT or crypto.

Response is an object with `value_coin` and `exchange_rate`, see https://cryptapi.io/docs/#operation/btcconvert for more information.


### Getting supported coins

```php
<?php
require 'vendor/autoload.php'; // Where your vendor directory is

$coins = CryptAPI\CryptAPI::get_supported_coins();
```

Response is an array with all support coins.


## Help

Need help?
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"autoload": {
"psr-4": {
"CryptAPI\\": "cryptapi/"
"CryptAPI\\": "CryptAPI/"
}
},
"version": "1.3.0"
Expand Down

0 comments on commit eb1a192

Please sign in to comment.