Skip to main content

Python API for TON (Telegram Open Network)

Project description

PyTONLib

PyPI PyPI - Python Version Github last commit

This is standalone Python library based on libtonlibjson, the functionality is similar to the ton-http-api with the following restrictions:

  • a client can connect to only one LiteServer;
  • a client is asyncronious;
  • no requests cache.

Installation

From PyPi

Currently, the library works for Windows, Mac and Linux only on Intel CPUs:

  • (Windows) Install OpenSSL v1.1.1 for Win64 from here.
  • Install Python 3 package: pip3 install pytonlib.

Docker

In this repo Compose file is provided to deploy the example of service with pytonlib:

docker-compose -f docker-compose.jupyter.yaml build
docker-compose -f docker-compose.jupyter.yaml up -d

Jupyter Notebook will be available on port 3100 (http://localhost:3100).

Examples

We recommend to use IPython or Jupyter Notebook for prototyping because they allow to run async code. An example of running async code from script could be found in the end of this section.

  • Connecting to the first LiteServer in mainnet config:
import requests
import asyncio
from pathlib import Path

from pytonlib import TonlibClient


# downloading mainnet config
ton_config = requests.get('https://ton.org/global.config.json').json()

# create keystore directory for tonlib
keystore_dir = '/tmp/ton_keystore'
Path(keystore_dir).mkdir(parents=True, exist_ok=True)

# init TonlibClient
client = TonlibClient(ls_index=0, # choose LiteServer index to connect
                      config=ton_config,
                      keystore=keystore_dir)

# init tonlibjson
await client.init()
  • Reading blocks info:
masterchain_info = await client.get_masterchain_info()
block_header = await client.get_block_header(**masterchain_info['last'])
shards = await client.get_shards(master_seqno=masterchain_info['last']['seqno'])
  • Reading Block Transactions for masterchain block:
masterchain_info = await client.get_masterchain_info()
txs = await client.get_block_transactions(**masterchain_info['last'], count=10)
  • Running async code from script:
import requests
import asyncio
from pathlib import Path

from pytonlib import TonlibClient


async def main():
    loop = asyncio.get_running_loop()
    ton_config = requests.get('https://ton.org/global.config.json').json()

    # create keystore directory for tonlib
    keystore_dir = '/tmp/ton_keystore'
    Path(keystore_dir).mkdir(parents=True, exist_ok=True)
    
    # init TonlibClient
    client = TonlibClient(ls_index=0, # choose LiteServer index to connect
                          config=ton_config,
                          keystore=keystore_dir,
                          loop=loop)
    
    # init tonlibjson
    await client.init()
    
    # reading masterchain info
    masterchain_info = await client.get_masterchain_info()

    # closing session
    await client.close()


if __name__ == '__main__':
    asyncio.run(main())

Running tests

To run tests in asyncio mode use the following command:

PYTHONPATH=./ pytest --asyncio-mode=strict tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytonlib-0.0.70.tar.gz (32.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytonlib-0.0.70-py3-none-any.whl (32.9 MB view details)

Uploaded Python 3

File details

Details for the file pytonlib-0.0.70.tar.gz.

File metadata

  • Download URL: pytonlib-0.0.70.tar.gz
  • Upload date:
  • Size: 32.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytonlib-0.0.70.tar.gz
Algorithm Hash digest
SHA256 0d765d85964ee83c35cdb9df8fc534ce61244566d1ba5c179ce5240a957d31b6
MD5 7fdf7f4886f26286f2af1e79aca9c202
BLAKE2b-256 8e483f8eb8cc756532ae4b45daf9ab40b83911d0a7c3c0b9c0549058b1ec0a49

See more details on using hashes here.

File details

Details for the file pytonlib-0.0.70-py3-none-any.whl.

File metadata

  • Download URL: pytonlib-0.0.70-py3-none-any.whl
  • Upload date:
  • Size: 32.9 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pytonlib-0.0.70-py3-none-any.whl
Algorithm Hash digest
SHA256 8074017fdf17df2cecc333a361f1a3c463845161a90c2c06facd74ac01574205
MD5 52f52f264da4c4c1612bc79600359750
BLAKE2b-256 fe7d45ca28995206eb9f75fea42fdc4bfd58fb66365fdb67f24c669e62e9cba0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page