From 64ee80336994e9e314f79c8818e92b07c732a3fb Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Tue, 19 Feb 2019 12:17:45 +0100 Subject: [PATCH] api: use new kucoin public api url and disable it private api to finish, tired to decrypt asian brains... --- web/yaamp/commands/ExchangeCommand.php | 2 +- web/yaamp/core/backend/markets.php | 29 ++++++++++-------- web/yaamp/core/backend/rawcoins.php | 1 + web/yaamp/core/exchange/kucoin.php | 37 ++++++++++++++--------- web/yaamp/core/trading/kucoin_trading.php | 14 ++++----- web/yaamp/defaultconfig.php | 1 + 6 files changed, 49 insertions(+), 35 deletions(-) diff --git a/web/yaamp/commands/ExchangeCommand.php b/web/yaamp/commands/ExchangeCommand.php index 03b4abd..51b39a4 100644 --- a/web/yaamp/commands/ExchangeCommand.php +++ b/web/yaamp/commands/ExchangeCommand.php @@ -197,7 +197,7 @@ class ExchangeCommand extends CConsoleCommand echo("kraken btc: ".json_encode($balance)."\n"); } if (!empty(EXCH_KUCOIN_KEY)) { - $balance = kucoin_api_user('account/BTC/balance'); + $balance = kucoin_api_user('accounts',array('currency=BTC')); if (!is_object($balance) || !isset($balance->data)) echo "kucoin error ".json_encode($balance)."\n"; else echo("kucoin: ".json_encode($balance->data)."\n"); } diff --git a/web/yaamp/core/backend/markets.php b/web/yaamp/core/backend/markets.php index 7413cae..6d1e519 100644 --- a/web/yaamp/core/backend/markets.php +++ b/web/yaamp/core/backend/markets.php @@ -1527,13 +1527,11 @@ function updateKuCoinMarkets() $list = getdbolist('db_markets', "name LIKE '$exchange%'"); if (empty($list)) return; - $markets = kucoin_api_query('open/symbols','market=BTC'); - if(!kucoin_result_valid($markets) || empty($markets->data)) return; + $symbols = kucoin_api_query('symbols','market=BTC'); + if(!kucoin_result_valid($symbols) || empty($symbols->data)) return; - $coininfo = NULL; //kucoin_api_query('market/open/coins'); - if(!kucoin_result_valid($coininfo) || empty($coininfo->data)) { - $coininfo = NULL; - } + $markets = kucoin_api_query('markets/allTickers'); + if(!kucoin_result_valid($markets) || empty($markets->data)) return; foreach($list as $market) { @@ -1550,18 +1548,25 @@ function updateKuCoinMarkets() $pair = strtoupper($symbol).'-BTC'; + $enableTrading = false; + foreach ($symbols->data as $sym) { + if (objSafeVal($sym,'symbol') != $pair) continue; + $enableTrading = objSafeVal($sym,'enableTrading',false); + break; + } + + if ($market->disabled == $enableTrading) { + $market->disabled = (int) (!$enableTrading); + $market->save(); + if ($market->disabled) continue; + } + foreach ($markets->data as $ticker) { if ($ticker->symbol != $pair) continue; if (objSafeVal($ticker,'buy',-1) == -1) continue; $market->price = AverageIncrement($market->price, $ticker->buy); $market->price2 = AverageIncrement($market->price2, objSafeVal($ticker,'sell',$ticker->buy)); - if (!empty($coininfo)) foreach ($coininfo->data as $info) { - if ($info->coin == $symbol) { - //todo: $market->withdrawfee = $info->withdrawMinFee; - break; - } - } $market->txfee = $ticker->feeRate * 100; // is 0.1% for trades (0.001) $market->priority = -1; $market->pricetime = time(); diff --git a/web/yaamp/core/backend/rawcoins.php b/web/yaamp/core/backend/rawcoins.php index 8a289b2..12402f4 100644 --- a/web/yaamp/core/backend/rawcoins.php +++ b/web/yaamp/core/backend/rawcoins.php @@ -17,6 +17,7 @@ function updateRawcoins() exchange_set_default('escodex', 'disabled', true); exchange_set_default('gateio', 'disabled', true); exchange_set_default('jubi', 'disabled', true); + exchange_set_default('kucoin', 'disabled', true); exchange_set_default('nova', 'disabled', true); exchange_set_default('stocksexchange', 'disabled', true); exchange_set_default('tradesatoshi', 'disabled', true); diff --git a/web/yaamp/core/exchange/kucoin.php b/web/yaamp/core/exchange/kucoin.php index 743c86e..8077f42 100644 --- a/web/yaamp/core/exchange/kucoin.php +++ b/web/yaamp/core/exchange/kucoin.php @@ -8,14 +8,14 @@ function kucoin_result_valid($obj, $method='') return true; } -// https://api.kucoin.com/v1/open/symbols/?market=BTC +// https://openapi-v2.kucoin.com/api/v1/symbols?market=BTC +// https://openapi-v2.kucoin.com/api/v1/currencies for labels function kucoin_api_query($method, $params='', $returnType='object') { $exchange = 'kucoin'; - $url = "https://api.kucoin.com/v1/$method/"; - if (!empty($params)) - $url .= "?$params"; + $url = "https://openapi-v2.kucoin.com/api/v1/$method"; + if (!empty($params)) $url .= "?$params"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -46,7 +46,7 @@ function kucoin_api_query($method, $params='', $returnType='object') return $ret; } -// https://api.kucoin.com/v1/account//wallet/address +// https://openapi-v2.kucoin.com/api/v1/deposit-addresses?currency= function kucoin_api_user($method, $params=NULL, $isPostMethod=false) { @@ -55,26 +55,33 @@ function kucoin_api_user($method, $params=NULL, $isPostMethod=false) if (!defined('EXCH_KUCOIN_SECRET')) define('EXCH_KUCOIN_SECRET', ''); if (empty(EXCH_KUCOIN_KEY) || empty(EXCH_KUCOIN_SECRET)) return false; + if (empty(EXCH_KUCOIN_PASSPHRASE)) return false; - $api_host = 'https://api.kucoin.com'; + $api_host = 'https://openapi-v2.kucoin.com'; $mt = explode(' ', microtime()); $nonce = $mt[1].substr($mt[0], 2, 3); - $url = $endpoint = "/v1/$method"; - $tosign = "$endpoint/$nonce/"; + $url = $endpoint = "/api/v1/$method"; if (empty($params)) $params = array(); $query = http_build_query($params); - if (strlen($query) && !$isPostMethod) { - $url .= '&'.$query; $query = ''; + $body = ""; + if ($isPostMethod) + $body = json_encode($params); + else if (strlen($query)) { + $body = '?'.$query; + $url .= $body; } - if ($isPostMethod) $post_data = $params; - $hmac = strtolower(hash_hmac('sha256', base64_encode($tosign.$query), EXCH_KUCOIN_SECRET)); + + $req = $isPostMethod ? "POST" : "GET"; + $tosign = "{$nonce}{$req}{$endpoint}{$body}"; + $hmac = strtolower(hash_hmac('sha256', base64_encode($tosign), EXCH_KUCOIN_SECRET)); $headers = array( 'Content-Type: application/json;charset=UTF-8', 'KC-API-KEY: '.EXCH_KUCOIN_KEY, - 'KC-API-NONCE: '.$nonce, - 'KC-API-SIGNATURE: '.$hmac, + 'KC-API-PASSPHRASE: '.EXCH_KUCOIN_PASSPHRASE, + 'KC-API-TIMESTAMP: '.$nonce, + 'KC-API-SIGN: '.$hmac, ); $ch = curl_init(); @@ -83,7 +90,7 @@ function kucoin_api_user($method, $params=NULL, $isPostMethod=false) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); if ($isPostMethod) { curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); + curl_setopt($ch, CURLOPT_POSTFIELDS, $params); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); diff --git a/web/yaamp/core/trading/kucoin_trading.php b/web/yaamp/core/trading/kucoin_trading.php index dd33182..78f6d7c 100644 --- a/web/yaamp/core/trading/kucoin_trading.php +++ b/web/yaamp/core/trading/kucoin_trading.php @@ -14,7 +14,7 @@ function doKuCoinTrading($quick=false) if (exchange_get($exchange, 'disabled')) return; - $data = kucoin_api_user('account/balance'); + $data = kucoin_api_user('accounts'); if (!kucoin_result_valid($data)) return; $savebalance = getdbosql('db_balances', "name='$exchange'"); @@ -22,19 +22,19 @@ function doKuCoinTrading($quick=false) if (is_array($data->data)) foreach($data->data as $balance) { - if ($balance->coinType == 'BTC') { + if ($balance->currency == 'BTC' && $balance->type == 'main') { if (is_object($savebalance)) { $savebalance->balance = $balance->balance; - $savebalance->onsell = $balance->freezeBalance; + $savebalance->onsell = $balance->holds; $savebalance->save(); } continue; } - if ($updatebalances) { + if ($updatebalances && $balance->type == 'main') { // store available balance in market table $coins = getdbolist('db_coins', "symbol=:symbol OR symbol2=:symbol", - array(':symbol'=>$balance->coinType) + array(':symbol'=>$balance->currency) ); if (empty($coins)) continue; foreach ($coins as $coin) { @@ -44,14 +44,14 @@ function doKuCoinTrading($quick=false) ); if (!$market) continue; $market->balance = $balance->balance; - $market->ontrade = $balance->freezeBalance; + $market->ontrade = $balance->holds; $market->balancetime = time(); $market->save(); $checked_today = cache()->get($exchange.'-deposit_address-check-'.$coin->symbol); if ($coin->installed && !$checked_today) { sleep(1); - $obj = kucoin_api_user("account/{$coin->symbol}/wallet/address"); + $obj = kucoin_api_user('deposit-addresses','currency='.$coin->symbol); if (!kucoin_result_valid($obj)) continue; $result = $obj->data; $deposit_address = objSafeVal($result,'address'); diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 8afbe3f..94fe214 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -43,6 +43,7 @@ if (!defined('EXCH_POLONIEX_KEY')) define('EXCH_POLONIEX_KEY', ''); if (!defined('EXCH_YOBIT_KEY')) define('EXCH_YOBIT_KEY', ''); if (!defined('EXCH_KRAKEN_KEY')) define('EXCH_KRAKEN_KEY', ''); if (!defined('EXCH_KUCOIN_KEY')) define('EXCH_KUCOIN_KEY', ''); +if (!defined('EXCH_KUCOIN_PASSPHRASE')) define('EXCH_KUCOIN_PASSPHRASE', ''); if (!defined('EXCH_LIVECOIN_KEY')) define('EXCH_LIVECOIN_KEY', ''); if (!defined('EXCH_NOVA_KEY')) define('EXCH_NOVA_KEY', ''); if (!defined('EXCH_STOCKSEXCHANGE_KEY')) define('EXCH_STOCKSEXCHANGE_KEY', '');