diff --git a/config/routes.php b/config/routes.php index e34d2c1..4b74090 100644 --- a/config/routes.php +++ b/config/routes.php @@ -50,6 +50,7 @@ Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/claims/*', ['controller' => 'Main', 'action' => 'claims']); $routes->connect('/find', ['controller' => 'Main', 'action' => 'find']); $routes->connect('/realtime', ['controller' => 'Main', 'action' => 'realtime']); + $routes->connect('/stats', ['controller' => 'Main', 'action' => 'stats']); $routes->connect('/tx/*', ['controller' => 'Main', 'action' => 'tx']); $routes->connect('/qr/*', ['controller' => 'Main', 'action' => 'qr']); diff --git a/sql/lbryexplorer.ddl.sql b/sql/lbryexplorer.ddl.sql index 53f6ab4..10b8f69 100644 --- a/sql/lbryexplorer.ddl.sql +++ b/sql/lbryexplorer.ddl.sql @@ -73,6 +73,7 @@ CREATE TABLE `Addresses` `FirstSeen` DATETIME, `TotalReceived` DECIMAL(18,8) DEFAULT 0 NOT NULL, `TotalSent` DECIMAL(18,8) DEFAULT 0 NOT NULL, + `Balance` DECIMAL(18,8) AS (`TotalReceived` - `TotalSent`) PERSISTENT, `Tag` VARCHAR(30) NOT NULL, `TagUrl` VARCHAR(200), `Created` DATETIME NOT NULL, @@ -82,6 +83,7 @@ CREATE TABLE `Addresses` UNIQUE KEY `Idx_AddressTag` (`Tag`), INDEX `Idx_AddressTotalReceived` (`TotalReceived`), INDEX `Idx_AddressTotalSent` (`TotalSent`), + INDEX `Idx_AddressBalance` (`Balance`), INDEX `Idx_AddressCreated` (`Created`), INDEX `Idx_AddressModified` (`Modified`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; @@ -232,4 +234,6 @@ CREATE TABLE `PriceHistory` 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 UNIQUE KEY `Idx_ClaimUnique` (`TransactionHash`, `Vout`, `ClaimId`); \ No newline at end of file +ALTER TABLE Claims ADD UNIQUE KEY `Idx_ClaimUnique` (`TransactionHash`, `Vout`, `ClaimId`); +ALTER TABLE Addresses ADD COLUMN `Balance` DECIMAL(18,8) AS (`TotalReceived` - `TotalSent`) PERSISTENT AFTER `TotalSent`; +ALTER TABLE Addresses ADD INDEX `Idx_AddressBalance` (`Balance`); \ No newline at end of file diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index 2be899c..040bae3 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -491,6 +491,37 @@ class MainController extends AppController { $this->set('sourceAddress', $sourceAddress); } + public function stats() { + $this->loadModel('Addresses'); + + $richList = $this->Addresses->find()->order(['Balance' => 'DESC'])->limit(500)->toArray(); + + $priceRate = 0; + $priceInfo = json_decode($this->redis->get(self::lbcPriceKey)); + if (isset($priceInfo->price)) { + $priceRate = $priceInfo->price; + } + + // calculate percentages + $totalBalance = 0; + $maxBalance = 0; + $minBalance = 0; + foreach ($richList as $item) { + $totalBalance = bcadd($totalBalance, $item->Balance, 8); + $minBalance = $minBalance == 0 ? $item->Balance : min($minBalance, $item->Balance); + $maxBalance = max($maxBalance, $item->Balance); + } + for ($i = 0; $i < count($richList); $i++) { + $item = $richList[$i]; + $percentage = bcdiv($item->Balance, $totalBalance, 8) * 100; + $richList[$i]->Top500Percent = $percentage; + $richList[$i]->MinMaxPercent = bcdiv($item->Balance, $maxBalance, 8) * 100; + } + + $this->set('richList', $richList); + $this->set('rate', $priceRate); + } + public function address($addr = null) { set_time_limit(0); @@ -558,7 +589,7 @@ class MainController extends AppController { $offset = ($page - 1) * $pageLimit; } - $stmt = $conn->execute('SELECT A.TotalReceived, A.TotalSent FROM Addresses A WHERE A.Id = ?', [$address->Id]); + $stmt = $conn->execute('SELECT A.TotalReceived, A.TotalSent, A.Balance FROM Addresses A WHERE A.Id = ?', [$address->Id]); $totals = $stmt->fetch(\PDO::FETCH_OBJ); $stmt = $conn->execute(sprintf('SELECT T.Id, T.Hash, T.InputCount, T.OutputCount, T.Value, ' . @@ -572,7 +603,7 @@ class MainController extends AppController { $totalRecvAmount = $totals->TotalReceived == 0 ? '0' : $totals->TotalReceived + 0; $totalSentAmount = $totals->TotalSent == 0 ? '0' : $totals->TotalSent + 0; - $balanceAmount = bcsub($totalRecvAmount, $totalSentAmount, 8) + 0; + $balanceAmount = $totals->Balance == 0 ? '0' : $totals->Balance + 0; } $this->set('offset', $offset); diff --git a/src/Template/Main/index.ctp b/src/Template/Main/index.ctp index 2d69dc0..e94b52b 100644 --- a/src/Template/Main/index.ctp +++ b/src/Template/Main/index.ctp @@ -116,7 +116,7 @@
Rank | +Address | +Balance (LBC) | +Balance (USD) | +First Seen | +% Top 500 | +
---|---|---|---|---|---|
+ | Address ?>
+ Tag) && strlen(trim($item->Tag)) > 0): ?>
+
+ TagUrl)) > 0): ?>Tag ?>Tag; endif; ?>
+
+ |
+ Balance, 8, '.', ',') ?> | +$Balance, $rate, 8), 2, '.', ',') ?> | +FirstSeen->format('d M Y H:i:s') . ' UTC'; ?> | +Top500Percent, 2, '.', '') ?>% |
+