cryptopia: add private api function/balance

This commit is contained in:
Tanguy Pruvot 2016-01-15 14:43:41 +01:00
parent 2a1ce0da24
commit 6d12e9aa80
6 changed files with 80 additions and 6 deletions

View file

@ -6,6 +6,7 @@ define('YIIMP_MYSQLDUMP_PASS', '<my_mysql_password>');
/* Keys required to create/cancel orders and access your balances/deposit addresses */
define('EXCH_BITTREX_SECRET', '<my_bittrex_api_secret_key>');
define('EXCH_CRYPTOPIA_SECRET', '');
define('EXCH_CRYPTSY_SECRET', '');
define('EXCH_BLEUTRADE_SECRET', '');
define('EXCH_CCEX_SECRET', '');

View file

@ -33,6 +33,7 @@ define('YAAMP_ADMIN_IP', '80.236.118.26');
define('YAAMP_USE_NGINX', false);
// Exchange public keys (private keys are in a separate config file)
define('EXCH_CRYPTOPIA_KEY', '');
define('EXCH_CRYPTSY_KEY', '');
define('EXCH_POLONIEX_KEY', '');
define('EXCH_BITTREX_KEY', '');

View file

@ -18,3 +18,64 @@ function cryptopia_api_query($method, $params='')
return $obj;
}
// https://www.cryptopia.co.nz/api/GetBalance
function cryptopia_api_user($method, $params=NULL)
{
require_once('/etc/yiimp/keys.php');
if (!defined('EXCH_CRYPTOPIA_SECRET')) define('EXCH_CRYPTOPIA_SECRET', '');
if (empty(EXCH_CRYPTOPIA_KEY) || empty(EXCH_CRYPTOPIA_SECRET)) return false;
$apikey = EXCH_CRYPTOPIA_KEY;
$mt = explode(' ', microtime());
$nonce = $mt[1].substr($mt[0], 2, 6);
$url = "https://www.cryptopia.co.nz/Api/$method";
if (empty($params)) $params = new stdclass;
$post_data = json_encode($params);
$hashpost = base64_encode(md5($post_data, true));
$url_encoded = strtolower(urlencode($url));
$sig = "{$apikey}POST{$url_encoded}{$nonce}{$hashpost}";
$hmac = base64_encode(hash_hmac('sha256', $sig, base64_decode(EXCH_CRYPTOPIA_SECRET), true));
$headers = array(
'Content-Type: application/json; charset=utf-8',
'Authorization: amx '.$apikey.':'.$hmac.':'.$nonce,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Cryptopia API PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
curl_setopt($ch, CURLOPT_ENCODING , '');
$res = curl_exec($ch);
if($res === false)
{
$e = curl_error($ch);
debuglog("cryptopia: $e");
curl_close($ch);
return false;
}
$result = json_decode($res);
if(!is_object($result) && !is_array($result)) {
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
debuglog("cryptopia: $method failed ($status) $res");
}
curl_close($ch);
return $result;
}

View file

@ -5,10 +5,22 @@ function doCryptopiaTrading($quick=false)
$flushall = rand(0, 4) == 0;
if($quick) $flushall = false;
$balance = getdbosql('db_balances', "name='cryptopia'");
if(!$balance) return;
$balances = getdbosql('db_balances', "name='cryptopia'");
if(!$balances) return;
// not available yet...
$filter = array("Currency"=>"BTC");
$query = cryptopia_api_user('GetBalance', $filter);
if (is_object($query) && is_array($query->Data))
foreach($query->Data as $balance)
{
if($balance->Symbol == 'BTC')
{
$balances->balance = $balance->Available;
$balances->save();
break;
}
}
if (!YAAMP_ALLOW_EXCHANGE) return;

View file

@ -10,8 +10,6 @@ if (!defined('YAAMP_DBHOST')) define('YAAMP_DBHOST', 'localhost');
if (!defined('YAAMP_DBNAME')) define('YAAMP_DBNAME', 'yaamp');
if (!defined('YAAMP_DBUSER')) define('YAAMP_DBUSER', 'root');
if (!defined('YAAMP_DBPASSWORD')) define('YAAMP_DBPASSWORD', '');
//if (!defined('YIIMP_MYSQLDUMP_USER')) define('YIIMP_MYSQLDUMP_USER', 'root');
//if (!defined('YIIMP_MYSQLDUMP_PASS')) define('YIIMP_MYSQLDUMP_PASS', '');
if (!defined('YIIMP_PUBLIC_EXPLORER')) define('YIIMP_PUBLIC_EXPLORER', true);
@ -26,6 +24,7 @@ if (!defined('EXCH_CRYPTSY_KEY')) define('EXCH_CRYPTSY_KEY', '');
if (!defined('EXCH_BITTREX_KEY')) define('EXCH_BITTREX_KEY', '');
if (!defined('EXCH_BLEUTRADE_KEY')) define('EXCH_BLEUTRADE_KEY', '');
if (!defined('EXCH_CCEX_KEY')) define('EXCH_CCEX_KEY', '');
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', '');

View file

@ -137,7 +137,7 @@ class CronjobController extends CommonController
doYobitTrading();
doCCexTrading();
//doAlcurexTrading();
//doCryptopiaTrading();
doCryptopiaTrading();
doEmpoexTrading();
doSafecexTrading();