Package: ton-client-py
Everscale ClientEverscale SDK Client library Python bindings.
Works for Python 3.7+InstallationCheck if Python 3.7+ is installedMacOS/Linux# Using pipenv
pipenv install ton-client-py

# Using pip
pip install ton-client-pyWindows# Using pipenv
py -m pipenv install ton-client-py

# Using pip
py -m pip install ton-client-pyTestsClone repository# Clone repository
git clone https://github.com/move-ton/ton-client-py.git

# Go to repo directory
cd ton-client-pyInstall dev dependenciesMacOS/Linux# Using pipenv
pipenv install --dev

# Using pip
pip install pytestWindows# Using pipenv
py -m pipenv install --dev

# Using pip
py -m pip install pytestRunning testsMacOS/Linux# Using pipenv
pipenv run pytest  # Display only module name while testing
pipenv run pytest -v  # Display module and method while testing
pipenv run pytest -v -s --log-cli-level=INFO  # Display methods logging while testing

# Without pipenv
python -m pytest
python -m pytest -v
python -m pytest -v -s --log-cli-level=INFOWindows# Using pipenv
py -m pipenv run pytest  # Display only module name while testing
py -m pipenv run pytest -v  # Display module and method while testing
py -m pipenv run pytest -v -s --log-cli-level=INFO  # Display methods logging while testing

# Without pipenv
py -m pytest
py -m pytest -v
py -m pytest -v -s --log-cli-level=INFOAlternative running tests
If you have problems with installingpytestpackage you can simply run# For MacOS/Linux
python -m unittest -v

# For Windows
py -m unittest -vClientCore client library has sync and async request modes.
Some core methods are available only in async request mode and
this mode is more prefferable, so python client created with async core requests by default.Create clientfromtonclient.typesimportClientConfigfromtonclient.clientimportTonClientclient=TonClient(config=ClientConfig())# If you need sync core requests for some reasonclient_sync_core=TonClient(config=ClientConfig(),is_core_async=False)Client created with default configfromtonclient.typesimportNetworkConfig,CryptoConfig,AbiConfig,BocConfig,ProofsConfig,ClientConfig# Default network config is below.# `None` attributes are filled by core with defaults values:#     `server_address=''`#     `endpoints=[]`#     `network_retries_count=5` (DEPRECATED)#     `message_retries_count=5`#     `max_reconnect_timeout=120000`#     `message_processing_timeout=40000`#     `wait_for_timeout=40000`#     `out_of_sync_threshold=15000`#     `sending_endpoint_count=2`#     `reconnect_timeout=` (DEPRECATED)#     `access_key=''`#     `latency_detection_interval=60000`#     `max_latency=60000`#     `query_timeout=60000`#     `queries_protocol='HTTP'`network=NetworkConfig(server_address='http://localhost',endpoints=None,network_retries_count=None,message_retries_count=None,message_processing_timeout=None,reconnect_timeout=None,wait_for_timeout=None,out_of_sync_threshold=None,sending_endpoint_count=None,access_key=None,max_reconnect_timeout=None,latency_detection_interval=None,max_latency=None,query_timeout=None,queries_protocol=None)# Default crypto config is below.# `None` attributes are filled by core with defaults values:#     `mnemonic_dictionary=1`#     `mnemonic_word_count=12`#     `hdkey_derivation_path="m/44'/396'/0'/0/0"`crypto=CryptoConfig(mnemonic_dictionary=None,mnemonic_word_count=None,hdkey_derivation_path=None)# Default abi config is below.# `None` attributes are filled by core with defaults values:#     `workchain=0`#     `message_expiration_timeout=40000`#     `message_expiration_timeout_grow_factor=1.5`abi=AbiConfig(workchain=None,message_expiration_timeout=None,message_expiration_timeout_grow_factor=None)# Default boc config is below.# `None` attributes are filled by core with defaults values:#     `cache_max_size=10000` (10MB)boc=BocConfig(cache_max_size=None)# Default proofs config is below.# `None` attributes are filled by core with defaults values:#     `cache_in_local_storage=True`proofs=ProofsConfig(cache_in_local_storage=None)# Then `ClientConfig` is createdconfig=ClientConfig(network=network,crypto=crypto,abi=abi,boc=boc,proofs=proofs,local_storage_path=None)You can override initial config while creating a clientfromtonclient.typesimportClientConfigfromtonclient.clientimportTonClient,DEVNET_BASE_URLSconfig=ClientConfig()config.network.endpoints=DEVNET_BASE_URLSconfig.abi.message_expiration_timeout=30000client=TonClient(config=config)version=client.version()Client contains all core modules and its methods.
You can get full list of modules and methods here:https://github.com/tonlabs/TON-SDK/blob/master/docs/modules.mdModule method called by templateclient.[module].[method]fromtonclient.typesimportClientConfig,ParamsOfParsefromtonclient.clientimportTonClient,DEVNET_BASE_URLSconfig=ClientConfig()config.network.endpoints=DEVNET_BASE_URLSclient=TonClient(config=config)# Generate random signing keyskeypair=client.crypto.generate_random_sign_keys()# Parse accountparse_params=ParamsOfParse(boc='Account base64 BOC')result=client.boc.parse_account(params=parse_params)You always can get information about method and its arguments in method docstring.Methods with callbacksSome library methods acceptcallbackargument to pass additional data to it.
E.g.net.subscribe_collectionimporttimefromdatetimeimportdatetimefromtonclient.errorsimportTonExceptionfromtonclient.typesimportClientConfig,ClientError,SubscriptionResponseType,\ParamsOfSubscribeCollection,ResultOfSubscriptionfromtonclient.clientimportDEVNET_BASE_URLS,TonClientconfig=ClientConfig()config.network.endpoints=DEVNET_BASE_URLSclient=TonClient(config=config)def__callback(response_data,response_type,loop):"""`loop` in args is just for example.It will have value only with `asyncio` and may be replaced by `_` or `*args`in synchronous requests"""ifresponse_type==SubscriptionResponseType.OK:result=ResultOfSubscription(**response_data)results.append(result.result)ifresponse_type==SubscriptionResponseType.ERROR:raiseTonException(error=ClientError(**response_data))results=[]now=int(datetime.now().timestamp())q_params=ParamsOfSubscribeCollection(collection='messages',result='created_at',filter={'created_at':{'gt':now}})subscription=client.net.subscribe_collection(params=q_params,callback=__callback)whileTrue:iflen(results)>0orint(datetime.now().timestamp())>now+10:client.net.unsubscribe(params=subscription)breaktime.sleep(1)Please, dig intonclient/test/test_net.py,tonclient/test/test_processing.py,tonclient/test/test_crypto.py,tonclient/test/test_debot.pyto get more examples.Client and asynciofromtonclient.typesimportClientConfigfromtonclient.clientimportTonClient,DEVNET_BASE_URLS# Create client with `is_async=True` argument.config=ClientConfig()config.network.endpoints=DEVNET_BASE_URLSclient=TonClient(config=config,is_async=True)# Get version (simple method with result)version=awaitclient.version()Please, dig intonclient/test/test_async.pyto get more info