This commit is contained in:
osiris account 2023-03-12 20:16:56 -07:00
parent d9a600a7dc
commit 78549114f6
6 changed files with 29 additions and 37 deletions

View File

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup(
name="indexer",
version='0.0.2',
version='0.0.3',
packages=find_packages(include=[
'src',
'src.blockchains',

View File

@ -39,7 +39,7 @@ def run_menu() -> argparse.ArgumentParser:
parser.add_argument('-b', dest='balance', nargs=1,
help="Fetch token balance for a given wallet. \
Example: indexer -b <wallet address>")
parser.add_argument('-t', dest='top', nargs=1,
parser.add_argument('-t', dest='top', action='store_true',
help="Fetch top token holders. \
Example: indexer -t <number of holders>")
parser.add_argument('-g', dest='change', nargs=1,
@ -82,7 +82,7 @@ def run() -> None:
elif args.balance:
fetch_token_balance(env_vars, args.balance[0])
elif args.top:
fetch_top_token_holders(env_vars, args.top[0])
fetch_top_token_holders(env_vars)
elif args.change:
fetch_change(env_vars, args.change[0])

View File

@ -4,16 +4,18 @@
import pymongo
from src.utils import os_utils
import src.utils.os_utils as os_utils
############################################
# Private methods: database #
############################################
def _get_db_collection(env_vars):
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']
@ -40,11 +42,12 @@ def _balancer_helper(item) -> dict:
# Public methods: database #
############################################
async def retrieve_top_balances(top_number: int, env_vars: dict) -> list:
async def retrieve_top_balances() -> list:
"""Retrieve top balances from the database."""
collection = _get_db_collection(env_vars)
top_balances = collection.find().sort({"balance"}, pymongo.DESCENDING).limit(top_number)
top_number = 100
collection = _get_db_collection()
top_balances = collection.find()#.sort({"balance"}, pymongo.DESCENDING).limit(top_number)
result = []
counter = 0
@ -55,13 +58,13 @@ async def retrieve_top_balances(top_number: int, env_vars: dict) -> list:
break
counter += 1
return result
return top_balances
async def retrieve_balance(wallet: str, env_vars: dict) -> dict:
async def retrieve_balance(wallet: str) -> dict:
"""Retrieve balance from the database."""
collection = _get_db_collection(env_vars)
collection = _get_db_collection()
balance = collection.find_one({"wallet": wallet})
if balance:
@ -70,10 +73,11 @@ async def retrieve_balance(wallet: str, env_vars: dict) -> dict:
return {}
async def retrieve_holder_weekly_change(address: str, env_vars: dict) -> int:
async def retrieve_holder_weekly_change(address: str) -> int:
"""Retrieve weekly change of a given address."""
collection = _get_db_collection(env_vars)
collection = _get_db_collection()
# todo
# get today time
# get last week time

View File

@ -7,7 +7,6 @@ from fastapi import APIRouter
from src.server.database import retrieve_balance, retrieve_top_balances, retrieve_holder_weekly_change
router = APIRouter()
@ -20,10 +19,10 @@ async def get_notes() -> dict:
@router.get("/balance/{address}")
async def get_token_balance(env_vars: dict, address: str) -> dict:
async def get_token_balance(address: str) -> dict:
"""Get a token balance for a given address."""
futures = [retrieve_balance(env_vars, address)]
futures = [retrieve_balance(address)]
result = await asyncio.gather(*futures)
if result:
return {"result": result}
@ -32,24 +31,23 @@ async def get_token_balance(env_vars: dict, address: str) -> dict:
@router.get("/top")
async def get_top_holders(env_vars: dict, top_number=None) -> dict:
async def get_top_holders() -> dict:
"""Get top holders of a given token."""
top_number = top_number or 100
futures = [retrieve_top_balances(env_vars, top_number)]
futures = [retrieve_top_balances()]
result = await asyncio.gather(*futures)
if result:
return {"top_holders": result}
return {"result": result}
else:
return {"error": "No holders found"}
@router.get("/weekly/{address}")
async def get_holder_weekly_change(env_vars: dict, address: str) -> dict:
"""Get weekly change of a given address."""
futures = [retrieve_holder_weekly_change(env_vars, address)]
futures = [retrieve_holder_weekly_change(address)]
result = await asyncio.gather(*futures)
if result:
return {"result": result}

View File

@ -10,7 +10,6 @@ import requests
from pathlib import Path
from dotenv import load_dotenv
from datetime import datetime
from urllib.parse import urlparse
def set_logging(log_level) -> None:
@ -94,13 +93,6 @@ def format_path(dir_path, filename) -> str:
return os.path.join(dir_path, filename)
def format_url(base_url, endpoint) -> str:
"""Format a URL full filepath."""
return urlparse.urljoin(base_url, endpoint)
def save_output(destination, data, mode="w") -> None:
"""Save data from memory to a destination in disk."""
@ -168,13 +160,11 @@ def send_post_request(url, headers=None, json=None) -> dict:
return {}
def send_get_request(url, params=None) -> dict:
def send_get_request(url) -> dict:
"""Send a request to a given URL"""
params = params or {}
try:
response = requests.get(url, params=params)
response = requests.get(url)
return response.json()
except requests.exceptions.HTTPError as e:

View File

@ -5,11 +5,11 @@
import src.utils.os_utils as os_utils
def fetch_token_balance(env_vars, wallet):
def fetch_token_balance(ewallet):
"""Test the fetch_token_balance function."""
url = os_utils.format_url(f"{env_vars['API_HOST_URL']}:{env_vars['API_HOST_PORT']}", \
f"/balance/{wallet}")
endpoint = f"balance/{wallet}"
url = f'http://localhost:80/{endpoint}'
response = os_utils.send_get_request(url)
os_utils.log_info(response)