Exemplo n.º 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}");
        }
    }
}
Exemplo n.º 2
0
 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");
 }