feat(withdraw): add user withdrawal flow using CryptoPay

This commit is contained in:
2026-07-20 20:29:36 +05:00
parent 8d4d75f7fd
commit 8ae14f415f
11 changed files with 269 additions and 137 deletions
+38 -57
View File
@@ -1,82 +1,63 @@
from __future__ import annotations
import aiohttp
import logging
from runtime import get_cp
from config import CRYPTO_PAY_TOKEN
async def _get_usdt_rub_rate(session: aiohttp.ClientSession) -> float:
rate = 90.0
async with session.get(
"https://testnet-pay.crypt.bot/api/getExchangeRates",
headers={"Crypto-Pay-API-Token": CRYPTO_PAY_TOKEN},
) as response:
data = await response.json()
if data.get("ok"):
for item in data.get("result", []):
if item.get("source") == "USDT" and item.get("target") == "RUB":
rate = float(item["rate"])
break
return rate
from config import CRYPTO_PAY_ASSET
async def create_crypto_check(amount_rub: float) -> str | None:
try:
headers = {"Crypto-Pay-API-Token": CRYPTO_PAY_TOKEN}
async with aiohttp.ClientSession() as session:
usdt_rub = await _get_usdt_rub_rate(session)
amount_usdt = round(amount_rub / usdt_rub, 4)
async with session.post(
"https://testnet-pay.crypt.bot/api/createCheck",
headers=headers,
json={"asset": "USDT", "amount": str(amount_usdt)},
) as response:
data = await response.json()
if data.get("ok"):
return data["result"]["bot_check_url"]
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:
headers = {"Crypto-Pay-API-Token": CRYPTO_PAY_TOKEN}
async with aiohttp.ClientSession() as session:
usdt_rub = await _get_usdt_rub_rate(session)
amount_usdt = round(amount_rub / usdt_rub, 4)
async with session.post(
"https://testnet-pay.crypt.bot/api/createInvoice",
headers=headers,
json={
"asset": "USDT",
"amount": str(amount_usdt),
"description": f"Treasury +{amount_rub} RUB",
"payload": f"treasury_{amount_rub}",
},
) as response:
data = await response.json()
if data.get("ok"):
result = data["result"]
return result["pay_url"], int(result["invoice_id"]), amount_usdt
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:
headers = {"Crypto-Pay-API-Token": CRYPTO_PAY_TOKEN}
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://testnet-pay.crypt.bot/api/getInvoices?invoice_ids={invoice_id}",
headers=headers,
) as response:
data = await response.json()
if data.get("ok"):
items = data.get("result", {}).get("items", [])
return bool(items and items[0].get("status") == "paid")
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)