Пример #1
1
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}");
        }
    }
}
Пример #2
1
function doPoloniexTrading()
{
    //	debuglog('-------------- doPoloniexTrading()');
    $flushall = rand(0, 4) == 0;
    $poloniex = new poloniex();
    $tickers = $poloniex->get_ticker();
    if (!$tickers) {
        return;
    }
    // upgrade orders
    $coins = getdbolist('db_coins', "enable and id in (select distinct coinid from markets where name='poloniex')");
    foreach ($coins as $coin) {
        if ($coin->dontsell) {
            continue;
        }
        $pair = "BTC_{$coin->symbol}";
        if (!isset($tickers[$pair])) {
            continue;
        }
        $orders = $poloniex->get_open_orders($pair);
        if (!$orders || !isset($orders[0])) {
            dborun("delete from orders where coinid={$coin->id} and market='poloniex'");
            continue;
        }
        foreach ($orders as $order) {
            if (!isset($order['orderNumber'])) {
                debuglog($order);
                continue;
            }
            if ($order['rate'] > $tickers[$pair]['lowestAsk'] + 5.0E-8 || $flushall) {
                //				debuglog("poloniex cancel order for $pair {$order['orderNumber']}");
                $poloniex->cancel_order($pair, $order['orderNumber']);
                $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderNumber']));
                if ($db_order) {
                    $db_order->delete();
                }
                sleep(1);
            } else {
                $db_order = getdbosql('db_orders', "uuid=:uuid", array(':uuid' => $order['orderNumber']));
                if ($db_order) {
                    continue;
                }
                debuglog("poloniex adding order {$coin->symbol}");
                $db_order = new db_orders();
                $db_order->market = 'poloniex';
                $db_order->coinid = $coin->id;
                $db_order->amount = $order['amount'];
                $db_order->price = $order['rate'];
                $db_order->ask = $tickers[$pair]['lowestAsk'];
                $db_order->bid = $tickers[$pair]['highestBid'];
                $db_order->uuid = $order['orderNumber'];
                $db_order->created = time();
                $db_order->save();
            }
        }
        $list = getdbolist('db_orders', "coinid={$coin->id} and market='poloniex'");
        foreach ($list as $db_order) {
            $found = false;
            foreach ($orders as $order) {
                if (!isset($order['orderNumber'])) {
                    debuglog($order);
                    continue;
                }
                if ($order['orderNumber'] == $db_order->uuid) {
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                debuglog("poloniex deleting order {$coin->name} {$db_order->amount}");
                $db_order->delete();
            }
        }
    }
    // add orders
    $savebalance = getdbosql('db_balances', "name='poloniex'");
    $balances = $poloniex->get_balances();
    foreach ($balances as $symbol => $balance) {
        if (!$balance) {
            continue;
        }
        if ($symbol == 'BTC') {
            $savebalance->balance = $balance;
            $savebalance->save();
            continue;
        }
        $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol));
        if (!$coin || $coin->dontsell) {
            continue;
        }
        $market = getdbosql('db_markets', "coinid={$coin->id} and name='poloniex'");
        if ($market) {
            $market->lasttraded = time();
            $market->save();
        }
        $pair = "BTC_{$symbol}";
        if (!isset($tickers[$pair])) {
            continue;
        }
        $sellprice = $tickers[$pair]['highestBid'];
        if ($balance * $sellprice < 0.0001) {
            continue;
        }
        //		debuglog("poloniex selling $pair, $sellprice, $balance");
        $res = $poloniex->sell($pair, $sellprice, $balance);
        if (!isset($res['orderNumber'])) {
            debuglog($res, 5);
            continue;
        }
        if (!isset($tickers[$pair])) {
            continue;
        }
        $coin = getdbosql('db_coins', "symbol=:symbol", array(':symbol' => $symbol));
        if (!$coin) {
            continue;
        }
        $db_order = new db_orders();
        $db_order->market = 'poloniex';
        $db_order->coinid = $coin->id;
        $db_order->amount = $balance;
        $db_order->price = $sellprice;
        $db_order->ask = $tickers[$pair]['lowestAsk'];
        $db_order->bid = $tickers[$pair]['highestBid'];
        $db_order->uuid = $res['orderNumber'];
        $db_order->created = time();
        $db_order->save();
    }
    if ($savebalance->balance >= 0.2) {
        $btcaddr = YAAMP_BTCADDRESS;
        //'14LS7Uda6EZGXLtRrFEZ2kWmarrxobkyu9';
        $amount = $savebalance->balance;
        // - 0.0002;
        debuglog("poloniex withdraw {$amount} to {$btcaddr}");
        sleep(1);
        $res = $poloniex->withdraw('BTC', $amount, $btcaddr);
        debuglog($res);
        if ($res && $res->success) {
            $withdraw = new db_withdraws();
            $withdraw->market = 'poloniex';
            $withdraw->address = $btcaddr;
            $withdraw->amount = $amount;
            $withdraw->time = time();
            //	$withdraw->uuid = $res->result->uuid;
            $withdraw->save();
        }
    }
    //	debuglog('-------------- doPoloniexTrading() done');
}
Пример #3
0
function BackendPayments()
{
    $list = getdbolist('db_coins', "enable and id in (select distinct coinid from accounts)");
    foreach ($list as $coin) {
        BackendCoinPayments($coin);
    }
    dborun("update accounts set balance=0 where coinid=0");
}
Пример #4
0
function BackendPricesUpdate()
{
    //	debuglog(__FUNCTION__);
    updateBittrexMarkets();
    updateCryptsyMarkets();
    updateCCexMarkets();
    updateBleutradeMarkets();
    updatePoloniexMarkets();
    updateYobitMarkets();
    updateJubiMarkets();
    $list2 = getdbolist('db_coins', "installed and symbol2 is not null");
    foreach ($list2 as $coin2) {
        $coin = getdbosql('db_coins', "symbol='{$coin2->symbol2}'");
        if (!$coin) {
            continue;
        }
        $list = getdbolist('db_markets', "coinid={$coin->id}");
        foreach ($list as $market) {
            $market2 = getdbosql('db_markets', "coinid={$coin2->id} and name='{$market->name}'");
            if (!$market2) {
                continue;
            }
            $market2->price = $market->price;
            $market2->price2 = $market->price2;
            $market2->deposit_address = $market->deposit_address;
            $market2->save();
        }
    }
    $coins = getdbolist('db_coins', "installed and id in (select distinct coinid from markets)");
    foreach ($coins as $coin) {
        if ($coin->symbol == 'BTC') {
            $coin->price = 1;
            $coin->price2 = 1;
            $coin->save();
            continue;
        }
        $market = getBestMarket($coin);
        if ($market) {
            $coin->price = $market->price * (1 - YAAMP_FEES_EXCHANGE / 100);
            $coin->price2 = $market->price2;
            $base_coin = !empty($market->base_coin) ? getdbosql('db_coins', "symbol='{$market->base_coin}'") : null;
            if ($base_coin) {
                $coin->price *= $base_coin->price;
                $coin->price2 *= $base_coin->price;
            }
            //			if($market->name == 'c-cex')
            //				$coin->price *= 0.95;
        } else {
            $coin->price = 0;
            $coin->price2 = 0;
        }
        $coin->save();
        dborun("update earnings set price={$coin->price} where status!=2 and coinid={$coin->id}");
    }
}
Пример #5
0
 public function actionRunBlocks()
 {
     //		debuglog(__METHOD__);
     set_time_limit(0);
     $this->monitorApache();
     $last_complete = memcache_get($this->memcache->memcache, "cronjob_block_time_start");
     if ($last_complete + 5 * 60 < time()) {
         dborun("update jobs set active=false");
     }
     BackendBlockFind1();
     BackendClearEarnings();
     BackendRentingUpdate();
     BackendProcessList();
     memcache_set($this->memcache->memcache, "cronjob_block_time_start", time());
     //		debuglog(__METHOD__);
 }
Пример #6
0
function BackendProcessList()
{
    $list = dbolist("show processlist");
    foreach ($list as $item) {
        $conn = getdbo('db_connections', $item['Id']);
        if (!$conn) {
            $conn = new db_connections();
            $conn->id = $item['Id'];
            $conn->user = $item['User'];
            $conn->host = $item['Host'];
            $conn->db = $item['db'];
            $conn->created = time();
        }
        $conn->idle = $item['Time'];
        $conn->last = time();
        $conn->save();
    }
    $delay = time() - 5 * 60;
    dborun("delete from connections where last<{$delay}");
}
Пример #7
0
 public function actionIndex()
 {
     $deposit = user()->getState('yaamp-deposit');
     if (!$deposit && !$this->admin) {
         $this->render('login');
         return;
     }
     $address = getparam('address');
     if ($this->admin && !empty($address)) {
         $deposit = $address;
         user()->setState('yaamp-deposit', $deposit);
     }
     $renter = getdbosql('db_renters', "address=:deposit", array(':deposit' => $deposit));
     if (!$renter) {
         $this->render('login');
         return;
     }
     $changed = false;
     if (isset($_POST['deposit_email'])) {
         $renter->email = $_POST['deposit_email'];
         $changed = true;
     }
     if (isset($_POST['deposit_password']) && !empty($_POST['deposit_password'])) {
         if ($_POST['deposit_password'] == $_POST['deposit_confirm']) {
             $renter->password = md5($_POST['deposit_password']);
             $changed = true;
         } else {
             user()->setFlash('error', "Confirm different from password.");
             $this->goback();
             return;
         }
     }
     if ($changed) {
         debuglog("saving renter {$_SERVER['REMOTE_ADDR']} {$renter->address}");
         dborun("update renters set email=:email, password=:password where id={$renter->id}", array(':email' => $renter->email, ':password' => $renter->password));
         //			$renter->save();
         user()->setFlash('message', "Settings saved.");
         $this->redirect("/renting");
     }
     $this->render('index', array('renter' => $renter));
 }
Пример #8
0
function BackendBlocksUpdate()
{
    //	debuglog(__METHOD__);
    $t1 = microtime(true);
    $list = getdbolist('db_blocks', "category='immature' order by time");
    foreach ($list as $block) {
        $coin = getdbo('db_coins', $block->coin_id);
        if (!$coin || !$coin->enable) {
            $block->delete();
            continue;
        }
        $remote = new Bitcoin($coin->rpcuser, $coin->rpcpasswd, $coin->rpchost, $coin->rpcport);
        if (empty($block->txhash)) {
            $blockext = $remote->getblock($block->blockhash);
            if (!$blockext || !isset($blockext['tx'][0])) {
                continue;
            }
            $block->txhash = $blockext['tx'][0];
        }
        $tx = $remote->gettransaction($block->txhash);
        if (!$tx) {
            continue;
        }
        $block->confirmations = $tx['confirmations'];
        if ($block->confirmations == -1) {
            $block->category = 'orphan';
        } else {
            if (isset($tx['details']) && isset($tx['details'][0])) {
                $block->category = $tx['details'][0]['category'];
            } else {
                if (isset($tx['category'])) {
                    $block->category = $tx['category'];
                }
            }
        }
        $block->save();
        if ($block->category == 'generate') {
            dborun("update earnings set status=1, mature_time=UNIX_TIMESTAMP() where blockid={$block->id}");
        } else {
            if ($block->category != 'immature') {
                dborun("delete from earnings where blockid={$block->id}");
            }
        }
    }
    $d1 = microtime(true) - $t1;
    controller()->memcache->add_monitoring_function(__METHOD__, $d1);
}
Пример #9
0
 public function actionUninstallCoin()
 {
     if (!$this->admin) {
         return;
     }
     $coin = getdbo('db_coins', getiparam('id'));
     if ($coin) {
         //	dborun("delete from blocks where coin_id=$coin->id");
         dborun("delete from exchange where coinid={$coin->id}");
         dborun("delete from earnings where coinid={$coin->id}");
         //	dborun("delete from markets where coinid=$coin->id");
         dborun("delete from orders where coinid={$coin->id}");
         dborun("delete from shares where coinid={$coin->id}");
         $coin->enable = false;
         $coin->installed = false;
         $coin->auto_ready = false;
         $coin->master_wallet = null;
         $coin->mint = 0;
         $coin->balance = 0;
         $coin->save();
     }
     $this->redirect("/site/admin");
 }
Пример #10
0
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}");
        }
    }
}
Пример #11
0
function updateRawcoins()
{
    //	debuglog(__FUNCTION__);
    $list = bittrex_api_query('public/getcurrencies');
    if (isset($list->result)) {
        dborun("update markets set deleted=true where name='bittrex'");
        foreach ($list->result as $currency) {
            updateRawCoin('bittrex', $currency->Currency, $currency->CurrencyLong);
        }
    }
    $list = bleutrade_api_query('public/getcurrencies');
    if (isset($list->result)) {
        dborun("update markets set deleted=true where name='bleutrade'");
        foreach ($list->result as $currency) {
            updateRawCoin('bleutrade', $currency->Currency, $currency->CurrencyLong);
        }
    }
    $list = cryptsy_api_query('getmarkets');
    if (isset($list['return'])) {
        dborun("update markets set deleted=true where name='cryptsy'");
        foreach ($list['return'] as $item) {
            updateRawCoin('cryptsy', $item['primary_currency_code'], $item['primary_currency_name']);
        }
    }
    $ccex = new CcexAPI();
    $list = $ccex->getPairs();
    if ($list) {
        dborun("update markets set deleted=true where name='c-cex'");
        foreach ($list as $item) {
            $e = explode('-', $item);
            $symbol = strtoupper($e[0]);
            updateRawCoin('c-cex', $symbol);
        }
    }
    $poloniex = new poloniex();
    $tickers = $poloniex->get_currencies();
    dborun("update markets set deleted=true where name='poloniex'");
    foreach ($tickers as $symbol => $ticker) {
        if ($ticker['disabled']) {
            continue;
        }
        if ($ticker['delisted']) {
            continue;
        }
        updateRawCoin('poloniex', $symbol);
    }
    $res = yobit_api_query('info');
    if ($res) {
        dborun("update markets set deleted=true where name='yobit'");
        foreach ($res->pairs as $i => $item) {
            $e = explode('_', $i);
            $symbol = strtoupper($e[0]);
            updateRawCoin('yobit', $symbol);
        }
    }
    //////////////////////////////////////////////////////////
    dborun("delete from markets where deleted");
    $list = getdbolist('db_coins', "not enable and not installed and id not in (select distinct coinid from markets)");
    foreach ($list as $coin) {
        debuglog("{$coin->symbol} is not longer active");
        $coin->delete();
    }
}