From 6d12e9aa807939012d8e3f7ac1bcd27144c08ad9 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Fri, 15 Jan 2016 14:43:41 +0100 Subject: [PATCH] cryptopia: add private api function/balance --- web/keys.sample.php | 1 + web/serverconfig.sample.php | 1 + web/yaamp/core/exchange/cryptopia.php | 61 +++++++++++++++++++ web/yaamp/core/trading/cryptopia_trading.php | 18 +++++- web/yaamp/defaultconfig.php | 3 +- .../modules/thread/CronjobController.php | 2 +- 6 files changed, 80 insertions(+), 6 deletions(-) diff --git a/web/keys.sample.php b/web/keys.sample.php index c0569ca..aa019a9 100644 --- a/web/keys.sample.php +++ b/web/keys.sample.php @@ -6,6 +6,7 @@ define('YIIMP_MYSQLDUMP_PASS', ''); /* Keys required to create/cancel orders and access your balances/deposit addresses */ define('EXCH_BITTREX_SECRET', ''); +define('EXCH_CRYPTOPIA_SECRET', ''); define('EXCH_CRYPTSY_SECRET', ''); define('EXCH_BLEUTRADE_SECRET', ''); define('EXCH_CCEX_SECRET', ''); diff --git a/web/serverconfig.sample.php b/web/serverconfig.sample.php index be10440..243ddc2 100644 --- a/web/serverconfig.sample.php +++ b/web/serverconfig.sample.php @@ -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', ''); diff --git a/web/yaamp/core/exchange/cryptopia.php b/web/yaamp/core/exchange/cryptopia.php index 9ca4424..64e5dfa 100644 --- a/web/yaamp/core/exchange/cryptopia.php +++ b/web/yaamp/core/exchange/cryptopia.php @@ -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; +} diff --git a/web/yaamp/core/trading/cryptopia_trading.php b/web/yaamp/core/trading/cryptopia_trading.php index a984c9a..c63c93e 100644 --- a/web/yaamp/core/trading/cryptopia_trading.php +++ b/web/yaamp/core/trading/cryptopia_trading.php @@ -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; diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 871121a..704973a 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -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', ''); diff --git a/web/yaamp/modules/thread/CronjobController.php b/web/yaamp/modules/thread/CronjobController.php index 2d03a40..5d829fa 100644 --- a/web/yaamp/modules/thread/CronjobController.php +++ b/web/yaamp/modules/thread/CronjobController.php @@ -137,7 +137,7 @@ class CronjobController extends CommonController doYobitTrading(); doCCexTrading(); //doAlcurexTrading(); - //doCryptopiaTrading(); + doCryptopiaTrading(); doEmpoexTrading(); doSafecexTrading();