Exemple #1
0
<?php

/**
 * Ticker check for notifications.
 * Finds out the changed values for a particular ticker.
 */
// get the most recent value
$ticker = get_latest_ticker($account['exchange'], $account['currency1'], $account['currency2']);
if (!$ticker) {
    // TODO maybe support failing for notifications, to disable notifications for e.g. accounts that no longer exist?
    // probably better to make sure that we can never *have* a referenced account that never exists
    throw new JobException("Could not find any recent ticker values for " . $account['exchange'] . " " . $account['currency1'] . "/" . $account['currency2']);
}
// TODO currently only supports last_trade, could also support buy/sell/volume
$current_value = $ticker['last_trade'];
// what was the last value?
// may need to generate this if no value exists, but hopefully this only occurs very rarely,
// since this may be a very heavy query
if ($notification['last_value'] === null) {
    crypto_log("No last value found: retrieving");
    // get the query string for this interval
    $periods = get_permitted_notification_periods();
    if (!isset($periods[$notification['period']]['interval'])) {
        throw new JobException("Unknown job period '" . $notification['period'] . "'");
    }
    $period = $periods[$notification['period']]['interval'];
    $q = db()->prepare("SELECT * FROM ticker WHERE exchange=:exchange AND currency1=:currency1 AND currency2=:currency2 AND created_at <= DATE_SUB(NOW(), {$period}) ORDER BY id DESC LIMIT 1");
    $q->execute(array("exchange" => $account['exchange'], "currency1" => $account['currency1'], "currency2" => $account['currency2']));
    $last = $q->fetch();
    if (!$last) {
        throw new JobException("Could not find any last values for " . $account['exchange'] . " " . $account['currency1'] . "/" . $account['currency2']);
Exemple #2
0
                    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=?");
Exemple #3
0
/**
 * /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;
}