Python SDK for Free TON
ton-client-p的Python项目详细描述
吨客户机
tonsdk客户端库Python绑定。 适用于Python 3.6+
安装
检查是否安装了Python3.6+
MacOS/Linux
# Using pipenv
pipenv install ton-client-py
# Using pip
pip install ton-client-py
Windows
^{pr2}$测试
- 克隆存储库
# Clone repository
git clone https://github.com/move-ton/ton-client-py.git
# Go to repo directory
cd ton-client-py
- 安装开发依赖项
MacOS/Linux
# Using pipenv
pipenv install --dev
# Using pip
pip install pytest
Windows
# Using pipenv
py -m pipenv install --dev
# Using pip
py -m pip install pytest
- 运行测试
MacOS/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=INFO
Windows
# 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=INFO
- 备选运行测试
如果您在安装pytest
包时遇到问题,您可以简单地运行
# For MacOS/Linux
python -m unittest -v
# For Windows
py -m unittest -v
客户
核心客户端库有同步和异步请求模式。一些核心方法仅在异步请求模式下可用,并且 这种模式更易于预处理,因此python客户机在默认情况下是使用异步核心请求创建的。在
创建客户端
fromtonclient.clientimportTonClientclient=TonClient()# If you need sync core requests for some reasonclient_sync_core=TonClient(is_core_async=False)
客户端是用默认配置创建的
CLIENT_DEFAULT_SETUP={'network':{'server_address':'http://localhost','network_retries_count':5,'message_retries_count':5,'message_processing_timeout':40000,'wait_for_timeout':40000,'out_of_sync_threshold':15000,'access_key':''},'crypto':{'mnemonic_dictionary':1,'mnemonic_word_count':12,'hdkey_derivation_path':"m/44'/396'/0'/0/0"},'abi':{'workchain':0,'message_expiration_timeout':40000,'message_expiration_timeout_grow_factor':1.5}}
您可以在创建客户机时重写初始配置
fromtonclient.clientimportTonClient,DEVNET_BASE_URLclient=TonClient(network={'server_address':DEVNET_BASE_URL},abi={'message_expiration_timeout':30000})version=client.version()
客户端包含所有核心模块及其方法。您可以在此处获得完整的模块和方法列表:
https://github.com/tonlabs/TON-SDK/blob/master/docs/modules.md
Module方法由模板client.[module].[method]
调用
fromtonclient.clientimportTonClient,DEVNET_BASE_URLclient=TonClient(network={'server_address':DEVNET_BASE_URL})# Generate random signing keyskeypair=client.crypto.generate_random_sign_keys()# Parse accountparsed=client.boc.parse_account(boc='Account base64 BOC')
始终可以在方法docstring中获取有关方法及其参数的信息。在
Net模块和图形查询
要为net模块方法创建GraphQL查询,请使用内置工厂。collection
和{
fromtonclient.netimportTonQLQuery# Create query collectionquery=TonQLQuery(collection='messages')# Set filter# `set_filter` methods accepts any number of kwargs for filtering data.# kwarg name contains field name and condition separated by __.# E.g. field__gt=111 is equal to GraphQL {'field': {'gt': 111}}query=query.set_filter(field__gt=111,field__eq='0:')# Set resultquery=query.set_result('id boc')# or you can set result fields as separate argsquery=query.set_result('id','boc')# or you can combine all of thisquery=query.set_result('id boc','field')# Set order (you can pass arguments in the same way as for `set_result`)query=query.set_order('created_at')# ASC sortingquery=query.set_order('-created_at')# DESC sorting# Set limitquery=query.set_limit(5)
事件
一些库方法net.subscribe_collection
,proccessing.send_message
,processing.wait_for_transaction
,
processing.process_message
可能返回结果或生成器。在
net.subscribe_collection
始终返回生成器proccessing.send_message
,processing.wait_for_transaction
,processing.process_message
仅当send_events
参数设置为True
时返回生成器
请深入tonclient/test/test_net.py
,tonclient/test/test_processing.py
来获得如何使用的示例
发电机。在
客户端和异步
fromtonclient.clientimportTonClient,DEVNET_BASE_URL# Create client with `is_async=True` argument.client=TonClient(network={'server_address':DEVNET_BASE_URL},is_async=True)# Get version (simple method with result)version=awaitclient.version()# Generatorsgenerator=client.net.subscribe_collection(query=TonQLQuery)asyncforeventingenerator:# Work with eventpass
请深入tonclient/test/test_async.py
获取更多信息。在
- 项目
标签: