Skip to content

Commit

Permalink
feat: bump pydantic, sqlmodel, and fastapi
Browse files Browse the repository at this point in the history
  • Loading branch information
robcxyz committed Oct 28, 2024
1 parent 1f31a5c commit e3fc2cc
Show file tree
Hide file tree
Showing 49 changed files with 652 additions and 450 deletions.
9 changes: 1 addition & 8 deletions .github/workflows/push-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ jobs:
uses: actions/checkout@v2

- name: Install deps
run: |
pip3 install \
-r requirements-api.txt \
-r requirements-cron.txt \
-r requirements-dev.txt
# sudo apt-get install librdkafka-dev
# -r requirements-streaming.txt \
run: make install

- name: Bring up stack
run: make up-dbs && sleep 10
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
.idea/
.vscode/

scratch*

.old/

# Byte-compiled / optimized / DLL files
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ ENV PYTHONPATH="/opt:${PYTHONPATH}"
WORKDIR /opt

RUN pip install --upgrade pip
COPY ./requirements-$SERVICE_NAME.txt .
RUN pip install -r ./requirements-$SERVICE_NAME.txt
COPY ./requirements-$SERVICE_NAME.txt ./requirements-common.txt ./
RUN pip install -r ./requirements-$SERVICE_NAME.txt -r ./requirements-common.txt

COPY balanced_backend ./balanced_backend

Expand Down
25 changes: 18 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.PHONY: test help

test: up-dbs test-unit test-integration
help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-16s\033[0m %s\n", $$1, $$2}'

test: up-dbs test-unit test-integration ## Run all tests

up-dbs: ## Bring up the DBs
docker compose -f docker-compose.db.yml up -d
Expand All @@ -22,7 +25,7 @@ up: ## Bring everything up as containers
down: ## Take down all the containers
docker compose -f docker-compose.db.yml -f docker-compose.yml down

clean:
clean: ## Clean all the containers and volumes
docker volume rm $(docker volume ls -q)

build: ## Build everything
Expand All @@ -34,8 +37,16 @@ ps: ## List all containers and running status
postgres-console: ## Start postgres terminal
docker compose -f docker-compose.db.yml -f docker-compose.yml exec postgres psql -U postgres

help:
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-16s\033[0m %s\n", $$1, $$2}'

install:
pip install -r requirements-api.txt -r requirements-cron.txt -r requirements-dev.txt
# install-streaming -> Not used right now
install: install-common install-api install-cron install-dev ## Install all deps

install-common:
pip install --upgrade -r requirements-common.txt
install-api:
pip install --upgrade -r requirements-api.txt
install-cron:
pip install --upgrade -r requirements-cron.txt
install-streaming:
pip install --upgrade -r requirements-streaming.txt
install-dev:
pip install --upgrade -r requirements-dev.txt
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# balanced-backend
[![loopchain](https://img.shields.io/badge/ICON-API-blue?logoColor=white&logo=icon&labelColor=31B8BB)](https://shields.io) [![GitHub Release](https://img.shields.io/github/release/balancednetwork/balanced-backend.svg?style=flat)]() ![](https://github.com/balancednetwork/balanced-backend/workflows/push-main/badge.svg?branch=main) ![](https://img.shields.io/github/license/balancednetwork/balanced-backend)

Backend service for Balanced Network.

Backend service for Balanced Network.

[API Docs](https://balanced.icon.community/api/v1/docs) + [Live Stats Page](https://stats.balanced.network/)

Expand All @@ -11,9 +10,18 @@ Backend service for Balanced Network.
For local development, you will want to run the `docker-compose.db.yml` as you develop. To run the tests,

```bash
python3.10 -m venv .venv && source .venv/bin/activate
make install
make test
# Alternatively you can manually run the tests but make sure the DB is up.
make up-dbs
# And then bring down the dbs
make down-dbs
# See all options with
make help
```

<!--
### Deployment
To bring up the whole stack, run:
Expand All @@ -22,7 +30,8 @@ To bring up the whole stack, run:
docker-compose -f docker-compose.db.yml -f docker-compose.yml up -d
```
If you are running in prod, be sure to change the DB password with an `.env` file. You'd be also wise to run a local ICON node. Note that sync time might be slow and is not tested outside of proximity to the ICON tracker.
If you are running in prod, be sure to change the DB password with an `.env` file. You'd be also wise to run a local ICON node. Note that sync time might be slow and is not tested outside of proximity to the ICON tracker.
-->

### License

Expand Down
7 changes: 4 additions & 3 deletions balanced_backend/addresses.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from pydantic import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict


class Addresses(BaseSettings):
Expand All @@ -25,8 +25,9 @@ class Addresses(BaseSettings):
REBALANCING_CONTRACT_ADDRESS: str = "cx40d59439571299bca40362db2a7d8cae5b0b30b0"
STABILITY_FUND_CONTRACT_ADDRESS: str = "cxa09dbb60dcb62fffbd232b6eae132d730a2aafa6"

class Config:
case_sensitive = True
model_config = SettingsConfigDict(
case_sensitive=True,
)


if os.environ.get("ADDRESS_ENV_FILE", False):
Expand Down
2 changes: 1 addition & 1 deletion balanced_backend/api/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def is_database_online(session: bool = Depends(get_session)):


def is_cache_updated():
for k, v in cache.dict().items():
for k, v in stablity_cached_output.dict().items():
if not v:
logger.info(f"Unhealthy cache item {k}")
return True
Expand Down
22 changes: 10 additions & 12 deletions balanced_backend/api/v1/endpoints/cmc.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
from typing import TYPE_CHECKING
from fastapi import APIRouter, HTTPException

from balanced_backend.api.v1.endpoints._utils import sleep_while_empty_cache
from balanced_backend.cache.cache import cache

if TYPE_CHECKING:
from balanced_backend.models.cmc import (
SummaryCMC,
TickerCMC,
OrderBookCMC,
TradeCMC,
)
from balanced_backend.models.cmc import (
SummaryCMC,
TickerCMC,
OrderBookCMC,
TradeCMC,
)

router = APIRouter()


@router.get("/summary")
async def get_cmc_summary() -> list['SummaryCMC']:
async def get_cmc_summary() -> list[SummaryCMC]:
await sleep_while_empty_cache('cmc_summary')
return cache.cmc_summary


@router.get("/ticker")
async def get_cmc_ticker() -> dict[str, 'TickerCMC']:
async def get_cmc_ticker() -> dict[str, TickerCMC]:
await sleep_while_empty_cache('cmc_tickers')
return cache.cmc_tickers


@router.get("/orderbook/{market_pair}")
async def get_cmc_orderbook(
market_pair: str,
) -> dict[str, 'OrderBookCMC']:
) -> OrderBookCMC:
await sleep_while_empty_cache('cmc_orderbook')
try:
return cache.cmc_orderbook[market_pair]
Expand All @@ -44,7 +42,7 @@ async def get_cmc_orderbook(
@router.get("/trades/{market_pair}")
async def get_cmc_trades(
market_pair: str,
) -> dict[str, 'TradeCMC']:
) -> list[TradeCMC]:
await sleep_while_empty_cache('cmc_trades')
try:
return cache.cmc_trades[market_pair]
Expand Down
71 changes: 45 additions & 26 deletions balanced_backend/api/v1/endpoints/coingecko.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,82 @@
from typing import TYPE_CHECKING
from fastapi import APIRouter, HTTPException
from __future__ import annotations

from typing import Union

from fastapi import APIRouter, HTTPException, Query
from starlette.responses import Response

from balanced_backend.api.v1.endpoints._utils import sleep_while_empty_cache
from balanced_backend.cache.cache import cache

if TYPE_CHECKING:
from balanced_backend.models.coingecko import (
PairsCoinGecko,
TickerCoinGecko,
OrderBookCoinGecko,
HistoricalCoinGecko,
)
from balanced_backend.models.coingecko import (
PairsCoinGecko,
TickerCoinGecko,
OrderBookCoinGecko,
HistoricalCoinGecko,
)

router = APIRouter()


# Per this guide
# https://docs.google.com/document/d/1v27QFoQq1SKT3Priq3aqPgB70Xd_PnDzbOCiuoCyixw/edit


@router.get("/pairs")
async def get_coingecko_summary() -> list['PairsCoinGecko']:
await sleep_while_empty_cache('coingecko_pairs')
async def get_coingecko_summary() -> list[PairsCoinGecko]:
await sleep_while_empty_cache("coingecko_pairs")
return cache.coingecko_pairs


@router.get("/tickers")
async def get_coingecko_ticker() -> dict[str, 'TickerCoinGecko']:
await sleep_while_empty_cache('coingecko_tickers')
async def get_coingecko_ticker() -> list[TickerCoinGecko]:
await sleep_while_empty_cache("coingecko_tickers")
return cache.coingecko_tickers


@router.get("/orderbook")
async def get_coingecko_orderbook(
ticker_id: str = None,
) -> dict[str, 'OrderBookCoinGecko']:
await sleep_while_empty_cache('coingecko_orderbook')
ticker_id: str = None,
) -> dict[str, OrderBookCoinGecko] | OrderBookCoinGecko:
await sleep_while_empty_cache("coingecko_orderbook")
if ticker_id is None:
return cache.coingecko_orderbook
try:
return cache.coingecko_orderbook[ticker_id]
except KeyError:
raise HTTPException(
status_code=204,
detail=f"ticker_id not found - check /summary for available pairs."
detail=f"ticker_id not found - check /summary for available pairs.",
)



@router.get("/historical_trades")
async def get_coingecko_trades(
response: Response,
ticker_id: str = None,
) -> dict[str, 'HistoricalCoinGecko']:
await sleep_while_empty_cache('coingecko_historical')
response: Response,
ticker_id: str = Query(None),
type: str = Query(None, regex="^(buy|sell)$"),
) -> Union[
dict[str, dict[str, list[HistoricalCoinGecko]]],
dict[str, list[HistoricalCoinGecko]],
list[HistoricalCoinGecko],
]:
await sleep_while_empty_cache("coingecko_historical")

if ticker_id is None:
response.headers["x-total-count"] = str(len(cache.coingecko_historical))
return cache.coingecko_historical
output = cache.coingecko_historical
if type:
for k, v in output.items():
output[k] = output[k][type]
return output
try:
return cache.coingecko_historical[ticker_id]
output = cache.coingecko_historical[ticker_id]
if type:
return output[type]
return output

except KeyError:
raise HTTPException(
status_code=204,
detail=f"ticker_id not found - check /summary for available pairs."
status_code=422,
detail=f"ticker_id not found - check /summary for available pairs.",
)
4 changes: 2 additions & 2 deletions balanced_backend/api/v1/endpoints/contract_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
router = APIRouter()


@router.get("/contract-methods")
@router.get("/contract-methods", response_model=List[DailyHistorical])
async def contract_methods(
session: AsyncSession = Depends(get_session),
skip: int = Query(0),
Expand Down Expand Up @@ -74,7 +74,7 @@ async def contract_methods(


# TODO: RM after FE change
@router.get("/historical")
@router.get("/historical", response_model=List[DailyHistorical])
async def historical(
session: AsyncSession = Depends(get_session),
skip: int = Query(0),
Expand Down
2 changes: 1 addition & 1 deletion balanced_backend/api/v1/endpoints/dex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
router = APIRouter()


@router.get("/dex/swaps")
@router.get("/dex/swaps", response_model=list[DexSwap])
async def swaps(
response: Response,
session: AsyncSession = Depends(get_session),
Expand Down
2 changes: 1 addition & 1 deletion balanced_backend/api/v1/endpoints/holders.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
router = APIRouter()


@router.get("/holders")
@router.get("/holders", response_model=list[Holder])
async def holders(
response: Response,
session: AsyncSession = Depends(get_session),
Expand Down
14 changes: 10 additions & 4 deletions balanced_backend/api/v1/endpoints/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
router = APIRouter()


@router.get("/pools")
@router.get("/pools", response_model=List[Pool])
async def get_pools(
response: Response,
session: AsyncSession = Depends(get_session),
Expand Down Expand Up @@ -109,7 +109,10 @@ async def get_pools(
INTERVALS = {k for k, _ in INTERVAL_MAP.items()}


@router.get("/pools/series/{pool_id}/{interval}/{start}/{end}")
@router.get(
"/pools/series/{pool_id}/{interval}/{start}/{end}",
response_model=List[PoolSeriesTableType],
)
async def get_pools_series(
response: Response,
session: AsyncSession = Depends(get_session),
Expand Down Expand Up @@ -164,7 +167,10 @@ async def get_pools_series(
return timeseries


@router.get("/pools/series/implied/{token_a}/{token_b}/{interval}/{start}/{end}")
@router.get(
"/pools/series/implied/{token_a}/{token_b}/{interval}/{start}/{end}",
response_model=List[PoolSeriesTableType],
)
async def get_pools_series_implied(
response: Response,
token_a: str,
Expand Down Expand Up @@ -272,7 +278,7 @@ def divide_attr(index, attr) -> float:
return output


@router.get("/pools/dividends")
@router.get("/pools/dividends", response_model=List[Dividend])
async def pools_dividends(
response: Response,
session: AsyncSession = Depends(get_session),
Expand Down
Loading

0 comments on commit e3fc2cc

Please sign in to comment.