<?php

/**
 * Crypto-Trade securities ticker job.
 */
$exchange = "securities_crypto-trade";
// get the relevant security
$q = db()->prepare("SELECT * FROM securities_cryptotrade WHERE id=?");
$q->execute(array($job['arg_id']));
$security = $q->fetch();
if (!$security) {
    throw new JobException("Cannot find a {$exchange} security " . $job['arg_id'] . " for user " . $job['user_id']);
}
$cur1 = $security['currency'];
$cur2 = strtolower($security['name']);
$rates = crypto_json_decode(crypto_get_contents(crypto_wrap_url("https://crypto-trade.com/api/1/ticker/" . $cur2 . "_" . $cur1)));
if (!isset($rates['data']['max_bid'])) {
    if (isset($rates['error'])) {
        throw new ExternalAPIException("Could not find {$cur1}/{$cur2} rate for {$exchange}: " . htmlspecialchars($rates['error']));
    }
    throw new ExternalAPIException("No {$cur1}/{$cur2} rate for {$exchange}");
}
crypto_log("Security {$cur1}/{$cur2} balance: " . $rates['data']['max_bid']);
// insert new balance
insert_new_balance($job, $security, 'securities_crypto-trade', $security['currency'], $rates['data']['max_bid']);
<?php

/**
 * Individual Crypto-Trade securities job.
 */
$exchange = "individual_crypto-trade";
// get the relevant address
$q = db()->prepare("SELECT * FROM accounts_individual_cryptotrade WHERE user_id=? AND id=?");
$q->execute(array($job['user_id'], $job['arg_id']));
$account = $q->fetch();
if (!$account) {
    throw new JobException("Cannot find a {$exchange} account " . $job['arg_id'] . " for user " . $job['user_id']);
}
// get the most recent ticker balance for this security
$q = db()->prepare("SELECT * FROM balances WHERE exchange=? AND account_id=? AND is_recent=1 LIMIT 1");
$q->execute(array('securities_crypto-trade', $account['security_id']));
$ticker = $q->fetch();
if (!$ticker) {
    throw new ExternalAPIException("Could not find any recent ticker balance for securities_crypto-trade ID=" . htmlspecialchars($account['security_id']));
} else {
    $calculated = $ticker['balance'] * $account['quantity'];
    crypto_log('security ' . htmlspecialchars($account['security_id']) . " @ " . htmlspecialchars($ticker['balance']) . " x " . number_format($account['quantity']) . " = " . htmlspecialchars($calculated));
    insert_new_balance($job, $account, $exchange, $ticker['currency'], $calculated);
}
示例#3
0
// also get all supported securities
$q = db()->prepare("SELECT * FROM securities_796");
$q->execute();
$securities = $q->fetchAll();
$securities_balance = 0;
foreach ($securities as $security_def) {
    if (isset($balance[$security_def['name']])) {
        // get the latest balance
        // the 'balance' for this security is the 'bid'
        $q = db()->prepare("SELECT * FROM balances WHERE exchange=:exchange AND account_id=:account_id AND is_recent=1 LIMIT 1");
        $q->execute(array("exchange" => "securities_796", "account_id" => $security_def['id']));
        $security_value = $q->fetch();
        if (!$security_value) {
            // we can't calculate the value of this security yet
            crypto_log("Security " . htmlspecialchars($security_def['name']) . " does not yet have a calculated value");
        } else {
            $calculated = $security_value['balance'] * $balance[$security_def['name']];
            crypto_log(htmlspecialchars($security_def['name']) . " @ " . htmlspecialchars($security_value['balance']) . " x " . number_format($balance[$security_def['name']]) . " = " . htmlspecialchars($calculated));
            $securities_balance += $calculated;
        }
        // insert security instance
        // but only if we actually have a quantity
        if ($balance[$security_def['name']] != 0) {
            $q = db()->prepare("INSERT INTO securities SET user_id=:user_id, exchange=:exchange, security_id=:security_id, quantity=:quantity, account_id=:account_id, is_recent=1");
            $q->execute(array('user_id' => $job['user_id'], 'exchange' => $exchange, 'security_id' => $security_def['id'], 'quantity' => $balance[$security_def['name']], 'account_id' => $account['id']));
        }
    }
}
// we've now calculated the value of all securities too
insert_new_balance($job, $account, $exchange . "_securities", $currency, $securities_balance);
示例#4
0
<?php

/**
 * 796 security value job.
 * Retrieves the current 'bid' value for a particular security.
 */
$exchange = "securities_796";
$currency = 'btc';
// get the relevant address
$q = db()->prepare("SELECT * FROM securities_796 WHERE id=?");
$q->execute(array($job['arg_id']));
$account = $q->fetch();
if (!$account) {
    throw new JobException("Cannot find a {$exchange} account " . $job['arg_id']);
}
$content = crypto_get_contents(crypto_wrap_url('http://api.796.com/v3/stock/ticker.html?type=' . urlencode($account['api_name'])));
if (!$content) {
    throw new ExternalAPIException("API returned empty data");
}
$data = crypto_json_decode($content);
// also available: last, high, low, vol, buy, sell
if (!isset($data['ticker']['last'])) {
    throw new ExternalAPIException("External API returned no last price");
}
$balance = $data['ticker']['last'];
crypto_log("Last price for " . htmlspecialchars($account['name']) . ": " . $balance);
insert_new_balance($job, $account, $exchange, $currency, $balance);
示例#5
0
    insert_new_address_balance($job, $address, $balance);
} else {
    if ($instance instanceof \Openclerk\Currencies\BlockBalanceableCurrency) {
        // get the most recent block count, to calculate confirmations
        $block = null;
        $q = db()->prepare("SELECT * FROM blockcount_" . $currency . " WHERE is_recent=1");
        $q->execute();
        if ($result = $q->fetch()) {
            $block = $result['blockcount'] - \Openclerk\Config::get($currency . "_confirmations", 6);
        }
        $balance = $instance->getBalanceAtBlock($address['address'], $block, $logger);
        insert_new_address_balance($job, $address, $balance);
    } else {
        // we can't do confirmations or block balances
        $balance = $instance->getBalance($address['address'], $logger);
        insert_new_address_balance($job, $address, $balance);
    }
}
if ($instance instanceof \Openclerk\Currencies\MultiBalanceableCurrency) {
    $balances = $instance->getMultiBalances($address['address'], $logger);
    foreach ($balances as $code => $balance) {
        if (in_array($code, get_all_currencies())) {
            if ($code != $currency) {
                // skip balances we've already inserted for this currency
                insert_new_balance($job, $address, 'ripple', $code, $balance);
            }
        } else {
            $logger->info("Unknown multi currency '{$code}'");
        }
    }
}
示例#6
0
        $query = array();
        $args = array();
        foreach ($data as $key => $value) {
            $query[] = $key . " = :" . $key;
            $args[$key] = $value;
        }
        $args["id"] = $this->account['id'];
        crypto_log("Self-updating table '{$table}'");
        $q = db()->prepare("UPDATE {$table} SET " . implode(", ", $query) . " WHERE id=:id");
        $q->execute($args);
    }
}
if ($instance instanceof \Account\SelfUpdatingAccount) {
    $callback = new SelfUpdatingAccountCallback($account, $table);
    $instance->registerAccountUpdateCallback(array($callback, 'callback'));
}
// normal balances
$balances = $instance->fetchBalances($account, $factory, $logger);
foreach ($balances as $currency => $balance) {
    // only store currencies we are actually interested in
    if (in_array($currency, \DiscoveredComponents\Currencies::getKeys())) {
        // some accounts, e.g. ghash, don't return a balance
        if (isset($balance['confirmed'])) {
            insert_new_balance($job, $account, $exchange, $currency, $balance['confirmed']);
        }
        // hashrate balances
        if (isset($balance['hashrate'])) {
            insert_new_hashrate($job, $account, $exchange, $currency, $balance['hashrate'] / 1000000.0);
        }
    }
}
示例#7
0
    $security = $row['ticker'];
    $bid = $row['bid'];
    // also available: avg_buy_price
    // make sure that a security definition exists
    $q = db()->prepare("SELECT * FROM securities_litecoininvest WHERE name=?");
    $q->execute(array($security));
    $security_def = $q->fetch();
    if (!$security_def) {
        // need to insert a new security definition, so we can later get its value
        // we can't calculate the value of this security yet
        crypto_log("No securities_litecoininvest definition existed for '" . htmlspecialchars($security) . "': adding in new definition");
        $q = db()->prepare("INSERT INTO securities_litecoininvest SET name=?");
        $q->execute(array($security));
        $security_def = array('id' => db()->lastInsertId());
    }
    // insert in a new balance
    $job2 = $job;
    $job2['user_id'] = get_site_config('system_user_id');
    /* need to insert security values as system user, or else they won't be displayed in a graph! */
    insert_new_balance($job2, $security_def, "securities_" . $exchange, $currency, $bid);
    $calculated = $bid * $row['quantity'];
    crypto_log(htmlspecialchars($security) . " @ " . htmlspecialchars($bid) . " x " . number_format($row['quantity']) . " = " . htmlspecialchars($calculated));
    $balance += $calculated;
    // insert security instance
    $q = db()->prepare("INSERT INTO securities SET user_id=:user_id, exchange=:exchange, security_id=:security_id, quantity=:quantity, account_id=:account_id, is_recent=1");
    $q->execute(array('user_id' => $job['user_id'], 'exchange' => $exchange, 'security_id' => $security_def['id'], 'quantity' => $row['quantity'], 'account_id' => $account['id']));
}
// we've now calculated both the wallet balance + the value of all securities
insert_new_balance($job, $account, $exchange . '_wallet', $currency, $wallet);
insert_new_balance($job, $account, $exchange . '_securities', $currency, $balance);
示例#8
0
 * so we will have historical data even if no user has the security yet.
 */
$exchange = "securities_litecoininvest";
$currency = 'ltc';
// get the API data
$json = crypto_json_decode(crypto_get_contents(crypto_wrap_url('https://www.litecoininvest.com/api/ticker')));
foreach ($json as $security => $data) {
    crypto_log("Parsing security '" . htmlspecialchars($security) . "'");
    // we now have a new value
    $balance = $data['bid'];
    // also available: ask, latest, outstanding, 24h_vol, etc
    // if this security has a balance of 0, then it's worthless and it's not really
    // worth saving into the database
    if ($balance == 0) {
        crypto_log("Security '" . htmlspecialchars($security) . "' had a bid of 0: ignoring");
        continue;
    }
    $q = db()->prepare("SELECT * FROM securities_litecoininvest WHERE name=?");
    $q->execute(array($security));
    $security_def = $q->fetch();
    if (!$security_def) {
        // need to insert a new security definition, so we can later get its value
        // we can't calculate the value of this security yet
        crypto_log("No securities_litecoininvest definition existed for '" . htmlspecialchars($security) . "': adding in new definition");
        $q = db()->prepare("INSERT INTO securities_litecoininvest SET name=?");
        $q->execute(array($security));
        $security_def = array('name' => $security, 'id' => db()->lastInsertId());
    }
    // since we already have bid data here, we might as well save it for free
    insert_new_balance($job, $security_def, $exchange, $currency, $balance);
}