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.69.tar.gz (27.4 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.69-py3-none-any.whl (27.5 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytonlib-0.0.69.tar.gz
  • Upload date:
  • Size: 27.4 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.69.tar.gz
Algorithm Hash digest
SHA256 694335fa3c87ac2c7f63f4eee970d32bea395241437434780e2fe6ae412252d8
MD5 7c7aaea6d9a11df7ef6849e5ca67524d
BLAKE2b-256 caa0f0e76b55f32323c9fcc26cf3026db05d8c59c4dd9ecb95e75feefd3e033d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytonlib-0.0.69-py3-none-any.whl
  • Upload date:
  • Size: 27.5 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.69-py3-none-any.whl
Algorithm Hash digest
SHA256 f55d8682f1c4fa34cb7a1d0f661c12aff55e40dd7faa11b2d89e471fd2986c50
MD5 6b4781295cf7a764df96bcd7bf25779d
BLAKE2b-256 8a6408b7594f187b76e3b8531ca756db84f8dddef115114564c974ba1a0ebd90

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