from __future__ import annotations import logging from runtime import get_cp from config import CRYPTO_PAY_ASSET async def create_crypto_check(amount_rub: float) -> str | None: try: cp = get_cp() amount_asset = await cp.exchange( amount=amount_rub, source="RUB", target=CRYPTO_PAY_ASSET ) createdCheck = await cp.create_check( amount=amount_asset, asset=CRYPTO_PAY_ASSET ) check_image_url = await createdCheck.get_image(fiat="RUB") return createdCheck.bot_check_url, check_image_url except Exception as exc: logging.error("crypto check: %s", exc) return None async def create_treasury_invoice(amount_rub: float) -> tuple[str, int, float] | None: try: cp = get_cp() createdInvoice = await cp.create_invoice( amount=amount_rub, currency_type="fiat", accepted_assets=[CRYPTO_PAY_ASSET], fiat="RUB", ) amount_asset = round( await cp.exchange(amount=amount_rub, source="RUB", target=CRYPTO_PAY_ASSET), 2, ) return (createdInvoice.pay_url, int(createdInvoice.invoice_id), amount_asset) except Exception as exc: logging.error("create_treasury_invoice: %s", exc) return None async def check_treasury_invoice(invoice_id: int) -> bool: try: cp = get_cp() paidInvoice = await cp.get_invoice(invoice_id) return paidInvoice.status == "paid" except Exception as exc: logging.error("check_treasury_invoice: %s", exc) return False