function BackendUpdateDeposit() { // debuglog(__FUNCTION__); $btc = getdbosql('db_coins', "symbol='BTC'"); if (!$btc) { return; } $remote = new Bitcoin($btc->rpcuser, $btc->rpcpasswd, $btc->rpchost, $btc->rpcport); $info = $remote->getinfo(); if (!$info) { return; } if (!isset($info['blocks'])) { return; } $hash = $remote->getblockhash(intval($info['blocks'])); if (!$hash) { return; } $block = $remote->getblock($hash); if (!$block) { return; } if (!isset($block['time'])) { return; } if ($block['time'] + 30 * 60 < time()) { return; } $list = $remote->listaccounts(1); foreach ($list as $r => $a) { if ($a == 0) { continue; } $b = preg_match('/renter-prod-([0-9]+)/', $r, $m); if (!$b) { continue; } $renter = getdbo('db_renters', $m[1]); if (!$renter) { continue; } $ts = $remote->listtransactions(yaamp_renter_account($renter), 1); if (!$ts || !isset($ts[0])) { continue; } $moved = $remote->move(yaamp_renter_account($renter), '', $a); if (!$moved) { continue; } debuglog("deposit {$renter->id} {$renter->address}, {$a}"); $rentertx = new db_rentertxs(); $rentertx->renterid = $renter->id; $rentertx->time = time(); $rentertx->amount = $a; $rentertx->type = 'deposit'; $rentertx->tx = isset($ts[0]['txid']) ? $ts[0]['txid'] : ''; $rentertx->save(); $renter->unconfirmed = 0; $renter->balance += $a; $renter->updated = time(); $renter->save(); } $list = $remote->listaccounts(0); foreach ($list as $r => $a) { if ($a == 0) { continue; } $b = preg_match('/renter-prod-([0-9]+)/', $r, $m); if (!$b) { continue; } $renter = getdbo('db_renters', $m[1]); if (!$renter) { continue; } debuglog("unconfirmed {$renter->id} {$renter->address}, {$a}"); $renter->unconfirmed = $a; $renter->updated = time(); $renter->save(); } ///////////////////////////////////////////////////////////////////////////////////////////////////// $received1 = $remote->getbalance('bittrex', 1); //nicehash payments if ($received1 > 0) { $moved = $remote->move('bittrex', '', $received1); debuglog("moved from bittrex {$received1}"); dborun("update renters set balance=balance+{$received1} where id=7"); dborun("update renters set custom_start=custom_start+{$received1} where id=7"); } ///////////////////////////////////////////////////////////////////////////////////////////////////// $fees = 0.0001; $list = getdbolist('db_rentertxs', "type='withdraw' and tx='scheduled'"); foreach ($list as $tx) { $renter = getdbo('db_renters', $tx->renterid); if (!$renter) { continue; } // debuglog("$renter->balance < $tx->amount + $fees"); $tx->amount = bitcoinvaluetoa(min($tx->amount, $renter->balance - $fees)); if ($tx->amount < $fees * 2) { $tx->tx = 'failed'; $tx->save(); continue; } debuglog("withdraw send {$renter->id} {$renter->address} sendtoaddress({$tx->address}, {$tx->amount})"); $tx->tx = $remote->sendtoaddress($tx->address, round($tx->amount, 8)); if (!$tx->tx) { $tx->tx = 'failed'; $tx->save(); continue; } $renter->balance -= $tx->amount + $fees; $renter->balance = max($renter->balance, 0); dborun("update renters set balance={$renter->balance} where id={$renter->id}"); $tx->save(); if ($renter->balance <= 0.0001) { dborun("update jobs set active=false, ready=false where id={$renter->id}"); } } }
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 BackendCoinPayments($coin) { // debuglog("BackendCoinPayments $coin->symbol"); $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $info = $remote->getinfo(); if (!$info) { debuglog("{$coin->symbol} cant connect to coin"); return; } $min = 0.001; // if(date("w", time()) == 0 && date("H", time()) > 12) // sunday afternoon // $min = 0.0001; $users = getdbolist('db_accounts', "balance>{$min} and coinid={$coin->id}"); if ($coin->symbol == 'MUE' || $coin->symbol == 'DIME') { foreach ($users as $user) { $user = getdbo('db_accounts', $user->id); if (!$user) { continue; } $amount = $user->balance; while ($user->balance > $min && $amount > $min) { debuglog("{$coin->symbol} sendtoaddress {$user->username} {$amount}"); $tx = $remote->sendtoaddress($user->username, round($amount, 8)); if (!$tx) { debuglog("error {$remote->error}, {$user->username}, {$amount}"); if ($remote->error == 'transaction too large' || $remote->error == 'invalid amount') { $amount /= 2; continue; } break; } $payout = new db_payouts(); $payout->account_id = $user->id; $payout->time = time(); $payout->amount = bitcoinvaluetoa($amount); $payout->fee = 0; $payout->tx = $tx; $payout->save(); $user->balance -= $amount; $user->save(); } } debuglog("payment done"); return; } $total_to_pay = 0; $addresses = array(); foreach ($users as $user) { $total_to_pay += round($user->balance, 8); $addresses[$user->username] = round($user->balance, 8); } if (!$total_to_pay) { // debuglog("nothing to pay"); return; } if ($info['balance'] - 0.001 < $total_to_pay) { debuglog("{$coin->symbol} wallet insufficient funds for payment {$info['balance']} < {$total_to_pay}"); return; } if ($coin->symbol == 'BTC') { global $cold_wallet_table; $balance = $info['balance']; $stats = getdbosql('db_stats', "1 order by time desc"); $renter = dboscalar("select sum(balance) from renters"); $pie = $balance - $total_to_pay - $renter - 1; debuglog("pie to split is {$pie}"); if ($pie > 0) { foreach ($cold_wallet_table as $coldwallet => $percent) { $coldamount = round($pie * $percent, 8); if ($coldamount < $min) { break; } debuglog("paying cold wallet {$coldwallet} {$coldamount}"); $addresses[$coldwallet] = $coldamount; $total_to_pay += $coldamount; } } } debuglog("paying {$total_to_pay} {$coin->symbol} min is {$min}"); $tx = $remote->sendmany('', $addresses, 1, ''); if (!$tx) { debuglog($remote->error); return; } foreach ($users as $user) { $user = getdbo('db_accounts', $user->id); if (!$user) { continue; } $payout = new db_payouts(); $payout->account_id = $user->id; $payout->time = time(); $payout->amount = bitcoinvaluetoa($user->balance); $payout->fee = 0; $payout->tx = $tx; $payout->save(); $user->balance = 0; $user->save(); } debuglog("payment done"); sleep(5); }
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; }