from sqlalchemy import Column, Integer, String, DateTime, JSON, Boolean from sqlalchemy.orm import relationship from app.core._defaults import DEFAULT_ASSET_INITOBJ from app.core.models.base import AlchemyBase class Asset(AlchemyBase): __tablename__ = 'assets' id = Column(Integer, autoincrement=True, primary_key=True) symbol = Column(String(32), nullable=False) name = Column(String(256), nullable=False) decimals = Column(Integer, nullable=False) network = Column(String(32), nullable=True) address = Column(String(1024), nullable=True) meta = Column(JSON, nullable=False, default={}) rates = Column(JSON, nullable=False, default={}) created = Column(DateTime, nullable=False, default=0) is_active = Column(Boolean, nullable=False, default=True) balances = relationship('UserBalance', back_populates='asset') internal_transactions = relationship('InternalTransaction', back_populates='asset') @classmethod def init_table(cls, engine): AlchemyBase.metadata.create_all(engine) @classmethod async def find_async(cls, session, **kwargs): from sqlalchemy import select, func if 'symbol' in kwargs: kwargs['symbol'] = kwargs['symbol'].upper() result = await session.execute(select(cls).filter_by(**kwargs)) row = result.scalars().first() if row: return row any_count = (await session.execute(select(func.count()).select_from(cls))).scalar() or 0 if any_count == 0: init_asset = cls(**DEFAULT_ASSET_INITOBJ) session.add(init_asset) await session.commit() return await cls.find_async(session, **kwargs) raise Exception(f"Asset not found: {kwargs}")