Skip to content

Commit 94a52ef

Browse files
committed
coinexchange public api/markets
1 parent 6b91caa commit 94a52ef

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

web/yaamp/core/backend/markets.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function BackendPricesUpdate()
2323
updateJubiMarkets();
2424
updateLiveCoinMarkets();
2525
updateNovaMarkets();
26+
updateCoinExchangeMarkets();
2627

2728
updateShapeShiftMarkets();
2829
updateOtherMarkets();
@@ -1087,6 +1088,46 @@ function updateLiveCoinMarkets()
10871088
}
10881089
}
10891090

1091+
function updateCoinExchangeMarkets()
1092+
{
1093+
$exchange = 'coinexchange';
1094+
if (exchange_get($exchange, 'disabled')) return;
1095+
1096+
$list = coinexchange_api_query('getmarkets');
1097+
if(!is_object($list)) return;
1098+
$markets = coinexchange_api_query('getmarketsummaries');
1099+
if(!is_object($markets)) return;
1100+
foreach($list->result as $currency)
1101+
{
1102+
$symbol = objSafeVal($currency,'MarketAssetCode','');
1103+
$exchid = objSafeVal($currency,'MarketAssetID',0);
1104+
if(empty($symbol) || !$exchid || $symbol == 'BTC') continue;
1105+
1106+
$coin = getdbosql('db_coins', "symbol=:sym", array(':sym'=>$symbol));
1107+
if(!$coin) continue;
1108+
1109+
$market = getdbosql('db_markets', "coinid={$coin->id} AND name='$exchange'");
1110+
if(!$market) continue;
1111+
1112+
if($market->disabled < 9) $market->disabled = !$currency->Active;
1113+
1114+
$market->save();
1115+
1116+
if($market->disabled || $market->deleted) continue;
1117+
1118+
foreach ($markets->result as $m) {
1119+
if ($m->MarketID == $exchid) {
1120+
$price2 = ($m->BidPrice + $m->AskPrice)/2;
1121+
$market->price2 = AverageIncrement($market->price2, $price2);
1122+
$market->price = AverageIncrement($market->price, $m->BidPrice);
1123+
$market->pricetime = time();
1124+
$market->save();
1125+
//debuglog("$exchange: $symbol price set to ".bitcoinvaluetoa($market->price));
1126+
}
1127+
}
1128+
}
1129+
}
1130+
10901131
// todo: store min/max txs limits
10911132
function updateShapeShiftMarkets()
10921133
{

web/yaamp/core/backend/rawcoins.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ function updateRawcoins()
9696
}
9797
}
9898

99+
if (!exchange_get('coinexchange', 'disabled')) {
100+
$list = coinexchange_api_query('getmarkets');
101+
if(isset($list->result) && !empty($list->result))
102+
{
103+
dborun("UPDATE markets SET deleted=true WHERE name='coinexchange'");
104+
foreach($list->result as $item) {
105+
if ($item->BaseCurrencyCode != 'BTC')
106+
continue;
107+
$symbol = $item->MarketAssetCode;
108+
$label = objSafeVal($item, 'MarketAssetName');
109+
updateRawCoin('coinexchange', $symbol, $label);
110+
}
111+
}
112+
}
113+
99114
if (!exchange_get('cryptopia', 'disabled')) {
100115
$list = cryptopia_api_query('GetMarkets');
101116
if(isset($list->Data))
@@ -249,7 +264,7 @@ function updateRawCoin($marketname, $symbol, $name='unknown')
249264
}
250265
}
251266

252-
if ($marketname == 'nova' || $marketname == 'askcoin') {
267+
if ($marketname == 'nova' || $marketname == 'askcoin' || $marketname == 'coinexchange') {
253268
// don't polute too much the db
254269
return;
255270
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
// http://coinexchangeio.github.io/slate/
4+
5+
function coinexchange_api_query($method, $params='')
6+
{
7+
$exchange = 'coinexchange';
8+
9+
$mt = explode(' ', microtime());
10+
$nonce = $mt[1].substr($mt[0], 2, 6);
11+
12+
$uri = "https://www.coinexchange.io/api/v1/$method?nonce=$nonce";
13+
if (!empty($params)) $uri .= '&'.$params;
14+
15+
$ch = curl_init($uri);
16+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
17+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
18+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
19+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
20+
curl_setopt($ch, CURLOPT_SSLVERSION, 1 /*CURL_SSLVERSION_TLSv1*/);
21+
curl_setopt($ch, CURLOPT_SSL_SESSIONID_CACHE, 0);
22+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
23+
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
24+
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; CoinExchange API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
25+
curl_setopt($ch, CURLOPT_ENCODING , '');
26+
27+
$data = curl_exec($ch);
28+
$obj = json_decode($data);
29+
30+
if(!is_object($obj)) {
31+
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
32+
debuglog("$exchange: $method failed ($status) ".strip_data($data).' '.curl_error($ch));
33+
}
34+
35+
curl_close($ch);
36+
37+
return $obj;
38+
}

web/yaamp/core/exchange/exchange.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function strip_data($data)
2525
require_once("cryptopia.php");
2626
require_once("livecoin.php");
2727
require_once("nova.php");
28+
require_once("coinexchange.php");
2829

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

0 commit comments

Comments
 (0)