Skip to main content

TonConnect

TonConnect is a Python library that allows you to connect to TON wallets, manage connections, handle messages, and send requests to the wallet. It provides a convenient way to interact with TON wallets and perform various operations.

Installation

To install TonConnect, you can use pip:

pip install ton-connect

Usage

TonConnect Class

The TonConnect class is responsible for managing connections to TON wallets. It provides methods to connect to a wallet, restore a connection, handle messages, start and stop listeners, and send requests to the wallet.

Initializing TonConnect

To initialize the TonConnect class, you need to provide the manifest URL and a storage instance. The manifest URL is the URL to the manifest file of your app, and the storage instance is used to store connection data. Note that the first argument to DictStorage is the user id.

from ton_connect.connector import TonConnect
from ton_connect.storage import DictBridgeStorage


manifest_url = "https://example.com/manifest.json"
storage = DictBridgeStorage("user_id")

ton_connect = TonConnect(manifest_url, storage)

Connecting to a Wallet

To connect to a wallet, you need to provide a WalletApp instance. The WalletApp class represents a wallet app and contains information such as the app name, bridge URL, and universal URL.

You can also use the TonConnect.get_wallets method to get a list of supported wallet apps.

from ton_connect.model.app.wallet import WalletApp
from ton_connect.connector import TonConnect

wallets = await TonConnect.get_wallets()
wallet = wallets[0]

connect_url = await ton_connect.connect(wallet)
print("Connect URL:", connect_url)

Restoring a Connection

To restore a connection to a wallet, you can use the restore_connection method. This method restores the connection using the stored connection data.

await ton_connect.restore_connection(wallet)

Sending Requests

The TonConnect class provides methods to send various requests to the wallet. For example, you can send a transaction request using the SendTransactionRequest class.

from ton_connect.model.app.request import SendTransactionRequest, SendTransactionMessage

request = SendTransactionRequest(
    address="0:1234567890abcdef",
    network=1,
    messages=[
        SendTransactionMessage(
            address="0:abcdef1234567890",
            amount="1000000000"
        )
    ]
)

response = await ton_connect.send(wallet.app_name, request)
print("Response:", response)

Listening for Wallet Events

The TonConnect class allows you to listen for wallet events by adding event listeners. You can add event listeners for events such as connect, disconnect, and errors.

from ton_connect.model.wallet.event import WalletEventName
from ton_connect.connector import ConnectorEvent

async def on_connect(event: ConnectorEvent):
    print("Connected:", event)

async def on_disconnect(event: ConnectorEvent):
    print("Disconnected:", event)

ton_connect.listen(WalletEventName.CONNECT, on_connect)
ton_connect.listen(WalletEventName.DISCONNECT, on_disconnect)

License

This project is licensed under the MIT License.

Release files for ton-connect-async 0.2.19

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ton-connect-async 0.2.19
File Size Uploaded
ton_connect_async-0.2.19.tar.gz 17.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ton-connect-async 0.2.19
File Interpreter ABI Platform
ton_connect_async-0.2.19-py3-none-any.whl Python 3 none any Details

Total release size: 39.4 kB

Release files / ton_connect_async-0.2.19.tar.gz

Download URL ton_connect_async-0.2.19.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
008f4096419fb00471fd382a3a40c671782b70ea613790b2b30b07b6e72d2fad
BLAKE2b-256 checksum
How to use checksums
a70f6770931e8d461091a7eecbcb44314bc9a06305b5806e457e1a5bd7a79e62
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.11.1 Darwin/24.4.0

Release files / ton_connect_async-0.2.19-py3-none-any.whl

Download URL ton_connect_async-0.2.19-py3-none-any.whl
Size 22.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3870df15243d5e6b17c36a546cc6bf833527b0cd235f5e50bc9e17f5ad9132e2
BLAKE2b-256 checksum
How to use checksums
05817a10ae86c176a5b41eb40a6c70bf38817d8fc3658d244ad5318e9b938057
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.11.1 Darwin/24.4.0
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page