public function get_database_scalar($key, $query, $params = array()) { $value = $this->get($key); if ($value === false) { $value = dboscalar($query, $params); $this->set($key, $value); } return $value; }
function BackendQuickClean() { $delay = time() - 24 * 60 * 60; $coins = getdbolist('db_coins', "installed"); foreach ($coins as $coin) { $id = dboscalar("select id from blocks where coin_id={$coin->id} and time<{$delay} and\r\n\t\t\tid not in (select blockid from earnings where coinid={$coin->id})\r\n\t\t\torder by id desc limit 200, 1"); if ($id) { dborun("delete from blocks where coin_id={$coin->id} and time<{$delay} and\r\n\t\t\tid not in (select blockid from earnings where coinid={$coin->id}) and id<{$id}"); } } dborun("delete from earnings where blockid in (select id from blocks where category='orphan')"); dborun("delete from earnings where blockid not in (select id from blocks)"); }
function showUser($userid, $what) { $user = getdbo('db_accounts', $userid); if (!$user) { return; } $d = datetoa2($user->last_login); $balance = bitcoinvaluetoa($user->balance); $paid = dboscalar("select sum(amount) from payouts where account_id={$user->id}"); $paid = bitcoinvaluetoa($paid); $t = time() - 24 * 60 * 60; $miner_count = getdbocount('db_workers', "userid={$user->id}"); $share_count = getdbocount('db_shares', "userid={$user->id}"); $block_count = getdbocount('db_blocks', "userid={$user->id} and time>{$t}"); $coin = getdbo('db_coins', $user->coinid); echo "<tr class='ssrow'>"; if ($coin) { echo "<td><img src='{$coin->image}' width=16> {$coin->symbol}</td>"; } else { echo "<td></td>"; } echo "<td>{$user->id}</td>"; echo "<td><a href='/site?address={$user->username}'>{$user->username}</a></td>"; echo "<td>{$what}</td>"; echo "<td>{$d}</td>"; echo "<td>{$block_count}</td>"; echo "<td>{$balance}</td>"; if (intval($paid) > 0.01) { echo "<td><b>{$paid}</b></td>"; } else { echo "<td>{$paid}</td>"; } echo "<td>{$miner_count}</td>"; echo "<td>{$share_count}</td>"; if ($user->is_locked) { echo "<td>locked</td>"; echo "<td><a href='/site/unblockuser?wallet={$user->username}'>unblock</a></td>"; } else { echo "<td></td>"; echo "<td><a href='/site/blockuser?wallet={$user->username}'>block</a></td>"; } echo "</tr>"; }
function BackendBlockNew($coin, $db_block) { // debuglog("NEW BLOCK $coin->name $db_block->height"); $reward = $db_block->amount; $total_hash_power = dboscalar("select sum(difficulty) from shares where valid and algo='{$coin->algo}'"); if (!$total_hash_power) { return; } $list = dbolist("SELECT userid, sum(difficulty) as total from shares where valid and algo='{$coin->algo}' group by userid"); foreach ($list as $item) { $hash_power = $item['total']; if (!$hash_power) { continue; } $user = getdbo('db_accounts', $item['userid']); if (!$user) { continue; } $amount = $reward * $hash_power / $total_hash_power; if (!$user->no_fees) { $amount = take_yaamp_fee($amount, $coin->algo); } $earning = new db_earnings(); $earning->userid = $user->id; $earning->coinid = $coin->id; $earning->blockid = $db_block->id; $earning->create_time = $db_block->time; $earning->amount = $amount; $earning->price = $coin->price; if ($db_block->category == 'generate') { $earning->mature_time = time(); $earning->status = 1; } else { // immature $earning->status = 0; } $earning->save(); $user->last_login = time(); $user->save(); } $delay = time() - 5 * 60; dborun("delete from shares where algo='{$coin->algo}' and time<{$delay}"); }
} echo "<td align=right style='font-size: .8em;'>{$pool_ttf}</td>"; echo "<td align=right style='font-size: .8em;'>{$pool_hash}</td>"; echo "<td align=right style='font-size: .8em;'><b>{$btcmhd}</b></td>"; echo "</tr>"; } show_orders($allorders, $services); show_services($services); echo "</table><br>"; ///////////////////////////////////////////////////////////////////////////////////////////// $target = yaamp_hashrate_constant($algo); $interval = yaamp_hashrate_step(); $delay = time() - $interval; $version = 'NiceHash/1.0.0'; $hashrate = dboscalar("select sum(difficulty) * {$target} / {$interval} / 1000 from shares where valid and time>{$delay} and\n\tworkerid in (select id from workers where algo=:algo and version='{$version}')", array(':algo' => $algo)); $invalid = dboscalar("select sum(difficulty) * {$target} / {$interval} / 1000 from shares where not valid and time>{$delay} and\n\tworkerid in (select id from workers where algo=:algo and version='{$version}')", array(':algo' => $algo)); $count = getdbocount('db_workers', "algo=:algo and version='{$version}'", array(':algo' => $algo)); $percent = $total_nicehash && $hashrate ? round($hashrate * 100 / $total_nicehash / 1000000000, 2) . '%' : ''; $hashrate = $hashrate ? Itoa2($hashrate) . 'h/s' : ''; $version = substr($version, 0, 30); $total_nicehash = round($total_nicehash, 3); echo "Total Nicehash: <b>{$total_nicehash} Gh/s</b> *** yaamp: "; echo "<b>{$hashrate}</b> "; echo "{$count} workers "; echo "{$percent} "; echo "</div></div><br>"; ////////////////////////////////////////////////////////////////////////////////////////////////// function show_services(&$services, $btcmhd = 0) { if (!controller()->admin || !$services) { return;
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); }
echo "<thead>"; echo "<tr>"; echo "<th>Renter</th>"; echo "<th>Address</th>"; echo "<th>Email</th>"; echo "<th>Spent</th>"; echo "<th>Balance</th>"; echo "<th>Unconfirmed</th>"; echo "<th>Jobs</th>"; echo "<th>Active</th>"; echo "</tr>"; echo "</thead><tbody>"; $list = getdbolist('db_renters', "balance>0 order by balance desc"); foreach ($list as $renter) { $count = dboscalar("select count(*) from jobs where renterid={$renter->id}"); $active = dboscalar("select count(*) from jobs where renterid={$renter->id} and active"); if ($deposit == $renter->address) { echo "<tr class='ssrow' style='background-color: #dfd'>"; } else { echo "<tr class='ssrow'>"; } echo "<td>{$renter->id}</td>"; echo "<td><a href='/renting?address={$renter->address}'>{$renter->address}</a></td>"; echo "<td>{$renter->email}</td>"; echo "<td>{$renter->spent}</td>"; echo "<td>{$renter->balance}</td>"; echo "<td>{$renter->unconfirmed}</td>"; echo "<td>{$count}</td>"; echo "<td>{$active}</td>"; echo "</tr>"; }
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; }
$total_balance = 0; $total_paid = 0; $total_unsold = 0; foreach ($users as $user) { $target = yaamp_hashrate_constant(); $interval = yaamp_hashrate_step(); $delay = time() - $interval; $user_rate = dboscalar("select sum(difficulty) * {$target} / {$interval} / 1000 from shares where valid and time>{$delay} and userid={$user->id}"); $user_bad = dboscalar("select sum(difficulty) * {$target} / {$interval} / 1000 from shares where not valid and time>{$delay} and userid={$user->id}"); $percent = $user_rate ? round($user_bad * 100 / $user_rate, 3) : 0; $balance = bitcoinvaluetoa($user->balance); $paid = dboscalar("select sum(amount) from payouts where account_id={$user->id}"); $d = datetoa2($user->last_login); $miner_count = getdbocount('db_workers', "userid={$user->id}"); $block_count = getdbocount('db_blocks', "userid={$user->id}"); $block_diff = $paid ? round(dboscalar("select sum(difficulty) from blocks where userid={$user->id}") / $paid, 3) : '?'; $paid = bitcoinvaluetoa($paid); $user_rate = Itoa2($user_rate); $user_bad = Itoa2($user_bad); echo "<tr class='ssrow'>"; echo "<td>{$user->id}</td>"; echo "<td><a href='/?address={$user->username}'><b>{$user->username}</b></a></td>"; echo "<td>{$d}</td>"; echo "<td align=right>{$miner_count}</td>"; echo "<td align=right>{$user_rate}</td>"; echo "<td align=right>{$user_bad}</td>"; if ($percent > 50) { echo "<td align=right><b>{$percent}%</b></td>"; } else { echo "<td align=right>{$percent}%</td>"; }
function yaamp_convert_earnings_user($user, $status) { $refcoin = getdbo('db_coins', $user->coinid); if (!$refcoin) { $refcoin = getdbosql('db_coins', "symbol='BTC'"); } if (!$refcoin || $refcoin->price2 <= 0) { return 0; } $value = dboscalar("select sum(amount*price) from earnings where {$status} and userid={$user->id}"); $value = $value / $refcoin->price2; return $value; }
<?php $last = dboscalar("select max(last) from connections"); $list = getdbolist('db_connections', "1 order by id desc"); echo count($list) . " connections<br>"; //echo "<table class='dataGrid'>"; showTableSorter('maintable'); echo "<thead>"; echo "<tr>"; echo "<th>ID</th>"; echo "<th>User</th>"; echo "<th>Host</th>"; echo "<th>Db</th>"; echo "<th>Idle</th>"; echo "<th>Created</th>"; echo "<th>Last</th>"; echo "<th></th>"; echo "</tr>"; echo "</thead><tbody>"; foreach ($list as $conn) { echo "<tr class='ssrow'>"; $d1 = sectoa($conn->idle); $d2 = datetoa2($conn->created); $d3 = datetoa2($conn->last); $b = Booltoa($conn->last == $last); echo "<td>{$conn->id}</td>"; echo "<td>{$conn->user}</td>"; echo "<td>{$conn->host}</td>"; echo "<td>{$conn->db}</td>"; echo "<td>{$d1}</td>"; echo "<td>{$d2}</td>";
<?php $coin = getdbo('db_coins', $_GET['id']); $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $reserved1 = dboscalar("select sum(balance) from accounts where coinid={$coin->id}"); $reserved2 = dboscalar("select sum(amount*price) from earnings\n\twhere status!=2 and userid in (select id from accounts where coinid={$coin->id})"); $reserved = ($reserved1 + $reserved2) * 2; $owed = dboscalar("select sum(amount) from earnings where status!=2 and coinid={$coin->id}"); $owed_btc = $owed ? bitcoinvaluetoa($owed * $coin->price) : ''; $owed = $owed ? altcoinvaluetoa($owed) : ''; echo "cleared {$reserved1}, earnings {$reserved2}, reserved {$reserved}, balance {$coin->balance}, owed {$owed}, owned btc {$owed_btc}<br><br>"; ////////////////////////////////////////////////////////////////////////////////////// $list = getdbolist('db_markets', "coinid={$coin->id} order by price desc"); echo "<table class='dataGrid'>"; echo "<thead class=''>"; echo "<tr>"; echo "<th>Name</th>"; echo "<th>Price</th>"; echo "<th>Price2</th>"; echo "<th>Sent</th>"; echo "<th>Traded</th>"; echo "<th>Late</th>"; echo "<th>Deposit</th>"; echo "<th>Message</th>"; echo "</tr>"; echo "</thead><tbody>"; $bestmarket = getBestMarket($coin); foreach ($list as $market) { $price = bitcoinvaluetoa($market->price); $price2 = bitcoinvaluetoa($market->price2); $lowsymbol = strtolower($coin->symbol);
$loop2_time = sectoa(time() - memcache_get($this->memcache->memcache, "cronjob_loop2_time_start")); $main_time2 = sectoa(time() - memcache_get($this->memcache->memcache, "cronjob_main_time_start")); $main_time = sectoa(memcache_get($this->memcache->memcache, "cronjob_main_time")); $main_text = cronstate2text($state_main); echo "*** main ({$main_time}) {$state_main} {$main_text} ({$main_time2}), loop2 ({$loop2_time}), block ({$block_time})<br>"; $topay = dboscalar("select sum(balance) from accounts where coinid={$btc->id}"); //here: take other currencies too $topay2 = bitcoinvaluetoa(dboscalar("select sum(balance) from accounts where coinid={$btc->id} and balance>0.001")); $renter = dboscalar("select sum(balance) from renters"); $stats = getdbosql('db_stats', "1 order by time desc"); $margin2 = bitcoinvaluetoa($btc->balance - $topay - $renter + $stats->balances + $stats->onsell + $stats->wallets); $margin = bitcoinvaluetoa($btc->balance - $topay - $renter); $topay = bitcoinvaluetoa($topay); $renter = bitcoinvaluetoa($renter); $immature = dboscalar("select sum(amount*price) from earnings where status=0"); $mints = dboscalar("select sum(mint*price) from coins where enable"); $off = $mints - $immature; $immature = bitcoinvaluetoa($immature); $mints = bitcoinvaluetoa($mints); $off = bitcoinvaluetoa($off); $btcaddr = YAAMP_BTCADDRESS; //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9'; echo "<a href='https://www.okcoin.com/market.do' target=_blank>Bitstamp {$mining->usdbtc}</a>, "; echo "<a href='https://blockchain.info/address/{$btcaddr}' target=_blank>wallet {$btc->balance}</a>, next payout {$topay2}<br>"; echo "pay {$topay}, renter {$renter}, marg {$margin}, {$margin2}<br>"; echo "mint {$mints} immature {$immature} off {$off}<br>"; echo '<br>'; ////////////////////////////////////////////////////////////////////////////////////////////////// echo "<div style='height: 160px;' id='graph_results_negative'></div>"; //echo "<div style='height: 160px;' id='graph_results_profit'></div>"; echo "<div style='height: 200px;' id='graph_results_assets'></div>";
function BackendUpdateServices() { // debuglog(__FUNCTION__); $table = array(0 => 'scrypt', 1 => 'sha256', 2 => 'scryptn', 3 => 'x11', 4 => 'x13', 5 => 'keccak', 6 => 'x15', 7 => 'nist5', 8 => 'neoscrypt', 9 => 'lyra2', 10 => 'whirlx', 11 => 'qubit', 12 => 'quark', 111 => 'c11'); $res = fetch_url('https://www.nicehash.com/api?method=stats.global.current'); if (!$res) { return; } $a = json_decode($res); if (!$a || !isset($a->result)) { return; } foreach ($a->result->stats as $stat) { if ($stat->price <= 0) { continue; } if (!isset($table[$stat->algo])) { continue; } $algo = $table[$stat->algo]; $service = getdbosql('db_services', "name='Nicehash' and algo=:algo", array(':algo' => $algo)); if (!$service) { $service = new db_services(); $service->name = 'Nicehash'; $service->algo = $algo; } $service->price = $stat->price / 1000; $service->speed = $stat->speed * 1000000000; $service->save(); $list = getdbolist('db_jobs', "percent>0 and algo=:algo and (host='stratum.westhash.com' or host='stratum.nicehash.com')", array(':algo' => $algo)); foreach ($list as $job) { $job->price = round($service->price * 1000 * (100 - $job->percent) / 100, 2); $job->save(); } } $list = getdbolist('db_renters', "custom_address is not null and custom_server is not null"); foreach ($list as $renter) { $res = fetch_url("https://{$renter->custom_server}/api?method=stats.provider&addr={$renter->custom_address}"); if (!$res) { continue; } $renter->custom_balance = 0; $renter->custom_accept = 0; $renter->custom_reject = 0; $a = json_decode($res); foreach ($a->result->stats as $stat) { if (!isset($table[$stat->algo])) { continue; } $algo = $table[$stat->algo]; $renter->custom_balance += $stat->balance; $renter->custom_accept += $stat->accepted_speed * 1000000000; $renter->custom_reject += $stat->rejected_speed * 1000000000; } $renter->save(); } /////////////////////////////////////////////////////////////////////////// // renting from nicehash if (!YAAMP_PRODUCTION) { return; } return; $apikey = 'c9534a11-0e4e-4d00-be64-a00e34cd927a'; $apiid = '7215'; $deposit = '1C23KmLeCaQSLLyKVykHEUse1R7jRDv9j9'; $amount = '0.01'; $res = fetch_url("https://www.nicehash.com/api?method=balance&id={$apiid}&key={$apikey}"); $a = json_decode($res); $balance = $a->result->balance_confirmed; foreach ($table as $i => $algo) { $nicehash = getdbosql('db_nicehash', "algo=:algo", array(':algo' => $algo)); if (!$nicehash) { $nicehash = new db_nicehash(); $nicehash->active = false; $nicehash->algo = $algo; } if (!$nicehash->active) { if ($nicehash->orderid) { $res = fetch_url("https://www.nicehash.com/api?method=orders.remove&id={$apiid}&key={$apikey}&algo={$i}&order={$nicehash->orderid}"); debuglog($res); $nicehash->orderid = null; } $nicehash->btc = null; $nicehash->price = null; $nicehash->speed = null; $nicehash->last_decrease = null; $nicehash->save(); continue; } $price = dboscalar("select price from hashrate where algo=:algo order by time desc limit 1", array(':algo' => $algo)); $minprice = $price * 0.5; $setprice = $price * 0.7; $maxprice = $price * 0.9; $cancelprice = $price * 1.1; $res = fetch_url("https://www.nicehash.com/api?method=orders.get&my&id={$apiid}&key={$apikey}&algo={$i}"); if (!$res) { break; } $a = json_decode($res); if (count($a->result->orders) == 0) { if ($balance < $amount) { continue; } $port = getAlgoPort($algo); $res = fetch_url("https://www.nicehash.com/api?method=orders.create&id={$apiid}&key={$apikey}&algo={$i}&amount={$amount}&price={$setprice}&limit=0&pool_host=yaamp.com&pool_port={$port}&pool_user={$deposit}&pool_pass=xx"); debuglog($res); $nicehash->last_decrease = time(); $nicehash->save(); continue; } $order = $a->result->orders[0]; debuglog("{$algo} {$order->price} {$minprice} {$setprice} {$maxprice} {$cancelprice}"); $nicehash->orderid = $order->id; $nicehash->btc = $order->btc_avail; $nicehash->workers = $order->workers; $nicehash->price = $order->price; $nicehash->speed = $order->limit_speed; $nicehash->accepted = $order->accepted_speed; $nicehash->rejected = $order->rejected_speed; if ($order->price > $cancelprice && $order->workers > 0) { debuglog("* cancel order {$algo}"); $res = fetch_url("https://www.nicehash.com/api?method=orders.remove&id={$apiid}&key={$apikey}&algo={$i}&order={$order->id}"); debuglog($res); } else { if ($order->price > $maxprice && $order->limit_speed == 0) { debuglog("* decrease speed {$algo}"); $res = fetch_url("https://www.nicehash.com/api?method=orders.set.limit&id={$apiid}&key={$apikey}&algo={$i}&order={$order->id}&limit=0.05"); debuglog($res); } else { if ($order->price > $maxprice && $nicehash->last_decrease + 10 * 60 < time()) { debuglog("* decrease price {$algo}"); $res = fetch_url("https://www.nicehash.com/api?method=orders.set.price.decrease&id={$apiid}&key={$apikey}&algo={$i}&order={$order->id}"); debuglog($res); $nicehash->last_decrease = time(); } else { if ($order->price < $minprice && $order->workers <= 0) { debuglog("* increase price {$algo}"); $res = fetch_url("https://www.nicehash.com/api?method=orders.set.price&id={$apiid}&key={$apikey}&algo={$i}&order={$order->id}&price={$setprice}"); debuglog($res); } else { if ($order->price < $maxprice && $order->limit_speed == 0.05) { debuglog("* increase speed {$algo}"); $res = fetch_url("https://www.nicehash.com/api?method=orders.set.limit&id={$apiid}&key={$apikey}&algo={$i}&order={$order->id}&limit=0"); debuglog($res); } } } } } $nicehash->save(); } }
function BackendCoinsUpdate() { // debuglog(__FUNCTION__); $t1 = microtime(true); $pool_rate = array(); foreach (yaamp_get_algos() as $algo) { $pool_rate[$algo] = yaamp_pool_rate($algo); } $coins = getdbolist('db_coins', "installed"); foreach ($coins as $coin) { // debuglog("doing $coin->name"); $coin = getdbo('db_coins', $coin->id); if (!$coin) { continue; } $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport); $info = $remote->getinfo(); if (!$info) { $coin->enable = false; // $coin->auto_ready = false; $coin->connections = 0; $coin->save(); continue; } // debuglog($info); $coin->enable = true; if (isset($info['difficulty'])) { $difficulty = $info['difficulty']; } else { $difficulty = $remote->getdifficulty(); } if (is_array($difficulty)) { $coin->difficulty = $difficulty['proof-of-work']; if (isset($difficulty['proof-of-stake'])) { $coin->difficulty_pos = $difficulty['proof-of-stake']; } } else { $coin->difficulty = $difficulty; } if ($coin->algo == 'quark') { $coin->difficulty /= 0x100; } if ($coin->difficulty == 0) { $coin->difficulty = 1; } $coin->errors = isset($info['errors']) ? $info['errors'] : ''; $coin->txfee = isset($info['paytxfee']) ? $info['paytxfee'] : ''; $coin->connections = isset($info['connections']) ? $info['connections'] : ''; $coin->balance = isset($info['balance']) ? $info['balance'] : 0; $coin->mint = dboscalar("select sum(amount) from blocks where coin_id={$coin->id} and category='immature'"); if (empty($coin->master_wallet)) { $coin->master_wallet = $remote->getaccountaddress(''); // debuglog($coin->master_wallet); } if (empty($coin->rpcencoding)) { $difficulty = $remote->getdifficulty(); if (is_array($difficulty)) { $coin->rpcencoding = 'POS'; } else { $coin->rpcencoding = 'POW'; } } if ($coin->hassubmitblock == NULL) { $remote->submitblock(''); if (strcasecmp($remote->error, 'method not found') == 0) { $coin->hassubmitblock = false; } else { $coin->hassubmitblock = true; } } if ($coin->auxpow == NULL) { $ret = $remote->getauxblock(); if (strcasecmp($remote->error, 'method not found') == 0) { $coin->auxpow = false; } else { $coin->auxpow = true; } } // if($coin->symbol != 'BTC') // { // if($coin->symbol == 'PPC') // $template = $remote->getblocktemplate(''); // else $template = $remote->getblocktemplate('{}'); if ($template && isset($template['coinbasevalue'])) { $coin->reward = $template['coinbasevalue'] / 100000000 * $coin->reward_mul; if ($coin->symbol == 'TAC' && isset($template['_V2'])) { $coin->charity_amount = $template['_V2'] / 100000000; } if (isset($template['payee_amount']) && $coin->symbol != 'LIMX') { $coin->charity_amount = $template['payee_amount'] / 100000000; $coin->reward -= $coin->charity_amount; // debuglog("$coin->symbol $coin->charity_amount $coin->reward"); } else { if (!empty($coin->charity_address)) { if ($coin->charity_amount) { } else { $coin->reward -= $coin->reward * $coin->charity_percent / 100; } } } if (isset($template['bits'])) { $target = decode_compact($template['bits']); $coin->difficulty = target_to_diff($target); } } else { if (strcasecmp($remote->error, 'method not found') == 0) { $template = $remote->getmemorypool(); if ($template && isset($template['coinbasevalue'])) { $coin->usememorypool = true; $coin->reward = $template['coinbasevalue'] / 100000000 * $coin->reward_mul; if (isset($template['bits'])) { $target = decode_compact($template['bits']); $coin->difficulty = target_to_diff($target); } } else { $coin->auto_ready = false; $coin->errors = $remote->error; } } else { $coin->auto_ready = false; $coin->errors = $remote->error; } } if (strcasecmp($coin->errors, 'No more PoW blocks') == 0) { $coin->dontsell = true; $coin->auto_ready = false; } // } if ($coin->block_height != $info['blocks']) { $count = $info['blocks'] - $coin->block_height; $ttf = (time() - $coin->last_network_found) / $count; if (empty($coin->actual_ttf)) { $coin->actual_ttf = $ttf; } $coin->actual_ttf = percent_feedback($coin->actual_ttf, $ttf, 5); $coin->last_network_found = time(); } $coin->version = $info['version']; $coin->block_height = $info['blocks']; $coin->save(); // debuglog(" end $coin->name"); } $coins = getdbolist('db_coins', "enable order by auxpow desc"); foreach ($coins as $coin) { $coin = getdbo('db_coins', $coin->id); if (!$coin) { continue; } if ($coin->difficulty) { $coin->index_avg = $coin->reward * $coin->price * 10000 / $coin->difficulty; if (!$coin->auxpow && $coin->rpcencoding == 'POW') { $indexaux = dboscalar("select sum(index_avg) from coins where enable and visible and auto_ready and auxpow and algo='{$coin->algo}'"); $coin->index_avg += $indexaux; } } if ($coin->network_hash) { $coin->network_ttf = $coin->difficulty * 0x100000000 / $coin->network_hash; } if (isset($pool_rate[$coin->algo])) { $coin->pool_ttf = $coin->difficulty * 0x100000000 / $pool_rate[$coin->algo]; } if (strstr($coin->image, 'http')) { $data = file_get_contents($coin->image); $coin->image = "/images/coin-{$coin->id}.png"; @unlink(YAAMP_HTDOCS . $coin->image); file_put_contents(YAAMP_HTDOCS . $coin->image, $data); } $coin->save(); } $d1 = microtime(true) - $t1; controller()->memcache->add_monitoring_function(__METHOD__, $d1); }
$btcmhd = yaamp_profitability($coin); $btcmhd = mbitcoinvaluetoa($btcmhd); $h = $coin->block_height - 100; $ss1 = dboscalar("select count(*) from blocks where coin_id={$coin->id} and height>={$h} and category!='orphan'"); $ss2 = dboscalar("select count(*) from blocks where coin_id={$coin->id} and height>={$h} and category='orphan'"); $percent_pool1 = $ss1 ? $ss1 . '%' : ''; $percent_pool2 = $ss2 ? $ss2 . '%' : ''; // echo "<td align=right style='font-size: .9em'>$network_ttf<br>$actual_ttf</td>"; // echo "<td align=right style='font-size: .9em'>$pool_ttf<br></td>"; if ($ss1 > 50) { echo "<td align=right style='font-size: .9em'><b>{$btcmhd}</b><br><span style='color: blue;'>{$percent_pool1}</span>"; } else { echo "<td align=right style='font-size: .9em'><b>{$btcmhd}</b><br>{$percent_pool1}"; } echo "<span style='color: red;'> {$percent_pool2}</span></td>"; $owed = dboscalar("select sum(balance) from accounts where coinid={$coin->id}"); $owed_btc = bitcoinvaluetoa($owed * $coin->price); $owed = bitcoinvaluetoa($owed); if ($coin->balance + $coin->mint < $owed) { echo "<td align=right style='font-size: .9em'><span style='color: red;'>{$owed}<br>{$owed_btc}</span></td>"; } else { echo "<td align=right style='font-size: .9em'>{$owed}<br>{$owed_btc}</td>"; } $btc = bitcoinvaluetoa($coin->balance * $coin->price); echo "<td align=right style='font-size: .9em'>{$coin->balance}<br>{$btc}</td>"; $btc = bitcoinvaluetoa($coin->mint * $coin->price); echo "<td align=right style='font-size: .9em'>{$coin->mint}<br>{$btc}</td>"; $price = bitcoinvaluetoa($coin->price); $price2 = bitcoinvaluetoa($coin->price2); // $marketcount = getdbocount('db_markets', "coinid=$coin->id"); $marketname = '';
$bad = $hashrate + $invalid ? round($invalid * 100 / ($hashrate + $invalid), 1) . '%' : ''; $hashrate = $hashrate ? Itoa2($hashrate) . 'h/s' : ''; $version = substr($version, 0, 30); echo "<tr class='ssrow'>"; echo "<td><b>{$version}</b></td>"; echo "<td align=right>{$count}</td>"; echo "<td align=right>{$extranonce}</td>"; echo "<td align=right>{$percent}</td>"; echo "<td align=right>{$hashrate}</td>"; echo "<td align=right title='{$title}'>{$bad}</td>"; echo "</tr>"; } echo "</tbody>"; $title = ''; foreach ($error_tab as $i => $s) { $invalid2 = dboscalar("select sum(difficulty) * {$target} / {$interval} / 1000 from shares where error={$i} and time>{$delay} and\n\t\tworkerid in (select id from workers where algo=:algo)", array(':algo' => $algo)); if ($invalid2) { $bad2 = round($invalid2 * 100 / ($total_hashrate + $invalid2), 2) . '%'; $title .= "{$bad2} - {$s}\n"; } } $bad = $total_hashrate + $total_invalid ? round($total_invalid * 100 / ($total_hashrate + $total_invalid), 1) . '%' : ''; $total_hashrate = Itoa2($total_hashrate) . 'h/s'; echo "<tr class='ssrow'>"; echo "<td><b>Total</b></td>"; echo "<td align=right>{$total_workers}</td>"; echo "<td align=right>{$total_extranonce}</td>"; echo "<td align=right></td>"; echo "<td align=right>{$total_hashrate}</td>"; echo "<td align=right title='{$title}'>{$bad}</td>"; echo "</tr>";
echo "<th>Nicehash</th>"; echo "<th>Yaamp</th>"; echo "<th>Price</th>"; echo "<th>Speed</th>"; echo "<th>Last Dec</th>"; echo "<th>Workers</th>"; echo "<th>Accepted</th>"; echo "<th>Rejected</th>"; echo "<th></th>"; echo "</tr>"; echo "</thead><tbody>"; $list = getdbolist('db_nicehash'); foreach ($list as $nicehash) { $price2 = mbitcoinvaluetoa(dboscalar("select price from services where algo='{$nicehash->algo}'") * 1000); $d = datetoa2($nicehash->last_decrease); $yaamp = mbitcoinvaluetoa(dboscalar("select price from hashrate where algo='{$nicehash->algo}' order by time desc limit 1")); echo "<tr class='ssrow'>"; echo "<td>{$nicehash->orderid}</td>"; echo "<td>{$nicehash->algo}</td>"; echo "<td>{$nicehash->btc}</td>"; echo "<td>{$price2}</td>"; if ($yaamp > $price2 * 1.1) { echo "<td style='color: #4a4'>{$yaamp}</td>"; } else { echo "<td>{$yaamp}</td>"; } if ($nicehash->price > $yaamp) { echo "<td style='color: #a44'>{$nicehash->price}</td>"; } else { echo "<td>{$nicehash->price}</td>"; }
function BackendRentingPayout() { // debuglog(__FUNCTION__); $total_cleared = 0; foreach (yaamp_get_algos() as $algo) { $delay = time() - 5 * 60; dborun("delete from jobsubmits where status=2 and algo=:algo and time<{$delay}", array(':algo' => $algo)); $amount = dboscalar("select sum(amount) from jobsubmits where status=1 and algo=:algo", array(':algo' => $algo)); if ($amount < 2.0E-5) { continue; } dborun("update jobsubmits set status=2 where status=1 and algo=:algo", array(':algo' => $algo)); $total_cleared += $amount; $block = new db_blocks(); $block->coin_id = 0; $block->time = time(); $block->amount = $amount; $block->price = 1; $block->algo = $algo; $block->category = 'generate'; $block->save(); $total_hash_power = dboscalar("SELECT sum(difficulty) FROM shares where valid and algo=:algo", array(':algo' => $algo)); if (!$total_hash_power) { continue; } $list = dbolist("SELECT userid, sum(difficulty) as total FROM shares where valid and algo=:algo GROUP BY userid", array(':algo' => $algo)); foreach ($list as $item) { $hash_power = $item['total']; if (!$hash_power) { continue; } $user = getdbo('db_accounts', $item['userid']); if (!$user) { continue; } $earning = new db_earnings(); $earning->userid = $user->id; $earning->coinid = 0; $earning->blockid = $block->id; $earning->create_time = time(); $earning->price = 1; $earning->status = 2; // cleared $earning->amount = $amount * $hash_power / $total_hash_power; if (!$user->no_fees) { $earning->amount = take_yaamp_fee($earning->amount, $algo); } $earning->save(); $refcoin = getdbo('db_coins', $user->coinid); $value = $earning->amount / ($refcoin && $refcoin->price2 ? $refcoin->price2 : 1); // $value = yaamp_convert_amount_user($coin, $earning->amount, $user); $user->last_login = time(); $user->balance += $value; $user->save(); } $delay = time() - 5 * 60; dborun("delete from shares where algo=:algo and time<{$delay}", array(':algo' => $algo)); } if ($total_cleared > 0) { debuglog("total cleared from rental {$total_cleared} BTC"); } }
function BackendStatsUpdate2() { // debuglog('----------------------------------'); // debuglog(__FUNCTION__); //////////////////////////////////////////////////////////////////////////////////////////////////// $step = 15; $t = floor(time() / $step / 60) * $step * 60; $list = dbolist("select userid, algo from shares where time>{$t} group by userid, algo"); foreach ($list as $item) { $stats = getdbosql('db_hashuser', "time={$t} and algo=:algo and userid=:userid", array(':algo' => $item['algo'], ':userid' => $item['userid'])); if (!$stats) { $stats = new db_hashuser(); $stats->userid = $item['userid']; $stats->time = $t; $stats->hashrate = dboscalar("select hashrate from hashuser where algo=:algo and userid=:userid order by time desc limit 1", array(':algo' => $item['algo'], ':userid' => $item['userid'])); $stats->hashrate_bad = 0; $stats->algo = $item['algo']; } $percent = 20; $user_rate = yaamp_user_rate($item['userid'], $item['algo']); $stats->hashrate = round(($stats->hashrate * (100 - $percent) + $user_rate * $percent) / 100); if ($stats->hashrate < 1000) { $stats->hashrate = 0; } $user_rate_bad = yaamp_user_rate_bad($item['userid'], $item['algo']); $stats->hashrate_bad = round(($stats->hashrate_bad * (100 - $percent) + $user_rate_bad * $percent) / 100); if ($stats->hashrate_bad < 1000) { $stats->hashrate_bad = 0; } $stats->save(); } //////////////////////////////////////////////////////////////////////////////////////////////////// $step = 15; $t = floor(time() / $step / 60) * $step * 60; $list = dbolist("select distinct jobid from jobsubmits where time>{$t}"); foreach ($list as $item) { $jobid = $item['jobid']; $stats = getdbosql('db_hashrenter', "time={$t} and jobid={$jobid}"); if (!$stats) { $stats = new db_hashrenter(); // $stats->renterid = ; $stats->jobid = $item['jobid']; $stats->time = $t; $stats->hashrate = dboscalar("select hashrate from hashrenter where jobid=:jobid order by time desc limit 1", array(':jobid' => $jobid)); $stats->hashrate_bad = 0; //dboscalar("select hashrate_bad from hashrenter where jobid=$jobid order by time desc limit 1"); } $percent = 20; $job_rate = yaamp_job_rate($jobid); $stats->hashrate = round(($stats->hashrate * (100 - $percent) + $job_rate * $percent) / 100); if ($stats->hashrate < 1000) { $stats->hashrate = 0; } $job_rate_bad = yaamp_job_rate_bad($jobid); $stats->hashrate_bad = round(($stats->hashrate_bad * (100 - $percent) + $job_rate_bad * $percent) / 100); if ($stats->hashrate_bad < 1000) { $stats->hashrate_bad = 0; } $stats->save(); } //////////////////////////////////////////////////////////////////////////////////////////////////// $t = floor(time() / $step / 60) * $step * 60; $d = time() - 24 * 60 * 60; $list = getdbolist('db_accounts', "balance>0 or last_login>{$d}"); foreach ($list as $user) { $stats = getdbosql('db_balanceuser', "time={$t} and userid={$user->id}"); if (!$stats) { $stats = new db_balanceuser(); $stats->userid = $user->id; $stats->time = $t; } // $refcoin = getdbo('db_coins', $user->coinid); // if(!$refcoin) $refcoin = getdbosql('db_coins', "symbol='BTC'"); // if(!$refcoin->price || !$refcoin->price2) continue; // $pending1 = dboscalar("select sum(amount*price) from earnings where coinid=$refcoin->id and status!=2 and userid=$user->id"); // $pending2 = dboscalar("select sum(amount*price) from earnings where coinid!=$refcoin->id and status!=2 and userid=$user->id"); $stats->pending = yaamp_convert_earnings_user($user, "status!=2"); $stats->pending = bitcoinvaluetoa($stats->pending); $stats->balance = $user->balance; $stats->save(); $id = dboscalar("select id from earnings where userid={$user->id} order by id desc limit 100, 1"); if ($id) { dborun("delete from earnings where status=2 and userid={$user->id} and id<{$id}"); } } }
public function actionOrderSave() { $renter = getdbo('db_renters', XssFilter(getparam('order_renterid'))); if (!$renter || $renter->address != user()->getState('yaamp-deposit')) { return; } $job = getdbo('db_jobs', XssFilter(getparam('order_id'))); if (!$job) { $job = new db_jobs(); $job->renterid = getparam('order_renterid'); } $job->algo = getparam('order_algo'); $job->username = getparam('order_username'); $job->password = getparam('order_password'); $job->percent = getparam('order_percent'); $job->price = getparam('order_price'); $job->speed = getparam('order_speed') * 1000000; if (empty($job->algo) || empty($job->username) || empty($job->password) || empty($job->price) || empty($job->speed) || empty(getparam('order_address')) || empty(getparam('order_host'))) { $this->redirect('/renting'); return; } if ($job->speed < 100000) { $this->redirect('/renting'); return; } $a = explode(':', getparam('order_host')); if (!isset($a[0]) || !isset($a[1])) { $this->redirect('/renting'); return; } $job->host = $a[0]; $job->port = $a[1]; $rent = dboscalar("select rent from hashrate where algo=:algo order by time desc limit 1", array(':algo' => $job->algo)); if ($job->price > $rent && $job->ready) { $job->active = true; } else { if ($job->price < $rent) { $job->active = false; } } $job->time = time(); // $job->difficulty = null; $job->save(); $this->redirect("/renting?address=" . getparam('order_address')); }
$list = getdbolist('db_jobs', "renterid={$renter->id} order by algo, price desc"); foreach ($list as $job) { $hashrate_bad = yaamp_job_rate_bad($job->id); $hashrate = yaamp_job_rate($job->id) + $hashrate_bad; $title_percent = ''; if ($hashrate_bad) { $percent = round($hashrate_bad / $hashrate * 100, 1) . '%'; $hashrate_bad = Itoa2($hashrate_bad) . 'h/s'; $title_percent = "Rejected {$hashrate_bad} ({$percent})"; } $hashrate = $hashrate ? Itoa2($hashrate) . 'h/s' : ''; $maxhash = $job->speed ? Itoa2($job->speed) . 'h/s' : ''; $title = "-o stratum+tcp://{$job->host}:{$job->port} -u {$job->username} -p {$job->password}"; $servername = substr($job->host, 0, 22); $price = mbitcoinvaluetoa($job->price); $rent = dboscalar("select rent from hashrate where algo=:algo order by time desc limit 1", array(':algo' => $job->algo)); $rent = mbitcoinvaluetoa($rent); $diff = $job->difficulty > 0 ? round($job->difficulty, 3) : ''; if ($job->active) { echo "<tr class='ssrow' style='background-color: #dfd'>"; } else { echo "<tr class='ssrow'>"; } echo "<td title='Show details'><a href='javascript:show_job_graph({$job->id})'>\r\n\t\t<img id='graph_toggle_job-{$job->id}' width=14 src='/images/plus2-78.png'></a></td>"; $p = $job->percent ? "({$job->percent}%)" : ''; echo "<td title='{$title}'>{$servername}</td>"; echo "<td>{$job->algo}</td>"; echo "<td align=right title='Current Price {$rent}'>{$price} {$p}</td>"; // echo "<td>$rent</td>"; echo "<td align=right>{$maxhash}</td>"; echo "<td align=right title='{$title_percent}'>{$hashrate}</td>";