function cryptostocks_api($key, $email, $method) { $token_id = rand(0, 0xffff); $request_time = time(); $token = $key . ";" . $email . ";" . $request_time . ";" . $token_id . ";" . $method; $url = url_add("https://cryptostocks.com/api/" . urlencode($method) . ".json", array('account' => $email, 'id' => $token_id, 'token' => hash('sha256', $token), 'timestamp' => $request_time)); $content = crypto_get_contents(crypto_wrap_url($url)); if (!$content) { throw new ExternalAPIException("API returned empty data"); } $data = crypto_json_decode($content); return $data; }
<?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 /** * 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);
<?php /** * Generic API job. */ $exchange = "generic"; // get the relevant address $q = db()->prepare("SELECT * FROM accounts_generic 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']); } $balance = crypto_get_contents(crypto_wrap_url($account['api_url'])); if (!is_numeric($balance)) { crypto_log("{$exchange} balance for " . htmlspecialchars($account['api_url']) . " is non-numeric: " . htmlspecialchars($balance)); throw new ExternalAPIException("Generic API returned non-numeric balance: " . htmlspecialchars(substr($balance, 0, 100))); } else { // issue #11: add editable multiplier crypto_log("{$exchange} balance: {$balance} * " . $account['multiplier']); $balance = $balance * $account['multiplier']; crypto_log("{$exchange} balance for " . htmlspecialchars($account['api_url']) . ": " . $balance); } insert_new_balance($job, $account, $exchange, $account['currency'], $balance);
<?php /** * Openclerk version check job. */ $exchange = "version_check"; crypto_log("Local version: " . get_site_config('openclerk_version')); // call cryptfolio.com to find the latest openclerk.org version $version = crypto_get_contents(crypto_wrap_url(url_add('https://cryptfolio.com/version', array('absolute_url' => get_site_config('absolute_url'), 'openclerk_version' => get_site_config('openclerk_version'))))); crypto_log("Remote version: " . $version); if (!$version) { throw new ExternalAPIException("Could not retrieve remote Openclerk version"); } // compare if (version_compare($version, get_site_config('openclerk_version')) > 0) { // delete any unread messages $q = db()->prepare("DELETE FROM admin_messages WHERE message_type=? AND is_read=0"); $q->execute(array('version_check')); // and insert a new one $q = db()->prepare("INSERT INTO admin_messages SET message_type=?, message=?"); $q->execute(array('version_check', '<a href="http://openclerk.org">A new version</a> of Openclerk is available: ' . $version)); crypto_log("Inserted new admin_message."); }
$account = $q->fetch(); if (!$account) { throw new JobException("Cannot find a {$exchange} account " . $job['arg_id'] . " for user " . $job['user_id']); } // first, get balances $data = crypto_json_decode(crypto_get_contents(crypto_wrap_url('https://www.litecoininvest.com/api/act?key=' . urlencode($account['api_key']))), false, true); if (!isset($data['balance'][get_currency_abbr($currency)])) { throw new ExternalAPIException("No " . get_currency_abbr($currency) . " balance found"); } $wallet = $data['balance'][get_currency_abbr($currency)]; // -- and now get securities -- // set is_recent=0 for all old security instances for this user $q = db()->prepare("UPDATE securities SET is_recent=0 WHERE user_id=? AND exchange=? AND account_id=?"); $q->execute(array($job['user_id'], $exchange, $account['id'])); $balance = 0; $data = crypto_json_decode(crypto_get_contents(crypto_wrap_url('https://www.litecoininvest.com/api/private/portfolio?key=' . urlencode($account['api_key']))), false, true); foreach ($data as $row) { $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());
<?php /** * A batch script to get all current Litecoininvest securities and queue them up for ticker values, * 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());
<?php /** * A batch script to get all current Havelock securities and queue them up for ticker values, * so we will have historical data even if no user has the security yet. */ $exchange = "securities_havelock"; $currency = 'btc'; // get the API data $content = crypto_get_contents(crypto_wrap_url('https://www.havelockinvestments.com/r/tickerfull')); if (!$content) { throw new ExternalAPIException("API returned empty data"); } $json = json_decode($content, true); if (!$json) { throw new ExternalAPIException("JSON was invalid"); } foreach ($json as $security => $data) { // $data only has last price, so we'll let securities_havelock job deal with the bid/ask $q = db()->prepare("SELECT * FROM securities_havelock 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_havelock definition existed for '" . htmlspecialchars($security) . "': adding in new definition"); $q = db()->prepare("INSERT INTO securities_havelock SET name=?"); $q->execute(array($security)); $security_def = array('name' => $security, 'id' => db()->lastInsertId()); } $balance = $data['last'];
<?php /** * Cryptostocks securities value job. * Retrieves the current 'bid' value for a particular security. */ $exchange = "securities_cryptostocks"; // get the relevant security $q = db()->prepare("SELECT * FROM securities_cryptostocks 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']); } $content = crypto_get_contents(crypto_wrap_url('https://cryptostocks.com/api/get_security_info.json?ticker=' . urlencode($security['name']))); if (!$content) { throw new ExternalAPIException("API returned empty data"); } $data = crypto_json_decode($content); // we now have a new value $balance = $data['highest_bid']; if ($balance === "") { $balance = 0; } $currency = strtolower($data['currency']); // this lets us keep track of shares in currencies we don't support yet if (strlen($currency) != 3) { throw new ExternalAPIException("Currency {$currency} is not 3 characters long"); } // update this security definition with the currency $q = db()->prepare("UPDATE securities_cryptostocks SET currency=? WHERE id=?");