Skip to content

Commit

Permalink
api: use new kucoin public api url and disable it
Browse files Browse the repository at this point in the history
private api to finish, tired to decrypt asian brains...
  • Loading branch information
tpruvot committed Feb 19, 2019
1 parent a36f11d commit 64ee803
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 35 deletions.
2 changes: 1 addition & 1 deletion web/yaamp/commands/ExchangeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testApiKeys()
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");
}
Expand Down
29 changes: 17 additions & 12 deletions web/yaamp/core/backend/markets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

This comment has been minimized.

Copy link
@pinpins

pinpins Feb 20, 2019

Small type to fix
kucoin_api_query('market/allTickers');

if(!kucoin_result_valid($markets) || empty($markets->data)) return;

This comment has been minimized.

Copy link
@pinpins

pinpins Feb 20, 2019

if(!kucoin_result_valid($markets) || empty($markets->data) || empty($markets->data->ticker) || !is_array($markets->data->ticker)) return;
$tickers = $markets->data->ticker;

This comment has been minimized.

Copy link
@pinpins

pinpins Feb 20, 2019

and the below second loop should be

		foreach ($tickers as $ticker) {
			if ($ticker->symbol != $pair) continue;

This comment has been minimized.

Copy link
@tpruvot

tpruvot Feb 20, 2019

Author Owner

tx, indeed


foreach($list as $market)
{
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions web/yaamp/core/backend/rawcoins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 22 additions & 15 deletions web/yaamp/core/exchange/kucoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions web/yaamp/core/trading/kucoin_trading.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ 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'");

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) {
Expand All @@ -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');
Expand Down
1 change: 1 addition & 0 deletions web/yaamp/defaultconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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', '');
Expand Down

0 comments on commit 64ee803

Please sign in to comment.