diff --git a/web/keys.sample.php b/web/keys.sample.php index a0fd40b..63509ca 100644 --- a/web/keys.sample.php +++ b/web/keys.sample.php @@ -7,6 +7,7 @@ define('YIIMP_MYSQLDUMP_PASS', ''); /* Keys required to create/cancel orders and access your balances/deposit addresses */ define('EXCH_BITTREX_SECRET', ''); define('EXCH_BITSTAMP_SECRET',''); +define('EXCH_BINANCE_SECRET', ''); define('EXCH_BLEUTRADE_SECRET', ''); define('EXCH_BTER_SECRET', ''); define('EXCH_CCEX_SECRET', ''); diff --git a/web/serverconfig.sample.php b/web/serverconfig.sample.php index 40c083e..59b4a7f 100644 --- a/web/serverconfig.sample.php +++ b/web/serverconfig.sample.php @@ -53,6 +53,7 @@ define('EXCH_CEXIO_ID', ''); define('EXCH_CEXIO_KEY', ''); define('EXCH_COINMARKETS_USER', ''); define('EXCH_COINMARKETS_PIN', ''); +define('EXCH_BINANCE_KEY', ''); define('EXCH_BITSTAMP_ID',''); define('EXCH_BITSTAMP_KEY',''); define('EXCH_HITBTC_KEY',''); diff --git a/web/yaamp/commands/ExchangeCommand.php b/web/yaamp/commands/ExchangeCommand.php index 44bc860..73f538f 100644 --- a/web/yaamp/commands/ExchangeCommand.php +++ b/web/yaamp/commands/ExchangeCommand.php @@ -122,6 +122,16 @@ class ExchangeCommand extends CConsoleCommand public function testApiKeys() { + if (!empty(EXCH_BINANCE_KEY)) { + $balance = binance_api_user('account'); + if (!is_object($balance)) echo "binance error ".json_encode($balance)."\n"; + else { + $assets = $balance->balances; + foreach ($assets as $asset) { + if ($asset->asset == 'BTC') echo("binance: ".json_encode($asset)."\n"); + } + } + } if (!empty(EXCH_BITSTAMP_KEY)) { $balance = bitstamp_api_user('balance'); if (!is_array($balance)) echo "bitstamp error ".json_encode($balance)."\n"; diff --git a/web/yaamp/core/exchange/binance.php b/web/yaamp/core/exchange/binance.php index a83b44c..456d09d 100644 --- a/web/yaamp/core/exchange/binance.php +++ b/web/yaamp/core/exchange/binance.php @@ -4,19 +4,91 @@ function binance_api_query($method) { + $exchange = 'binance'; $uri = "https://www.binance.com/api/v1/$method"; $ch = curl_init($uri); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Binance API PHP client; '.php_uname('s').'; PHP/'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.')'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $res = curl_exec($ch); $obj = json_decode($res); + if(!is_object($obj) && !is_array($obj)) { + $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + debuglog("$exchange: $method failed ($status) ".strip_data($res)); + } return $obj; } +// https://api.binance.com/api/v3/account + +function binance_api_user($method, $params=NULL) +{ + $exchange = 'binance'; + require_once('/etc/yiimp/keys.php'); + if (!defined('EXCH_BINANCE_SECRET')) define('EXCH_BINANCE_SECRET', ''); + + if (empty(EXCH_BINANCE_KEY) || empty(EXCH_BINANCE_SECRET)) return false; + + $mt = explode(' ', microtime()); + $nonce = $mt[1].substr($mt[0], 2, 3); + $url = "https://api.binance.com/api/v3/$method"; + + if (empty($params)) $params = array(); + $params['timestamp'] = $nonce; + $query = http_build_query($params, '', '&'); + + $hmac = strtolower(hash_hmac('sha256', $query, EXCH_BINANCE_SECRET)); + $isPostMethod = ($method == 'order'); + if ($isPostMethod) + $query .= '&signature='.$hmac; + else + $url .= '?'.$query.'&signature='.$hmac; + + $headers = array( + 'Content-Type: application/json;charset=UTF-8', + 'X-MBX-APIKEY: '.EXCH_BINANCE_KEY, + ); + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + if ($isPostMethod) { + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $query); + } + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; Binance API PHP client; '.php_uname('s').'; PHP/'.PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.')'); + curl_setopt($ch, CURLOPT_ENCODING , ''); + //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + //curl_setopt($ch, CURLOPT_VERBOSE, 1); + + $res = curl_exec($ch); + if($res === false) { + $e = curl_error($ch); + debuglog("$exchange: $method $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("$exchange: $method failed ($status) ".strip_data($res)); + } + + curl_close($ch); + + return $result; +} + ////////////////////////////////////////////////////////////////////////////////////////////////////////////// // manual update of one market diff --git a/web/yaamp/defaultconfig.php b/web/yaamp/defaultconfig.php index 26ff494..195eb21 100644 --- a/web/yaamp/defaultconfig.php +++ b/web/yaamp/defaultconfig.php @@ -26,6 +26,7 @@ if (!defined('YAAMP_PAYMENTS_MINI')) define('YAAMP_PAYMENTS_MINI', 0.001); if (!defined('YAAMP_ALLOW_EXCHANGE')) define('YAAMP_ALLOW_EXCHANGE', false); if (!defined('EXCH_AUTO_WITHDRAW')) define('EXCH_AUTO_WITHDRAW', 9999.9999); +if (!defined('EXCH_BINANCE_KEY')) define('EXCH_BINANCE_KEY', ''); if (!defined('EXCH_BITTREX_KEY')) define('EXCH_BITTREX_KEY', ''); if (!defined('EXCH_BITSTAMP_ID')) define('EXCH_BITSTAMP_ID', ''); if (!defined('EXCH_BITSTAMP_KEY')) define('EXCH_BITSTAMP_KEY','');