Skip to content

Commit

Permalink
exchange: handle crex24 public api
Browse files Browse the repository at this point in the history
  • Loading branch information
tpruvot committed Sep 10, 2018
1 parent b6aa813 commit 1c2a513
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 3 deletions.
61 changes: 60 additions & 1 deletion web/yaamp/core/backend/markets.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function BackendPricesUpdate()
updateKrakenMarkets();
updateKuCoinMarkets();
updateCCexMarkets();
updateCrex24Markets();
updateCryptopiaMarkets();
updateHitBTCMarkets();
updateYobitMarkets();
Expand Down Expand Up @@ -975,7 +976,65 @@ function updateAlcurexMarkets()
$coin->price2 = $market->price2;
$coin->save();
}
// debuglog("alcurex: $pair $market->price ".bitcoinvaluetoa($market->price2));
//debuglog("$exchange: $pair price updated to {$market->price}");
break;
}
}
}
}

function updateCrex24Markets()
{
$exchange = 'crex24';
if (exchange_get($exchange, 'disabled')) return;

$list = getdbolist('db_markets', "name LIKE '$exchange%'");
if (empty($list)) return;

$data = crex24_api_query('tickers');
if(!is_array($data)) return;

foreach($list as $market)
{
$coin = getdbo('db_coins', $market->coinid);
if(!$coin) continue;

$symbol = $coin->getOfficialSymbol();
$pair = strtoupper($symbol).'-BTC';

$sqlFilter = '';
if (!empty($market->base_coin)) {
$pair = strtoupper($symbol.'-'.$market->base_coin);
$sqlFilter = "AND base_coin='{$market->base_coin}'";
}

if (market_get($exchange, $symbol, "disabled")) {
$market->disabled = 1;
$market->message = 'disabled from settings';
$market->save();
continue;
}

foreach ($data as $ticker) {
if ($ticker->instrument === $pair) {
if ($market->disabled < 9) {
$nbm = (int) dboscalar("SELECT COUNT(id) FROM markets WHERE coinid={$coin->id} $sqlFilter");
$market->disabled = ($ticker->bid < $ticker->ask/2) && ($nbm > 1);
}

$price2 = ($ticker->bid+$ticker->ask)/2;
$market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $ticker->bid);
$market->pricetime = time(); // $ticker->timestamp "2018-08-31T12:48:56Z"
$market->save();

if (empty($coin->price) && $ticker->ask) {
$coin->price = $market->price;
$coin->price2 = $price2;
$coin->save();
}
//debuglog("$exchange: $pair price updated to {$market->price}");
break;
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions web/yaamp/core/backend/rawcoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function updateRawcoins()

if (!exchange_get('bitz', 'disabled')) {
$list = bitz_api_query('tickerall');

if (!empty($list)) {
dborun("UPDATE markets SET deleted=true WHERE name='bitz'");
foreach($list as $c => $ticker) {
$e = explode('_', $c);
Expand All @@ -47,6 +47,7 @@ function updateRawcoins()
$symbol = strtoupper($e[0]);
updateRawCoin('bitz', $symbol);
}
}
}

if (!exchange_get('bleutrade', 'disabled')) {
Expand All @@ -64,6 +65,19 @@ function updateRawcoins()
}
}

if (!exchange_get('crex24', 'disabled')) {
$list = crex24_api_query('currencies');
if(is_array($list) && !empty($list)) {
dborun("UPDATE markets SET deleted=true WHERE name='crex24'");
foreach ($list as $currency) {
$symbol = objSafeVal($currency, 'symbol');
$name = objSafeVal($currency, 'name');
if ($currency->isFiat || $currency->isDelisted) continue;
updateRawCoin('crex24', $symbol, $name);
}
}
}

if (!exchange_get('poloniex', 'disabled')) {
$poloniex = new poloniex;
$tickers = $poloniex->get_currencies();
Expand Down Expand Up @@ -436,7 +450,7 @@ function updateRawCoin($marketname, $symbol, $name='unknown')
}

// some other to ignore...
if (in_array($marketname, array('yobit','kucoin','tradesatoshi')))
if (in_array($marketname, array('crex24','yobit','kucoin','tradesatoshi')))
return;

if (market_get($marketname, $symbol, "disabled")) {
Expand Down
26 changes: 26 additions & 0 deletions web/yaamp/core/exchange/crex24.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

// https://docs.crex24.com/trade-api/v2/
// https://api.crex24.com/v2/public/currencies

function crex24_api_query($method, $params='', $returnType='object')
{
$uri = "https://api.crex24.com/v2/public/{$method}";
if (!empty($params)) $uri .= "?{$params}";

$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$execResult = strip_tags(curl_exec($ch));

if ($returnType == 'object')
$ret = json_decode($execResult);
else
$ret = json_decode($execResult,true);

return $ret;
}

3 changes: 3 additions & 0 deletions web/yaamp/core/exchange/exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function strip_data($data)
require_once("bleutrade.php");
require_once("ccexapi.php");
require_once("cexio.php");
require_once("crex24.php");
require_once("cryptobridge.php");
require_once("gateio.php");
require_once("graviex.php");
Expand Down Expand Up @@ -97,6 +98,8 @@ function getMarketUrl($coin, $marketName)
$url = "https://www.coinexchange.io/market/{$symbol}/{$base}";
else if($market == 'coinsmarkets')
$url = "https://coinsmarkets.com/trade-{$base}-{$symbol}.htm";
else if($market == 'crex24')
$url = "https://crex24.com/exchange/{$symbol}-{$base}";
else if($market == 'cryptobridge')
$url = "https://wallet.crypto-bridge.org/market/BRIDGE.{$symbol}_BRIDGE.{$base}";
else if($market == 'cryptohub')
Expand Down

0 comments on commit 1c2a513

Please sign in to comment.