diff --git a/web/yaamp/core/backend/markets.php b/web/yaamp/core/backend/markets.php index f2d3e68..2103336 100644 --- a/web/yaamp/core/backend/markets.php +++ b/web/yaamp/core/backend/markets.php @@ -102,13 +102,13 @@ function getBestMarket($coin) // else take one of the big exchanges... $market = getdbosql('db_markets', "coinid=$coin->id AND price!=0 AND deposit_address IS NOT NULL AND deposit_address != '' AND - name IN ('poloniex','bittrex','cryptsy') ORDER BY price DESC"); + name IN ('poloniex','bittrex') ORDER BY price DESC"); } if(!$market) { // else the best price $market = getdbosql('db_markets', "coinid=$coin->id AND price!=0 AND deposit_address IS NOT NULL AND deposit_address != '' - AND name!='yobit' ORDER BY price DESC"); + AND name!='safecex' AND name!='cryptsy' ORDER BY price DESC"); } if(!$market) { $market = getdbosql('db_markets', "coinid=$coin->id AND price!=0 AND @@ -703,13 +703,31 @@ function updateSafecexMarkets() $market->price2 = AverageIncrement($market->price2, $price2); $market->price = AverageIncrement($market->price, $ticker->bid*0.98); $market->save(); + if (empty($coin->price)) { $coin->price = $market->price; $coin->price2 = $market->price2; $coin->save(); } -// debuglog("safecex: $pair $market->price ".bitcoinvaluetoa($market->price2)); - break; + + if(empty($market->deposit_address)) { + if (!isset($getbalances_called)) { + // only one query is enough + $balances = safecex_api_user('getbalances'); + $getbalances_called = true; + } + if(is_array($balances)) foreach ($balances as $balance) { + if ($balance->symbol == $coin->symbol) { + if (empty($market->deposit_address)) { + $market->deposit_address = $balance->deposit; + debuglog("safecex: {$coin->symbol} deposit address filled"); + $market->save(); + } else if ($market->deposit_address != $balance->deposit) { + debuglog("safecex: {$coin->symbol} deposit address differs!"); + } + } + } + } } } } diff --git a/web/yaamp/core/exchange/bittrex.php b/web/yaamp/core/exchange/bittrex.php index 71e2405..0a574b5 100644 --- a/web/yaamp/core/exchange/bittrex.php +++ b/web/yaamp/core/exchange/bittrex.php @@ -3,6 +3,11 @@ function bittrex_api_query($method, $params='') { require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_BITTREX_SECRET')) define('EXCH_BITTREX_SECRET', ''); + + // optional secret key + if (empty(EXCH_BITTREX_SECRET) && strpos($method, 'public') === FALSE) return FALSE; + if (empty(EXCH_BITTREX_KEY) && strpos($method, 'public') === FALSE) return FALSE; $apikey = EXCH_BITTREX_KEY; // your API-key $apisecret = EXCH_BITTREX_SECRET; // your Secret-key diff --git a/web/yaamp/core/exchange/bleutrade.php b/web/yaamp/core/exchange/bleutrade.php index ff125f5..7974336 100644 --- a/web/yaamp/core/exchange/bleutrade.php +++ b/web/yaamp/core/exchange/bleutrade.php @@ -5,28 +5,35 @@ function bleutrade_api_query($method, $params='') { require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_BLEUTRADE_SECRET')) define('EXCH_BLEUTRADE_SECRET', ''); + + // optional secret key + if (empty(EXCH_BLEUTRADE_SECRET) && strpos($method, 'public') === FALSE) return false; + if (empty(EXCH_BLEUTRADE_KEY) && strpos($method, 'public') === FALSE) return false; $apikey = EXCH_BLEUTRADE_KEY; // your API-key $apisecret = EXCH_BLEUTRADE_SECRET; // your Secret-key $nonce = time(); + $uri = "https://bleutrade.com/api/v2/$method?apikey=$apikey&nonce=$nonce$params"; - if (strpos($method,'public/') === FALSE && !strpos($method,'getdepositaddress')) - debuglog("bleutrade $method $params"); +// if (strpos($method,'public/') === FALSE && !strpos($method,'getdepositaddress')) +// debuglog("bleutrade $method $params"); $sign = hash_hmac('sha512', $uri, $apisecret); $ch = curl_init($uri); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("apisign:$sign")); + curl_setopt($ch, CURLOPT_ENCODING , 'gzip'); - $execResult = curl_exec($ch); - $obj = json_decode($execResult); + $data = curl_exec($ch); + $obj = json_decode($data); + + if(!is_object($obj)) debuglog("bleutrade: $method fail ".strip_tags($data).curl_error($ch)); + + curl_close($ch); return $obj; } - - - - diff --git a/web/yaamp/core/exchange/ccexapi.php b/web/yaamp/core/exchange/ccexapi.php index 6f0e65b..b2c1a46 100644 --- a/web/yaamp/core/exchange/ccexapi.php +++ b/web/yaamp/core/exchange/ccexapi.php @@ -11,7 +11,7 @@ class CcexAPI protected $api_url = 'https://c-cex.com/t/'; protected $api_key = EXCH_CCEX_KEY; - protected $api_secret = EXCH_CCEX_SECRET; // not used yet + protected $api_secret; // = EXCH_CCEX_SECRET; // not used yet // public function __construct($api_key = '') { // $this->api_key = $api_key; diff --git a/web/yaamp/core/exchange/cryptsy.php b/web/yaamp/core/exchange/cryptsy.php index f05955f..32d6028 100644 --- a/web/yaamp/core/exchange/cryptsy.php +++ b/web/yaamp/core/exchange/cryptsy.php @@ -5,6 +5,9 @@ function cryptsy_api_query($method, array $req = array()) // debuglog("calling cryptsy_api_query $method"); require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_CRYPTSY_SECRET')) define('EXCH_CRYPTSY_SECRET', ''); + + if (empty(EXCH_CRYPTSY_KEY)) return FALSE; // API settings $key = EXCH_CRYPTSY_KEY; // your API-key diff --git a/web/yaamp/core/exchange/empoex.php b/web/yaamp/core/exchange/empoex.php index 91304ba..d8fc552 100644 --- a/web/yaamp/core/exchange/empoex.php +++ b/web/yaamp/core/exchange/empoex.php @@ -20,8 +20,12 @@ function empoex_api_query($method) function empoex_api_user($method, $params = "") { require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_EMPOEX_SECKEY')) define('EXCH_EMPOEX_SECKEY', ''); - $api_key = EXCH_EMPOEX_SECKEY; + // optional secret key + if (empty(EXCH_EMPOEX_SECKEY) && strpos($method, 'public') === FALSE) return FALSE; + + $api_key = EXCH_EMPOEX_SECKEY; $url = "https://api.empoex.com/$method/$api_key/$params"; diff --git a/web/yaamp/core/exchange/poloniex.php b/web/yaamp/core/exchange/poloniex.php index 78f3308..b2c41f3 100644 --- a/web/yaamp/core/exchange/poloniex.php +++ b/web/yaamp/core/exchange/poloniex.php @@ -95,7 +95,7 @@ class poloniex { } public function generate_address($currency) { - debuglog("generate_address($currency)"); + debuglog("poloniex: generate address $currency"); return $this->query( array( 'command' => 'generateNewAddress', diff --git a/web/yaamp/core/exchange/safecex.php b/web/yaamp/core/exchange/safecex.php index 07c6df7..09eae06 100644 --- a/web/yaamp/core/exchange/safecex.php +++ b/web/yaamp/core/exchange/safecex.php @@ -17,3 +17,53 @@ function safecex_api_query($method, $params='') return $obj; } + +////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +// https://safecex.com/api/getbalances (seems unavailable yet) + +function safecex_api_user($method, $params='') +{ + require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_SAFECEX_SECRET')) define('EXCH_SAFECEX_SECRET', ''); + + if (empty(EXCH_SAFECEX_KEY) || empty(EXCH_SAFECEX_SECRET)) return false; + + $apikey = EXCH_SAFECEX_KEY; + + $nonce = time(); +// $mt = explode(' ', microtime()); +// $nonce = $mt[1].substr($mt[0], 2, 6); + $url = "https://safecex.com/api/$method?apikey=$apikey&nonce=$nonce$params"; + + $ch = curl_init($url); + + $sign = hash_hmac('sha512', $url, EXCH_SAFECEX_SECRET); + + curl_setopt($ch, CURLOPT_HTTPHEADER, array("apisign:$sign")); + 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; Safecex PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); + curl_setopt($ch, CURLOPT_ENCODING , ''); + + $res = curl_exec($ch); + if($res === false) + { + $e = curl_error($ch); + debuglog("safecex: $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("safecex: $method failed ($status) $res"); + } + + curl_close($ch); + + return $result; +} diff --git a/web/yaamp/core/exchange/yobit.php b/web/yaamp/core/exchange/yobit.php index 06f50c4..7d40dee 100644 --- a/web/yaamp/core/exchange/yobit.php +++ b/web/yaamp/core/exchange/yobit.php @@ -30,8 +30,11 @@ function yobit_api_query($method) function yobit_api_query2($method, $req = array()) { require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_YOBIT_SECRET')) define('EXCH_YOBIT_SECRET', ''); - $api_key = EXCH_YOBIT_KEY; + if (empty(EXCH_YOBIT_SECRET)) return FALSE; + + $api_key = EXCH_YOBIT_SECRET; $api_secret = EXCH_YOBIT_SECRET; $req['method'] = $method; diff --git a/web/yaamp/core/trading/safecex_trading.php b/web/yaamp/core/trading/safecex_trading.php new file mode 100644 index 0000000..8996576 --- /dev/null +++ b/web/yaamp/core/trading/safecex_trading.php @@ -0,0 +1,29 @@ +balance = 0; + + if (is_array($balances)) foreach($balances as $balance) + { + if($balance->symbol == 'BTC') { + $savebalance->balance = $balance->balance; + $savebalance->save(); + break; + } + } + } + + if (!YAAMP_ALLOW_EXCHANGE) return; + + // implement trade here... +} diff --git a/web/yaamp/core/trading/trading.php b/web/yaamp/core/trading/trading.php index 820df04..178e77c 100644 --- a/web/yaamp/core/trading/trading.php +++ b/web/yaamp/core/trading/trading.php @@ -9,3 +9,4 @@ require_once('empoex_trading.php'); require_once('yobit_trading.php'); require_once('alcurex_trading.php'); require_once('cryptopia_trading.php'); +require_once('safecex_trading.php'); diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 18e29cc..871121a 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -10,8 +10,8 @@ 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_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); @@ -30,15 +30,6 @@ 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', ''); -if (!defined('EXCH_BITTREX_SECRET')) define('EXCH_BITTREX_SECRET', ''); -if (!defined('EXCH_BLEUTRADE_SECRET')) define('EXCH_BLEUTRADE_SECRET', ''); -if (!defined('EXCH_CCEX_SECRET')) define('EXCH_CCEX_SECRET', ''); -if (!defined('EXCH_CRYPTSY_SECRET')) define('EXCH_CRYPTSY_SECRET', ''); -if (!defined('EXCH_EMPOEX_SECKEY')) define('EXCH_EMPOEX_SECKEY', ''); -if (!defined('EXCH_POLONIEX_SECRET')) define('EXCH_POLONIEX_SECRET', ''); -if (!defined('EXCH_SAFECEX_SECRET')) define('EXCH_SAFECEX_SECRET', ''); -if (!defined('EXCH_YOBIT_SECRET')) define('EXCH_YOBIT_SECRET', ''); - if (!defined('YAAMP_BTCADDRESS')) define('YAAMP_BTCADDRESS', ''); if (!defined('YAAMP_SITE_URL')) define('YAAMP_SITE_URL', 'localhost'); if (!defined('YAAMP_SITE_NAME')) define('YAAMP_SITE_NAME', 'YiiMP'); diff --git a/web/yaamp/modules/site/SiteController.php b/web/yaamp/modules/site/SiteController.php index ca58b38..2073d35 100644 --- a/web/yaamp/modules/site/SiteController.php +++ b/web/yaamp/modules/site/SiteController.php @@ -749,6 +749,11 @@ class SiteController extends CommonController updateEmpoexMarkets(); break; + case 'safecex': + doSafecexTrading(true); + updateSafecexMarkets(); + break; + case 'yobit': doYobitTrading(true); updateYobitMarkets(); diff --git a/web/yaamp/modules/thread/CronjobController.php b/web/yaamp/modules/thread/CronjobController.php index b145d60..2d03a40 100644 --- a/web/yaamp/modules/thread/CronjobController.php +++ b/web/yaamp/modules/thread/CronjobController.php @@ -139,6 +139,7 @@ class CronjobController extends CommonController //doAlcurexTrading(); //doCryptopiaTrading(); doEmpoexTrading(); + doSafecexTrading(); break;