pytoniq
Pytoniq is a Python SDK for the TON Blockchain. This library extends pytoniq-core with native LiteClient
and ADNL
.
If you have any questions join Python - TON developers chat.
Documentation
GitBook
Installation
pip install pytoniq
Examples
You can find them in the examples folder.
LiteClient
General LiteClient usage examples
Client initializing
from pytoniq import LiteClient
async def main():
client = LiteClient.from_mainnet_config(
ls_i=0,
trust_level=2,
timeout=15
)
await client.connect()
await client.get_masterchain_info()
await client.reconnect()
await client.close()
""" or use it with context manager: """
async with LiteClient.from_mainnet_config(ls_i=0, trust_level=2, timeout=15) as client:
await client.get_masterchain_info()
Blocks transactions scanning
See BlockScanner
code here.
from pytoniq_core import BlockIdExt
from pytoniq import LiteClient
from examples.blocks.block_scanner import BlockScanner
async def handle_block(block: BlockIdExt):
if block.workchain == -1:
return
print(block)
transactions = await client.raw_get_block_transactions_ext(block)
for transaction in transactions:
print(transaction.in_msg)
client = LiteClient.from_mainnet_config(ls_i=14, trust_level=0, timeout=20)
async def main():
await client.connect()
await BlockScanner(client=client, block_handler=handle_block).run()
LiteBalancer
LiteBalancer
is constantly pinging LiteServers to identify "alive" peers.
When you make a request through LiteBalancer
, it forwards the request to the "best" peer -
the "alive" peer with the maximum last masterchain block seqno among all and minimum average response time.
LiteBalancer
can also retry the request if a asyncio.TimeoutError
occurs, but this must be explicitly set using the
LiteBalancer.set_max_retries(retries_num)
method.
client = LiteBalancer.from_mainnet_config(trust_level=1)
await client.start_up()
result = await client.run_get_method(address='EQBvW8Z5huBkMJYdnfAEM5JqTNkuWX3diqYENkWsIL0XggGG', method='seqno', stack=[])
await client.close_all()
""" or use it with context manager: """
async with LiteBalancer.from_mainnet_config(trust_level=1) as client:
result = await client.run_get_method(address='EQBvW8Z5huBkMJYdnfAEM5JqTNkuWX3diqYENkWsIL0XggGG', method='seqno', stack=[])
Moreover, one of the most important features of LiteBalancer
is that it detects archival LiteServers,
so you can do requests only to archival LiteServers providing True
for argument only_archive
in any method:
blk, _ = await client.lookup_block(-1, -2**63, 100, only_archive=True)
blk, _ = await client.lookup_block(-1, -2**63, 25000000, only_archive=True)
result = await client.run_get_method(address='EQBvW8Z5huBkMJYdnfAEM5JqTNkuWX3diqYENkWsIL0XggGG', method='seqno',
stack=[], block=blk, only_archive=True)
Blockstore
The library can prove all data it receives from a Liteserver (Learn about trust levels here).
If you want to use LiteClient
or LiteBalancer
with the zero trust level, at the first time run library will prove block link from the init_block
to the last masterchain block.
Last proved blocks will be stored in the .blockstore
folder. The file data contains ttl
and gen_utime
of the last synced key block, its data serialized according to the BlockIdExt
TL scheme (but in big–endian), last synced masterchain block data.
Filename is first 88 bytes of data described above with init block hash.
ADNL
from pytoniq.adnl.adnl import AdnlTransport, Node
adnl = AdnlTransport(timeout=3)
await adnl.start()
peer = Node('172.104.59.125', 14432, "/YDNd+IwRUgL0mq21oC0L3RxrS8gTu0nciSPUrhqR78=", adnl)
await adnl.connect_to_peer(peer)
await peer.disconnect()
await asyncio.sleep(10)
await adnl.close()
DHT
import time
from pytoniq.adnl.adnl import AdnlTransport
from pytoniq.adnl.dht import DhtClient, DhtNode
adnl = AdnlTransport(timeout=5)
client = DhtClient.from_mainnet_config(adnl)
await adnl.start()
foundation_adnl_addr = '516618cf6cbe9004f6883e742c9a2e3ca53ed02e3e36f4cef62a98ee1e449174'
resp = await client.find_value(key=DhtClient.get_dht_key_id(bytes.fromhex(foundation_adnl_addr)))
print(resp)
key = client.get_dht_key(id_=adnl.client.get_key_id())
ts = int(time.time())
value_data = {
'addrs': [
{
"@type": "adnl.address.udp",
"ip": 1111111,
"port": 12000
}
],
'version': ts,
'reinit_date': ts,
'priority': 0,
'expire_at': 0,
}
value = client.schemas.serialize(client.schemas.get_by_name('adnl.addressList'), value_data)
stored = await client.store_value(
key=key,
value=value,
private_key=adnl.client.ed25519_private.encode(),
ttl=100,
try_find_after=False
)
print(stored)
await client.close()