diff --git a/sql/lbryexplorer.ddl.sql b/sql/lbryexplorer.ddl.sql index 12719d5..d8bdd2c 100644 --- a/sql/lbryexplorer.ddl.sql +++ b/sql/lbryexplorer.ddl.sql @@ -192,6 +192,8 @@ CREATE TABLE `Claims` `Language` VARCHAR(20) CHARACTER SET latin1 COLLATE latin1_general_ci, `ThumbnailUrl` TEXT, `Title` TEXT, + `Fee` DECIMAL(18,8) DEFAULT 0 NOT NULL, + `FeeCurrency` CHAR(3), `Created` DATETIME NOT NULL, `Modified` DATETIME NOT NULL, @@ -216,4 +218,7 @@ CREATE TABLE ClaimStreams `Stream` MEDIUMTEXT NOT NULL, PRIMARY KEY `PK_ClaimStream` (`Id`), FOREIGN KEY `PK_ClaimStreamClaim` (`Id`) REFERENCES `Claims` (`Id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; \ No newline at end of file +) 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/Controller/MainController.php b/src/Controller/MainController.php index c6f99e3..efe75ab 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -111,9 +111,19 @@ class MainController extends AppController { $this->loadModel('Claims'); $this->loadModel('Transactions'); + $canConvert = false; + $priceInfo = json_decode($this->redis->get(self::lbcPriceKey)); + if (isset($priceInfo->price)) { + $canConvert = true; + } + if (!$id) { $claims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])->order(['Claims.Created' => 'DESC'])->limit(96)->toArray(); for ($i = 0; $i < count($claims); $i++) { + if ($canConvert && $claims[$i]->Fee > 0 && $claims[$i]->FeeCurrency == 'USD') { + $claims[$i]->Price = $claims[$i]->Fee * $priceInfo->price; + } + if (isset($claims[$i]->Stream)) { $json = json_decode($claims[$i]->Stream->Stream); if (isset($json->metadata->license)) { @@ -131,6 +141,10 @@ class MainController extends AppController { return $this->redirect('/'); } + if ($canConvert && $claim->Fee > 0 && $claim->FeeCurrency == 'USD') { + $claim->Price = $claim->Fee * $priceInfo->price; + } + if (isset($claim->Stream)) { $json = json_decode($claim->Stream->Stream); if (isset($json->metadata->license)) { @@ -146,8 +160,12 @@ class MainController extends AppController { // find more claims for the publisher $moreClaims = $this->Claims->find()->contain(['Stream', 'Publisher' => ['fields' => ['Name']]])-> where(['Claims.ClaimType' => 2, 'Claims.Id <>' => $claim->Id, 'Claims.PublisherId' => isset($claim->Publisher) ? $claim->Publisher->ClaimId : $claim->ClaimId])-> - limit(9)->order(['RAND()' => 'DESC'])->toArray(); + limit(9)->order(['Claims.Fee' => 'DESC'])->toArray(); for ($i = 0; $i < count($moreClaims); $i++) { + if ($canConvert && $moreClaims[$i]->Fee > 0 && $moreClaims[$i]->FeeCurrency == 'USD') { + $moreClaims[$i]->Price = $moreClaims[$i]->Fee * $priceInfo->price; + } + if (isset($moreClaims[$i]->Stream)) { $json = json_decode($moreClaims[$i]->Stream->Stream); if (isset($json->metadata->license)) { diff --git a/src/Shell/BlockShell.php b/src/Shell/BlockShell.php index 8c6f82a..76fb123 100644 --- a/src/Shell/BlockShell.php +++ b/src/Shell/BlockShell.php @@ -48,6 +48,30 @@ class BlockShell extends Shell { return $string; } + public function updateclaimfees() { + self::lock('claimfees'); + + $conn = ConnectionManager::get('default'); + try { + $stmt = $conn->execute('SELECT CS.Id, CS.Stream FROM ClaimStreams CS JOIN Claims C ON C.Id = CS.Id WHERE C.Fee = 0 AND C.Id <= 11462 ORDER BY Id ASC'); + $claims = $stmt->fetchAll(\PDO::FETCH_OBJ); + foreach ($claims as $claim) { + $stream = json_decode($claim->Stream); + if (isset($stream->metadata->fee) && $stream->metadata->fee->amount > 0) { + $fee = $stream->metadata->fee->amount; + $currency = $stream->metadata->fee->currency; + + $conn->execute('UPDATE Claims SET Fee = ?, FeeCurrency = ? WHERE Id = ?', [$fee, $currency, $claim->Id]); + echo "Updated fee for claim ID: $claim->Id. Fee: $fee, Currency: $currency\n"; + } + } + } catch (\Exception $e) { + print_r($e); + } + + self::unlock('claimfees'); + } + public function buildclaimindex() { self::lock('buildindex'); @@ -58,11 +82,13 @@ class BlockShell extends Shell { $redis_key = 'claim.oid'; $last_claim_oid = $redis->exists($redis_key) ? $redis->get($redis_key) : 0; try { - $stmt = $conn->execute('SELECT COUNT(Id) AS RecordCount FROM Outputs WHERE Id > ? AND TransactionId <= 1776540', [$last_claim_oid]); + $stmt = $conn->execute('SELECT COUNT(Id) AS RecordCount FROM Outputs WHERE Id > ?', [$last_claim_oid]); $count = min(500000, $stmt->fetch(\PDO::FETCH_OBJ)->RecordCount); $idx = 0; - $stmt = $conn->execute('SELECT O.Id, O.TransactionId, O.Vout, O.ScriptPubKeyAsm, T.Hash, IFNULL(T.TransactionTime, T.CreatedTime) AS TxTime FROM Outputs O JOIN Transactions T ON T.Id = O.TransactionId WHERE O.Id > ? AND O.TransactionId <= 1776540 ORDER BY O.Id ASC LIMIT 500000', [$last_claim_oid]); + $stmt = $conn->execute('SELECT O.Id, O.TransactionId, O.Vout, O.ScriptPubKeyAsm, T.Hash, IFNULL(T.TransactionTime, T.CreatedTime) AS TxTime FROM Outputs O ' . + 'JOIN Transactions T ON T.Id = O.TransactionId WHERE O.Id > ? ORDER BY O.Id ASC LIMIT 500000', + [$last_claim_oid]); while ($out = $stmt->fetch(\PDO::FETCH_OBJ)) { $idx++; $idx_str = str_pad($idx, strlen($count), '0', STR_PAD_LEFT); @@ -71,6 +97,14 @@ class BlockShell extends Shell { $vout = $out->Vout; if (strpos($out->ScriptPubKeyAsm, 'OP_CLAIM_NAME') !== false) { + // check if the claim already exists in the claims table + $stmt2 = $conn->execute('SELECT Id FROM Claims WHERE TransactionHash = ? AND Vout = ?', [$out->Hash, $out->Vout]); + $exist_claim = $stmt2->fetch(\PDO::FETCH_OBJ); + if ($exist_claim) { + echo "[$idx_str/$count] claim already exists for [$out->Hash:$vout]. Skipping.\n"; + continue; + } + $asm_parts = explode(' ', $out->ScriptPubKeyAsm, 4); $name_hex = $asm_parts[1]; $claim_name = @pack('H*', $name_hex); @@ -123,6 +157,8 @@ class BlockShell extends Shell { 'Author' => isset($claim->stream->metadata->author) ? $claim->stream->metadata->author : null, 'ThumbnailUrl' => isset($claim->stream->metadata->thumbnail) ? $claim->stream->metadata->thumbnail : null, 'IsNSFW' => isset($claim->stream->metadata->nsfw) ? $claim->stream->metadata->nsfw : 0, + 'Fee' => isset($claim->stream->metadata->fee) ? $claim->stream->metadata->fee->amount : 0, + 'FeeCurrency' => isset($claim->stream->metadata->fee) ? $claim->stream->metadata->fee->currency : 0, 'Created' => $tx_dt->format('Y-m-d H:i:s'), 'Modified' => $tx_dt->format('Y-m-d H:i:s') ]; @@ -245,6 +281,8 @@ class BlockShell extends Shell { 'Author' => isset($claim->stream->metadata->author) ? $claim->stream->metadata->author : null, 'ThumbnailUrl' => isset($claim->stream->metadata->thumbnail) ? $claim->stream->metadata->thumbnail : null, 'IsNSFW' => isset($claim->stream->metadata->nsfw) ? $claim->stream->metadata->nsfw : 0, + 'Fee' => isset($claim->stream->metadata->fee) ? $claim->stream->metadata->fee->amount : 0, + 'FeeCurrency' => isset($claim->stream->metadata->fee) ? $claim->stream->metadata->fee->currency : 0, 'Created' => $tx_dt->format('Y-m-d H:i:s'), 'Modified' => $tx_dt->format('Y-m-d H:i:s') ]; diff --git a/src/Template/Main/claims.ctp b/src/Template/Main/claims.ctp index 6138ba5..9b4347c 100644 --- a/src/Template/Main/claims.ctp +++ b/src/Template/Main/claims.ctp @@ -50,6 +50,13 @@ if ($claim->ClaimType == 1) { $autoThumbText = strtoupper(substr($claim->Name, 1 $autoThumbText = strtoupper(substr($str, 0, min( strlen($str), 10 ))); } +$cost = 'Free'; +if (isset($claim->Price) && $claim->Price > 0) { + $cost = $this->Amount->formatCurrency($claim->Price) . ' LBC'; +} else if (isset($claim->Fee) && strtolower($claim->FeeCurrency) === 'lbc') { + $cost = $this->Amount->formatCurrency($claim->Price) . ' LBC'; +} + $desc = $claim->Description; if (strlen(trim($desc)) == 0) { $desc = 'No description available.'; @@ -96,8 +103,13 @@ if (strlen(trim($desc)) == 0) {
ClaimType == 2): ?> -