mirror of
https://github.com/bohd4nx/FragmentAPI.git
synced 2026-07-25 06:14:29 +00:00
efcdda6b74
- Migrate from tonutils 0.5.x to 2.0.0 API: - ToncenterClient → TonapiClient with NetworkGlobalID - tonutils.client → tonutils.clients - tonutils.wallet → tonutils.contracts.wallet - pub_key.as_hex property, wallet.balance nanotons - async with client context manager - Centralize TON client init into initialize_ton_client() in wallet.py - Move process_transaction logic into wallet.py, transaction.py re-exports - Add WALLET_VERSION config (V4R2/V5R1, default V5R1) - Add WALLET_CLASSES and SUPPORTED_WALLET_VERSIONS to constants.py - Restore balance check before broadcasting - Wait for seqno confirmation after transfer (120×2s) to prevent duplicate seqno - Fix Fragment hash fetching: use browser navigation headers - Remove accept-encoding from BASE_HEADERS (httpx handles decompression) - Add cookies.example.json template - Add cookies.json to .gitignore
60 lines
2.1 KiB
Python
60 lines
2.1 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from tonutils.clients import TonapiClient
|
|
from tonutils.types import NetworkGlobalID
|
|
|
|
from app.core import config
|
|
from app.core.constants import WALLET_CLASSES
|
|
from app.core.exceptions import TransactionError, WalletError
|
|
from app.utils.decoder import clean_decode
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
async def process_transaction(transaction_data: dict) -> str:
|
|
logger.debug("transaction_data: %s", transaction_data)
|
|
|
|
if "transaction" not in transaction_data or "messages" not in transaction_data["transaction"]:
|
|
raise TransactionError(
|
|
"Fragment returned an invalid transaction payload. "
|
|
"The API response is missing expected 'transaction.messages' data."
|
|
)
|
|
|
|
client = TonapiClient(network=NetworkGlobalID.MAINNET, api_key=config.API_KEY)
|
|
async with client:
|
|
wallet_cls = WALLET_CLASSES[config.WALLET_VERSION]
|
|
wallet, _, _, _ = wallet_cls.from_mnemonic(client=client, mnemonic=config.SEED)
|
|
|
|
# Check balance before broadcasting
|
|
try:
|
|
await wallet.refresh()
|
|
balance_ton = wallet.balance / 1_000_000_000
|
|
if balance_ton < 0.056:
|
|
raise WalletError(
|
|
f"TON wallet balance is too low: {balance_ton:.2f} TON. "
|
|
"Minimum required is 0.056 TON."
|
|
)
|
|
except WalletError:
|
|
raise
|
|
except Exception as exc:
|
|
raise WalletError(f"Wallet balance check failed: {exc}") from exc
|
|
|
|
try:
|
|
message = transaction_data["transaction"]["messages"][0]
|
|
payload = clean_decode(message["payload"])
|
|
|
|
await wallet.refresh()
|
|
result = await wallet.transfer(
|
|
destination=message["address"],
|
|
amount=int(message["amount"]), # nanotons, not TON
|
|
body=payload,
|
|
)
|
|
|
|
return result
|
|
except (WalletError, TransactionError):
|
|
raise
|
|
except Exception as exc:
|
|
raise TransactionError(f"Transaction broadcast failed: {exc}") from exc
|
|
|