Skip to content

Commit

Permalink
Update - just aesthetic changes + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SinaKhalili committed Dec 18, 2024
1 parent 1d5ab02 commit dfef15a
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 174 deletions.
3 changes: 3 additions & 0 deletions .streamlit/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ base = "dark"

[browser]
gatherUsageStats = false

[client]
toolbarMode = "minimal"
8 changes: 2 additions & 6 deletions backend/api/asset_liability.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import functools

from backend.state import BackendRequest
from backend.state import BackendState
from backend.utils.matrix import get_matrix
from backend.utils.waiting_for import waiting_for
from driftpy.pickle.vat import Vat
from fastapi import APIRouter

from backend.state import BackendRequest
from backend.utils.matrix import get_matrix

router = APIRouter()

Expand Down
49 changes: 23 additions & 26 deletions backend/api/health.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import heapq

from backend.state import BackendRequest
from driftpy.constants import BASE_PRECISION
from driftpy.constants import PRICE_PRECISION
from driftpy.constants import SPOT_BALANCE_PRECISION
import pandas as pd
from driftpy.constants import BASE_PRECISION, PRICE_PRECISION, SPOT_BALANCE_PRECISION
from driftpy.pickle.vat import Vat
from driftpy.types import is_variant
from fastapi import APIRouter
import pandas as pd

from backend.state import BackendRequest

router = APIRouter()

Expand Down Expand Up @@ -42,32 +40,31 @@ def get_account_health_distribution(request: BackendRequest):
"""
vat: Vat = request.state.backend_state.vat
health_notional_distributions = {
"0-10%": 0,
"10-20%": 0,
"20-30%": 0,
"30-40%": 0,
"40-50%": 0,
"50-60%": 0,
"60-70%": 0,
"70-80%": 0,
"80-90%": 0,
"90-100%": 0,
"0-10%": 0.0,
"10-20%": 0.0,
"20-30%": 0.0,
"30-40%": 0.0,
"40-50%": 0.0,
"50-60%": 0.0,
"60-70%": 0.0,
"70-80%": 0.0,
"80-90%": 0.0,
"90-100%": 0.0,
}
health_counts = {
"0-10%": 0,
"10-20%": 0,
"20-30%": 0,
"30-40%": 0,
"40-50%": 0,
"50-60%": 0,
"60-70%": 0,
"70-80%": 0,
"80-90%": 0,
"90-100%": 0,
"0-10%": 0.0,
"10-20%": 0.0,
"20-30%": 0.0,
"30-40%": 0.0,
"40-50%": 0.0,
"50-60%": 0.0,
"60-70%": 0.0,
"70-80%": 0.0,
"80-90%": 0.0,
"90-100%": 0.0,
}

for user in vat.users.values():
# print(user.user_public_key)
try:
total_collateral = user.get_total_collateral() / PRICE_PRECISION
current_health = user.get_health()
Expand Down
8 changes: 5 additions & 3 deletions backend/api/liquidation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from backend.state import BackendRequest
from driftpy.constants import BASE_PRECISION
from driftpy.constants import PRICE_PRECISION
from driftpy.constants import BASE_PRECISION, PRICE_PRECISION
from driftpy.pickle.vat import Vat
from fastapi import APIRouter

from backend.state import BackendRequest

router = APIRouter()

Expand All @@ -14,6 +13,9 @@ def get_liquidation_curve(request: BackendRequest, market_index: int):
liquidations_long: list[tuple[float, float, str]] = []
liquidations_short: list[tuple[float, float, str]] = []
market_price = vat.perp_oracles.get(market_index)
if market_price is None:
print("Market price is None")
return {"liquidations_long": [], "liquidations_short": [], "market_price_ui": 0}
market_price_ui = market_price.price / PRICE_PRECISION
for pubkey, user in vat.users.user_map.items():
perp_position = user.get_perp_position(market_index)
Expand Down
3 changes: 1 addition & 2 deletions backend/api/metadata.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from backend.state import BackendRequest
from backend.state import BackendState
from fastapi import APIRouter

from backend.state import BackendRequest, BackendState

router = APIRouter()

Expand Down
4 changes: 2 additions & 2 deletions backend/api/price_shock.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Optional

from backend.state import BackendRequest
from backend.utils.user_metrics import get_user_leverages_for_price_shock
from driftpy.drift_client import DriftClient
from driftpy.pickle.vat import Vat
from fastapi import APIRouter

from backend.state import BackendRequest
from backend.utils.user_metrics import get_user_leverages_for_price_shock

router = APIRouter()

Expand Down
6 changes: 2 additions & 4 deletions backend/api/snapshot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from backend.state import BackendRequest
from backend.state import BackendState
from fastapi import APIRouter
from fastapi import BackgroundTasks
from fastapi import APIRouter, BackgroundTasks

from backend.state import BackendRequest, BackendState

router = APIRouter()

Expand Down
5 changes: 1 addition & 4 deletions backend/api/ucache.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import functools

from backend.state import BackendRequest
from backend.state import BackendState
from fastapi import APIRouter
from fastapi.responses import FileResponse

from backend.state import BackendRequest

router = APIRouter()

Expand Down
28 changes: 13 additions & 15 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
from contextlib import asynccontextmanager
import glob
import os
import shutil
from contextlib import asynccontextmanager

from backend.api import asset_liability
from backend.api import health
from backend.api import liquidation
from backend.api import metadata
from backend.api import price_shock
from backend.api import snapshot
from backend.api import ucache
from backend.middleware.cache_middleware import CacheMiddleware
from backend.middleware.readiness import ReadinessMiddleware
from backend.state import BackendState
from backend.utils.repeat_every import repeat_every
from dotenv import load_dotenv
from fastapi import BackgroundTasks
from fastapi import FastAPI
from fastapi.testclient import TestClient

from backend.api import (
asset_liability,
health,
liquidation,
metadata,
price_shock,
snapshot,
ucache,
)
from backend.middleware.cache_middleware import CacheMiddleware
from backend.middleware.readiness import ReadinessMiddleware
from backend.state import BackendState

load_dotenv()
state = BackendState()
Expand Down
9 changes: 4 additions & 5 deletions backend/middleware/cache_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import hashlib
import json
import os
from typing import Callable, Dict, List, Optional
from typing import Callable, Dict, List

from backend.state import BackendRequest
from backend.state import BackendState
from fastapi import BackgroundTasks
from fastapi import Response
from fastapi import BackgroundTasks, Response
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.types import ASGIApp

from backend.state import BackendRequest, BackendState


class CacheMiddleware(BaseHTTPMiddleware):
def __init__(self, app: ASGIApp, state: BackendState, cache_dir: str = "cache"):
Expand Down
4 changes: 2 additions & 2 deletions backend/middleware/readiness.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from backend.state import BackendRequest
from backend.state import BackendState
from fastapi import HTTPException
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.types import ASGIApp

from backend.state import BackendRequest, BackendState


class ReadinessMiddleware(BaseHTTPMiddleware):
def __init__(self, app: ASGIApp, state: BackendState):
Expand Down
27 changes: 13 additions & 14 deletions backend/scripts/generate_ucache.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import asyncio
from concurrent.futures import ProcessPoolExecutor
from dataclasses import dataclass
import glob
from itertools import islice
import json
import multiprocessing
import os
from typing import Optional
from dataclasses import dataclass
from itertools import islice

from dotenv import load_dotenv
from fastapi.responses import JSONResponse

from backend.api.asset_liability import _get_asset_liability_matrix
from backend.api.price_shock import _get_price_shock
from backend.state import BackendState
from backend.utils.waiting_for import waiting_for
from dotenv import load_dotenv
from fastapi.responses import JSONResponse

load_dotenv()


def chunk_list(lst, n):
Expand All @@ -33,7 +32,7 @@ class Endpoint:
async def process_multiple_endpoints(state_pickle_path: str, endpoints: list[Endpoint]):
"""Process a single endpoint in its own process"""
state = BackendState()
state.initialize(os.getenv("RPC_URL"))
state.initialize(os.getenv("RPC_URL") or "")
await state.load_pickle_snapshot(state_pickle_path)

results = []
Expand Down Expand Up @@ -120,17 +119,17 @@ async def main():

if not use_snapshot:
state = BackendState()
state.initialize(os.getenv("RPC_URL"))
state.initialize(os.getenv("RPC_URL") or "")
print("Taking snapshot")
await state.bootstrap()
await state.take_pickle_snapshot()
await state.close()

endpoints = [
# Endpoint(
# endpoint="asset-liability/matrix",
# params={"mode": 0, "perp_market_index": 0},
# ),
Endpoint(
endpoint="asset-liability/matrix",
params={"mode": 0, "perp_market_index": 0},
),
Endpoint(
endpoint="price-shock/usermap",
params={
Expand Down
23 changes: 11 additions & 12 deletions backend/state.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
from asyncio import create_task
from asyncio import gather
from datetime import datetime
import os
from typing import TypedDict
from asyncio import create_task, gather
from datetime import datetime

from anchorpy import Wallet
from backend.utils.vat import load_newest_files
from backend.utils.waiting_for import waiting_for
from anchorpy.provider import Wallet
from driftpy.account_subscription_config import AccountSubscriptionConfig
from driftpy.drift_client import DriftClient
from driftpy.market_map.market_map import MarketMap
from driftpy.market_map.market_map_config import MarketMapConfig
from driftpy.market_map.market_map_config import (
WebsocketConfig as MarketMapWebsocketConfig,
)
from driftpy.market_map.market_map_config import MarketMapConfig
from driftpy.pickle.vat import Vat
from driftpy.types import MarketType
from driftpy.user_map.user_map import UserMap
from driftpy.user_map.user_map_config import UserMapConfig, UserStatsMapConfig
from driftpy.user_map.user_map_config import (
WebsocketConfig as UserMapWebsocketConfig,
)
from driftpy.user_map.user_map_config import UserMapConfig
from driftpy.user_map.user_map_config import UserStatsMapConfig
from driftpy.user_map.userstats_map import UserStatsMap
from fastapi import Request
import pandas as pd
from solana.rpc.async_api import AsyncClient

from backend.utils.vat import load_newest_files
from backend.utils.waiting_for import waiting_for


class BackendState:
connection: AsyncClient
Expand Down Expand Up @@ -118,7 +115,9 @@ async def load_pickle_snapshot(self, directory: str):
perp_oracles_filename=pickle_map["perporacles"],
)

self.last_oracle_slot = pickle_map["perporacles"].split("_")[-1].split(".")[0]
self.last_oracle_slot = int(
pickle_map["perporacles"].split("_")[-1].split(".")[0]
)
return pickle_map

async def close(self):
Expand Down
Empty file removed backend/utils/cache_decorator.py
Empty file.
6 changes: 3 additions & 3 deletions backend/utils/matrix.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from backend.utils.user_metrics import get_user_leverages_for_asset_liability
import pandas as pd
from driftpy.constants.spot_markets import mainnet_spot_market_configs
from driftpy.drift_client import DriftClient
from driftpy.pickle.vat import Vat
import pandas as pd

from backend.utils.user_metrics import get_user_leverages_for_asset_liability


def calculate_effective_leverage(assets: float, liabilities: float) -> float:
Expand Down
2 changes: 1 addition & 1 deletion backend/utils/repeat_every.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Periodic Task Execution Decorator
Provides a `repeat_every` decorator for periodic execution of tasks in asynchronous environments.
Provides a `repeat_every` decorator for periodic execution of tasks in asynchronous environments.
Modified from fastapi_utils library to support passing a state object to the repeated function.
Features:
Expand Down
3 changes: 1 addition & 2 deletions backend/utils/user_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from typing import List, Optional

from driftpy.accounts.cache import DriftClientCache
from driftpy.constants.numeric_constants import MARGIN_PRECISION
from driftpy.constants.numeric_constants import QUOTE_PRECISION
from driftpy.constants.numeric_constants import MARGIN_PRECISION, QUOTE_PRECISION
from driftpy.constants.perp_markets import mainnet_perp_market_configs
from driftpy.constants.spot_markets import mainnet_spot_market_configs
from driftpy.drift_client import DriftClient
Expand Down
2 changes: 1 addition & 1 deletion backend/utils/vat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def load_newest_files(directory: Optional[str] = None) -> dict[str, str]:
prefix = filename[: start - 1]
end = filename.index(".")
slot = int(filename[start:end])
if not prefix in newest_files or slot > newest_files[prefix][1]:
if prefix not in newest_files or slot > newest_files[prefix][1]:
newest_files[prefix] = (directory + "/" + filename, slot)

# mapping e.g { 'spotoracles' : 'spotoracles_272636137.pkl' }
Expand Down
Loading

0 comments on commit dfef15a

Please sign in to comment.