Skip to main content

Python API for TON (Telegram Open Network)

Project description

PyTONLib

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 pytonlib import TonlibClient


# downloading mainnet config
ton_config = requests.get('https://newton-blockchain.github.io/global.config.json').json()

# get running event loop
loop = asyncio.get_running_loop()

# init TonlibClient
client = TonlibClient(ls_index=0, # choose LiteServer index to connect
                      config=ton_config,
                      keystore='/tmp/ton_keystore',
                      loop=loop)

# 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 pytonlib import TonlibClient


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

    # init TonlibClient
    client = TonlibClient(ls_index=0, # choose LiteServer index to connect
                          config=ton_config,
                          keystore='/tmp/ton_keystore',
                          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.9.tar.gz (9.6 MB view details)

Uploaded Source

Built Distribution

pytonlib-0.0.9-py3-none-any.whl (9.7 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytonlib-0.0.9.tar.gz
  • Upload date:
  • Size: 9.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pytonlib-0.0.9.tar.gz
Algorithm Hash digest
SHA256 1775d7b3b911ec8ad9d0b01e9dbaecc199bf6918676fe5801580a6110ded1880
MD5 fbf279af631ce30ef337adcfd0db5d4d
BLAKE2b-256 b5acd01028922dd2100d8b781405f66c11bd3628952cb9b479285349e64c6ee8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytonlib-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 9.7 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for pytonlib-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 706b547a4a8c2a183529344d4e9c9a29f7820330f9d2d98b0858f312cca983de
MD5 ade2a6491e2727365010a90e2f9e4293
BLAKE2b-256 5bdd96be9bb521a293c89dd47ac850169696daaf9d856bdb3a96f200c61e83e2

See more details on using hashes here.

Supported by

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