load pools/tokens from db

This commit is contained in:
Tim
2024-04-18 16:30:03 -04:00
parent 0946d82f52
commit 6871187461
3 changed files with 18 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
import logging
from typing import TypedDict, Optional
from typing import TypedDict, Optional, NotRequired
from sqlalchemy.orm import Mapped, mapped_column
@@ -15,7 +15,7 @@ class TokenDict (TypedDict):
s: str
d: int
w: Optional[bool] # approved ("w"hitelisted)
x: Optional[dict] # extra data
x: NotRequired[dict] # extra data
# OldTokenDict is the primary dict we use in-memory, with basic JSON-able types
@@ -28,7 +28,7 @@ class OldTokenDict (TypedDict):
symbol: str
decimals: int
approved: bool # whether this token is in the whitelist or not
x: Optional[dict] # extra data
x: NotRequired[dict] # extra data
# the database object is primarily write-only so we are able to index queries for pools-by-token from the nodejs server

View File

@@ -3,16 +3,18 @@ import logging
from datetime import datetime
from typing import Optional
from sqlalchemy.exc import NoResultFound
from web3.exceptions import ContractLogicError
from web3.types import EventData
from dexorder import dec, ADDRESS_0, from_timestamp
from dexorder import dec, ADDRESS_0, from_timestamp, db
from dexorder.addrmeta import address_metadata
from dexorder.base.chain import current_chain
from dexorder.base.orderlib import Exchange
from dexorder.blockstate import BlockDict
from dexorder.blockstate.blockdata import K, V
from dexorder.blocks import get_block_timestamp
from dexorder.database.model import Pool
from dexorder.database.model.pool import OldPoolDict
from dexorder.metadata import is_generating_metadata
from dexorder.tokens import get_token
@@ -33,6 +35,10 @@ async def load_pool(address: str) -> OldPoolDict:
found = None
chain_id = current_chain.get().id
# todo other exchanges
pool = db.session.get(Pool, (chain_id, address))
if pool is not None:
return OldPoolDict(type='Pool', chain=chain_id, address=address, exchange=pool.exchange.value,
base=pool.base, quote=pool.quote, fee=pool.fee, decimals=pool.decimals)
try:
v3 = UniswapV3Pool(address)
t0, t1 = await asyncio.gather(v3.token0(), v3.token1())

View File

@@ -4,10 +4,11 @@ from typing import Optional
from eth_abi.exceptions import InsufficientDataBytes
from web3.exceptions import ContractLogicError, BadFunctionCallOutput
from dexorder import ADDRESS_0, config
from dexorder import ADDRESS_0, config, db
from dexorder.addrmeta import address_metadata
from dexorder.base.chain import current_chain
from dexorder.contract import ERC20, ContractProxy, CONTRACT_ERRORS
from dexorder.database.model import Token
from dexorder.database.model.token import OldTokenDict
from dexorder.metadata import get_metadata
@@ -27,7 +28,12 @@ async def get_token(address) -> Optional[OldTokenDict]:
async def load_token(address: str) -> Optional[OldTokenDict]:
contract = ERC20(address)
chain_id = current_chain.get().id
token = db.session.get(Token, (chain_id, address))
if token is not None:
return OldTokenDict(type='Token', chain=chain_id, address=address,
name=token.name, symbol=token.symbol, decimals=token.decimals,
approved=token.approved)
async def get_string_or_bytes32(func_name):
try:
result = await getattr(contract, func_name)()