api: use new kucoin public api url and disable it

private api to finish, tired to decrypt asian brains...
This commit is contained in:
Tanguy Pruvot 2019-02-19 12:17:45 +01:00
parent a36f11d6ba
commit 64ee803369
6 changed files with 49 additions and 35 deletions

View file

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

View file

@ -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();

View file

@ -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);

View file

@ -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/<coin>/wallet/address
// https://openapi-v2.kucoin.com/api/v1/deposit-addresses?currency=<coin>
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);

View file

@ -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');

View file

@ -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', '');