Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exchange: handle bitfinex public api #308

Open
wants to merge 8 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions web/yaamp/core/backend/markets.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function BackendPricesUpdate()

settings_prefetch_all();

updateBitfinexMarkets();
updateBittrexMarkets();
updateBitzMarkets();
updatePoloniexMarkets();
Expand Down Expand Up @@ -292,6 +293,61 @@ function updateBleutradeMarkets()

/////////////////////////////////////////////////////////////////////////////////////////////

function updateBitfinexMarkets()
{
$exchange = 'bitfinex';
if (exchange_get($exchange, 'disabled')) return;
$list = getdbolist('db_markets', "name LIKE '$exchange%'");
if (empty($list)) return;

$count=0;
foreach($list as $market)
{
$coin = getdbo('db_coins', $market->coinid);
if(!$coin) continue;
if (!($coin->installed || $coin->watch)) continue;
$symbol = $coin->getOfficialSymbol();
$pair = strtolower($symbol).'btc';

$ticker = bitfinex_api_query('pubticker', $pair);

$count++;
if ($count > 10) {sleep(10);$count=0;} // Rate limiting https://docs.bitfinex.com/docs/rest-general

$sqlFilter = '';
if (!empty($market->base_coin)) {
$pair = strtolower($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;
}

// 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}");
}
}

/////////////////////////////////////////////////////////////////////////////////////////////

function updateBitzMarkets($force = false)
{
$exchange = 'bitz';
Expand Down
14 changes: 13 additions & 1 deletion web/yaamp/core/backend/rawcoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ function updateRawcoins()
}
}

if (!exchange_get('bitfinex', 'disabled')) {
$list = bitfinex_api_query('symbols');
if(is_array($list) && !empty($list)) {
dborun("UPDATE markets SET deleted=true WHERE name='bitfinex'");
foreach ($list as $pair) {
if (strpos($pair, 'usd') || !strpos($pair, 'btc')) continue;
$symbol = strtoupper(str_replace('btc', '', $pair));
updateRawCoin('bitfinex', $symbol);
}
}
}

if (!exchange_get('bitz', 'disabled')) {
$list = bitz_api_query('tickerall');
if (!empty($list)) {
Expand Down Expand Up @@ -444,7 +456,7 @@ function updateRawCoin($marketname, $symbol, $name='unknown')
}
}

if (in_array($marketname, array('nova','askcoin','binance','bitz','coinexchange','coinsmarkets','cryptobridge','hitbtc'))) {
if (in_array($marketname, array('nova','askcoin','binance','bitfinex','bitz','coinexchange','coinsmarkets','cryptobridge','hitbtc'))) {
// don't polute too much the db with new coins, its better from exchanges with labels
return;
}
Expand Down
24 changes: 24 additions & 0 deletions web/yaamp/core/exchange/bitfinex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

// https://docs.bitfinex.com/docs/rest-general
// https://docs.bitfinex.com/v1/reference#rest-public-ticker
// https://api.bitfinex.com/v1/symbols
// not used yet : https://api.bitfinex.com/v1/symbols_details
// https://api.bitfinex.com/v1/pubticker/btcusd

function bitfinex_api_query($method, $params='', $returnType='object')
{
$uri = "https://api.bitfinex.com/v1/{$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 @@ -13,6 +13,7 @@ function strip_data($data)
return $out;
}

require_once("bitfinex.php");
require_once("bitstamp.php");
require_once("bittrex.php");
require_once("bitz.php");
Expand Down Expand Up @@ -82,6 +83,8 @@ function getMarketUrl($coin, $marketName)
$url = "https://alcurex.com/#{$symbol}-{$base}";
else if($market == 'binance')
$url = "https://www.binance.com/trade.html?symbol={$symbol}_{$base}";
else if($market == 'bitfinex')
$url = "https://www.bitfinex.com/t/{$symbol}:{$base}";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{$lowsymbol}:{$lowbase}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review.
I will adapt accordingly.

else if($market == 'bittrex')
$url = "https://bittrex.com/Market/Index?MarketName={$base}-{$symbol}";
else if($market == 'bitz')
Expand Down