/** * Test a BTC/DOGE exchange increase notification, particularly the precision. * Issue #257 */ function testDogecoin() { // create a notification $id = $this->createNotificationTicker($this->user, array('exchange' => get_default_currency_exchange('dog'), 'currency1' => 'btc', 'currency2' => 'dog')); $arg_id = $this->createNotification($this->user, array('last_value' => 2.0E-7, 'notification_type' => 'ticker', 'type_id' => $id, 'trigger_condition' => 'increases', 'trigger_value' => 1, 'is_percent' => 0)); // execute the job $this->executeJob($this->user, $arg_id); $mails = $this->getMails(); $this->assertEquals(1, count($mails)); $exchange_name = get_exchange_name(get_default_currency_exchange('dog')); $this->assertHasLine("The exchange rate on {$exchange_name} for BTC/DOGE has increased, from 0.00000020 BTC/DOGE to 0.00000030 BTC/DOGE (50%), in the last hour.", $mails[0]); }
function executeSum($user, $currencies) { // insert in summary currencies $summary_map = array(); foreach ($currencies as $cur) { $q = db()->prepare("INSERT INTO summaries SET user_id=?, summary_type=?"); $q->execute(array($user['id'], 'summary_' . $cur . (is_fiat_currency($cur) ? '_' . get_default_currency_exchange($cur) : ''))); } $this->executeJob($user, -1); // now, find all summary_instances $q = db()->prepare("SELECT * FROM summary_instances WHERE user_id=? AND is_recent=1"); $q->execute(array($user['id'])); $result = array(); while ($si = $q->fetch()) { $result[$si['summary_type']] = $si['balance']; } return $result; }
function get_all_recent_rates() { global $global_all_recent_rates; if ($global_all_recent_rates === null) { $global_all_recent_rates = array(); $query = ""; foreach (get_all_currencies() as $cur) { if ($cur == 'btc') { continue; } // we don't provide a 'btcbtc' rate $exchange = get_default_currency_exchange($cur); $query .= "(currency1 = 'btc' AND currency2 = '{$cur}' AND exchange='{$exchange}') OR"; $query .= "(currency1 = '{$cur}' AND currency2 = 'btc' AND exchange='{$exchange}') OR"; } $q = db()->prepare("SELECT * FROM ticker_recent WHERE 1 AND ({$query} 0)"); $q->execute(); while ($ticker = $q->fetch()) { $global_all_recent_rates[$ticker['currency1'] . $ticker['currency2']] = $ticker; } } return $global_all_recent_rates; }
$cryptos = get_all_cryptocurrencies(); $fiats = get_all_fiat_currencies(); $commodities = get_all_commodity_currencies(); // go through all fiat currencies and, if no exchange is selected, select a default one foreach ($fiats as $c) { if (in_array($c, $currencies)) { $found = false; foreach ($exchanges as $e) { $prefix = "summary_" . $c . "_"; if (substr($e, 0, strlen($prefix)) == $prefix) { // found one $found = true; } } if (!$found) { $exchanges[] = "summary_" . $c . "_" . get_default_currency_exchange($c); } } else { // or, if the currency isn't selected, remove any default exchanges $result = array(); foreach ($exchanges as $e) { $prefix = "summary_" . $c . "_"; if (!(substr($e, 0, strlen($prefix)) == $prefix)) { $result[] = $e; } } $exchanges = $result; } } // go through all crypto currencies and add just summary_CUR foreach ($currencies as $c) {
/** * Return some text describing the default exchanges used for the given currencies. * For example: * array('ltc', 'ftc', 'usd', 'ghs') => 'BTC-e for LTC/FTC, Mt.Gox for USD, CEX.io for GHS' * @see get_default_currency_exchange() */ function get_default_exchange_text($currencies) { $result = array(); foreach ($currencies as $c) { $default = get_default_currency_exchange($c); if (!isset($result[$default])) { $result[$default] = array(); } $result[$default][] = get_currency_abbr($c); } $result2 = array(); foreach ($result as $exchange => $currencies) { $result2[] = get_exchange_name($exchange) . " for " . implode("/", $currencies); } return implode(", ", $result2); }
/** * Calculate all of the different types of managed graphs that * may be provided to a given user, in each category of managed * graphs (see get_managed_graph_categories()). */ function calculate_all_managed_graphs($user) { $result = array(); $summaries = get_all_summary_currencies(); $all_summaries = get_all_summaries(); $currencies = get_all_currencies(); $accounts = account_data_grouped(); $wallets = get_supported_wallets(); $order_currency = array(); foreach (get_all_currencies() as $c) { $order_currency[$c] = count($order_currency); } $order_exchange = array(); foreach (get_all_exchanges() as $key => $label) { $order_exchange[$key] = count($order_exchange) * 10; } $default_order = array('btc_equivalent' => -1000, 'composition_pie' => 0, 'balances_table' => 1000, 'exchange_daily' => 2000, 'total_daily' => 3000, 'all_daily' => 4000, 'composition_daily' => 5000, 'hashrate_daily' => 6000); $result['summary'] = array(); $result['all_summary'] = array(); $result['summary']['balances_table'] = array('order' => $default_order['balances_table'], 'width' => get_site_config('default_user_graph_height'), 'free' => true, 'priority' => 1); if (count($summaries) >= 2 && isset($summaries['btc'])) { $result['summary']['btc_equivalent'] = array('order' => $default_order['btc_equivalent'], 'width' => get_site_config('default_user_graph_height'), 'free' => true, 'priority' => 2); } foreach (get_all_cryptocurrencies() as $cur) { if (isset($summaries[$cur])) { $result['summary']["composition_" . $cur . "_pie"] = array('order' => $default_order['composition_pie'] + $order_currency[$cur], 'width' => get_site_config('default_user_graph_height'), 'free' => $cur == $user['preferred_crypto'], 'priority' => $cur == $user['preferred_crypto'] ? 100 : 300 + $order_currency[$cur]); $result['summary']["composition_" . $cur . "_daily"] = array('order' => $default_order['composition_daily'] + $order_currency[$cur], 'free' => $cur == $user['preferred_crypto'], 'priority' => $cur == $user['preferred_crypto'] ? 105 : 200 + $order_currency[$cur]); $result['summary']['total_' . $cur . '_daily'] = array('order' => $default_order['total_daily'] + $order_currency[$cur], 'source' => $cur, 'free' => $cur == $user['preferred_crypto'], 'priority' => 100 + $order_currency[$cur]); } } foreach (get_all_commodity_currencies() as $cur) { if (isset($summaries[$cur])) { $result['summary']['total_' . $cur . '_daily'] = array('order' => $default_order['total_daily'] + $order_currency[$cur], 'source' => $cur, 'free' => $cur == $user['preferred_crypto'], 'priority' => 100 + $order_currency[$cur]); } } $result['currency'] = array(); $result['all_currency'] = array(); foreach (get_exchange_pairs() as $exchange => $pairs) { foreach ($pairs as $pair) { // we are interested in both of these currencies if (isset($summaries[$pair[0]]) && isset($summaries[$pair[1]])) { // and one of these currencies are a preferred currency if ($pair[0] == $user['preferred_crypto'] || $pair[0] == $user['preferred_fiat'] || $pair[1] == $user['preferred_crypto'] || $pair[1] == $user['preferred_fiat']) { // and we have a summary instance for this pair somewhere $possible_summaries = array('summary_' . $pair[0] . '_' . $exchange, 'summary_' . $pair[1] . '_' . $exchange); if (in_array($pair[0], get_all_cryptocurrencies())) { $possible_summaries[] = "summary_" . $pair[0]; } if (in_array($pair[1], get_all_cryptocurrencies())) { $possible_summaries[] = "summary_" . $pair[1]; } foreach ($possible_summaries as $p) { if (isset($all_summaries[$p])) { $is_default = is_fiat_currency($pair[0]) && get_default_currency_exchange($pair[0]) == $exchange || is_fiat_currency($pair[1]) && get_default_currency_exchange($pair[1]) == $exchange; $result['all_currency'][$exchange . "_" . $pair[0] . $pair[1] . "_daily"] = array('order' => $default_order['exchange_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'priority' => 150 + $order_currency[$pair[0]]); if ($is_default) { $result['currency'][$exchange . "_" . $pair[0] . $pair[1] . "_daily"] = array('order' => $default_order['exchange_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'free' => true, 'priority' => 150 + $order_currency[$pair[0]]); } // don't display all2btc etc if (!in_array(substr($p, strlen("summary_")), get_all_cryptocurrencies())) { $result['all_summary']['all2' . substr($p, strlen("summary_")) . '_daily'] = array('order' => $default_order['all_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'priority' => 50 + $order_currency[$pair[0]]); if ($is_default) { $result['summary']['all2' . substr($p, strlen("summary_")) . '_daily'] = array('order' => $default_order['all_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'free' => $pair[0] == $user['preferred_crypto'] || $pair[0] == $user['preferred_fiat'], 'priority' => 5 + $order_currency[$pair[0]]); } } break; } } } } } } $result['securities'] = array(); // no graphs to put in here yet... // TODO in the future: securities composition graphs? e.g. composition_litecoinglobal_daily $result['mining'] = array(); foreach (get_all_hashrate_currencies() as $cur) { if (isset($summaries[$cur])) { // we need to have at least one pool that supports reporting hashrate $has_hashing_account = false; foreach ($accounts['Mining pools'] as $key => $account) { if (!isset($wallets[$key])) { continue; } $instances = get_all_user_account_instances($key); if ($instances) { if (in_array('hash', $wallets[$key])) { $has_hashing_account = $key; } } } if (!$has_hashing_account) { continue; } $result['mining']["hashrate_" . $cur . "_daily"] = array('order' => $default_order['hashrate_daily'] + $order_currency[$cur], 'source' => $has_hashing_account, 'free' => $cur == $user['preferred_crypto'], 'priority' => 150 + $order_currency[$cur]); } } // all 'summary' are also 'all_summary' etc foreach ($result['summary'] as $key => $value) { $result['all_summary'][$key] = $value; } foreach ($result['currency'] as $key => $value) { $result['all_currency'][$key] = $value; } // go through each category and sort by order foreach ($result as $key => $value) { uasort($result[$key], '_sort_by_order_key'); } return $result; }
crypto_log("+ from BTC: " . $temp); $total += $temp; } } // other cryptocurrencies are converted first to BTC, and then to the given currency foreach (array_merge(get_all_cryptocurrencies(), get_all_commodity_currencies()) as $c) { if ($c == $currency || $c == 'btc') { continue; } // e.g. NMC to BTC if (isset($totals[$c])) { if ($ticker = get_latest_ticker(get_default_currency_exchange($c), "btc", $c)) { $temp = $totals[$c] * ($ticker['ask'] ? $ticker['ask'] : $ticker['last_trade']); crypto_log("+ from " . get_currency_abbr($c) . " (BTC): " . $temp); // and then BTC to CUR if ($ticker = get_latest_ticker(get_default_currency_exchange($currency), "btc", $currency)) { $temp2 = $temp / ($ticker['bid'] ? $ticker['bid'] : $ticker['last_trade']); crypto_log("+ from " . get_currency_abbr($c) . " (" . get_currency_abbr($currency) . "): " . $temp2); $total += $temp2; } } } } crypto_log("Total converted " . get_currency_abbr($currency) . " balance for user " . $job['user_id'] . ": " . $total); add_summary_instance($job, 'crypto2' . $currency, $total); } } crypto_log("</ul>"); } // update last_sum_job $q = db()->prepare("UPDATE user_properties SET last_sum_job=NOW() WHERE id=?");
<li> <input type="checkbox" name="exchanges[]" value="<?php echo htmlspecialchars($key); ?> " id="exchanges_<?php echo htmlspecialchars($key); ?> "<?php echo isset($summaries[$key]) ? " checked" : ""; ?> > <label for="exchanges_<?php echo htmlspecialchars($key); ?> " class="<?php echo get_default_currency_exchange($c) == $exchange ? "default-exchange" : ""; ?> "><?php echo htmlspecialchars(get_exchange_name($exchange)); ?> </label> </li> <?php } ?> </ul> </div> </div> </li> <?php }
/** * /api/v1/rates.json */ function api_get_all_rates($with_extra = true) { $rates = array(); foreach (get_all_currencies() as $cur1) { foreach (get_all_currencies() as $cur2) { if ($cur1 == $cur2) { continue; } $rate = -1; $exchange = array(); $pair = array(); if ($cur1 == "btc") { if (!is_fiat_currency($cur2)) { // btc/ltc and btc/ghs $exchange = get_default_currency_exchange($cur2); $ticker = get_latest_ticker($exchange, $cur1, $cur2); $rate = $ticker['last_trade'] == 0 ? 0 : 1 / $ticker['last_trade']; $pair = $cur1 . $cur2; } else { // btc/usd $exchange = get_default_currency_exchange($cur2); $ticker = get_latest_ticker($exchange, $cur2, $cur1); $rate = $ticker['last_trade']; $pair = $cur2 . $cur1; } } else { if ($cur2 == "btc") { if (!is_fiat_currency($cur1)) { // btc/ltc and btc/ghs $exchange = get_default_currency_exchange($cur1); $ticker = get_latest_ticker($exchange, $cur2, $cur1); $rate = $ticker['last_trade']; $pair = $cur2 . $cur1; } else { // btc/usd $exchange = get_default_currency_exchange($cur1); $ticker = get_latest_ticker($exchange, $cur1, $cur2); $rate = $ticker['last_trade'] == 0 ? 0 : 1 / $ticker['last_trade']; $pair = $cur1 . $cur2; } } else { // first have to convert to btc and then to the target currency if (!is_fiat_currency($cur1)) { // ltc/? and ghs/? $exchange = array(get_default_currency_exchange($cur1)); $ticker = get_latest_ticker($exchange[0], 'btc', $cur1); $rate = $ticker['last_trade']; $pair = array('btc' . $cur1); } else { // usd/? $exchange = array(get_default_currency_exchange($cur1)); $ticker = get_latest_ticker($exchange[0], $cur1, 'btc'); $rate = $ticker['last_trade'] == 0 ? 0 : 1 / $ticker['last_trade']; $pair = array($cur1 . 'btc'); } // and then to the second currency if (!is_fiat_currency($cur2)) { // ?/ltc and ?/ghs $exchange[] = get_default_currency_exchange($cur2); $ticker = get_latest_ticker($exchange[1], 'btc', $cur2); $rate = $ticker['last_trade'] == 0 ? 0 : $rate / $ticker['last_trade']; $pair[] = 'btc' . $cur2; } else { // ?/usd $exchange[] = get_default_currency_exchange($cur2); $ticker = get_latest_ticker($exchange[1], $cur2, 'btc'); $rate = $rate * $ticker['last_trade']; $pair[] = $cur2 . 'btc'; } } } $rates[$cur1 . '_' . $cur2] = array('rate' => $rate, 'exchanges' => is_array($exchange) ? get_exchange_name($exchange[0]) . " and " . get_exchange_name($exchange[1]) : get_exchange_name($exchange)); if (is_array($exchange)) { $rates[$cur1 . '_' . $cur2]['exchange1'] = get_exchange_name($exchange[0]); $rates[$cur1 . '_' . $cur2]['exchange2'] = get_exchange_name($exchange[1]); if ($with_extra) { $rates[$cur1 . '_' . $cur2]['pair1'] = $pair[0]; $rates[$cur1 . '_' . $cur2]['pair2'] = $pair[1]; $rates[$cur1 . '_' . $cur2]['url1'] = absolute_url(url_for('historical', array('id' => $exchange[0] . '_' . $pair[0] . '_daily'))); // TODO add analytics $rates[$cur1 . '_' . $cur2]['url2'] = absolute_url(url_for('historical', array('id' => $exchange[0] . '_' . $pair[1] . '_daily'))); } } else { $rates[$cur1 . '_' . $cur2]['exchange1'] = get_exchange_name($exchange); if ($with_extra) { $rates[$cur1 . '_' . $cur2]['pair1'] = $pair; $rates[$cur1 . '_' . $cur2]['url1'] = absolute_url(url_for('historical', array('id' => $exchange . '_' . $pair . '_daily'))); // TODO add analytics } } } } return $rates; }