mirror of
https://github.com/autistic-symposium/blockchain-data-engineering-toolkit.git
synced 2025-06-21 21:34:24 -04:00
fix routes
This commit is contained in:
parent
678f09fb33
commit
eccd1fd0f4
4 changed files with 48 additions and 52 deletions
|
@ -122,9 +122,6 @@ class TokenIndexer:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for log in logs:
|
for log in logs:
|
||||||
print(log)
|
|
||||||
import sys
|
|
||||||
sys.exit()
|
|
||||||
processed_logs[log['transactionHash']] = {}
|
processed_logs[log['transactionHash']] = {}
|
||||||
processed_logs[log['transactionHash']]['blockNumber'] = convert_hex_to_int(log['blockNumber'])
|
processed_logs[log['transactionHash']]['blockNumber'] = convert_hex_to_int(log['blockNumber'])
|
||||||
processed_logs[log['transactionHash']]['from'] = '0x' + log['topics'][1][26:]
|
processed_logs[log['transactionHash']]['from'] = '0x' + log['topics'][1][26:]
|
||||||
|
|
|
@ -9,22 +9,19 @@ from src.utils import os_utils
|
||||||
from src.server.routes import router
|
from src.server.routes import router
|
||||||
|
|
||||||
|
|
||||||
def create_app():
|
app = FastAPI()
|
||||||
"""Create the FastAPI app."""
|
app.include_router(router)
|
||||||
|
|
||||||
app = FastAPI()
|
env_vars = os_utils.load_config()
|
||||||
app.include_router(router)
|
url = env_vars['MONGODB_URL']
|
||||||
|
db_name = env_vars['MONGODB_DB_NAME']
|
||||||
|
|
||||||
env_vars = os_utils.load_config()
|
@app.on_event("startup")
|
||||||
url = env_vars['MONGODB_URL']
|
def startup_db_client():
|
||||||
db_name = env_vars['MONGODB_DB_NAME']
|
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")
|
@app.on_event("shutdown")
|
||||||
def startup_db_client():
|
def shutdown_db_client():
|
||||||
app.mongodb_client = MongoClient(url)
|
app.mongodb_client.close()
|
||||||
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()
|
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
import motor.motor_asyncio
|
# -*- encoding: utf-8 -*-
|
||||||
import json
|
# server/database.py
|
||||||
MONGO_DETAILS = "mongodb://localhost:27017"
|
# This class implements the database connection.
|
||||||
|
|
||||||
#client = motor.motor_asyncio.AsyncIOMotorClient(MONGO_DETAILS)
|
|
||||||
|
|
||||||
from pymongo import MongoClient
|
from pymongo import MongoClient
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
client = MongoClient(MONGO_DETAILS)
|
client = MongoClient(MONGO_DETAILS)
|
||||||
|
|
||||||
database = client.balances
|
database = client.balances
|
||||||
|
@ -46,7 +48,13 @@ def balancer_helper(item) -> dict:
|
||||||
|
|
||||||
|
|
||||||
# Retrieve a student with a matching ID
|
# Retrieve a student with a matching ID
|
||||||
async def retrieve_student(wallet: str) -> dict:
|
async def retrieve_balance(wallet: str) -> dict:
|
||||||
student = collection.find_one({"wallet": wallet})
|
balance = collection.find_one({"wallet": wallet})
|
||||||
if student:
|
if balance:
|
||||||
return balancer_helper(student)
|
return balancer_helper(balance)
|
||||||
|
|
||||||
|
async def retrieve_top_balances():
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def retrieve_holder_weekly_change(address: str):
|
||||||
|
pass
|
|
@ -1,19 +1,11 @@
|
||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
# server/routes.py
|
||||||
|
# This class implements the routes for the API.
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import ethereum as APIEth
|
from fastapi import APIRouter
|
||||||
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 database import retrieve_balance, retrieve_top_balances, retrieve_holder_weekly_change
|
||||||
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
@ -21,9 +13,9 @@ router = APIRouter()
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
async def get_notes() -> dict:
|
async def get_notes() -> dict:
|
||||||
|
"""Get a message to check server status."""
|
||||||
return {
|
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:
|
async def get_token_balance(address: str) -> dict:
|
||||||
"""Get a token balance for a given address."""
|
"""Get a token balance for a given address."""
|
||||||
|
|
||||||
futures = [retrieve_student(address)]
|
futures = [retrieve_balance(address)]
|
||||||
result = await asyncio.gather(*futures)
|
result = await asyncio.gather(*futures)
|
||||||
return {"result": result}
|
if result:
|
||||||
|
return {"result": result}
|
||||||
|
else:
|
||||||
|
return {"error": "wallet not found"}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/top")
|
@router.get("/top")
|
||||||
async def get_top_holders() -> dict:
|
async def get_top_holders() -> dict:
|
||||||
"""Get top holders of a given token."""
|
"""Get top holders of a given token."""
|
||||||
|
|
||||||
futures = [retrieve_students()]
|
futures = [retrieve_top_balances()]
|
||||||
result = await asyncio.gather(*futures)
|
result = await asyncio.gather(*futures)
|
||||||
if result:
|
if result:
|
||||||
return {"top_holders": result}
|
return {"top_holders": result}
|
||||||
|
@ -52,10 +47,9 @@ async def get_top_holders() -> dict:
|
||||||
async def get_holder_weekly_change(address: str) -> dict:
|
async def get_holder_weekly_change(address: str) -> dict:
|
||||||
"""Get weekly change of a given address."""
|
"""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)
|
result = await asyncio.gather(*futures)
|
||||||
print(result)
|
if result:
|
||||||
return {"result": result}
|
return {"result": result}
|
||||||
|
else:
|
||||||
|
return {"error": "wallet not found"}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue