예제 #1
0
function place_order($have_amount_disp, $have_currency, $want_amount_disp, $want_currency)
{
    global $is_logged_in;
    $have_currency = strtoupper($have_currency);
    $want_currency = strtoupper($want_currency);
    curr_supported_check($have_currency);
    curr_supported_check($want_currency);
    // convert for inclusion into database
    $have_amount = numstr_to_internal($have_amount_disp);
    $want_amount = numstr_to_internal($want_amount_disp);
    if ($have_currency == 'BTC') {
        order_worthwhile_check($have_amount, $have_amount_disp, $have_currency, MINIMUM_BTC_AMOUNT);
        order_worthwhile_check($want_amount, $want_amount_disp, $want_currency, MINIMUM_FIAT_AMOUNT);
    } else {
        order_worthwhile_check($have_amount, $have_amount_disp, $have_currency, MINIMUM_FIAT_AMOUNT);
        order_worthwhile_check($want_amount, $want_amount_disp, $want_currency, MINIMUM_BTC_AMOUNT);
    }
    enough_money_check($have_amount, $have_currency);
    do_query("START TRANSACTION");
    // deduct money from their account
    deduct_funds($have_amount, $have_currency);
    // add the money to the order book
    $query = "\n        INSERT INTO orderbook (\n            uid,\n            initial_amount,\n            amount,\n            type,\n            initial_want_amount,\n            want_amount,\n            want_type)\n        VALUES (\n            '{$is_logged_in}',\n            '{$have_amount}',\n            '{$have_amount}',\n            '{$have_currency}',\n            '{$want_amount}',\n            '{$want_amount}',\n            '{$want_currency}');\n    ";
    $result = do_query($query);
    $orderid = mysql_insert_id();
    do_query("COMMIT");
    return $orderid;
}
예제 #2
0
function fetch_depth($rate_query, $field, $have, $want)
{
    $ret = array();
    $minimum_btc_amount = numstr_to_internal(MINIMUM_BTC_AMOUNT);
    $minimum_fiat_amount = numstr_to_internal(MINIMUM_FIAT_AMOUNT);
    if ($have == "BTC") {
        $big_enough = "amount >= {$minimum_btc_amount}  AND want_amount >= {$minimum_fiat_amount}";
    } else {
        $big_enough = "amount >= {$minimum_fiat_amount} AND want_amount >= {$minimum_btc_amount} ";
    }
    $query = "\n    SELECT\n        {$rate_query} AS rate,\n        {$field} as amount\n    FROM\n        orderbook\n    WHERE\n        type='{$have}'\n        AND want_type='{$want}'\n        AND status='OPEN'\n        AND {$big_enough}\n    ORDER BY\n        rate DESC\n    ";
    $result = do_query($query);
    while ($row = mysql_fetch_assoc($result)) {
        $amount = internal_to_numstr($row['amount']);
        $rate = $row['rate'];
        //bitcoincharts uses NUMERIC(18,8)
        if ($rate < 1000000000) {
            array_push($ret, "[{$rate}, {$amount}]");
        }
    }
    return implode($ret, ", ");
}
예제 #3
0
function check_btc_withdraw_limit($uid, $amount)
{
    $withdrawn = btc_withdrawn_today($uid);
    $limit = numstr_to_internal(MAXIMUM_DAILY_BTC_WITHDRAW);
    $available = gmp_sub($limit, $withdrawn);
    if (gmp_cmp($amount, $available) > 0) {
        throw new Problem(_('Daily limit exceeded'), sprintf(_('You can only withdraw %s per day.'), internal_to_numstr($limit) . ' BTC'));
    }
}
예제 #4
0
require_once '../../util.php';
if (count($argv) < 3) {
    echo "fix_badref [bank statement ID (bid)] [deposref]\n";
    exit(-1);
}
$bid = $argv[1];
$deposref = $argv[2];
$query = "\n    SELECT entry\n    FROM bank_statement\n    WHERE\n        bid='{$bid}'\n        AND reqid IS NULL\n        AND status='BADREF'\n    ";
$result = do_query($query);
if (!has_results($result)) {
    echo "No results found for bank statement {$bid}...\n";
    exit(-1);
}
$row = get_row($result);
$entry = $row['entry'];
$entry = split(',', $entry);
$amount = $entry[6];
$amount = numstr_to_internal($amount);
$query = "\n    SELECT uid\n    FROM users\n    WHERE deposref='{$deposref}'\n    ";
$result = do_query($query);
if (!has_results($result)) {
    echo "No user found with deposref of {$deposref}...\n";
    exit(-1);
}
$row = get_row($result);
$uid = $row['uid'];
$query = "\n    INSERT INTO requests (\n        req_type,\n        uid,\n        amount,\n        curr_type\n    ) VALUES (\n        'DEPOS',\n        {$uid},\n        {$amount},\n        '" . CURRENCY . "'\n    )";
do_query($query);
$query = "\n    UPDATE bank_statement\n    SET\n        reqid=LAST_INSERT_ID(),\n        status='FINAL'\n    WHERE\n        bid='{$bid}'\n        AND reqid is NULL\n        AND status='BADREF'\n    ";
do_query($query);
echo "Done.\n";
예제 #5
0
function do_withdraw($amount_disp, $curr_type, &$voucher_code, &$reqid)
{
    global $is_logged_in;
    if (!ENABLE_LOCAL_VOUCHERS && isset($_POST['voucher'])) {
        throw Error('Vouchers are not enabled on this site', 'Withdrawing to a voucher code is disabled.');
    }
    $amount = numstr_to_internal($amount_disp);
    // dollar amounts should be truncated to cents, but Bitcoins are more divisible
    if ($curr_type == 'BTC') {
        $amount = truncate_num($amount, BTC_WITHDRAW_DECIMAL_PLACES);
    } else {
        $amount = truncate_num($amount, 2);
    }
    curr_supported_check($curr_type);
    order_worthwhile_check($amount, $amount_disp, $curr_type, MINIMUM_WITHDRAW);
    enough_money_check($amount, $curr_type);
    check_withdraw_limit($is_logged_in, $amount, $curr_type);
    if (!save_details($is_logged_in, $amount, $curr_type, $voucher_code, $reqid)) {
        throw Error('We had to admit it sometime...', 'Stop trading on thie site. Contact the admin FAST.');
    }
    // actually take the money now
    deduct_funds($amount, $curr_type);
    // request is submitted to the queue for the cron job to actually execute (unless it's a voucher)
}
예제 #6
0
function show_statement($userid, $interval = 'forever', $from_zero, $deposit_btc, $withdraw_btc, $deposit_fiat, $withdraw_fiat, $buy, $sell)
{
    global $is_logged_in, $is_admin;
    if ($userid) {
        $specified_user = true;
    } else {
        $specified_user = false;
        $userid = $is_logged_in;
    }
    $show_increments = false;
    $show_prices = true;
    echo "<div class='content_box'>\n";
    $all_users = $userid == 'all';
    $deposit_address = $create_timestamp = false;
    if ($all_users) {
        echo "<h3>" . _("Statement for All Users") . "</h3>\n";
        $check_stuff = "";
    } else {
        $openid = get_openid_for_user($userid);
        echo "<h3>" . sprintf(_("Statement for UID %s"), $userid) . "</h3>\n";
        $check_stuff = "uid='{$userid}' AND ";
        if ($is_admin) {
            $create_timestamp = get_account_creation_timest_for_user($userid);
            try {
                $deposit_address = bitcoin_get_account_address($userid);
            } catch (Exception $e) {
            }
        }
    }
    echo "<form method='get'>\n" . "<p>\n" . _("Show entries from ") . "\n" . "<input type='hidden' name='page' value='statement' />\n";
    echo "<select onChange='this.form.submit()' name='interval'>\n";
    foreach (array('4 hour' => _('the last 4 hours'), '12 hour' => _('the last 12 hours'), '1 day' => _('the last 24 hours'), '3 day' => _('the last 3 days'), '1 week' => _('the last 7 days'), '1 month' => _('the last month'), '2 month' => _('the last 2 months'), '3 month' => _('the last 3 months'), '6 month' => _('the last 6 months'), '1 year' => _('the last year'), 'forever' => _('forever'), 'pending' => _('still pending')) as $key => $text) {
        printf("<option %s value='%s'>%s</option>\n", $interval == $key ? "selected='selected'" : "", $key, $text);
    }
    echo "</select>\n";
    if ($is_admin) {
        echo " for <select onChange='this.form.submit()' name='user'>\n";
        if ($all_users) {
            printf("<option value='{$is_logged_in}'>%s</option>\n", _("my account"));
            printf("<option value='all' selected='selected'>all users</option>\n");
        } else {
            if ($userid != $is_logged_in) {
                printf("<option value='{$is_logged_in}'>%s</option>\n", _("my account"));
            }
            printf("<option value='{$userid}' selected='selected'>%s</option>\n", $userid == $is_logged_in ? _("my account") : "UID {$userid}");
            echo "<option value='all'>all users</option>\n";
        }
        echo "</select>\n";
        echo " or UID or OpenID: ";
        echo "<input class='nline' type='text' name='uid'>\n";
    }
    $pending = $interval == 'pending';
    $use_interval = $interval != 'forever' && !$pending;
    $args = $specified_user ? "user={$userid}&" : "";
    $args .= "interval={$interval}";
    if ($from_zero) {
        $args .= "&fromz=1";
    }
    echo "<input type='hidden' name='form' value='1' /><br />\n";
    echo statement_checkbox('dbtc', $deposit_btc, _("Deposit") . " " . "BTC", $args);
    echo statement_checkbox('wbtc', $withdraw_btc, _("Withdraw") . " " . "BTC", $args);
    echo statement_checkbox('dfiat', $deposit_fiat, _("Deposit") . " " . CURRENCY, $args);
    echo statement_checkbox('wfiat', $withdraw_fiat, _("Withdraw") . " " . CURRENCY, $args);
    echo statement_checkbox('bbtc', $buy, _("Buy") . " " . "BTC", $args);
    echo statement_checkbox('sbtc', $sell, _("Sell") . " " . "BTC", $args);
    if ($interval != 'forever') {
        echo statement_checkbox('fromz', $from_zero, _("Start at Zero"));
    } else {
        if ($from_zero) {
            echo "<input type='hidden' name='fromz' value='1' />\n";
        }
    }
    echo "</p>\n";
    echo "</form>\n";
    if (!$all_users) {
        echo "<p>" . _("OpenID") . ": <a href=\"{$openid}\">{$openid}</a></p>\n";
        if ($deposit_address) {
            echo "<p>" . _("Deposit Address") . ": {$deposit_address}</p>\n";
        }
    }
    $query = "\n        SELECT\n            uid,\n            txid, a_orderid AS orderid,\n            a_amount AS gave_amount, '" . CURRENCY . "' AS gave_curr,\n            (b_amount-b_commission) AS got_amount,  'BTC' AS got_curr,\n            NULL as reqid,  NULL as req_type,\n            NULL as amount, NULL as curr_type, NULL as addy, NULL as voucher, NULL as final, NULL as bank, NULL as acc_num,\n            " . sql_format_date('transactions.timest') . " AS date,\n            transactions.timest as timest, " . ($use_interval ? "transactions.timest > NOW() - INTERVAL {$interval}" : ($pending ? "0" : "1")) . " AS new\n        FROM\n            transactions\n        JOIN\n            orderbook\n        ON\n            orderbook.orderid = transactions.a_orderid\n        WHERE\n            {$check_stuff}\n            b_amount != -1\n\n    UNION\n\n        SELECT\n            uid,\n            txid, b_orderid AS orderid,\n            b_amount AS gave_amount, 'BTC' AS gave_curr,\n            (a_amount-a_commission) AS got_amount,  '" . CURRENCY . "' AS got_curr,\n            NULL, NULL,\n            NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n            " . sql_format_date('transactions.timest') . " AS date,\n            transactions.timest as timest, " . ($use_interval ? "transactions.timest > NOW() - INTERVAL {$interval}" : ($pending ? "0" : "1")) . " AS new\n        FROM\n            transactions\n        JOIN\n            orderbook\n        ON\n            orderbook.orderid=transactions.b_orderid\n        WHERE\n            {$check_stuff}\n            b_amount != -1\n\n    UNION\n\n        SELECT\n            uid,\n            NULL, NULL,\n            NULL, NULL,\n            NULL, NULL,\n            requests.reqid,  req_type,\n            amount, curr_type, addy, CONCAT(prefix, '-...') as voucher, status = 'FINAL', bank, acc_num,\n            " . sql_format_date('timest') . " AS date,\n            timest, " . ($use_interval ? "timest > NOW() - INTERVAL {$interval}" : ($pending ? "status != 'FINAL'" : "1")) . " AS new\n        FROM\n            requests\n        LEFT JOIN\n            bitcoin_requests\n        ON\n            requests.reqid = bitcoin_requests.reqid\n        LEFT JOIN\n            voucher_requests\n        ON\n            (requests.reqid = voucher_requests.reqid OR\n             requests.reqid = voucher_requests.redeem_reqid)\n        LEFT JOIN\n            uk_requests\n        ON\n            requests.reqid = uk_requests.reqid\n        WHERE\n            {$check_stuff}\n            status != 'CANCEL'\n\n    ORDER BY\n        timest, txid, got_curr\n    ";
    $first = true;
    $result = do_query($query);
    $fiat = $btc = numstr_to_internal(0);
    $total_fiat_deposit = $total_fiat_withdrawal = $total_btc_deposit = $total_btc_withdrawal = numstr_to_internal(0);
    $total_fiat_got = $total_fiat_given = $total_btc_got = $total_btc_given = numstr_to_internal(0);
    $period_fiat_deposit = $period_fiat_withdrawal = $period_btc_deposit = $period_btc_withdrawal = numstr_to_internal(0);
    $period_fiat_got = $period_fiat_given = $period_btc_got = $period_btc_given = numstr_to_internal(0);
    echo "<table class='display_data'>\n";
    echo "<tr>";
    echo "<th>" . _("Date") . "</th>";
    if ($all_users) {
        echo "<th>" . _("User") . "</th>";
    }
    echo "<th>" . _("Description") . "</th>";
    if (!$pending) {
        if ($show_prices) {
            echo "<th class='right'>" . _("Price") . "</th>";
        }
        if ($show_increments) {
            echo "<th class='right'>+/-</th>";
        }
        echo "<th class='right'>BTC</th>";
        if ($show_increments) {
            echo "<th class='right'>+/-</th>";
        }
        echo "<th class='right'>" . CURRENCY . "</th>";
    }
    echo "</tr>\n";
    if ($create_timestamp && !$pending) {
        printf("<tr><td>%s</td><td>%s</td></tr>\n", $create_timestamp, _("Create Account"));
    }
    $all_final = true;
    while ($row = mysql_fetch_array($result)) {
        $new = $row['new'];
        $uid = $row['uid'];
        $date = $row['date'];
        if ($first && $new) {
            if ($from_zero) {
                $btc = $fiat = numstr_to_internal(0);
            }
            show_balances_in_statement(_("Opening Balances"), $btc, $fiat, $all_users, $show_prices, $show_increments, $pending);
            $first = false;
        }
        if (isset($row['txid'])) {
            /* buying or selling */
            $txid = $row['txid'];
            $orderid = $row['orderid'];
            $gave_amount = $row['gave_amount'];
            $gave_curr = $row['gave_curr'];
            $got_amount = $row['got_amount'];
            $got_curr = $row['got_curr'];
            if ($got_curr == 'BTC') {
                /* buying BTC */
                if ($buy) {
                    $fiat = gmp_sub($fiat, $gave_amount);
                    $btc = gmp_add($btc, $got_amount);
                }
                $total_btc_got = gmp_add($total_btc_got, $got_amount);
                $total_fiat_given = gmp_add($total_fiat_given, $gave_amount);
                $got_str = internal_to_numstr($got_amount, BTC_PRECISION);
                $gave_str = internal_to_numstr($gave_amount, FIAT_PRECISION);
                if ($new && $buy) {
                    $period_btc_got = gmp_add($period_btc_got, $got_amount);
                    $period_fiat_given = gmp_add($period_fiat_given, $gave_amount);
                    if (string_is_zero($got_str) && string_is_zero($gave_str)) {
                        continue;
                    }
                    echo "<tr><td>{$date}</td>";
                    if ($all_users) {
                        echo active_table_cell_link_to_user_statement($uid, $interval);
                    }
                    active_table_cell_for_order(sprintf(_("Buy %s %s for %s %s"), $got_str, $got_curr, $gave_str, $gave_curr), $orderid);
                    if ($show_prices) {
                        printf("<td>%s</td>", trade_price($got_amount, $gave_amount));
                    }
                    if ($show_increments) {
                        printf("<td class='right'>+ %s</td>", $got_str);
                    }
                    printf("<td class='right'> %s</td>", internal_to_numstr($btc, BTC_PRECISION));
                    if ($show_increments) {
                        printf("<td class='right'>- %s</td>", $gave_str);
                    }
                    printf("<td class='right'> %s</td>", internal_to_numstr($fiat, FIAT_PRECISION));
                    echo "</tr>\n";
                }
            } else {
                /* selling BTC */
                if ($sell) {
                    $fiat = gmp_add($fiat, $got_amount);
                    $btc = gmp_sub($btc, $gave_amount);
                }
                $total_fiat_got = gmp_add($total_fiat_got, $got_amount);
                $total_btc_given = gmp_add($total_btc_given, $gave_amount);
                $gave_str = internal_to_numstr($gave_amount, BTC_PRECISION);
                $got_str = internal_to_numstr($got_amount, FIAT_PRECISION);
                if ($new && $sell) {
                    $period_fiat_got = gmp_add($period_fiat_got, $got_amount);
                    $period_btc_given = gmp_add($period_btc_given, $gave_amount);
                    if (string_is_zero($got_str) && string_is_zero($gave_str)) {
                        continue;
                    }
                    echo "<tr><td>{$date}</td>";
                    if ($all_users) {
                        echo active_table_cell_link_to_user_statement($uid, $interval);
                    }
                    active_table_cell_for_order(sprintf(_("Sell %s %s for %s %s"), $gave_str, $gave_curr, $got_str, $got_curr), $orderid);
                    if ($show_prices) {
                        printf("<td>%s</td>", trade_price($gave_amount, $got_amount));
                    }
                    if ($show_increments) {
                        printf("<td class='right'>-%s</td>", $gave_str);
                    }
                    // don't show balances between pairs of buy and sell rows if we're showing buy as well as sell
                    printf("<td class='right'>%s</td>", $all_users && $buy ? "" : internal_to_numstr($btc, BTC_PRECISION));
                    if ($show_increments) {
                        printf("<td class='right'>+%s</td>", $got_str);
                    }
                    printf("<td class='right'>%s</td>", $all_users && $buy ? "" : internal_to_numstr($fiat, FIAT_PRECISION));
                    echo "</tr>\n";
                }
            }
        } else {
            /* withdrawal or deposit */
            $reqid = $row['reqid'];
            $req_type = $row['req_type'];
            $amount = $row['amount'];
            $curr_type = $row['curr_type'];
            $voucher = $row['voucher'];
            $final = $row['final'];
            // echo "final is $final<br/>\n";
            $show = $req_type == 'DEPOS' && ($curr_type == 'BTC' && $deposit_btc || $curr_type != 'BTC' && $deposit_fiat) || $req_type != 'DEPOS' && ($curr_type == 'BTC' && $withdraw_btc || $curr_type != 'BTC' && $withdraw_fiat);
            if ($new && $show) {
                echo "<tr><td>{$date}</td>";
                if ($all_users) {
                    echo active_table_cell_link_to_user_statement($uid, $interval);
                }
            }
            if (!$final) {
                $all_final = false;
            }
            if ($req_type == 'DEPOS') {
                /* deposit */
                $title = '';
                if ($voucher) {
                    $title = sprintf(_("from voucher") . " &quot;%s&quot;", $voucher);
                }
                if ($curr_type == 'BTC') {
                    /* deposit BTC */
                    if ($show) {
                        $btc = gmp_add($btc, $amount);
                    }
                    $total_btc_deposit = gmp_add($total_btc_deposit, $amount);
                    if ($new && $show) {
                        $period_btc_deposit = gmp_add($period_btc_deposit, $amount);
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s BTC%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Redeem voucher") . ":" : _("Deposit"), internal_to_numstr($amount, BTC_PRECISION), $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td class='right'>+%s</td>", internal_to_numstr($amount, BTC_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($btc, BTC_PRECISION));
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                        }
                    }
                } else {
                    /* deposit FIAT */
                    if ($show) {
                        $fiat = gmp_add($fiat, $amount);
                    }
                    $total_fiat_deposit = gmp_add($total_fiat_deposit, $amount);
                    if ($new && $show) {
                        $period_fiat_deposit = gmp_add($period_fiat_deposit, $amount);
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s %s%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Redeem voucher") . ":" : _("Deposit"), internal_to_numstr($amount, FIAT_PRECISION), CURRENCY, $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                            if ($show_increments) {
                                printf("<td class='right'>+%s</td>", internal_to_numstr($amount, FIAT_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($fiat, FIAT_PRECISION));
                        }
                    }
                }
            } else {
                /* withdrawal */
                if ($curr_type == 'BTC') {
                    /* withdraw BTC */
                    if ($show) {
                        $btc = gmp_sub($btc, $amount);
                    }
                    $total_btc_withdrawal = gmp_add($total_btc_withdrawal, $amount);
                    if ($new && $show) {
                        $period_btc_withdrawal = gmp_add($period_btc_withdrawal, $amount);
                        $addy = $row['addy'];
                        if ($addy) {
                            $title = sprintf(_("to Bitcoin address") . " &quot;%s&quot;", $addy);
                        } else {
                            if ($voucher) {
                                $title = sprintf(_("to %svoucher") . " &quot;%s&quot;", $final ? "" : _("unredeemed") . " ", $voucher);
                            }
                        }
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s BTC%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Create voucher") . ":" : _("Withdraw"), internal_to_numstr($amount, BTC_PRECISION), $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td class='right'>-%s</td>", internal_to_numstr($amount, BTC_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($btc, BTC_PRECISION));
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                        }
                    }
                } else {
                    /* withdraw FIAT */
                    if ($show) {
                        $fiat = gmp_sub($fiat, $amount);
                    }
                    $total_fiat_withdrawal = gmp_add($total_fiat_withdrawal, $amount);
                    if ($new && $show) {
                        $period_fiat_withdrawal = gmp_add($period_fiat_withdrawal, $amount);
                        $title = '';
                        if ($voucher) {
                            $title = sprintf(_("to %svoucher") . " &quot;%s&quot;", $final ? "" : _("unredeemed") . " ", $voucher);
                        } else {
                            $title = sprintf(_("to account %s at %s"), $row['acc_num'], $row['bank']);
                        }
                        active_table_cell_for_request(sprintf("<strong title='%s'>%s%s %s %s%s</strong>", $title, $final ? "" : "* ", $voucher ? _("Create voucher") . ":" : _("Withdraw"), internal_to_numstr($amount, FIAT_PRECISION), CURRENCY, $final ? "" : " *"), $reqid);
                        if (!$pending) {
                            if ($show_prices) {
                                printf("<td></td>");
                            }
                            if ($show_increments) {
                                printf("<td></td>");
                            }
                            printf("<td></td>");
                            if ($show_increments) {
                                printf("<td class='right'>-%s</td>", internal_to_numstr($amount, FIAT_PRECISION));
                            }
                            printf("<td class='right'>%s</td>", internal_to_numstr($fiat, FIAT_PRECISION));
                        }
                    }
                }
            }
            if ($new) {
                echo "</tr>\n";
            }
        }
    }
    if ($first && $from_zero) {
        $fiat = $btc = numstr_to_internal(0);
    }
    show_balances_in_statement($first ? _("There are no entries for this period") : _("Closing Balances"), $btc, $fiat, $all_users, $show_prices, $show_increments, $pending);
    echo "</table>\n";
    if (!$all_final) {
        echo "<p>" . _("Items marked with '*' are not yet final.") . "</p>\n";
        echo "<p>" . _("Any such withdrawals and vouchers can be cancelled.") . "</p>\n";
        echo "<p>" . _("Any such deposits are pending, and should be finalised within a minute or two.") . "</p>\n";
    }
    echo "</div>";
    if (gmp_cmp($total_fiat_deposit, $period_fiat_deposit) != 0 || gmp_cmp($total_fiat_withdrawal, $period_fiat_withdrawal) != 0 || gmp_cmp($total_btc_deposit, $period_btc_deposit) != 0 || gmp_cmp($total_btc_withdrawal, $period_btc_withdrawal) != 0 || gmp_cmp($total_fiat_got, $period_fiat_got) != 0 || gmp_cmp($total_fiat_given, $period_fiat_given) != 0 || gmp_cmp($total_btc_got, $period_btc_got) != 0 || gmp_cmp($total_btc_given, $period_btc_given) != 0) {
        show_statement_summary(_("Summary of displayed entries"), $period_fiat_deposit, $period_fiat_withdrawal, $period_btc_deposit, $period_btc_withdrawal, $period_fiat_got, $period_fiat_given, $period_btc_got, $period_btc_given);
    }
    show_statement_summary(_("Account Summary"), $total_fiat_deposit, $total_fiat_withdrawal, $total_btc_deposit, $total_btc_withdrawal, $total_fiat_got, $total_fiat_given, $total_btc_got, $total_btc_given);
}
예제 #7
0
        ?>
    </div>
<?php 
    }
    ?>

    <div class='content_box'>
    <h3><?php 
    echo _("Withdraw BTC to Bitcoin Address");
    ?>
</h3>
<?php 
    $balances = fetch_balances($is_logged_in);
    $btc = $balances['BTC'];
    $withdrawn = btc_withdrawn_today($is_logged_in);
    $limit = numstr_to_internal(MAXIMUM_DAILY_BTC_WITHDRAW);
    $available = gmp_sub($limit, $withdrawn);
    if (gmp_cmp($btc, $available) > 0) {
        echo "    <p>" . sprintf(_("You can withdraw up to %s BTC each day"), internal_to_numstr($limit)) . " (", day_time_range_string(), ").</p>\n";
        if ($withdrawn) {
            echo "    <p>" . sprintf(_("You have withdrawn %s BTC today"), internal_to_numstr($withdrawn)) . "\n";
            if (gmp_cmp($available, '0') > 0) {
                echo "    " . sprintf(_("and so can withdraw up to %s BTC more."), internal_to_numstr($available));
            } else {
                echo "    " . _("and so cannot withdraw any more until tomorrow.");
            }
            echo "</p>\n";
        }
    }
    if (gmp_cmp($btc, '0') <= 0) {
        echo "    <p>" . _("You don't have any BTC to withdraw.") . "</p>\n";
예제 #8
0
function test_voucher_comms()
{
    test_voucher_comm(numstr_to_internal("0.01"));
    test_voucher_comm(numstr_to_internal("0.12"));
    test_voucher_comm(numstr_to_internal("1.23"));
    test_voucher_comm(numstr_to_internal("12.34"));
    test_voucher_comm(numstr_to_internal("123.40"));
    test_voucher_comm(numstr_to_internal("1234.00"));
    test_voucher_comm(numstr_to_internal("12345.00"));
}
예제 #9
0
function redeem_mtgox_fiat_voucher($code)
{
    global $is_logged_in;
    if (!ENABLE_MTGOX_VOUCHERS) {
        throw Error('MtGox vouchers are not enabled on this site', 'Redeeming MtGox voucher codes is disabled.');
    }
    $mtgox = new MtGox_API(MTGOX_KEY, MTGOX_SECRET);
    $result = $mtgox->deposit_coupon($code);
    // echo "result: <pre>" . var_dump($result) . "</pre><br/>\n";
    // successful coupon deposit:
    //
    // array(4) {
    //   ["amount"]=>  float(0.01)
    //   ["currency"]=>  string(3) "BTC"
    //   ["reference"]=>  string(36) "beabf9ce-07b6-4852-ae71-4cfc671ff35d"
    //   ["status"]=>     string(49) "Your account has been credited by 0.01000000 BTC"
    // }
    // trying to redeem an already-spent code - note no 'status':
    //
    // array(1) {
    //   ["error"]=>  string(59) "This code cannot be redeemed (non existing or already used)"
    // }
    if (isset($result['error'])) {
        throw new Exception($result['error']);
    }
    $amount = numstr_to_internal(cleanup_string($result['amount']));
    $curr_type = cleanup_string($result['currency']);
    // $reference = cleanup_string($result['reference'], '-');
    $status = cleanup_string($result['status']);
    // echo "<p>When we tried to redeem that voucher into our account, MtGox said: <strong>$status</strong></p>\n";
    $commission = commission_on_deposit_mtgox_fiat_voucher($amount);
    $amount = gmp_strval(gmp_sub($amount, $commission));
    $query = "\n        INSERT INTO requests (req_type, uid, amount, commission, curr_type, status)\n        VALUES ('DEPOS', '{$is_logged_in}', '{$amount}', '{$commission}', '{$curr_type}', 'FINAL');\n    ";
    do_query($query);
    add_funds(1, $commission, $curr_type);
    add_funds($is_logged_in, $amount, $curr_type);
    return array($curr_type, $amount);
}
예제 #10
0
<?php

global $minimum_btc_amount, $minimum_fiat_amount;
$minimum_btc_amount = numstr_to_internal(MINIMUM_BTC_AMOUNT);
$minimum_fiat_amount = numstr_to_internal(MINIMUM_FIAT_AMOUNT);
function active_table_cell_trade($id, $index, $content, $url, $right = false)
{
    printf("<td id='{$id}{$index}' class='active%s' %s %s %s>%s</td>\n", $right ? " right" : "", 'onmouseover="ObjById(\'' . $id . 'l\').style.backgroundColor=\'#8ae3bf\'; ObjById(\'' . $id . 'r\').style.backgroundColor=\'#8ae3bf\';"', 'onmouseout="ObjById(\'' . $id . 'l\').style.backgroundColor=\'#7ad3af\'; ObjById(\'' . $id . 'r\').style.backgroundColor=\'#7ad3af\';"', "onclick=\"document.location='{$url}';\"", $content);
}
function show_mini_orderbook_table_cell($id, $curr, $price, $have, $want, $fiat_depth, $btc_depth)
{
    // $have and $want is what the 'worst priced' existing order has and wants, and is used here to set the price
    // $fiat_depth and $btc_depth are combined amounts available which we want to match, and may include orders at better prices
    // $curr is the currency type they want
    if ($curr == 'BTC') {
        // we are selling BTC
        $depth = $btc_depth;
        $p = clean_sql_numstr(bcdiv($have, $want, 8));
    } else {
        // we are buying BTC
        $depth = $fiat_depth;
        $p = clean_sql_numstr(bcdiv($want, $have, 8));
    }
    list($w, $r) = gmp_div_qr(gmp_mul($depth, $have), $want);
    $w = gmp_strval(gmp_cmp($r, 0) ? gmp_sub($w, 1) : $w);
    $h = gmp_strval($depth);
    active_table_cell_trade($id, 'l', internal_to_numstr($btc_depth, BTC_PRECISION), "?page=trade&in={$curr}&have={$h}&want={$w}&rate={$p}", 'right');
    active_table_cell_trade($id, 'r', internal_to_numstr($fiat_depth, FIAT_PRECISION), "?page=trade&in={$curr}&have={$h}&want={$w}&rate={$p}", 'right');
}
function show_mini_orderbook_table_row($id, $curr, $price, $have, $want, $this_fiat, $this_btc, $sum_fiat, $sum_btc, $mine)
{
예제 #11
0
     show_header('login', $uid);
     echo "                    <div class='content_box'>\n";
     echo "                        <h3>" . _("Successful login!") . "</h3>\n";
     echo "                        <p>" . _("Welcome back commander. Welcome back.") . "</p>\n";
 } else {
     // make the first user to sign up an administrator
     $query = "SELECT COUNT(*) AS count FROM users";
     $result = do_query($query);
     $row = mysql_fetch_assoc($result);
     $admin = $row['count'] == 1 ? 1 : 0;
     // generate random str for deposit reference
     $query = "\n                        INSERT INTO users (\n                            is_admin,\n                            oidlogin,\n                            deposref\n                        ) VALUES (\n                            {$admin},\n                            '{$oidlogin}',\n                            CONCAT(FLOOR(RAND() * 900 + 100),\n                                   LPAD(FLOOR(RAND() * 1000),3,'0'),\n                                   LPAD(FLOOR(RAND() * 1000),3,'0'))\n                        );\n                    ";
     do_query($query);
     $uid = (string) mysql_insert_id();
     $free_fiat = numstr_to_internal(FREE_FIAT_ON_SIGNUP);
     $free_btc = numstr_to_internal(FREE_BTC_ON_SIGNUP);
     $query = "\n                        INSERT INTO purses\n                            (uid, amount, type)\n                        VALUES\n                            (LAST_INSERT_ID(), {$free_fiat}, '" . CURRENCY . "');\n                    ";
     do_query($query);
     $query = "\n                        INSERT INTO purses\n                            (uid, amount, type)\n                        VALUES\n                            (LAST_INSERT_ID(), {$free_btc}, 'BTC');\n                    ";
     do_query($query);
     addlog(LOG_LOGIN, sprintf("  new user UID %s (openid %s)", $uid, $oidlogin));
     show_header('login', $uid);
     echo "                    <div class='content_box'>\n";
     echo "                        <h3>" . _("Successful login!") . "</h3>\n";
     echo "                        <p>" . _("Nice to finally see you here, <i>new</i> user.") . "</p>\n";
     if (gmp_cmp($free_fiat, 0) > 0 or gmp_cmp($free_btc, 0)) {
         echo "                        <p>" . sprintf("We've given you %s and %s to test the exchange with.", internal_to_numstr($free_btc) . " BTC", internal_to_numstr($free_fiat) . " " . CURRENCY) . "</p>\n";
     }
     echo "                        <p>" . sprintf("Now you may wish to %sdeposit%s funds before continuing.", '<a href="?page=deposit">', '</a>') . "</p>\n";
 }
 // store for later
        $amount = $row['amount'];
        $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) {