diff --git a/README.md b/README.md index 6c7b21e..ea5dfc8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@
-##### 👉 this repository contains my blockchain engineering projects such as scalable event scanners and infrastructure setups for on-chain analysis and machine learning models training (*e.g.*, high-frequency trading with deep learning). +##### 👉 this repository contains my blockchain engineering projects such as scalable event scanners and infrastructure setups for on-chain analysis and machine learning models training (*e.g.*, HFT with deep learning). ##### 🛠 here is a high-level system design chart for a possible blockchain intelligence data platform (all deployed in kubernetes): diff --git a/token-scanner-api/src/server/database.py b/token-scanner-api/src/server/database.py index 0766422..744b891 100644 --- a/token-scanner-api/src/server/database.py +++ b/token-scanner-api/src/server/database.py @@ -15,7 +15,6 @@ def _get_db_collection(): """Connect to the database.""" env_vars = os_utils.load_config() - url = env_vars['MONGODB_URL'] db_name = env_vars['MONGODB_DB_NAME'] collection = env_vars['MONGODB_COLLECTION_NAME'] @@ -25,12 +24,6 @@ def _get_db_collection(): return database[collection] -def _wallet_helper(item) -> dict: - return { - "wallet": item["wallet"], - } - - def _balancer_helper(item) -> dict: return { "wallet": item["wallet"], @@ -42,23 +35,21 @@ def _balancer_helper(item) -> dict: # Public methods: database # ############################################ -async def retrieve_top_balances() -> list: +async def retrieve_top_balances(top_number=None) -> list: """Retrieve top balances from the database.""" - top_number = 100 + top_number = top_number or 100 collection = _get_db_collection() - top_balances = collection.find()#.sort({"balance"}, pymongo.DESCENDING).limit(top_number) + top_balances = collection.find().sort("balance", -1).limit(top_number) result = [] - counter = 0 - - for balance in top_balances: - result.append(_wallet_helper(balance)) - if counter > top_number: - break - counter += 1 + for item in top_balances: + result.append({ + "wallet": item["wallet"], + "balance": item["balance"], + }) - return top_balances + return result async def retrieve_balance(wallet: str) -> dict: @@ -73,7 +64,6 @@ async def retrieve_balance(wallet: str) -> dict: return {} - async def retrieve_holder_weekly_change(address: str) -> int: """Retrieve weekly change of a given address.""" diff --git a/token-scanner-api/src/server/routes.py b/token-scanner-api/src/server/routes.py index 1df6bba..13fb5a3 100644 --- a/token-scanner-api/src/server/routes.py +++ b/token-scanner-api/src/server/routes.py @@ -27,7 +27,7 @@ async def get_token_balance(address: str) -> dict: if result: return {"result": result} else: - return {"error": "wallet not found"} + return {"error": "Wallet not found"} @router.get("/top") @@ -37,14 +37,14 @@ async def get_top_holders() -> dict: futures = [retrieve_top_balances()] result = await asyncio.gather(*futures) if result: - return {"result": result} + return {"result": result[0]} else: return {"error": "No holders found"} @router.get("/weekly/{address}") -async def get_holder_weekly_change(env_vars: dict, address: str) -> dict: +async def get_holder_weekly_change(address: str) -> dict: """Get weekly change of a given address.""" futures = [retrieve_holder_weekly_change(address)] @@ -52,4 +52,4 @@ async def get_holder_weekly_change(env_vars: dict, address: str) -> dict: if result: return {"result": result} else: - return {"error": "wallet not found"} + return {"error": "Wallet not found"}