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://newton-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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytonlib-0.0.6.tar.gz
.
File metadata
- Download URL: pytonlib-0.0.6.tar.gz
- Upload date:
- Size: 6.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce59fa278a4e01f1ada9b5989d67ce297f325887a8a3e0e41f1e3f3777a30f6f |
|
MD5 | aaa7ccbf2cd8b724e5180f5706477bcf |
|
BLAKE2b-256 | 5a0857577d31ec16d6d0270bac62175237c99d6c404e030402225432f6fc0719 |
File details
Details for the file pytonlib-0.0.6-py3-none-any.whl
.
File metadata
- Download URL: pytonlib-0.0.6-py3-none-any.whl
- Upload date:
- Size: 8.4 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6737d11b687bf33521769a0f4dcc52b372ee2e0cd0de1db54d4d6b27af7f3be3 |
|
MD5 | 11a4fde092fb313351befbcc8f2eecc2 |
|
BLAKE2b-256 | 5c4148e182c4fe12e88793984a0b8db05c9c5b7cc489ad647dcac29aa319a236 |