function internal_to_numstr($num, $precision = -1, $round = true) { if ($precision == -1) { $precision = 8; $tidy = true; } else { $tidy = false; } if (!is_string($num) && !is_resource($num)) { throw new Error('Coding error!', "internal_to_numstr argument has type '" . gettype($num) . "'"); } $repr = gmp_strval($num); if ($round) { if ($repr > 0) { $repr = bcadd($repr, pow(10, 8 - $precision) / 2); } else { $repr = bcsub($repr, pow(10, 8 - $precision) / 2); } } $repr = bcdiv($repr, pow(10, 8), $precision); // now tidy output... if ($tidy) { return clean_sql_numstr($repr); } return sprintf("%.{$precision}f", $repr); }
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 display_double_entry($curr_a, $curr_b, $base_curr, $uid, $is_admin) { if (isset($_GET['show_all']) && get('show_all') == 'true') { $show_all = true; } else { $show_all = false; } echo "<div class='content_box'>\n"; if ($curr_a == 'BTC') { echo "<h3>" . sprintf(_("People selling %s for %s"), $curr_a, $curr_b) . "</h3>\n"; } else { echo "<h3>" . sprintf(_("People buying %s for %s"), $curr_b, $curr_a) . "</h3>\n"; } $exchange_fields = calc_exchange_rate($curr_a, $curr_b, $base_curr); if (!$exchange_fields) { if ($curr_a == 'BTC') { echo "<p>" . sprintf(_("Nobody is selling %s for %s."), $curr_a, $curr_b) . "</p>"; } else { echo "<p>" . sprintf(_("Nobody is buying %s for %s."), $curr_b, $curr_a) . "</p>"; } echo "</div>"; return; } list($total_amount, $total_want_amount, $rate) = $exchange_fields; echo "<p>" . _("Best exchange rate is") . " "; if ($base_curr == BASE_CURRENCY::A) { echo "<b>{$rate} {$curr_b}/{$curr_a}</b>"; } else { echo "<b>{$rate} {$curr_a}/{$curr_b}</b>"; } echo ".</p>"; if (!$show_all) { echo "<p>" . sprintf(_("Showing top %d entries"), DEFAULT_ORDERBOOK_DEPTH) . ":</p>"; } ?> <table class='display_data'> <tr> <th><?php echo _("Cost / BTC"); ?> </th> <th><?php echo _("Giving"); ?> </th> <th><?php echo _("Wanted"); ?> </th> <?php if ($is_admin) { ?> <th><?php echo _("User"); ?> </th> <?php } if (SHOW_CUMULATIVE_DEPTH) { ?> <th><?php echo _("Cumulative Give"); ?> </th> <th><?php echo _("Cumulative Want"); ?> </th> <?php } ?> </tr><?php $show_query = 'LIMIT ' . DEFAULT_ORDERBOOK_DEPTH; if ($show_all) { $show_query = ''; } $query = "\n SELECT\n orderid,\n amount,\n want_amount,\n uid={$uid} as me,\n uid,\n IF(\n type='BTC',\n initial_want_amount/initial_amount,\n initial_amount/initial_want_amount\n ) AS rate\n FROM orderbook\n WHERE type='{$curr_a}' AND want_type='{$curr_b}' AND status='OPEN'\n ORDER BY\n IF(type='BTC', rate, -rate) ASC, timest ASC\n {$show_query}\n "; $result = do_query($query); $cumulative_curr_a = 0; $cumulative_curr_b = 0; if ($curr_a == 'BTC') { $precision_a = BTC_PRECISION; $precision_b = FIAT_PRECISION; } else { $precision_a = FIAT_PRECISION; $precision_b = BTC_PRECISION; } while ($row = mysql_fetch_array($result)) { $amount_i = $row['amount']; $amount = internal_to_numstr($amount_i, $precision_a); $cumulative_curr_a = gmp_add($cumulative_curr_a, $amount_i); $want_amount_i = $row['want_amount']; $want_amount = internal_to_numstr($want_amount_i, $precision_b); $cumulative_curr_b = gmp_add($cumulative_curr_b, $want_amount_i); // MySQL kindly computes this for us. // we trim the excessive 0 $rate = clean_sql_numstr($row['rate']); $me = $row['me']; $uid = $row['uid']; if ($me) { echo " ", active_table_row("me", "?page=view_order&orderid={$row['orderid']}"); } else { echo " ", active_table_row("them", "?page=trade&in={$curr_b}&have={$want_amount_i}&want={$amount_i}&rate={$rate}"); } echo " <td>{$rate}</td>\n"; echo " <td>{$amount} {$curr_a}</td>\n"; echo " <td>{$want_amount} {$curr_b}</td>\n"; if ($is_admin) { echo " <td>{$uid}</td>\n"; } if (SHOW_CUMULATIVE_DEPTH) { echo " <td>" . internal_to_numstr($cumulative_curr_a, $precision_a) . " {$curr_a}</td>\n"; echo " <td>" . internal_to_numstr($cumulative_curr_b, $precision_b) . " {$curr_b}</td>\n"; } echo " </tr>\n"; } echo " <tr>\n"; echo " <td>" . _("Total") . ":</td>\n"; // strstr's 3rd argument only works in PHP 5.3.0 and newer // http://php.net/manual/en/function.strstr.php // use explode instead $total_amount = explode('.', $total_amount, 2); $total_amount = $total_amount[0]; echo " <td>{$total_amount} {$curr_a}</td>\n"; echo " <td></td>\n"; echo " </tr>\n"; echo "</table>\n"; if ($show_all) { echo "<p><a href='?page=orderbook&show_all=false'>>> " . _("hide") . "</a></p>\n"; } else { echo "<p><a href='?page=orderbook&show_all=true'>>> " . _("show all") . "</a></p>\n"; } echo "</div>\n"; }
} } if ($b_is_me) { $mine++; echo "<td class='right' style='font-weight:bold;'>{$b_amount_str}</td>"; } else { echo "<td class='right'>{$b_amount_str}</td>"; } echo "<td class='right'>{$price}</td>"; echo "<td>{$timest}</td>"; echo "</tr>\n"; } if ($first) { echo "<p>There are no recent trades.</p>\n"; } else { $price = clean_sql_numstr(bcdiv(gmp_strval($amount_fiat_total), gmp_strval($amount_btc_total), 4)); echo " <tr>\n"; if ($is_admin) { echo " <td></td><td></td><td class='right'>--------</td><td></td><td class='right'>--------</td><td class='right'>--------</td>\n"; } else { echo " <td></td><td class='right'>--------</td><td class='right'>--------</td><td class='right'>--------</td>\n"; } echo " </tr>\n"; echo " <tr>\n"; echo " <td></td>"; if ($is_admin) { echo " <td></td>"; } echo " <td>", internal_to_numstr($amount_fiat_total, FIAT_PRECISION), "</td>"; if ($is_admin) { echo " <td></td>";
function show_content_header_ticker() { global $buy, $sell; $spaces = ' '; list($high, $low, $avg, $vwap, $vol, $last, $buy, $sell) = get_ticker_data(); if ($buy > $sell && $buy != 0 && $sell != 0) { $style = " style='color:#af0;'"; } else { $style = ''; } // include prices up to 0.001% worse than the best $include_very_close_prices = '0.99999'; // ask for 0.001% less than we need to match the worst price we want // $request_less_for_match = '0.99999'; $request_less_for_match = '1'; if ($buy) { list($buy_have, $buy_want, $worst_price) = find_total_trades_available_at_rate(bcmul($buy, $include_very_close_prices, 8), CURRENCY); $buy_have = bcmul(bcmul($buy_want, $worst_price), $request_less_for_match); $worst_price = clean_sql_numstr($worst_price); $buy_link = "<a {$style} href=\"?page=trade&in=BTC&have={$buy_want}&want={$buy_have}&rate={$worst_price}\">{$buy}</a>"; } else { $buy_link = "none"; } if ($sell) { list($sell_have, $sell_want, $worst_price) = find_total_trades_available_at_rate(bcdiv($sell, $include_very_close_prices, 8), 'BTC'); $sell_have = bcmul(bcdiv($sell_want, $worst_price), $request_less_for_match); $worst_price = clean_sql_numstr($worst_price); $sell_link = "<a {$style} href=\"?page=trade&in=" . CURRENCY . "&have={$sell_want}&want={$sell_have}&rate={$worst_price}\">{$sell}</a>"; } else { $sell_link = "none"; } echo " <div class='content_header_box'>\n"; echo " ", SPACE, _("24 hour volume"), ": <a class=\"fancy\" href=\"?fancy&page=view_trades\">{$vol} BTC</a></div>\n"; echo " <div class='content_header_box'>\n"; echo " ", SPACE; echo _("buy") . ": {$buy_link}{$spaces}" . _("sell") . ": {$sell_link}"; echo SPACE, _("last") . ": {$last}", SPACE, _("high") . ": {$high}", SPACE, _("low") . ": {$low}", SPACE, _("avg") . ": {$vwap}\n"; echo " </div>\n"; }