From a81493224d6b9f4464aa25cc0ce9c55132f02c72 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Fri, 15 Jan 2016 17:45:01 +0100 Subject: [PATCH] add banx private api (v3) support get deposit addresses... --- web/keys.sample.php | 1 + web/serverconfig.sample.php | 1 + web/yaamp/commands/ExchangeCommand.php | 5 ++ web/yaamp/core/backend/markets.php | 50 +++++++++++++++++-- web/yaamp/core/exchange/banxio.php | 46 +++++++++++++++++ web/yaamp/core/trading/banx_trading.php | 26 ++++++++++ web/yaamp/core/trading/trading.php | 2 + web/yaamp/defaultconfig.php | 1 + web/yaamp/modules/site/SiteController.php | 7 ++- .../modules/thread/CronjobController.php | 1 + 10 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 web/yaamp/core/trading/banx_trading.php diff --git a/web/keys.sample.php b/web/keys.sample.php index aa019a9..9b89ea0 100644 --- a/web/keys.sample.php +++ b/web/keys.sample.php @@ -8,6 +8,7 @@ define('YIIMP_MYSQLDUMP_PASS', ''); define('EXCH_BITTREX_SECRET', ''); define('EXCH_CRYPTOPIA_SECRET', ''); define('EXCH_CRYPTSY_SECRET', ''); +define('EXCH_BANX_SECKEY', ''); define('EXCH_BLEUTRADE_SECRET', ''); define('EXCH_CCEX_SECRET', ''); define('EXCH_EMPOEX_SECKEY', ''); diff --git a/web/serverconfig.sample.php b/web/serverconfig.sample.php index 243ddc2..41f19fa 100644 --- a/web/serverconfig.sample.php +++ b/web/serverconfig.sample.php @@ -41,6 +41,7 @@ define('EXCH_BLEUTRADE_KEY', ''); define('EXCH_YOBIT_KEY', ''); define('EXCH_CCEX_KEY', ''); define('EXCH_SAFECEX_KEY', ''); +define('EXCH_BANX_USERNAME', ''); // Automatic withdraw to Yaamp btc wallet if btc balance > 0.3 define('EXCH_AUTO_WITHDRAW', 0.3); diff --git a/web/yaamp/commands/ExchangeCommand.php b/web/yaamp/commands/ExchangeCommand.php index 7fdffc1..84d01d6 100644 --- a/web/yaamp/commands/ExchangeCommand.php +++ b/web/yaamp/commands/ExchangeCommand.php @@ -92,6 +92,11 @@ class ExchangeCommand extends CConsoleCommand if (!arraySafeVal($info,'success',0) || !is_array($info['return'])) echo "error\n"; else echo("yobit btc: ".json_encode($info['return']['funds']['btc'])."\n"); } + if (!empty(EXCH_BANX_USERNAME)) { + //$balance = banx_api_user('account/getbalance','?currency=BTC'); + $balance = banx_api_user('account/getbalances'); + echo("banx all: ".json_encode($balance->result)."\n"); + } // only one secret key $balance = empoex_api_user('account/balance','BTC'); diff --git a/web/yaamp/core/backend/markets.php b/web/yaamp/core/backend/markets.php index e5ed59d..f2db5f6 100644 --- a/web/yaamp/core/backend/markets.php +++ b/web/yaamp/core/backend/markets.php @@ -745,8 +745,10 @@ function updateBanxioMarkets() $data = banx_public_api_query('getmarketsummaries'); if(!$data || !is_array($data->result)) return; - $list = getdbolist('db_markets', "name='banx'"); - foreach($list as $market) + $symbols = array(); + + $currencies = getdbolist('db_markets', "name='banx'"); + foreach($currencies as $market) { $coin = getdbo('db_coins', $market->coinid); if(!$coin || !$coin->installed) continue; @@ -767,12 +769,54 @@ function updateBanxioMarkets() if ($coin->name == 'unknown' && !empty($ticker->currencylong)) { $coin->name = $ticker->currencylong; $coin->save(); - debuglog("banx: update $symbol name {$coin->name}"); + debuglog("$exchange: update $symbol label {$coin->name}"); } + // store for deposit addresses + $symbols[$ticker->currencylong] = $coin->symbol; + $symbols[$ticker->partnerlong] = $coin->symbol; break; } } } + + if(!empty(EXCH_BANX_USERNAME)) + { + // deposit addresses + $last_checked = cache()->get($exchange.'-deposit_address-check'); + if (!$last_checked) { + // no coin symbols in the results wtf ! only labels :/ + $query = banx_api_user('account/getdepositaddresses'); + } + if (!isset($query)) return; + if (!is_object($query)) return; + if (!$query->success || !is_array($query->result)) return; + + foreach($query->result as $account) + { + if (!isset($account->currency) || !isset($account->address)) continue; + if (empty($account->currency) || empty($account->address)) continue; + + $label = $account->currency; + if (!isset($symbols[$label])) continue; + + $symbol = $symbols[$label]; + + if($symbol == 'BTC') continue; + + $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol'=>$symbol)); + if(!$coin) continue; + + $market = getdbosql('db_markets', "coinid={$coin->id} and name='$exchange'"); + if(!$market) continue; + + if ($market->deposit_address != $account->address) { + $market->deposit_address = $account->address; + $market->save(); + debuglog("$exchange: deposit address for $symbol updated"); + } + } + cache()->set($exchange.'-deposit_address-check', time(), 12*3600); + } } function updateSafecexMarkets() diff --git a/web/yaamp/core/exchange/banxio.php b/web/yaamp/core/exchange/banxio.php index 83a0966..9d5d2c3 100644 --- a/web/yaamp/core/exchange/banxio.php +++ b/web/yaamp/core/exchange/banxio.php @@ -33,3 +33,49 @@ function banx_public_api_query($method, $params='') return $obj; } + +// methods ok getbalances +// method failed getbalance "?currency=BTC" + +function banx_api_user($method, $params='') +{ + require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_BANX_SECKEY')) define('EXCH_BANX_SECKEY', ''); + + if (empty(EXCH_BANX_USERNAME) || empty(EXCH_BANX_SECKEY)) return false; + + $uri = "https://www.banx.io/api/v3/$method$params"; + + //$nonce = time(); + $mt = explode(' ', microtime()); + $nonce = $mt[1].substr($mt[0], 2, 6); + + $headers = array( + 'Content-Type: application/json; charset=utf-8', + 'uts: '.$nonce, + 'uid: '.EXCH_BANX_USERNAME, + 'aut: '.EXCH_BANX_SECKEY, + ); + + $ch = curl_init($uri); + curl_setopt($ch, CURLOPT_POST, 0); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + //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_USERAGENT, 'Mozilla/4.0 (compatible; Banx API PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); + curl_setopt($ch, CURLOPT_ENCODING , ''); + + $data = curl_exec($ch); + $res = json_decode($data); + + if(!is_object($res) || !$res->success) { + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + debuglog("banx: $method failed ($status) ".strip_tags($data).' '.curl_error($ch)); + } + + curl_close($ch); + + return $res; +} \ No newline at end of file diff --git a/web/yaamp/core/trading/banx_trading.php b/web/yaamp/core/trading/banx_trading.php new file mode 100644 index 0000000..17f42bc --- /dev/null +++ b/web/yaamp/core/trading/banx_trading.php @@ -0,0 +1,26 @@ +result) || !$balances->success) return; + + $savebalance = getdbosql('db_balances', "name='banx'"); + if (!is_object($savebalance)) return; + + $savebalance->balance = 0; + + foreach($balances->result as $balance) + { + if($balance->currency == 'BTC') { + $savebalance->balance = $balance->available; + $savebalance->save(); + break; + } + } + + if (!YAAMP_ALLOW_EXCHANGE) return; +} diff --git a/web/yaamp/core/trading/trading.php b/web/yaamp/core/trading/trading.php index 178e77c..058fe72 100644 --- a/web/yaamp/core/trading/trading.php +++ b/web/yaamp/core/trading/trading.php @@ -8,5 +8,7 @@ require_once('c-cex_trading.php'); require_once('empoex_trading.php'); require_once('yobit_trading.php'); require_once('alcurex_trading.php'); +require_once('banx_trading.php'); require_once('cryptopia_trading.php'); require_once('safecex_trading.php'); + diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 704973a..ca2ac45 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -28,6 +28,7 @@ if (!defined('EXCH_CRYPTOPIA_KEY')) define('EXCH_CRYPTOPIA_KEY', ''); if (!defined('EXCH_POLONIEX_KEY')) define('EXCH_POLONIEX_KEY', ''); if (!defined('EXCH_SAFECEX_KEY')) define('EXCH_SAFECEX_KEY', ''); if (!defined('EXCH_YOBIT_KEY')) define('EXCH_YOBIT_KEY', ''); +if (!defined('EXCH_BANX_USERNAME')) define('EXCH_BANX_USERNAME', ''); if (!defined('YAAMP_BTCADDRESS')) define('YAAMP_BTCADDRESS', ''); if (!defined('YAAMP_SITE_URL')) define('YAAMP_SITE_URL', 'localhost'); diff --git a/web/yaamp/modules/site/SiteController.php b/web/yaamp/modules/site/SiteController.php index 2073d35..e867730 100644 --- a/web/yaamp/modules/site/SiteController.php +++ b/web/yaamp/modules/site/SiteController.php @@ -724,8 +724,13 @@ class SiteController extends CommonController updateAlcurexMarkets(); break; + case 'banx': + doBanxTrading(true); + updateBanxMarkets(); + break; + case 'cryptopia': - //doCryptopiaTrading(true); + doCryptopiaTrading(true); updateCryptopiaMarkets(); break; diff --git a/web/yaamp/modules/thread/CronjobController.php b/web/yaamp/modules/thread/CronjobController.php index 5d829fa..9761a97 100644 --- a/web/yaamp/modules/thread/CronjobController.php +++ b/web/yaamp/modules/thread/CronjobController.php @@ -137,6 +137,7 @@ class CronjobController extends CommonController doYobitTrading(); doCCexTrading(); //doAlcurexTrading(); + doBanxTrading(); doCryptopiaTrading(); doEmpoexTrading(); doSafecexTrading();