From 4b6d0e984bcbbbfc057f54d500e6361da8e12cd6 Mon Sep 17 00:00:00 2001 From: Haruka Date: Sat, 14 Sep 2024 15:05:44 +0900 Subject: [PATCH] add total supply to block info --- db/block.py | 14 ++++++++++++++ webapi/block_routes.py | 2 ++ 2 files changed, 16 insertions(+) diff --git a/db/block.py b/db/block.py index 20521fbb..dd61631e 100644 --- a/db/block.py +++ b/db/block.py @@ -1197,6 +1197,20 @@ async def get_block_target_sum_by_height(self, height: int) -> int: if (res := await cur.fetchone()) is None: return 0 return res["target_sum"] + except Exception as e: + await self.message_callback(ExplorerMessage(ExplorerMessage.Type.DatabaseError, e)) + raise + + async def get_total_supply_at_height(self, height: int) -> int: + async with self.pool.connection() as conn: + async with conn.cursor() as cur: + try: + await cur.execute( + "SELECT total_supply FROM block WHERE height = %s", (height,) + ) + if (res := await cur.fetchone()) is None: + return 0 + return res["total_supply"] except Exception as e: await self.message_callback(ExplorerMessage(ExplorerMessage.Type.DatabaseError, e)) raise \ No newline at end of file diff --git a/webapi/block_routes.py b/webapi/block_routes.py index 02633fb7..f5100232 100644 --- a/webapi/block_routes.py +++ b/webapi/block_routes.py @@ -1,4 +1,5 @@ import math +from decimal import Decimal from typing import Any from starlette.requests import Request @@ -92,6 +93,7 @@ async def block_route(request: Request): "validators": validators, "all_validators": all_validators, "solutions": css, + "total_supply": Decimal(await db.get_total_supply_at_height(height)), } result["resolved_addresses"] = \ await UIAddress.resolve_recursive_detached(