Merge pull request #77 from TheSerapher/ticket-cron

Ticket cron
This commit is contained in:
Sebastian Grewe 2013-05-29 02:04:19 -07:00
commit 43320a77d8
8 changed files with 114 additions and 15 deletions

36
cronjobs/tickerupdate.php Executable file
View File

@ -0,0 +1,36 @@
#!/usr/bin/php
<?php
/*
Copyright:: 2013, Sebastian Grewe
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Include all settings and classes
require_once('shared.inc.php');
// Include additional file not set in autoloader
require_once(BASEPATH . CLASS_DIR . '/tools.class.php');
verbose("Running ticket updates\n");
if ($aData = $tools->getApi($config['price']['url'], $config['price']['target'])) {
if (!$setting->setValue('price', $aData['ticker']['avg']))
verbose("ERR Table update failed");
} else {
verbose("ERR Failed download JSON data from " . $config['price']['url'].$config['price']['target'] . "\n");
}
?>

View File

@ -13,4 +13,4 @@ require_once(CLASS_DIR . '/share.class.php');
require_once(CLASS_DIR . '/worker.class.php');
require_once(CLASS_DIR . '/statistics.class.php');
require_once(CLASS_DIR . '/transaction.class.php');
require_once(CLASS_DIR . '/settings.class.php');
require_once(CLASS_DIR . '/setting.class.php');

View File

@ -4,7 +4,7 @@
if (!defined('SECURITY'))
die('Hacking attempt');
class Settings {
class Setting {
public function __construct($debug, $mysqli, $salt) {
$this->debug = $debug;
$this->mysqli = $mysqli;
@ -13,7 +13,7 @@ class Settings {
}
public function getValue($name) {
$query = $this->mysqli->prepare("SELECT value FROM $this->table WHERE setting=? LIMIT 1");
$query = $this->mysqli->prepare("SELECT value FROM $this->table WHERE name=? LIMIT 1");
if ($query) {
$query->bind_param('s', $name);
$query->execute();
@ -26,6 +26,19 @@ class Settings {
}
return $value;
}
public function setValue($name, $value) {
$stmt = $this->mysqli->prepare("
INSERT INTO $this->table (name, value)
VALUES (?, ?)
ON DUPLICATE KEY UPDATE value = ?
");
if ($stmt && $stmt->bind_param('sss', $name, $value, $value) && $stmt->execute())
return true;
$this->debug->append("Failed to set $name to $value");
echo $this->mysqli->error;
return false;
}
}
$settings = new Settings($debug, $mysqli, SALT);
$setting = new Setting($debug, $mysqli, SALT);

View File

@ -0,0 +1,41 @@
<?php
// Make sure we are called from index.php
if (!defined('SECURITY'))
die('Hacking attempt');
class Tools {
public function __construct($debug) {
$this->debug = $debug;
}
/**
* Fetch JSON data from an API
* @param url string API URL
* @param target string API method
* @param auth array Optional authentication data to be sent with
* @return dec array JSON decoded PHP array
**/
public function getApi($url, $target, $auth=NULL) {
static $ch = null;
static $ch = null;
if (is_null($ch)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
}
curl_setopt($ch, CURLOPT_URL, $url . $target);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// run the query
$res = curl_exec($ch);
if ($res === false) throw new Exception('Could not get reply: '.curl_error($ch));
$dec = json_decode($res, true);
if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
return $dec;
}
}
$tools = new Tools($debug);

View File

@ -21,11 +21,17 @@ define('DEBUG', 0);
define('SALT', 'PLEASEMAKEMESOMETHINGRANDOM');
$config = array(
'price' => array(
'url' => 'https://btc-e.com/api/2',
'target' => '/ltc_usd/ticker'
),
'website' => array(
'name' => 'The Pool',
'slogan' => 'Resistance is futile',
'email' => 'test@example.com', // Mail address used for notifications
),
'blockexplorer' => 'http://explorer.litecoin.net/search?q=', // URL for block searches, prefixed to each block number
'chaininfo' => 'http://allchains.info', // Link to Allchains for Difficulty information
'fees' => 0,
'difficulty' => '31', // Target difficulty for this pool as set in pushpoold json
'reward' => '50', // Reward for finding blocks, fixed value but changes someday

View File

@ -23,8 +23,9 @@ $aGlobal = array(
'fees' => $config['fees'],
'confirmations' => $config['confirmations'],
'reward' => $config['reward'],
'blockexplorer' => 'http://explorer.litecoin.net/search?q=',
'chaininfo' => 'http://allchains.info'
'price' => $setting->getValue('price'),
'blockexplorer' => $config['blockexplorer'],
'chaininfo' => $config['chaininfo']
);
// We don't want these session infos cached

View File

@ -4,6 +4,7 @@
<div id="ministats">
<table border="0">
<tr>
<td><li>LTC/usd: {$GLOBAL.price|default:"n/a"}&nbsp;&nbsp;&nbsp;&nbsp;</li></td>
<td><li>Pool Hashrate: {$GLOBAL.hashrate / 1000} MH/s&nbsp;&nbsp;&nbsp;&nbsp;</li></td>
<td><li>Pool Sharerate: {$GLOBAL.sharerate} Shares/s&nbsp;&nbsp;&nbsp;&nbsp;</li></td>
<td><li>Pool Workers: {$GLOBAL.workers}&nbsp;&nbsp;&nbsp;&nbsp;</li></td>

View File

@ -3,7 +3,7 @@
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: May 27, 2013 at 02:39 PM
-- Generation Time: May 29, 2013 at 11:00 AM
-- Server version: 5.5.31-0ubuntu0.13.04.1
-- PHP Version: 5.4.9-4ubuntu2
@ -64,7 +64,7 @@ CREATE TABLE IF NOT EXISTS `blocks` (
PRIMARY KEY (`id`),
UNIQUE KEY `height` (`height`,`blockhash`),
KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service';
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Discovered blocks persisted from Litecoin Service';
-- --------------------------------------------------------
@ -73,9 +73,10 @@ CREATE TABLE IF NOT EXISTS `blocks` (
--
CREATE TABLE IF NOT EXISTS `settings` (
`setting` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) DEFAULT NULL,
PRIMARY KEY (`setting`)
PRIMARY KEY (`name`),
UNIQUE KEY `setting` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
@ -98,7 +99,7 @@ CREATE TABLE IF NOT EXISTS `shares` (
KEY `upstream_result` (`upstream_result`),
KEY `our_result` (`our_result`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
@ -117,7 +118,7 @@ CREATE TABLE IF NOT EXISTS `shares_archive` (
PRIMARY KEY (`id`),
UNIQUE KEY `share_id` (`share_id`),
KEY `time` (`time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Archive shares for potential later debugging purposes';
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Archive shares for potential later debugging purposes';
-- --------------------------------------------------------
@ -134,7 +135,7 @@ CREATE TABLE IF NOT EXISTS `statistics_shares` (
PRIMARY KEY (`id`),
KEY `account_id` (`account_id`),
KEY `block_id` (`block_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
@ -154,7 +155,7 @@ CREATE TABLE IF NOT EXISTS `transactions` (
KEY `block_id` (`block_id`),
KEY `account_id` (`account_id`),
KEY `type` (`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
@ -171,7 +172,7 @@ CREATE TABLE IF NOT EXISTS `workers` (
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
KEY `account_id` (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;