-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
balanced_backend/alembic/versions/ef669e9ed598_v0_2_3_add_swap_index.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"""v0.2.3-add-swap-index | ||
Revision ID: ef669e9ed598 | ||
Revises: c0be6c7b7b02 | ||
Create Date: 2024-02-07 21:53:12.854330 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'ef669e9ed598' | ||
down_revision = 'c0be6c7b7b02' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_index(op.f('ix_dex_swaps_pool_id'), 'dex_swaps', ['pool_id'], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f('ix_dex_swaps_pool_id'), table_name='dex_swaps') | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from fastapi import APIRouter, Depends, Response, Query | ||
from sqlalchemy.ext.asyncio import AsyncSession | ||
from sqlmodel import select, func | ||
|
||
from balanced_backend.config import settings | ||
from balanced_backend.db import get_session | ||
from balanced_backend.tables.dex import DexSwap | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/dex/swaps") | ||
async def swaps( | ||
response: Response, | ||
session: AsyncSession = Depends(get_session), | ||
skip: int = Query(0), | ||
limit: int = Query(100, gt=0, lt=101), | ||
chain_id: int = Query(None), | ||
transaction_hash: str = Query(None), | ||
log_index: int = Query(None), | ||
start_timestamp: int = Query(None), | ||
end_timestamp: int = Query(None), | ||
start_block_number: int = Query(None), | ||
end_block_number: int = Query(None), | ||
|
||
) -> list[DexSwap]: | ||
query = select(DexSwap).offset(skip).limit(limit).where( | ||
DexSwap.chain_id == settings.CHAIN_ID) | ||
query_count = select([func.count(DexSwap.chain_id)]).where( | ||
DexSwap.chain_id == settings.CHAIN_ID) | ||
|
||
if transaction_hash: | ||
query = query.where(DexSwap.transaction_hash == transaction_hash) | ||
query_count = query_count.where(DexSwap.transaction_hash == transaction_hash) | ||
if log_index: | ||
query = query.where(DexSwap.log_index == log_index) | ||
query_count = query.where(DexSwap.log_index == log_index) | ||
if chain_id: | ||
query = query.where(DexSwap.chain_id == chain_id) | ||
query_count = query.where(DexSwap.chain_id == chain_id) | ||
if start_timestamp: | ||
query = query.where(DexSwap.timestamp >= start_timestamp) | ||
query_count = query.where(DexSwap.timestamp >= start_timestamp) | ||
if end_timestamp: | ||
query = query.where(DexSwap.timestamp <= end_timestamp) | ||
query_count = query.where(DexSwap.timestamp <= end_timestamp) | ||
if start_block_number: | ||
query = query.where(DexSwap.block_number >= start_block_number) | ||
query_count = query.where(DexSwap.block_number >= start_block_number) | ||
if end_block_number: | ||
query = query.where(DexSwap.block_number <= end_block_number) | ||
query_count = query.where(DexSwap.block_number <= end_block_number) | ||
|
||
result = await session.execute(query) | ||
swaps = result.scalars().all() | ||
|
||
# # Return the count in header | ||
# # TODO: Can't be cached and very expensive... 2M records | ||
# result_count = await session.execute(query_count) | ||
# total_count = str(result_count.scalars().all()[0]) | ||
# response.headers["x-total-count"] = total_count | ||
|
||
response.headers["x-total-count"] = str(len(swaps)) | ||
return swaps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters