Getting Started with TON Blockchain using aiotx: A Comprehensive Guide

Andreevichprudnikov
2 min readJul 8, 2024

--

Are you looking to interact with the TON (The Open Network) blockchain in your Python projects? Look no further! The aiotx library provides a simple and intuitive way to work with TON. In this guide, we’ll walk you through the process of setting up and using aiotx for TON operations.

Installation

First, let’s install the aiotx library. Open your terminal and run:

pip install aiotx

Importing and Initializing the TON Client

To get started, import the TON client from aiotx and create an instance:

from aiotx.clients import AioTxTONClient
import asyncio
# Initialize the TON client
ton_client = AioTxTONClient("https://toncenter.com/api/v2/jsonRPC")

Note: Replace the URL with your preferred TON node if you’re using a different one.

Generating a New Wallet

async def generate_wallet():
mnemonic, address, raw_address = await ton_client.generate_address()
print(f"Mnemonic: {mnemonic}")
print(f"Address: {address}")
print(f"Raw Address: {raw_address}")
asyncio.run(generate_wallet())

This will output the mnemonic phrase, user-friendly address, and raw address for your new wallet.

Checking Wallet Balance

To check the balance of a wallet:

async def check_balance(address):
balance = await ton_client.get_balance(address)
print(f"Balance: {ton_client.from_nano(balance)} TON")
asyncio.run(check_balance("YOUR_WALLET_ADDRESS"))

Replace “YOUR_WALLET_ADDRESS” with the actual address you want to check.

Sending TON

To send TON to another address:

async def send_ton(mnemonic, to_address, amount):
amount_nano = ton_client.to_nano(amount)
tx_hash = await ton_client.send(mnemonic, to_address, amount_nano)
print(f"Transaction Hash: {tx_hash}")
mnemonic = "your wallet mnemonic phrase here"
to_address = "recipient_address_here"
amount = 0.1 # Amount in TON
asyncio.run(send_ton(mnemonic, to_address, amount))

Working with Jettons (TON Tokens)

aiotx also supports operations with Jettons. Here’s how to check a Jetton balance and transfer Jettons:

async def check_jetton_balance(address, jetton_master_address):

balance = await ton_client.get_jetton_wallet_balance(address, jetton_master_address)
print(f"Jetton Balance: {balance}")

async def transfer_jettons(mnemonic, to_address, jetton_master_address, amount, memo):
tx_hash = await ton_client.transfer_jettons(
mnemonic, to_address, jetton_master_address, amount, memo
)
print(f"Jetton Transfer Transaction Hash: {tx_hash}")

# Example usage
user_address = "your_address_here"
jetton_master_address = "jetton_master_address_here"
to_address = "recipient_address_here"
amount = 1000000000 # 1 Jetton (assuming 9 decimal places)
memo = "Payment for services"
asyncio.run(check_jetton_balance(user_address, jetton_master_address))
asyncio.run(transfer_jettons(mnemonic, to_address, jetton_master_address, amount, memo))

Monitoring TON Blockchain

aiotx provides a powerful monitoring feature. Here’s how to monitor new blocks and transactions:


@ton_client.monitor.on_block
async def handle_block(block):
print(f"New block: {block}")

@ton_client.monitor.on_transaction
async def handle_transaction(transaction):
print(f"New transaction: {transaction['hash']}")

@ton_client.monitor.on_block_transactions
async def handle_block_transactions(transactions):
print(f"Transactions in shard: {transactions}")

await ton_client.start_monitoring()

try:
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
ton_client.stop_monitoring()

This script will continuously print new blocks and transactions until you stop it with Ctrl+C.

Conclusion

With these steps, you’re now equipped to interact with the TON blockchain using aiotx. From generating wallets and checking balances to sending TON, working with Jettons, and monitoring the blockchain, aiotx provides a comprehensive set of tools for TON development.

Remember to handle exceptions and implement proper error checking in your production code. Also, always keep your mnemonics and private keys secure.

Happy coding with TON and aiotx!

--

--