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)); }
public function actionWithdraw() { $fees = 0.0001; $deposit = user()->getState('yaamp-deposit'); if (!$deposit) { $this->render('login'); return; } $renter = getrenterparam($deposit); if (!$renter) { $this->render('login'); return; } $amount = getparam('withdraw_amount'); $address = getparam('withdraw_address'); $amount = floatval(bitcoinvaluetoa(min($amount, $renter->balance - $fees))); if ($amount < 0.001) { user()->setFlash('error', 'Minimum withdraw is 0.001'); $this->redirect("/renting"); return; } $coin = getdbosql('db_coins', "symbol='BTC'"); if (!$coin) { return; } $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $res = $remote->validateaddress($address); if (!$res || !isset($res['isvalid']) || !$res['isvalid']) { user()->setFlash('error', 'Invalid address'); $this->redirect("/renting"); return; } $rentertx = new db_rentertxs(); $rentertx->renterid = $renter->id; $rentertx->time = time(); $rentertx->amount = $amount; $rentertx->type = 'withdraw'; $rentertx->address = $address; $rentertx->tx = 'scheduled'; $rentertx->save(); debuglog("withdraw scheduled {$renter->id} {$renter->address}, {$amount} to {$address}"); user()->setFlash('message', "withdraw scheduled"); $this->redirect("/renting"); }
function BackendUsersUpdate() { $t1 = microtime(true); $list = getdbolist('db_accounts', "coinid is null or coinsymbol != ''"); foreach ($list as $user) { // debuglog("testing user $user->username, $user->coinsymbol"); if (!empty($user->coinsymbol)) { $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $user->coinsymbol)); $user->coinsymbol = ''; if ($coin) { if ($user->coinid == $coin->id) { $user->save(); continue; } $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $b = $remote->validateaddress($user->username); if ($b && isset($b['isvalid']) && $b['isvalid']) { if ($user->balance > 0) { $coinref = getdbo('db_coins', $user->coinid); if (!$coinref) { $coinref = getdbosql('db_coins', "symbol='BTC'"); } $user->balance = $user->balance * $coinref->price / $coin->price; } $user->coinid = $coin->id; $user->save(); debuglog("{$user->username} set to {$coin->symbol}, balance {$user->balance}"); continue; } } } $user->coinid = 0; $coins = getdbolist('db_coins', "enable order by difficulty desc"); foreach ($coins as $coin) { $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $b = $remote->validateaddress($user->username); if (!$b || !isset($b['isvalid']) || !$b['isvalid']) { continue; } $user->balance = 0; $user->coinid = $coin->id; debuglog("{$user->username} set to {$coin->symbol}, balance {$user->balance}"); break; } $user->save(); } // $delay=time()-60*60; // $list = dborun("update coins set dontsell=1 where id in (select coinid from accounts where balance>0 or last_login>$delay group by coinid)"); // $list = dborun("update coins set dontsell=0 where id not in (select coinid from accounts where balance>0 or last_login>$delay group by coinid)"); // $list = getdbolist('db_workers', "dns is null"); // foreach($list as $worker) // { // $worker->dns = $worker->ip; // $res = system("resolveip $worker->ip"); // if($res) // { // $a = explode(' ', $res); // if($a && isset($a[5])) // $worker->dns = $a[5]; // } // $worker->save(); // } $d1 = microtime(true) - $t1; controller()->memcache->add_monitoring_function(__METHOD__, $d1); }
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; }