fix: tonutils 2.0.0 API — as_hex property, wallet.refresh(), balance as int nanotons

This commit is contained in:
bohd4nx
2026-03-05 05:04:17 +02:00
parent 64f8058c60
commit 2d7860682d
6 changed files with 22 additions and 18 deletions
-4
View File
@@ -20,10 +20,6 @@ logs/
# Environment variables # Environment variables
.env .env
# Test files
tests/
*.test.py
# System files # System files
.DS_Store .DS_Store
Thumbs.db Thumbs.db
-1
View File
@@ -22,7 +22,6 @@ DEVICE: str = json.dumps({
# Each method merges these with its own "referer" and "x-aj-referer". # Each method merges these with its own "referer" and "x-aj-referer".
BASE_HEADERS: dict[str, str] = { BASE_HEADERS: dict[str, str] = {
"accept": "application/json, text/javascript, */*; q=0.01", "accept": "application/json, text/javascript, */*; q=0.01",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "en-US,en;q=0.9,uk;q=0.8,ru;q=0.7", "accept-language": "en-US,en;q=0.9,uk;q=0.8,ru;q=0.7",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8", "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"origin": "https://fragment.com", "origin": "https://fragment.com",
+10 -2
View File
@@ -14,11 +14,19 @@ async def get_fragment_hash(
headers: dict[str, str], headers: dict[str, str],
page_url: str, page_url: str,
) -> str: ) -> str:
# Must look like a real browser navigation — not an XHR — otherwise Fragment
# returns JSON (no hash in it) instead of full HTML.
page_headers = { page_headers = {
**headers, k: v for k, v in headers.items()
if k not in ("accept", "accept-encoding", "content-type", "x-requested-with", "x-aj-referer")
}
page_headers.update({
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"referer": "https://fragment.com/", "referer": "https://fragment.com/",
} "sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"upgrade-insecure-requests": "1",
})
async with httpx.AsyncClient(cookies=cookies) as client: async with httpx.AsyncClient(cookies=cookies) as client:
response = await client.get(page_url, headers=page_headers) response = await client.get(page_url, headers=page_headers)
+7 -6
View File
@@ -1,7 +1,7 @@
import logging import logging
from tonutils.client import ToncenterV3Client from tonutils.clients import ToncenterClient
from tonutils.wallet import WalletV5R1 from tonutils.contracts.wallet import WalletV5R1
from app.core import config from app.core import config
from app.core.exceptions import TransactionError, WalletError from app.core.exceptions import TransactionError, WalletError
@@ -19,15 +19,16 @@ async def process_transaction(transaction_data: dict) -> str:
"The API response is missing expected 'transaction.messages' data." "The API response is missing expected 'transaction.messages' data."
) )
client = ToncenterV3Client(api_key=config.API_KEY, is_testnet=False) client = ToncenterClient(api_key=config.API_KEY)
wallet, _, _, _ = WalletV5R1.from_mnemonic(client=client, mnemonic=config.SEED) wallet, _, _, _ = WalletV5R1.from_mnemonic(client=client, mnemonic=config.SEED)
# Check balance before broadcasting # Check balance before broadcasting
try: try:
balance = await wallet.balance() await wallet.refresh()
if float(balance) < 0.056: balance_ton = wallet.balance / 1_000_000_000
if balance_ton < 0.056:
raise WalletError( raise WalletError(
f"TON wallet balance is too low: {balance} TON. " f"TON wallet balance is too low: {balance_ton:.2f} TON. "
"Minimum required is 0.056 TON." "Minimum required is 0.056 TON."
) )
except WalletError: except WalletError:
+4 -4
View File
@@ -4,8 +4,8 @@ import logging
from typing import Any from typing import Any
import httpx import httpx
from tonutils.client import ToncenterV3Client from tonutils.clients import ToncenterClient
from tonutils.wallet import WalletV5R1 from tonutils.contracts.wallet import WalletV5R1
from app.core import config from app.core import config
from app.core.constants import DEVICE from app.core.constants import DEVICE
@@ -17,12 +17,12 @@ logger = logging.getLogger(__name__)
async def get_account_info() -> dict[str, Any]: async def get_account_info() -> dict[str, Any]:
try: try:
client = ToncenterV3Client(api_key=config.API_KEY, is_testnet=False) client = ToncenterClient(api_key=config.API_KEY)
wallet, pub_key, _, _ = WalletV5R1.from_mnemonic(client=client, mnemonic=config.SEED) wallet, pub_key, _, _ = WalletV5R1.from_mnemonic(client=client, mnemonic=config.SEED)
boc = wallet.state_init.serialize().to_boc() boc = wallet.state_init.serialize().to_boc()
return { return {
"address": wallet.address.to_str(False, False), "address": wallet.address.to_str(False, False),
"publicKey": pub_key.hex(), "publicKey": pub_key.as_hex,
"chain": "-239", "chain": "-239",
"walletStateInit": base64.b64encode(boc).decode(), "walletStateInit": base64.b64encode(boc).decode(),
} }
+1 -1
View File
@@ -1,4 +1,4 @@
python-dotenv==1.2.2 python-dotenv==1.2.2
asyncio==4.0.0 asyncio==4.0.0
httpx==0.28.1 httpx==0.28.1
tonutils==2.0.0 tonutils[pytoniq]==2.0.0