From 028d6610568639cd1aa5c0178520e0fbd8dff0be Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Wed, 1 Nov 2017 02:38:45 +0100 Subject: [PATCH] binance public api (markets) disabled by default, and not enough infos about symbols (no coin label etc) --- web/yaamp/core/backend/markets.php | 47 ++++++++++++++++++++- web/yaamp/core/backend/rawcoins.php | 18 +++++++- web/yaamp/core/exchange/binance.php | 62 ++++++++++++++++++++++++++++ web/yaamp/core/exchange/exchange.php | 5 ++- 4 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 web/yaamp/core/exchange/binance.php diff --git a/web/yaamp/core/backend/markets.php b/web/yaamp/core/backend/markets.php index b302455..ea5531c 100644 --- a/web/yaamp/core/backend/markets.php +++ b/web/yaamp/core/backend/markets.php @@ -19,6 +19,7 @@ function BackendPricesUpdate() updateHitBTCMarkets(); updateYobitMarkets(); updateAlcurexMarkets(); + updateBinanceMarkets(); updateBterMarkets(); //updateEmpoexMarkets(); updateJubiMarkets(); @@ -653,7 +654,7 @@ function updateYobitMarkets() $last_checked = cache()->get($exchange.'-deposit_address-check-'.$symbol); if ($last_checked) continue; - sleep(1); // for the tapi nonce + sleep(1); // for the api nonce $address = yobit_api_query2('GetDepositAddress', array("coinName"=>$symbol)); if (!empty($address) && isset($address['return']) && $address['success']) { $addr = $address['return']['address']; @@ -851,7 +852,7 @@ function updateCryptopiaMarkets() $addr = arraySafeVal($addresses, $symbol); if ($needCurrencyQueries) { if(!$coin->installed) continue; - sleep(1); + sleep(2); $query = cryptopia_api_user('GetDepositAddress', array('Currency'=>$symbol)); $dep = objSafeVal($query,'Data'); $addr = objSafeVal($dep,'Address'); @@ -1026,6 +1027,48 @@ function updateNovaMarkets() } } +function updateBinanceMarkets() +{ + $exchange = 'binance'; + if (exchange_get($exchange, 'disabled')) return; + + $tickers = binance_api_query('ticker/allBookTickers'); + if(!is_array($tickers)) return; + + $list = getdbolist('db_markets', "name='$exchange'"); + foreach($list as $market) + { + $coin = getdbo('db_coins', $market->coinid); + if(!$coin) continue; + + $symbol = $coin->getOfficialSymbol(); + if (market_get($exchange, $symbol, "disabled")) { + $market->disabled = 1; + $market->message = 'disabled from settings'; + $market->save(); + continue; + } + + $pair = $symbol.'BTC'; + foreach ($tickers as $ticker) { + if ($pair != $ticker->symbol) continue; + + $price2 = ($ticker->bidPrice+$ticker->askPrice)/2; + $market->price = AverageIncrement($market->price, $ticker->bidPrice); + $market->price2 = AverageIncrement($market->price2, $price2); + $market->pricetime = time(); + if ($market->disabled < 9) $market->disabled = (floatval($ticker->bidQty) < 0.01); + $market->save(); + + if (empty($coin->price2)) { + $coin->price = $market->price; + $coin->price2 = $market->price2; + $coin->save(); + } + } + } +} + function updateBterMarkets() { $exchange = 'bter'; diff --git a/web/yaamp/core/backend/rawcoins.php b/web/yaamp/core/backend/rawcoins.php index b152dd1..9ce7b94 100644 --- a/web/yaamp/core/backend/rawcoins.php +++ b/web/yaamp/core/backend/rawcoins.php @@ -8,6 +8,7 @@ function updateRawcoins() // debuglog(__FUNCTION__); exchange_set_default('alcurex', 'disabled', true); + exchange_set_default('binance', 'disabled', true); exchange_set_default('bter', 'disabled', true); exchange_set_default('empoex', 'disabled', true); exchange_set_default('coinexchange', 'disabled', true); @@ -193,6 +194,21 @@ function updateRawcoins() } } + if (!exchange_get('binance', 'disabled')) { + $list = binance_api_query('ticker/allBookTickers'); + if(is_array($list)) + { + dborun("UPDATE markets SET deleted=true WHERE name='binance'"); + foreach($list as $ticker) { + $base = substr($ticker->symbol, -3, 3); + // XXXBTC XXXETH BTCUSDT (no separator!) + if ($base != 'BTC') continue; + $symbol = substr($ticker->symbol, 0, strlen($ticker->symbol)-3); + updateRawCoin('binance', $symbol); + } + } + } + if (!exchange_get('nova', 'disabled')) { $list = nova_api_query('markets'); if(is_object($list) && !empty($list->markets)) @@ -325,7 +341,7 @@ function updateRawCoin($marketname, $symbol, $name='unknown') } } - if (in_array($marketname, array('nova','askcoin','coinexchange','coinsmarkets','hitbtc'))) { + if (in_array($marketname, array('nova','askcoin','binance','coinexchange','coinsmarkets','hitbtc'))) { // don't polute too much the db with new coins, its better from exchanges with labels return; } diff --git a/web/yaamp/core/exchange/binance.php b/web/yaamp/core/exchange/binance.php new file mode 100644 index 0000000..a83b44c --- /dev/null +++ b/web/yaamp/core/exchange/binance.php @@ -0,0 +1,62 @@ +$symbol)); + if(!$coin) return false; + $pair = strtoupper($symbol).'BTC'; + $market = getdbosql('db_markets', "coinid={$coin->id} AND name='$exchange'"); + if(!$market) return false; + + } else if (is_object($market)) { + + $coin = getdbo('db_coins', $market->coinid); + if(!$coin) return false; + $symbol = $coin->getOfficialSymbol(); + $pair = strtoupper($symbol).'BTC'; + if (!empty($market->base_coin)) $pair = strtoupper($symbol).$market->base_coin; + } + + $t1 = microtime(true); + $tickers = binance_api_query('ticker/allBookTickers'); + if(empty($tickers) || !is_array($tickers)) return false; + foreach ($tickers as $t) { + if ($t->symbol == $pair) $ticker = $t; + } + if (!isset($ticker)) return false; + + $price2 = ($ticker->bidPrice+$ticker->askPrice)/2; + $market->price = AverageIncrement($market->price, $ticker->bidPrice); + $market->price2 = AverageIncrement($market->price2, $price2); + $market->pricetime = time(); + $market->save(); + + $apims = round((microtime(true) - $t1)*1000,3); + user()->setFlash('message', "$exchange $symbol price updated in $apims ms"); + + return true; +} diff --git a/web/yaamp/core/exchange/exchange.php b/web/yaamp/core/exchange/exchange.php index c477a7c..1df219b 100644 --- a/web/yaamp/core/exchange/exchange.php +++ b/web/yaamp/core/exchange/exchange.php @@ -25,6 +25,7 @@ require_once("bter.php"); require_once("empoex.php"); require_once("jubi.php"); require_once("alcurex.php"); +require_once("binance.php"); require_once("cryptopia.php"); require_once("hitbtc.php"); require_once("livecoin.php"); @@ -69,7 +70,9 @@ function getMarketUrl($coin, $marketName) } if($market == 'alcurex') - $url = "https://alcurex.org/index.php/crypto/market?pair={$lowsymbol}_{$lowbase}"; + $url = "https://alcurex.com/#{$symbol}-{$base}"; + else if($market == 'binance') + $url = "https://www.binance.com/trade.html?symbol={$symbol}_{$base}"; else if($market == 'bittrex') $url = "https://bittrex.com/Market/Index?MarketName={$base}-{$symbol}"; else if($market == 'poloniex')