diff --git a/web/yaamp/core/backend/rawcoins.php b/web/yaamp/core/backend/rawcoins.php index ec655c1..3fbcc02 100644 --- a/web/yaamp/core/backend/rawcoins.php +++ b/web/yaamp/core/backend/rawcoins.php @@ -265,7 +265,10 @@ function updateRawCoin($marketname, $symbol, $name='unknown') } } - if ($marketname == 'nova' || $marketname == 'askcoin' || $marketname == 'coinexchange') { + if ($marketname == 'nova' && !defined('EXCH_NOVA_KEY')) { + return; + } + if ($marketname == 'askcoin' || $marketname == 'coinexchange') { // don't polute too much the db return; } diff --git a/web/yaamp/core/exchange/nova.php b/web/yaamp/core/exchange/nova.php index 108984a..4c79646 100644 --- a/web/yaamp/core/exchange/nova.php +++ b/web/yaamp/core/exchange/nova.php @@ -2,11 +2,9 @@ // markets -function nova_api_query($method, $params='') +function nova_api_query($method) { $uri = "https://novaexchange.com/remote/$method/"; - if (!empty($params)) - $uri .= "$params"; $ch = curl_init($uri); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -17,14 +15,14 @@ function nova_api_query($method, $params='') return $obj; } -function nova_api_user($method, $params='') +function nova_api_user($method, $params=array()) { require_once('/etc/yiimp/keys.php'); if (!defined('EXCH_NOVA_SECRET')) define('EXCH_NOVA_SECRET', ''); if (empty(EXCH_NOVA_KEY) || empty(EXCH_NOVA_SECRET)) return false; - $uri = "https://novaexchange.com/remote/private/$method/$params"; + $uri = "https://novaexchange.com/remote/private/$method/"; $headers = array( 'Content-Type: application/x-www-form-urlencoded', @@ -33,12 +31,13 @@ function nova_api_user($method, $params='') $mt = explode(' ', microtime()); $nonce = $mt[1].substr($mt[0], 2, 6); - $postdata = array( - 'apikey'=>EXCH_NOVA_KEY, - 'apisecret'=>EXCH_NOVA_SECRET, - 'nonce'=>$nonce, - ); - $postdata = http_build_query($postdata, '', '&'); + $params['apikey'] = EXCH_NOVA_KEY; + $params['apisecret'] = EXCH_NOVA_SECRET; + $params['nonce'] = $nonce; + + $postdata = http_build_query($params, '', '&'); + // Clear the params array so that we don't accidentaly leak our keys + $params = array(); $ch = curl_init($uri); //curl_setopt($ch, CURLOPT_POST, 1); diff --git a/web/yaamp/core/trading/nova_trading.php b/web/yaamp/core/trading/nova_trading.php index bb9c69b..414e6fe 100644 --- a/web/yaamp/core/trading/nova_trading.php +++ b/web/yaamp/core/trading/nova_trading.php @@ -1,5 +1,26 @@ success === TRUE) { + $db_order = getdbosql( + 'db_orders', + 'market=:market AND uuid=:uuid', + array(':market'=>'nova', ':uuid'=>$id) + ); + + if ($db_order) { + $db_order->delete(); + } + } +} + function doNovaTrading($quick=false) { $exchange = 'nova'; @@ -16,8 +37,9 @@ function doNovaTrading($quick=false) if (is_object($savebalance)) { $savebalance->balance = 0; $savebalance->save(); - - dborun("UPDATE markets SET balance=0 WHERE name='{$exchange}'"); + } else { + dborun("INSERT INTO balances (name,balance) VALUES ('$exchange',0)"); + $savebalance = getdbosql('db_balances', "name='{$exchange}'"); } foreach($balances->balances as $balance) @@ -51,4 +73,171 @@ function doNovaTrading($quick=false) $flushall = rand(0, 8) == 0; if($quick) $flushall = false; + + + $min_btc_trade = exchange_get($exchange, 'trade_min_btc', 0.0001); + $sell_ask_pct = exchange_get($exchange, 'trade_sell_ask_pct', 1.05); + $cancel_ask_pct = exchange_get($exchange, 'trade_cancel_ask_pct', 1.20); + + // upgrade orders + $coins = getdbolist('db_coins', "enable=1 AND IFNULL(dontsell,0)=0 AND id IN (SELECT DISTINCT coinid FROM markets WHERE name='{$exchange}')"); + foreach ($coins as $coin) { + if ($coin->dontsell || $coin->symbol == 'BTC') { + continue; + } + + $pair = $coin->symbol.'_BTC'; + sleep(1); + $orders = nova_api_user("myopenorders_market/{$pair}"); + + if (isset($orders->items)) { + $order_data = $orders->items; + } else { + $order_data = array(); + } + + foreach ($order_data as $order) { + $uuid = $order->orderid; + $pair = $order->market; + sleep(1); + $ticker = nova_api_query("market/info/{$pair}"); + + if (!is_object($ticker) || !$order->price) { + continue; + } + + if ($order->price > $cancel_ask_pct*$ticker->ask || $flushall) { + sleep(1); + doNovaCancelOrder($uuid); + } else { + $db_order = getdbosql( + 'db_orders', + 'market=:market AND uuid=:uuid', + array(':market'=>$exchange, ':uuid'=>$uuid) + ); + + if ($db_order) { + continue; + } + + $db_order = new db_orders; + $db_order->market = $exchange; + $db_order->coinid = $coin->id; + $db_order->amount = $order->fromamount; // + $db_order->price = $order->price; // + $db_order->ask = $ticker->ask; // + $db_order->bid = $ticker->bid; // + $db_order->uuid = $uuid; + $db_order->created = time(); + $db_order->save(); + } + } + $list = getdbolist('db_orders', "coinid=$coin->id and market='$exchange'"); + foreach ($list as $db_order) { + $found = false; + foreach ($order_data as $order) { + $uuid = $order->id; + if ($uuid == $db_order->uuid) { + $found = true; + break; + } + } + if (!$found) { + debuglog("Nova: Deleting order $coin->name $db_order->amount"); + $db_order->delete(); + } + } + } + sleep(2); + + /* Update balances and sell */ + if (!$balances) { + return; + } + + foreach ($balances->balances as $balance) { + $amount = $balance->amount; + $symbol = $balance->currency; + if ($symbol == 'BTC') { + continue; + } + + $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol'=>$symbol)); + if (!$coin || $coin->dontsell) { + continue; + } + + $market2 = getdbosql('db_markets', "coinid={$coin->id} AND (name='bittrex' OR name='poloniex')"); + if ($market2) { + continue; + } + + $market = getdbosql('db_markets', "coinid=$coin->id and name='{$exchange}'"); + if ($market) { + $market->lasttraded = time(); + $market->save(); + } + + if ($amount*$coin->price < $min_btc_trade) { + continue; + } + + sleep(1); + + $pair = "{$symbol}_BTC"; + $ticker = nova_api_query("market/info/{$pair}"); + if(!(isset($ticker->bid) && isset($ticker->ask))) continue; + if($coin->sellonbid) + $sellprice = bitcoinvaluetoa($ticker->bid); + else + $sellprice = bitcoinvaluetoa($ticker->ask * $sell_ask_pct); + + if ($amount*$sellprice > $min_btc_trade) { + debuglog("Nova: Selling market $pair, $amount, $sellprice"); + sleep(1); + + $res = nova_api_user("trade/{$pair}", array("tradetype=SELL", "tradeprice={$sellprice}", "tradeamount={$amount}", "tradebase=1")); + if (!$res->status == 'success') { + debuglog('Nova: Sell failed'); + continue; + } + } + + $db_order = new db_orders; + $db_order->market = $exchange; + $db_order->coinid = $coin->id; + $db_order->amount = $amount; + $db_order->price = $sellprice; + $db_order->ask = $ticker->ask; + $db_order->bid = $ticker->bid; + $db_order->uuid = $res->tradeitems[0]->orderid; + $db_order->created = time(); + $db_order->save(); + } + + /* Withdrawals */ + $btcaddr = YAAMP_BTCADDRESS; + $withdraw_min = exchange_get($exchange, 'withdraw_min_btc', EXCH_AUTO_WITHDRAW); + $withdraw_fee = exchange_get($exchange, 'withdraw_fee_btc', 0.0005); + if (floatval($withdraw_min) > 0 && $savebalance->balance >= ($withdraw_min + $withdraw_fee)) { + $amount = $savebalance->balance - $withdraw_fee; + debuglog("$exchange: withdraw $amount BTC to $btcaddr"); + sleep(1); + $res = nova_api_user("withdraw/BTC", array("currency=BTC", "amount={$amount}", "address={$btcaddr}")); + debuglog("$exchange: withdraw ".json_encode($res)); + if ($res->status == 'success') { + $withdraw = new db_withdraws; + $withdraw->market = $exchange; + $withdraw->address = $btcaddr; + $withdraw->amount = $amount; + $withdraw->time = time(); + //$withdraw->uuid = $res->id; + $withdraw->save(); + $savebalance->balance = $res->amount_after_withdraw; + $savebalance->save(); + } else { + debuglog("$exchange: Withdraw Failed ".json_encode($res)); + } + } + }