public function actionSellto() { if (!$this->admin) { return; } $market = getdbo('db_markets', getiparam('id')); $coin = getdbo('db_coins', $market->coinid); $amount = getparam('amount'); $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $info = $remote->getinfo(); if (!$info || !$info['balance']) { return false; } $deposit_info = $remote->validateaddress($market->deposit_address); if (!$deposit_info || !isset($deposit_info['isvalid']) || !$deposit_info['isvalid']) { user()->setFlash('error', "invalid address {$coin->name}, {$market->deposit_address}"); $this->redirect(array('site/coin', 'id' => $coin->id)); } $amount = min($amount, $info['balance'] - $info['paytxfee']); // $amount = max($amount, $info['balance'] - $info['paytxfee']); $amount = round($amount, 8); debuglog("selling ({$market->deposit_address}, {$amount})"); $tx = $remote->sendtoaddress($market->deposit_address, $amount); if (!$tx) { user()->setFlash('error', $remote->error); $this->redirect(array('site/coin', 'id' => $coin->id)); } $exchange = new db_exchange(); $exchange->market = $market->name; $exchange->coinid = $coin->id; $exchange->send_time = time(); $exchange->quantity = $amount; $exchange->price_estimate = $coin->price; $exchange->status = 'waiting'; $exchange->tx = $tx; $exchange->save(); $this->redirect(array('site/coin', 'id' => $coin->id)); }
function sellCoinToExchange($coin) { if ($coin->dontsell) { return; } $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $info = $remote->getinfo(); if (!$info || !$info['balance']) { return false; } if (!empty($coin->symbol2)) { $coin2 = getdbosql('db_coins', "symbol='{$coin->symbol2}'"); if (!$coin2) { return; } $amount = $info['balance'] - $info['paytxfee']; $amount *= 0.9; // debuglog("sending $amount $coin->symbol to main wallet"); $tx = $remote->sendtoaddress($coin2->master_wallet, $amount); // if(!$tx) debuglog($remote->error); return; } $market = getBestMarket($coin); if (!$market) { return; } if (!$coin->sellonbid && $market->lastsent != null && $market->lastsent > $market->lasttraded) { // debuglog("*** not sending $coin->name to $market->name. last tx is late ***"); return; } $deposit_address = $market->deposit_address; $marketname = $market->name; if (empty($deposit_address)) { return false; } $reserved1 = dboscalar("select sum(balance) from accounts where coinid={$coin->id}"); $reserved2 = dboscalar("select sum(amount*price) from earnings\n\t\twhere status!=2 and userid in (select id from accounts where coinid={$coin->id})"); $reserved = ($reserved1 + $reserved2) * 10; $amount = $info['balance'] - $info['paytxfee'] - $reserved; // if($reserved>0) // { // debuglog("$reserved1 $reserved2 out of {$info['balance']}"); // debuglog("reserving $reserved $coin->symbol out of $coin->balance, available $amount"); // } if ($amount < $coin->reward / 4) { // debuglog("not enough $coin->symbol to sell $amount < $coin->reward /4"); return false; } $deposit_info = $remote->validateaddress($deposit_address); if (!$deposit_info || !isset($deposit_info['isvalid']) || !$deposit_info['isvalid']) { debuglog("sell invalid address {$deposit_address}"); return; } $amount = round($amount, 8); // debuglog("sending $amount $coin->symbol to $marketname, $deposit_address"); $market->lastsent = time(); $market->save(); // sleep(1); $tx = $remote->sendtoaddress($deposit_address, $amount); if (!$tx) { // debuglog($remote->error); if ($coin->symbol == 'MUE') { $amount = min($amount, 5000); } else { if ($coin->symbol == 'DIME') { $amount = min($amount, 10000000); } else { if ($coin->symbol == 'CNOTE') { $amount = min($amount, 10000); } else { if ($coin->symbol == 'SRC') { $amount = min($amount, 500); } else { $amount = round($amount * 0.99, 8); } } } } // debuglog("sending $amount $coin->symbol to $deposit_address"); sleep(1); $tx = $remote->sendtoaddress($deposit_address, $amount); if (!$tx) { debuglog("sending {$amount} {$coin->symbol} to {$deposit_address}"); debuglog($remote->error); return; } } $exchange = new db_exchange(); $exchange->market = $marketname; $exchange->coinid = $coin->id; $exchange->send_time = time(); $exchange->quantity = $amount; $exchange->price_estimate = $coin->price; $exchange->status = 'waiting'; $exchange->tx = $tx; $exchange->save(); return; }