mirror of
https://github.com/bohd4nx/FragmentAPI.git
synced 2026-07-25 06:14:29 +00:00
9f11ccb3ee
- Moved PaymentMethod and other enums from models to enums.py for better organization. - Refactored imports in various files to reflect the new structure. - Created new model files for ads, anonymous numbers, giveaways, marketplace, purchases, and tonapi. - Removed the old models.py file to clean up the codebase. - Updated all relevant imports in the codebase to use the new model locations.
74 lines
2.9 KiB
Python
74 lines
2.9 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
from typing import TYPE_CHECKING
|
|
|
|
from pyfragment.core.constants import ADS_TOPUP_PAGE, DEVICE_INFO, GRAM_TOPUP_MAX, GRAM_TOPUP_MIN
|
|
from pyfragment.domains.payments import parse_required_payment_amount
|
|
from pyfragment.domains.tonapi.account import get_account_info
|
|
from pyfragment.domains.tonapi.transaction import process_transaction
|
|
from pyfragment.exceptions import (
|
|
ConfigurationError,
|
|
FragmentAPIError,
|
|
FragmentError,
|
|
UnexpectedError,
|
|
UserNotFoundError,
|
|
VerificationError,
|
|
)
|
|
from pyfragment.domains.ads.models import AdsTopupResult
|
|
|
|
if TYPE_CHECKING:
|
|
from pyfragment.client import FragmentClient
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def topup_gram(client: FragmentClient, username: str, amount: int, show_sender: bool = True) -> AdsTopupResult:
|
|
if not isinstance(amount, int) or not (GRAM_TOPUP_MIN <= amount <= GRAM_TOPUP_MAX):
|
|
raise ConfigurationError(ConfigurationError.INVALID_GRAM_AMOUNT)
|
|
|
|
try:
|
|
await client.call("updateAdsTopupState", {"mode": "new"}, page_url=ADS_TOPUP_PAGE)
|
|
|
|
result = await client.call("searchAdsTopupRecipient", {"query": username}, page_url=ADS_TOPUP_PAGE)
|
|
recipient = result.get("found", {}).get("recipient")
|
|
if not recipient:
|
|
raise UserNotFoundError(UserNotFoundError.NOT_FOUND.format(username=username))
|
|
|
|
result = await client.call("initAdsTopupRequest", {"recipient": recipient, "amount": amount}, page_url=ADS_TOPUP_PAGE)
|
|
required_payment_amount = parse_required_payment_amount(result)
|
|
req_id = result.get("req_id")
|
|
if not req_id:
|
|
raise FragmentAPIError(FragmentAPIError.NO_REQUEST_ID.format(context="GRAM (ex TON) topup"))
|
|
|
|
account = await get_account_info(client)
|
|
transaction = await client.call(
|
|
"getAdsTopupLink",
|
|
{
|
|
"account": json.dumps(account),
|
|
"device": json.dumps(DEVICE_INFO),
|
|
"transaction": 1,
|
|
"id": req_id,
|
|
"show_sender": int(show_sender),
|
|
},
|
|
page_url=ADS_TOPUP_PAGE,
|
|
)
|
|
if transaction.get("need_verify"):
|
|
raise VerificationError(VerificationError.KYC_REQUIRED)
|
|
|
|
tx_hash = await process_transaction(client, transaction, required_payment_amount=required_payment_amount)
|
|
return AdsTopupResult(transaction_id=tx_hash, username=username, amount=amount)
|
|
|
|
except FragmentError as exc:
|
|
logger.error(
|
|
"Failed to top up GRAM (ex TON) for user '%s' with %s GRAM (ex TON): %s", username, amount, exc, exc_info=True
|
|
)
|
|
raise
|
|
except Exception as exc:
|
|
logger.exception(
|
|
"Failed to top up GRAM (ex TON) for user '%s' with %s GRAM (ex TON) due to an unexpected error", username, amount
|
|
)
|
|
raise UnexpectedError(UnexpectedError.UNEXPECTED.format(exc=exc)) from exc
|