mirror of
https://github.com/autistic-symposium/blockchain-data-engineering-toolkit.git
synced 2025-05-18 06:30:36 -04:00
first commit
This commit is contained in:
commit
bb17a2a56e
29 changed files with 1238 additions and 0 deletions
1
token-scanner-api/src/server/__init__.py
Normal file
1
token-scanner-api/src/server/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
# -*- encoding: utf-8 -*-
|
25
token-scanner-api/src/server/api.py
Normal file
25
token-scanner-api/src/server/api.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
from fastapi import FastAPI
|
||||
from routes import router
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(router)
|
||||
|
||||
|
||||
from pymongo import MongoClient
|
||||
|
||||
|
||||
url = "mongodb://localhost:27017/"
|
||||
DB_NAME = "balances"
|
||||
|
||||
@app.on_event("startup")
|
||||
def startup_db_client():
|
||||
app.mongodb_client = MongoClient(url)
|
||||
app.database = app.mongodb_client[DB_NAME]
|
||||
|
||||
print("Connected to the MongoDB database!")
|
||||
|
||||
@app.on_event("shutdown")
|
||||
def shutdown_db_client():
|
||||
app.mongodb_client.close()
|
52
token-scanner-api/src/server/database.py
Normal file
52
token-scanner-api/src/server/database.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
import motor.motor_asyncio
|
||||
import json
|
||||
MONGO_DETAILS = "mongodb://localhost:27017"
|
||||
|
||||
#client = motor.motor_asyncio.AsyncIOMotorClient(MONGO_DETAILS)
|
||||
|
||||
from pymongo import MongoClient
|
||||
|
||||
client = MongoClient(MONGO_DETAILS)
|
||||
|
||||
database = client.balances
|
||||
|
||||
collection = database.get_collection("balances_fuckkk")
|
||||
|
||||
|
||||
print(database.list_collection_names())
|
||||
|
||||
|
||||
def wallet_helper(item) -> dict:
|
||||
return {
|
||||
"wallet": item["wallet"],
|
||||
}
|
||||
|
||||
|
||||
from bson.objectid import ObjectId
|
||||
|
||||
async def retrieve_students():
|
||||
bals = collection.find()
|
||||
|
||||
res = []
|
||||
counter = 0
|
||||
for i in bals:
|
||||
res.append(wallet_helper(i))
|
||||
if counter > 2:
|
||||
break
|
||||
counter += 1
|
||||
|
||||
return res
|
||||
|
||||
|
||||
def balancer_helper(item) -> dict:
|
||||
return {
|
||||
"wallet": item["wallet"],
|
||||
"balance": item["balance"],
|
||||
}
|
||||
|
||||
|
||||
# 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)
|
27
token-scanner-api/src/server/models.py
Normal file
27
token-scanner-api/src/server/models.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
|
||||
|
||||
class WalletsSchema(BaseModel):
|
||||
wallet: float = Field(...)
|
||||
|
||||
class Config:
|
||||
allow_population_by_field_name = True
|
||||
schema_extra = {
|
||||
"example": {
|
||||
"wallet": "balance"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def ResponseModel(data, message):
|
||||
return {
|
||||
"data": [data],
|
||||
"code": 200,
|
||||
"message": message,
|
||||
}
|
||||
|
||||
|
||||
def ErrorResponseModel(error, code, message):
|
||||
return {"error": error, "code": code, "message": message}
|
61
token-scanner-api/src/server/routes.py
Normal file
61
token-scanner-api/src/server/routes.py
Normal file
|
@ -0,0 +1,61 @@
|
|||
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,
|
||||
)
|
||||
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/")
|
||||
async def get_notes() -> dict:
|
||||
|
||||
return {
|
||||
"message": "server is up and running!"
|
||||
}
|
||||
|
||||
|
||||
@router.get("/balance/{address}")
|
||||
async def get_token_balance(address: str) -> dict:
|
||||
"""Get a token balance for a given address."""
|
||||
|
||||
futures = [retrieve_student(address)]
|
||||
result = await asyncio.gather(*futures)
|
||||
return {"result": result}
|
||||
|
||||
|
||||
@router.get("/top")
|
||||
async def get_top_holders() -> dict:
|
||||
"""Get top holders of a given token."""
|
||||
|
||||
futures = [retrieve_students()]
|
||||
result = await asyncio.gather(*futures)
|
||||
if result:
|
||||
return {"top_holders": result}
|
||||
else:
|
||||
return {"error": "No holders found"}
|
||||
|
||||
|
||||
@router.get("/weekly/{address}")
|
||||
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)]
|
||||
result = await asyncio.gather(*futures)
|
||||
print(result)
|
||||
return {"result": result}
|
||||
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue