refactor: migrate to tonutils 2.0.0 and restructure codebase

- 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
This commit is contained in:
bohd4nx
2026-03-05 05:47:27 +02:00
parent b9c02646be
commit efcdda6b74
19 changed files with 274 additions and 159 deletions
+20 -15
View File
@@ -20,7 +20,7 @@ logger = logging.getLogger(__name__)
# Page-specific headers
HEADERS: dict[str, str] = {
**BASE_HEADERS,
"referer": ADS_PAGE,
"referer": ADS_PAGE,
"x-aj-referer": ADS_PAGE,
}
@@ -33,12 +33,14 @@ async def search_ads_recipient(
) -> str:
await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS, cookies=cookies,
headers=HEADERS,
cookies=cookies,
data={"mode": "new", "method": "updateAdsTopupState"},
)
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS, cookies=cookies,
headers=HEADERS,
cookies=cookies,
data={"query": username, "method": "searchAdsTopupRecipient"},
)
result = parse_json_response(resp, "searchAdsTopupRecipient")
@@ -60,7 +62,8 @@ async def init_ads_topup(
) -> str:
resp = await client.post(
f"https://fragment.com/api?hash={fragment_hash}",
headers=HEADERS, cookies=cookies,
headers=HEADERS,
cookies=cookies,
data={"recipient": recipient, "amount": amount, "method": "initAdsTopupRequest"},
)
result = parse_json_response(resp, "initAdsTopupRequest")
@@ -78,32 +81,34 @@ async def topup_ton(username: str, amount: int) -> dict:
return {"success": False, "error": "Amount must be an integer >= 1 TON."}
try:
cookies = load_cookies()
cookies = load_cookies()
fragment_hash = await get_fragment_hash(cookies, HEADERS, ADS_PAGE)
account = await get_account_info()
account = await get_account_info()
async with httpx.AsyncClient() as client:
recipient = await search_ads_recipient(client, fragment_hash, cookies, username)
req_id = await init_ads_topup(client, fragment_hash, cookies, recipient, amount)
req_id = await init_ads_topup(client, fragment_hash, cookies, recipient, amount)
tx_data = {
"account": json.dumps(account),
"device": DEVICE,
"account": json.dumps(account),
"device": DEVICE,
"transaction": 1,
"id": req_id,
"id": req_id,
"show_sender": 1,
"method": "getAdsTopupLink",
"method": "getAdsTopupLink",
}
transaction = await execute_transaction_request(client, HEADERS, cookies, account, tx_data, fragment_hash)
transaction = await execute_transaction_request(
client, HEADERS, cookies, account, tx_data, fragment_hash
)
tx_hash = await process_transaction(transaction)
return {
"success": True,
"data": {
"transaction_id": tx_hash,
"username": username,
"amount": amount,
"timestamp": int(time.time()),
"username": username,
"amount": amount,
"timestamp": int(time.time()),
},
}