TelebotCreator
  • Introduction to Telebot Creator
  • Getting Started
  • Command in TPY
  • TPY Language Reference
  • TBC Libraries (Libs)
  • Bot Features and Functionalities
  • Advanced Features
  • Real-World Use Cases
  • Tips, Best Practices, and Troubleshooting
  • Frequently Asked Questions (FAQs)
  • Glossary and Key Concepts
  • Broadcast Function In TBC
  • Coinbase Library TBC
  • TON Library Documentation
  • Version 4.7.0 Update
  • Version 4.8.0 Update
  • Version 4.9.0 Update
  • Conclusion and Next Steps
  • Crypto libraries Documentation
Powered by GitBook
On this page
  • Introduction
  • Getting Started
  • Core Functions
  • Wallet Management
  • TON Operations
  • TON Connect Integration
  • Jetton Operations
  • Examples
  • Creating a Simple TON Wallet Bot
  • TON Connect Integration Example
  • Best Practices
  • Limitations
  • Further Resources

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.

result = libs.TonLib.generateWallet()
address = result["address"]
mnemonics = result["mnemonics"]

setKeys(mnemonics)

Stores a mnemonic phrase for later use.

libs.TonLib.setKeys("word1 word2 ... word24")

getWalletAddress(mnemonics=None)

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

# Using stored keys
address = libs.TonLib.getWalletAddress()

# Or with specific mnemonics
address = libs.TonLib.getWalletAddress("word1 word2 ... word24")

TON Operations

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

Checks the TON balance of an address.

balance = libs.TonLib.getBalance("EQD...")

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

Sends TON to another address.

result = libs.TonLib.sendTON(
    to_address="EQD...",
    amount=0.1,  # In TON
    comment="Payment for service"
)

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

Gets the recent transactions for an address.

transactions = libs.TonLib.checkTONTransaction("EQD...")
for tx in transactions:
    if tx["type"] == "incoming":
        from_address = tx["from"]
        amount = tx["amount"]
        # Process the incoming transaction...

TON Connect Integration

create_ton_connect_session(user_id, expiry_seconds=86400)

Creates a TON Connect session for wallet connection.

session = libs.TonLib.create_ton_connect_session(user_id="12345")
connect_url = session["connect_url"]
# Send this URL to the user for connection

verify_ton_connect_session(session_id)

Checks if a wallet has connected to the session.

status = libs.TonLib.verify_ton_connect_session(session_id)
if status["status"] == "connected":
    wallet_address = status["wallet_address"]
    # User wallet is connected

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

Requests a TON transfer from a connected wallet.

request = libs.TonLib.request_ton_transaction(
    to_address="EQD...",
    amount=1.5,
    comment="Donation",
    callback_url="https://your-app.com/callback"
)
# Send the request["connect_url"] to the user

Jetton Operations

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

Retrieves information about a Jetton (token).

metadata = libs.TonLib.get_jetton_metadata("EQD...")
name = metadata["name"]
symbol = metadata["symbol"]
total_supply = metadata["total_supply"]

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

Checks the Jetton balance of an address.

balance = libs.TonLib.get_jetton_balance(
    owner_address="EQD...",
    jetton_master_address="EQD..."
)

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

Requests a Jetton transfer from a connected wallet.

request = libs.TonLib.request_jetton_transfer(
    to_address="EQD...",
    jetton_master_address="EQD...",
    amount=10,
    callback_url="https://your-app.com/callback"
)

Examples

Creating a Simple TON Wallet Bot

def handle_start(message):
    # Generate a new wallet
    wallet = libs.TonLib.generateWallet()
    
    # Store the mnemonics
    libs.TonLib.setKeys(wallet["mnemonics"])
    
    # Send information to the user
    bot.send_message(
        chat_id=message.chat.id,
        text=f"Your new TON wallet address:\n`{wallet['address']}`\n\n"
             f"Seed phrase (keep it safe):\n`{wallet['mnemonics']}`",
        parse_mode="Markdown"
    )

def handle_balance(message):
    # Get wallet address
    address = libs.TonLib.getWalletAddress()
    
    # Get balance
    balance = libs.TonLib.getBalance(address)
    
    # Send balance to user
    bot.send_message(
        chat_id=message.chat.id,
        text=f"Your balance: {balance} TON"
    )

TON Connect Integration Example

# Step 1: Initialize wallet connection
def handle_connect_wallet(message):
    user_id = str(message.from_user.id)
    session = libs.TonLib.create_ton_connect_session(user_id)
    
    # Store session_id for later verification
    db.set_user_data(user_id, "ton_session_id", session["session_id"])
    
    # Send connection link to user
    bot.send_message(
        chat_id=message.chat.id,
        text=f"Connect your TON wallet by opening this link:\n{session['connect_url']}"
    )

# Step 2: Check wallet connection status
def handle_check_connection(message):
    user_id = str(message.from_user.id)
    session_id = db.get_user_data(user_id, "ton_session_id")
    
    if not session_id:
        return bot.send_message(message.chat.id, "You haven't started a connection yet")
    
    status = libs.TonLib.verify_ton_connect_session(session_id)
    
    if status["status"] == "connected":
        bot.send_message(
            chat_id=message.chat.id,
            text=f"Wallet connected: {status['wallet_address']}"
        )
    else:
        bot.send_message(
            chat_id=message.chat.id,
            text="Wallet not connected yet. Try again later."
        )

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

PreviousCoinbase Library TBCNextVersion 4.7.0 Update

Last updated 22 days ago

The Open Network Documentation
TON Connect Documentation
TBC Libraries Documentation