function bitcoin_withdraw($uid, $amount, $curr_type, &$voucher_code, &$reqid)
{
    $voucher = isset($_POST['voucher']);
    if ($voucher) {
        syslog(LOG_NOTICE, "address=voucher");
        $query = "\n            INSERT INTO requests (req_type, uid, amount, curr_type)\n            VALUES ('WITHDR', '{$uid}', '{$amount}', '{$curr_type}');\n        ";
    } else {
        $addy = post('address');
        try {
            $validaddy = bitcoin_validate_address($addy);
        } catch (Exception $e) {
            if ($e->getMessage() != 'Unable to connect.') {
                throw $e;
            }
            throw new Problem(_("Sorry..."), _("We are currently experiencing trouble connecting to the Bitcoin network and so cannot verify that you entered a valid Bitcoin address.") . "</p><p>" . _("Your withdrawal request has been cancelled.") . "</p><p>" . _("Please try again in a few minutes."));
        }
        if (!$validaddy['isvalid']) {
            throw new Problem(_('Bitcoin says no'), _('That address you supplied was invalid.'));
        }
        syslog(LOG_NOTICE, "address={$addy}");
        $we_have = bitcoin_get_balance("*", 0);
        if (gmp_cmp($we_have, $amount) <= 0) {
            $message = sprintf(_("User %s is asking to withdraw %s BTC.  We only have %s BTC."), $uid, internal_to_numstr($amount, BTC_PRECISION), internal_to_numstr($we_have, BTC_PRECISION));
            email_tech(_("Exchange Wallet Balance is Too Low"), $message);
        }
        $query = "\n            INSERT INTO requests (req_type, uid, amount, curr_type)\n            VALUES ('WITHDR', '{$uid}', '{$amount}', '{$curr_type}');\n        ";
    }
    endlog();
    do_query($query);
    $reqid = mysql_insert_id();
    if ($voucher) {
        $voucher_code = store_new_bitcoin_voucher_code($reqid);
    } else {
        $query = "\n            INSERT INTO bitcoin_requests (reqid, addy)\n            VALUES ('{$reqid}', '{$addy}');\n        ";
        do_query($query);
    }
}
Example #2
0
function sync_to_bitcoin($uid)
{
    if (!is_string($uid)) {
        throw new Error('Coding error!', "sync_to_bitcoin() expects a string, not type '" . gettype($uid) . "'");
    }
    try {
        $balance = bitcoin_get_balance($uid, CONFIRMATIONS_FOR_DEPOSIT);
        if (is_float($balance)) {
            throw new Error(_("bitcoind version error"), _("bitcoind getbalance should return an integer not a float"));
        }
        if (gmp_cmp($balance, '0') > 0) {
            bitcoin_move($uid, '', $balance);
            $query = "\n            INSERT INTO requests (req_type, uid, amount, curr_type)\n            VALUES ('DEPOS', '{$uid}', '{$balance}', 'BTC');\n        ";
            do_query($query);
            $we_have = bitcoin_get_balance('*', 1);
            if (gmp_cmp($we_have, numstr_to_internal(WARN_HIGH_WALLET_THRESHOLD)) > 0) {
                email_tech(_("Exchange Wallet Balance is High"), sprintf(_("The exchange wallet has %s BTC available."), internal_to_numstr($we_have, BTC_PRECISION)));
            }
        }
    } catch (Exception $e) {
        if ($e->getMessage() != 'Unable to connect.') {
            throw $e;
        }
    }
}
Example #3
0
function handle_uploaded_identity_docs()
{
    global $is_logged_in, $is_admin;
    ?>
    <div class='content_box'>
    <h3>Upload Results</h3>
<?php 
    if ($is_admin && isset($_POST['uid'])) {
        $uid = post('uid');
        if ($uid == '') {
            $uid = $is_logged_in;
        } else {
            get_openid_for_user($uid);
        }
        // will throw exception if user doesn't exist
    } else {
        $uid = $is_logged_in;
    }
    $uploaded = 0;
    for ($i = 0; $i < ID_FILE_UPLOAD_SLOTS; $i++) {
        $uploaded += upload_identity_doc($i, $uid);
    }
    echo "<p>" . _("Documents uploaded") . ": {$uploaded}</p>\n";
    echo "</div>\n";
    if ($uploaded && !$is_admin) {
        email_tech(_("User Uploaded New Identity Documents"), sprintf("%s\n\n%s", sprintf(_("User %s uploaded %s new file(s)."), $is_logged_in, $uploaded), sprintf("%s?page=docs&uid=%s", SITE_URL, $is_logged_in)));
    }
}
        $addy = $row['addy'];
        $we_have = bitcoin_get_balance("*", CONFIRMATIONS_FOR_DEPOSIT);
        // add on anything we've recently sent from offline storage but which isn't fully confirmed yet
        $main_unconfirmed = gmp_sub(bitcoin_get_balance("", 1), bitcoin_get_balance("", CONFIRMATIONS_FOR_DEPOSIT));
        $we_have = gmp_add($we_have, $main_unconfirmed);
        addlog(LOG_CRONJOB, "Attempting to withdraw " . internal_to_numstr($amount) . " of " . internal_to_numstr($we_have) . " BTC for user {$uid} (reqid {$reqid})");
        if (gmp_cmp($we_have, $amount) >= 0) {
            update_req($reqid, "PROCES");
            // use 'sendtoaddress' rather than 'sendfrom' because it can 'go overdrawn'
            // so long as there are funds in other accounts (pending deposits) to cover it
            bitcoin_send_to_address($addy, $amount);
            update_req($reqid, "FINAL");
            $we_have = bitcoin_get_balance("*", 0);
            addlog(LOG_CRONJOB, "We have " . internal_to_numstr($we_have) . " BTC in total");
            if (gmp_cmp($we_have, numstr_to_internal(WARN_LOW_WALLET_THRESHOLD)) < 0) {
                email_tech(_("Exchange Wallet Balance is Low"), sprintf(_("The exchange wallet only has %s BTC available."), internal_to_numstr($we_have, BTC_PRECISION)));
            }
        } else {
            $message = sprintf(_("We only have %s BTC so can't withdraw %s BTC"), internal_to_numstr($we_have, BTC_PRECISION), internal_to_numstr($amount, BTC_PRECISION));
            addlog(LOG_CRONJOB, $message);
            // email_tech(_("Exchange Wallet Balance is Too Low"), $message);
        }
    }
} catch (Error $e) {
    report_exception($e, SEVERITY::ERROR);
    // Same as below, but flag + log this for review,
    echo "\nError: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
} catch (Problem $e) {
    echo "\nProblem: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";
} catch (Exception $e) {
    echo "\nException: \"{$e->getTitle()}\"\n  {$e->getMessage()}\n";