Skip to content

Commit

Permalink
coinexchange public api/markets
Browse files Browse the repository at this point in the history
  • Loading branch information
tpruvot committed Mar 24, 2017
1 parent 6b91caa commit 94a52ef
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
41 changes: 41 additions & 0 deletions web/yaamp/core/backend/markets.php
Expand Up @@ -23,6 +23,7 @@ function BackendPricesUpdate()
updateJubiMarkets();
updateLiveCoinMarkets();
updateNovaMarkets();
updateCoinExchangeMarkets();

updateShapeShiftMarkets();
updateOtherMarkets();
Expand Down Expand Up @@ -1087,6 +1088,46 @@ function updateLiveCoinMarkets()
}
}

function updateCoinExchangeMarkets()
{
$exchange = 'coinexchange';
if (exchange_get($exchange, 'disabled')) return;

$list = coinexchange_api_query('getmarkets');
if(!is_object($list)) return;
$markets = coinexchange_api_query('getmarketsummaries');
if(!is_object($markets)) return;
foreach($list->result as $currency)
{
$symbol = objSafeVal($currency,'MarketAssetCode','');
$exchid = objSafeVal($currency,'MarketAssetID',0);
if(empty($symbol) || !$exchid || $symbol == 'BTC') continue;

$coin = getdbosql('db_coins', "symbol=:sym", array(':sym'=>$symbol));
if(!$coin) continue;

$market = getdbosql('db_markets', "coinid={$coin->id} AND name='$exchange'");
if(!$market) continue;

if($market->disabled < 9) $market->disabled = !$currency->Active;

$market->save();

if($market->disabled || $market->deleted) continue;

foreach ($markets->result as $m) {
if ($m->MarketID == $exchid) {
$price2 = ($m->BidPrice + $m->AskPrice)/2;
$market->price2 = AverageIncrement($market->price2, $price2);
$market->price = AverageIncrement($market->price, $m->BidPrice);
$market->pricetime = time();
$market->save();
//debuglog("$exchange: $symbol price set to ".bitcoinvaluetoa($market->price));
}
}
}
}

// todo: store min/max txs limits
function updateShapeShiftMarkets()
{
Expand Down
17 changes: 16 additions & 1 deletion web/yaamp/core/backend/rawcoins.php
Expand Up @@ -96,6 +96,21 @@ function updateRawcoins()
}
}

if (!exchange_get('coinexchange', 'disabled')) {
$list = coinexchange_api_query('getmarkets');
if(isset($list->result) && !empty($list->result))
{
dborun("UPDATE markets SET deleted=true WHERE name='coinexchange'");
foreach($list->result as $item) {
if ($item->BaseCurrencyCode != 'BTC')
continue;
$symbol = $item->MarketAssetCode;
$label = objSafeVal($item, 'MarketAssetName');
updateRawCoin('coinexchange', $symbol, $label);
}
}
}

if (!exchange_get('cryptopia', 'disabled')) {
$list = cryptopia_api_query('GetMarkets');
if(isset($list->Data))
Expand Down Expand Up @@ -249,7 +264,7 @@ function updateRawCoin($marketname, $symbol, $name='unknown')
}
}

if ($marketname == 'nova' || $marketname == 'askcoin') {
if ($marketname == 'nova' || $marketname == 'askcoin' || $marketname == 'coinexchange') {
// don't polute too much the db
return;
}
Expand Down
38 changes: 38 additions & 0 deletions web/yaamp/core/exchange/coinexchange.php
@@ -0,0 +1,38 @@
<?php

// http://coinexchangeio.github.io/slate/

function coinexchange_api_query($method, $params='')
{
$exchange = 'coinexchange';

$mt = explode(' ', microtime());
$nonce = $mt[1].substr($mt[0], 2, 6);

$uri = "https://www.coinexchange.io/api/v1/$method?nonce=$nonce";
if (!empty($params)) $uri .= '&'.$params;

$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 1 /*CURL_SSLVERSION_TLSv1*/);
curl_setopt($ch, CURLOPT_SSL_SESSIONID_CACHE, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; CoinExchange API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_ENCODING , '');

$data = curl_exec($ch);
$obj = json_decode($data);

if(!is_object($obj)) {
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
debuglog("$exchange: $method failed ($status) ".strip_data($data).' '.curl_error($ch));
}

curl_close($ch);

return $obj;
}
3 changes: 3 additions & 0 deletions web/yaamp/core/exchange/exchange.php
Expand Up @@ -25,6 +25,7 @@ function strip_data($data)
require_once("cryptopia.php");
require_once("livecoin.php");
require_once("nova.php");
require_once("coinexchange.php");

/* Format an exchange coin Url */
function getMarketUrl($coin, $marketName)
Expand Down Expand Up @@ -59,6 +60,8 @@ function getMarketUrl($coin, $marketName)
$url = "https://bleutrade.com/exchange/{$symbol}/{$base}";
else if($market == 'bter')
$url = "https://bter.com/trade/{$lowsymbol}_{$lowbase}";
else if($market == 'coinexchange')
$url = "https://www.coinexchange.io/market/{$symbol}/{$base}";
else if($market == 'cryptopia')
$url = "https://www.cryptopia.co.nz/Exchange?market={$symbol}_{$base}";
else if($market == 'c-cex')
Expand Down

0 comments on commit 94a52ef

Please sign in to comment.