function getJSON($arguments) { // important for url_for() links define('FORCE_NO_RELATIVE', true); $result = array(); $result['currencies'] = array(); foreach (get_all_currencies() as $cur) { $result['currencies'][] = array('code' => $cur, 'abbr' => get_currency_abbr($cur), 'name' => get_currency_name($cur), 'fiat' => is_fiat_currency($cur)); } require __DIR__ . "/../../inc/api.php"; $result['rates'] = api_get_all_rates(); return $result; }
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; }
public function getData($days) { $key_column = array('type' => 'string', 'title' => ct("Currency")); $columns = array(); // get all balances $balances = get_all_summary_instances($this->getUser()); $last_updated = find_latest_created_at($balances, "total"); // and convert them using the most recent rates $rates = get_all_recent_rates(); // create data // TODO refactor this into generic any-currency balances $data = array(); if (isset($balances['totalbtc']) && $balances['totalbtc']['balance'] != 0) { $columns[] = array('type' => 'number', 'title' => get_currency_abbr('btc')); $data[] = graph_number_format(demo_scale($balances['totalbtc']['balance'])); } foreach (get_all_currencies() as $cur) { // if the key is a currency, use the same currency colour across all graphs (#293) $color = array_search($cur, get_all_currencies()); if ($cur == 'btc') { continue; } if (!is_fiat_currency($cur) && isset($balances['total' . $cur]) && $balances['total' . $cur]['balance'] != 0 && isset($rates['btc' . $cur])) { $columns[] = array('type' => 'number', 'title' => get_currency_abbr($cur), 'color' => $color); $data[] = graph_number_format(demo_scale($balances['total' . $cur]['balance'] * $rates['btc' . $cur]['bid'])); } if (is_fiat_currency($cur) && isset($balances['total' . $cur]) && $balances['total' . $cur]['balance'] != 0 && isset($rates[$cur . 'btc']) && $rates[$cur . 'btc']['ask']) { $columns[] = array('type' => 'number', 'title' => get_currency_abbr($cur), 'color' => $color); $data[] = graph_number_format(demo_scale($balances['total' . $cur]['balance'] / $rates[$cur . 'btc']['ask'])); } } // display a helpful message if there's no data if (!$data) { throw new NoDataGraphException_AddAccountsAddresses(); } // sort data by balance arsort($data); $data = array(get_currency_abbr('btc') => $data); return array('key' => $key_column, 'columns' => $columns, 'data' => $data, 'last_updated' => $last_updated); }
/** * 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; }
$exchange = $bits[2]; if (!$exchange) { throw new JobException("Invalid summary exchange '{$exchange}'"); } $total = 0; // BTC is converted at the exchange's last sell rate // fail if there is no current rate (otherwise there is no point of this job, we don't want erraneous zero balances) if ($ticker = get_latest_ticker($exchange, $currency, "btc")) { $total += $crypto2btc * ($ticker['ask'] ? $ticker['ask'] : $ticker['last_trade']); crypto_log("From converted BTC: " . $total); } else { throw new JobException("There is no recent ticker balance for {$currency}/btc on {$exchange} - cannot convert"); } // issue #112: also add all other fiat balances that are not this current currency foreach ($totals as $cur => $fiat_total) { if (is_fiat_currency($cur)) { if ($cur == $currency) { // add total FIAT balances calculated earlier $total += $totals[$currency]; crypto_log("From fiat currency: " . $totals[$currency]); } elseif (isset($equivalent_btc[$cur])) { // we need to calculate the equivalent exchange rate if ($ticker = get_latest_ticker($exchange, $currency, "btc")) { $temp = $equivalent_btc[$cur] * ($ticker['bid'] ? $ticker['bid'] : $ticker['last_trade']); crypto_log("From equivalent " . get_currency_abbr($cur) . " in BTC: " . $temp); $total += $temp; } else { throw new JobException("There is no recent ticker balance for {$cur}/btc on {$exchange} - cannot convert"); } } }
} if (!$account_title && $account_full && isset($account_full['address']) && $account_full['address']) { $account_title = $account_full['address']; } } $transactions[$id]['exchange_name'] = $account; $transactions[$id]['account_title'] = $account_title; // get rates for that day if ($page_args['include_rates']) { foreach (array(1, 2) as $index) { if ($transaction['currency' . $index] && $transaction['value' . $index]) { if ($transaction['currency' . $index] == 'btc') { $currency1 = 'usd'; $currency2 = 'btc'; } else { if (is_fiat_currency($transaction['currency' . $index])) { $currency1 = $transaction['currency' . $index]; $currency2 = 'btc'; } else { $currency1 = 'btc'; $currency2 = $transaction['currency' . $index]; } } $exchange = 'average'; $key = $exchange . "_" . $currency1 . "_" . $currency2 . "_" . $transaction['transaction_date_day']; if (!isset($rates[$key])) { $args = array('transaction_date_day' => $transaction['transaction_date_day'], 'currency1' => $currency1, 'currency2' => $currency2, 'exchange' => $exchange); // try recent data $q = db()->prepare("SELECT * FROM ticker WHERE created_at_day=:transaction_date_day AND currency1=:currency1 AND currency2=:currency2 AND exchange=:exchange AND is_daily_data=1 LIMIT 1"); $q->execute($args); $rate = $q->fetch();
require __DIR__ . "/../graphs/managed.php"; // get all of our limits $accounts = user_limits_summary(user_id()); $preferred_crypto = require_post("preferred_crypto", false); $preferred_fiat = require_post("preferred_fiat", false); $preference = require_post("preference"); $managed = require_post("managed", array()); $categories = get_managed_graph_categories(); // checks if ($preference == "managed" && !$managed) { $errors[] = t("You need to select at least one category of graph portfolio preferences."); } if (!in_array($preferred_crypto, get_all_cryptocurrencies())) { $errors[] = t("Invalid preferred cryptocurrency."); } if (!is_fiat_currency($preferred_fiat)) { $errors[] = t("Invalid preferred fiat currency."); } if (!in_array($preference, array('auto', 'managed', 'none'))) { $errors[] = t("Invalid graph management preference."); } if ($preference != "none" && !$preferred_fiat) { $errors[] = t("You need to select at least :one_currency in order to use managed graphs.", array(':one_currency' => link_to(url_for('wizard_currencies'), t("one fiat currency")))); } if ($preference != "none" && !$preferred_crypto) { $errors[] = t("You need to select at least :one_currency in order to use managed graphs.", array(':one_currency' => link_to(url_for('wizard_currencies'), t("one cryptocurrency")))); } foreach ($managed as $m) { if (!isset($categories[$m])) { $errors[] = "'" . htmlspecialchars($m) . "' is not a valid graph portfolio preference."; }
/** * /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; }