For the complete documentation index, see llms.txt. This page is also available as Markdown.

TON Blockchain

Introduction

The TON Library (TonLib) is a powerful addition to TeleBot Creator that enables seamless integration with The Open Network blockchain. With TonLib, you can create wallets, check balances, send TON, work with jettons (TON's tokens), and integrate TON Connect for user wallet connections.

Note: This is a development version of the TON Library introduced in version 4.9.0. Most bugs have been fixed and the library is stable for production use.

Getting Started

Unlike other libraries, TonLib doesn't require an import statement. It's globally available in your bot code.

# ❌ Don't use imports
# import TonLib  # This will cause an error

# ✅ Instead, use the library directly
wallet = libs.TonLib.generateWallet()

Core Functions

Wallet Management

generateWallet()

Creates a new TON wallet and returns its address and mnemonic phrase.

setKeys(mnemonics)

Stores a mnemonic phrase for later use.

getWalletAddress(mnemonics=None)

Retrieves the wallet address from a mnemonic phrase or from stored keys.

TON Operations

getBalance(address, api_key=None, endpoint=None)

Checks the TON balance of an address.

sendTON(to_address, amount, comment=None, mnemonics=None, api_key=None, endpoint=None, is_testnet=False)

Sends TON to another address.

checkTONTransaction(address, api_key=None, endpoint=None, limit=10)

Gets the recent transactions for an address.

TON Connect Integration

create_ton_connect_session(user_id, expiry_seconds=86400)

Creates a TON Connect session for wallet connection.

verify_ton_connect_session(session_id)

Checks if a wallet has connected to the session.

register_ton_connect_wallet(session_id, wallet_address)

Marks a session as connected and stores the connected wallet address. Use this in your callback/handler once the user approves the connection on their wallet, so that subsequent verify_ton_connect_session calls report the wallet.

Returns: A dict with status ("success" or "error"), a message, and on success the wallet_address. Errors are returned for unknown or expired sessions.

create_ton_connect_payload(callback_url, items=None, return_url=None)

Builds a raw TON Connect payload and deep link (connect_url) for custom request flows. This is the lower-level primitive used by request_ton_transaction / request_jetton_transfer; call it directly when you need to bundle custom items.

Returns: A dict with request_id, connect_url, and the raw payload.

request_ton_transaction(to_address, amount, comment=None, callback_url="", return_url=None)

Requests a TON transfer from a connected wallet.

Jetton Operations

get_jetton_metadata(jetton_master_address, api_key=None, endpoint=None)

Retrieves information about a Jetton (token).

get_jetton_wallet_address(owner_address, jetton_master_address, api_key=None, endpoint=None)

Resolves the address of the Jetton wallet that an owner holds for a given Jetton master contract. (Each owner has a distinct Jetton wallet per token.) get_jetton_balance uses this internally, but you can call it directly when you need the wallet address itself.

get_jetton_balance(owner_address, jetton_master_address, api_key=None, endpoint=None)

Checks the Jetton balance of an address.

request_jetton_transfer(to_address, jetton_master_address, amount, comment=None, callback_url="", return_url=None)

Requests a Jetton transfer from a connected wallet.

Examples

Creating a Simple TON Wallet Bot

TON Connect Integration Example

Best Practices

  1. Security: Never store sensitive mnemonic phrases in plain text. Consider encrypting them or using a secure key management solution.

  2. Error Handling: Always wrap TON operations in try/except blocks to handle potential errors gracefully.

  3. Testnet First: When developing, use the testnet (is_testnet=True) before moving to mainnet.

  4. Rate Limiting: Be mindful of API request limits when checking balances or transactions frequently.

  5. User Experience: Provide clear instructions and feedback to users, especially for wallet connection steps.

Limitations

  • Maximum code execution time is 120 seconds

  • time.sleep() function is limited to 10 seconds maximum

Further Resources

Last updated