From be2a458db7599bc79da6970f252f3412d96663e3 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Fri, 16 Jun 2017 16:47:12 +0100 Subject: [PATCH] added price history handling and storage --- sql/lbryexplorer.ddl.sql | 12 +++- src/Model/Entity/PriceHistory.php | 11 ++++ src/Model/Table/PriceHistoryTable.php | 16 ++++++ src/Shell/AuxShell.php | 82 +++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/Model/Entity/PriceHistory.php create mode 100644 src/Model/Table/PriceHistoryTable.php diff --git a/sql/lbryexplorer.ddl.sql b/sql/lbryexplorer.ddl.sql index d8bdd2c..c725079 100644 --- a/sql/lbryexplorer.ddl.sql +++ b/sql/lbryexplorer.ddl.sql @@ -212,7 +212,7 @@ CREATE TABLE `Claims` INDEX `Idx_ClaimTitle` (`Title`(191)) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; -CREATE TABLE ClaimStreams +CREATE TABLE `ClaimStreams` ( `Id` BIGINT UNSIGNED NOT NULL, `Stream` MEDIUMTEXT NOT NULL, @@ -220,5 +220,15 @@ CREATE TABLE ClaimStreams FOREIGN KEY `PK_ClaimStreamClaim` (`Id`) REFERENCES `Claims` (`Id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; +CREATE TABLE `PriceHistory` +( + `Id` SERIAL, + `BTC` DECIMAL(18,8) DEFAULT 0 NOT NULL, + `USD` DECIMAL(18,2) DEFAULT 0 NOT NULL, + `Created` DATETIME NOT NULL, + PRIMARY KEY `PK_PriceHistory` (`Id`), + UNIQUE KEY `Idx_PriceHistoryCreated` (`Created`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; + ALTER TABLE Claims ADD COLUMN Fee DECIMAL(18,8) DEFAULT 0 NOT NULL AFTER Title; ALTER TABLE Claims ADD COLUMN FeeCurrency CHAR(3) AFTER Fee; \ No newline at end of file diff --git a/src/Model/Entity/PriceHistory.php b/src/Model/Entity/PriceHistory.php new file mode 100644 index 0000000..2d0f333 --- /dev/null +++ b/src/Model/Entity/PriceHistory.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/src/Model/Table/PriceHistoryTable.php b/src/Model/Table/PriceHistoryTable.php new file mode 100644 index 0000000..1b9fe02 --- /dev/null +++ b/src/Model/Table/PriceHistoryTable.php @@ -0,0 +1,16 @@ +primaryKey('Id'); + $this->table('PriceHistory'); + } +} + +?> \ No newline at end of file diff --git a/src/Shell/AuxShell.php b/src/Shell/AuxShell.php index 98d003b..692127f 100644 --- a/src/Shell/AuxShell.php +++ b/src/Shell/AuxShell.php @@ -10,6 +10,12 @@ use Mdanter\Ecc\EccFactory; class AuxShell extends Shell { + const bittrex = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LBC'; + + const blockchainticker = 'https://blockchain.info/ticker'; + + const lbcpricekey = 'lbc.price'; + const pubKeyAddress = [0, 85]; const scriptAddress = [5, 122]; @@ -116,6 +122,62 @@ class AuxShell extends Shell { self::unlock('auxverify'); } + public function pricehistory() { + self::lock('pricehistory'); + + $conn = ConnectionManager::get('default'); + $redis = new \Predis\Client('tcp://127.0.0.1:6379'); + + try { + // Only allow 5-minute update intervals + $stmt = $conn->execute('SELECT MAX(Created) AS LastUpdate FROM PriceHistory'); + $res = $stmt->fetch(\PDO::FETCH_OBJ); + + $now = new \DateTime('now', new \DateTimeZone('UTC')); + if ($res && strlen(trim($res->LastUpdate)) > 0) { + $dt = new \DateTime($res->LastUpdate, new \DateTimeZone('UTC')); + $diff = $now->diff($dt); + $diffMinutes = $diff->i; + if ($diffMinutes < 5) { + echo "Last update is less than 5 minutes ago. Quitting.\n"; + self::unlock('pricehistory'); + return; + } + } + + $btrxjson = json_decode(self::curl_get(self::bittrex)); + $blckjson = json_decode(self::curl_get(self::blockchainticker)); + + if ($btrxjson->success) { + $btc = $btrxjson->result->Bid; + $usd = 0; + if (isset($blckjson->USD)) { + $usd = $btc * $blckjson->USD->buy; + $priceInfo = new \stdClass(); + $priceInfo->price = number_format($usd, 2, '.', ''); + $priceInfo->time = $now->format('c'); + if ($redis) { + $redis->set(self::lbcpricekey, json_encode($priceInfo)); + } + + // save the price history if both prices are > 0 + if ($usd > 0 && $btc > 0) { + $conn->execute('INSERT INTO PriceHistory (USD, BTC, Created) VALUES (?, ?, UTC_TIMESTAMP())', [$usd, $btc]); + echo "Inserted price history item. USD: $usd, BTC: $btc.\n"; + } else { + echo "Could not insert price history item. USD: $usd, BTC: $btc.\n"; + } + } + } else { + echo "bittrex requrest returned an invalid result.\n"; + } + } catch (\Exception $e) { + print_r($e); + } + + self::unlock('pricehistory'); + } + public static function lock($process_name) { if (!is_dir(TMP . 'lock')) { mkdir(TMP . 'lock'); @@ -135,6 +197,26 @@ class AuxShell extends Shell { } return true; } + + public static function curl_get($url) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + if ($response === false) { + $error = curl_error($ch); + $errno = curl_errno($ch); + curl_close($ch); + + throw new \Exception(sprintf('The request failed: %s', $error), $errno); + } else { + curl_close($ch); + } + + return $response; + } } ?>