fix routes

This commit is contained in:
osiris account 2023-03-12 12:17:31 -07:00
parent 678f09fb33
commit eccd1fd0f4
4 changed files with 48 additions and 52 deletions

View file

@ -122,9 +122,6 @@ class TokenIndexer:
try:
for log in logs:
print(log)
import sys
sys.exit()
processed_logs[log['transactionHash']] = {}
processed_logs[log['transactionHash']]['blockNumber'] = convert_hex_to_int(log['blockNumber'])
processed_logs[log['transactionHash']]['from'] = '0x' + log['topics'][1][26:]

View file

@ -9,22 +9,19 @@ from src.utils import os_utils
from src.server.routes import router
def create_app():
"""Create the FastAPI app."""
app = FastAPI()
app.include_router(router)
app = FastAPI()
app.include_router(router)
env_vars = os_utils.load_config()
url = env_vars['MONGODB_URL']
db_name = env_vars['MONGODB_DB_NAME']
env_vars = os_utils.load_config()
url = env_vars['MONGODB_URL']
db_name = env_vars['MONGODB_DB_NAME']
@app.on_event("startup")
def startup_db_client():
app.mongodb_client = MongoClient(url)
app.database = app.mongodb_client[db_name]
os_utils.log_info("Connected to the MongoDB database!")
@app.on_event("startup")
def startup_db_client():
app.mongodb_client = MongoClient(url)
app.database = app.mongodb_client[db_name]
os_utils.log_info("Connected to the MongoDB database!")
@app.on_event("shutdown")
def shutdown_db_client():
app.mongodb_client.close()
@app.on_event("shutdown")
def shutdown_db_client():
app.mongodb_client.close()

View file

@ -1,11 +1,13 @@
import motor.motor_asyncio
import json
MONGO_DETAILS = "mongodb://localhost:27017"
# -*- encoding: utf-8 -*-
# server/database.py
# This class implements the database connection.
#client = motor.motor_asyncio.AsyncIOMotorClient(MONGO_DETAILS)
from pymongo import MongoClient
client = MongoClient(MONGO_DETAILS)
database = client.balances
@ -46,7 +48,13 @@ def balancer_helper(item) -> dict:
# Retrieve a student with a matching ID
async def retrieve_student(wallet: str) -> dict:
student = collection.find_one({"wallet": wallet})
if student:
return balancer_helper(student)
async def retrieve_balance(wallet: str) -> dict:
balance = collection.find_one({"wallet": wallet})
if balance:
return balancer_helper(balance)
async def retrieve_top_balances():
pass
async def retrieve_holder_weekly_change(address: str):
pass

View file

@ -1,19 +1,11 @@
# -*- encoding: utf-8 -*-
# server/routes.py
# This class implements the routes for the API.
import asyncio
import ethereum as APIEth
from fastapi import APIRouter, Body
from fastapi.encoders import jsonable_encoder
from database import (
retrieve_students,
retrieve_student,
)
from models import (
WalletsSchema,
ResponseModel,
ErrorResponseModel,
)
from fastapi import APIRouter
from database import retrieve_balance, retrieve_top_balances, retrieve_holder_weekly_change
router = APIRouter()
@ -21,9 +13,9 @@ router = APIRouter()
@router.get("/")
async def get_notes() -> dict:
"""Get a message to check server status."""
return {
"message": "server is up and running!"
"message": "🪙 Token indexer server is up and running!"
}
@ -31,16 +23,19 @@ async def get_notes() -> dict:
async def get_token_balance(address: str) -> dict:
"""Get a token balance for a given address."""
futures = [retrieve_student(address)]
futures = [retrieve_balance(address)]
result = await asyncio.gather(*futures)
return {"result": result}
if result:
return {"result": result}
else:
return {"error": "wallet not found"}
@router.get("/top")
async def get_top_holders() -> dict:
"""Get top holders of a given token."""
futures = [retrieve_students()]
futures = [retrieve_top_balances()]
result = await asyncio.gather(*futures)
if result:
return {"top_holders": result}
@ -52,10 +47,9 @@ async def get_top_holders() -> dict:
async def get_holder_weekly_change(address: str) -> dict:
"""Get weekly change of a given address."""
futures = [APIEth.fetch_weekly_balance_change_by_address(address)]
futures = [retrieve_holder_weekly_change(address)]
result = await asyncio.gather(*futures)
print(result)
return {"result": result}
if result:
return {"result": result}
else:
return {"error": "wallet not found"}