TON Library Documentation

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.

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_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