No project description provided
Project description
Pytoncenter
Introduction
Pytoncenter is a TON Center client with type hints that introduces advanced features such as address subscriptions, obtaining transaction flows similar to TON Viewer, parallel processing of multiple calls, and robust debug tools. Developers can use this package to create TON data analysis platforms, Dapp backends, and other services with enhanced functionality and efficiency.
Quick Start
1. Install the package
To get started, install Pytoncenter using pip:
pip3 install pytoncenter
2. Export the TONCENTER_API_KEY
To use the TON Center API, you need to obtain an API key from the TON Center. After obtaining the API key from @tonapibot, export it as an environment variable:
export TONCENTER_API_KEY=your_api_key
Example 1. Decode Jetton Get Method Result (V3)
Here is an example for decoding get method by declaring the decoder and Type of the field explicitly. Decoder will decode the result based on the type of the field. If you are not sure about the type of the field, you can use AutoDecoder to decode the result.
import asyncio
from pprint import pprint
from pytoncenter import get_client
from pytoncenter.decoder import AutoDecoder, JettonDataDecoder
from pytoncenter.v3.models import *
async def main():
client = get_client(version="v3", network="testnet")
req = GetAccountRequest(address="kQBqSpvo4S87mX9tjHaG4zhYZeORhVhMapBJpnMZ64jhrP-A")
account_info = await client.get_account(req)
# Check account status is active
assert account_info.status == "active", "Account is not active"
print("=====================================")
req = RunGetMethodRequest(address="kQBqSpvo4S87mX9tjHaG4zhYZeORhVhMapBJpnMZ64jhrP-A", method="get_jetton_data", stack=[])
result = await client.run_get_method(req)
print(result)
print("===============Jetton Decoder======================")
decoder = JettonDataDecoder()
jetton_data = decoder.decode(result)
pprint(jetton_data, width=120)
print("===============Auto Decoder======================")
decoder = AutoDecoder()
jetton_data = decoder.decode(result)
pprint(jetton_data, width=120)
if __name__ == "__main__":
asyncio.run(main())
You may get the following jetton data in the console:
OrderedDict([('total_supply', 5000000000),
('mintable', True),
('admin_address', EQC8zFHM8LCMp9Xs--w3g9wmf7RwuDgJcQtV-oHZRSCqQZ__),
('jetton_content', <CellSlice [9] bits, [1] refs, [A21FCFE4756B6AD7A1E88E65483CCDAB3BBBD9F8AEF5F5060C5FC8A36737AC36] hash>),
('jetton_wallet_code', 'b5ee9c7241022501000a......'),])
If you use AutoDecoder, you may get the following result:
OrderedDict([('idx_0', 5000000000),
('idx_1', -1), # Because auto decoder does not know the type, it will decode the result as number
('idx_2', EQC8zFHM8LCMp9Xs--w3g9wmf7RwuDgJcQtV-oHZRSCqQZ__), # Address field will automatically decode to Address object
('idx_3', 'b5ee9c7241022501000a......'), # Cell and Slice will apply b64decode to hex string
('idx_4', 'b5ee9c7241022501000a......'),])
However, for jetton data, there is a more efficient way to retreive the result by V3 API.
client = get_client(version="v3", network="testnet")
jettons = await client.get_jetton_masters(GetJettonMastersRequest(address="kQBqSpvo4S87mX9tjHaG4zhYZeORhVhMapBJpnMZ64jhrP-A"))
jetton = jettons[0]
print("Total Supply: ", jetton.total_supply)
print("Mintable: ", jetton.mintable)
print("last transaction lt: ", jetton.last_transaction_lt)
if jetton.jetton_content is not None:
print("Jetton content - Symbol: ", jetton.jetton_content.symbol)
print("Jetton content - Name: ", jetton.jetton_content.name)
print("Jetton content - Decimals: ", jetton.jetton_content.decimals)
print("Jetton content - Image: ", jetton.jetton_content.image)
The output will be:
Total Supply: 5000000000
Mintable: True
last transaction lt: 19051958000005
Jetton content - Symbol: USDT
Jetton content - Name: USDT
Jetton content - Decimals: 6
Jetton content - Image: https://coinhere.io/wp-content/uploads/2020/08/Tether-USDT-icon-1.png
Example 2. Obtain a Transaction Flow (V2)
The following example demonstrates how to obtain the transaction flow for a specified transaction. This transaction is associated with a contract deployed using the TON Dynasty Contract Jetton Template.
- Contract Address on Testnet TON Viewer
- JettonMint Message Transaction on Testnet TON Viewer
from pytoncenter import get_client
from pytoncenter.v2.tools import pretty_print_trace_tx, create_named_mapping_func
from pytoncenter.address import Address
import asyncio
import json
async def main():
# Initialize the client and query a transaction
client = get_client(version="v2", network="testnet")
txs = await client.get_transactions(address="kQAreQ23eabjRO5glLCbhZ4KxQ9SOIjtw2eM2PuEXXhIZeh3", hash="Lomkyzxh1WBkxvxZ3cJNS2bAYIPC7dPZA67wDomGM4U=", limit=1)
tx = txs[0]
result = await client.trace_tx(tx)
# Pretty print the transaction trace with name mapping for addresses
named_func = create_named_mapping_func(
{
Address("EQC8zFHM8LCMp9Xs--w3g9wmf7RwuDgJcQtV-oHZRSCqQZ__"): "Alan WalletV4R2",
Address("0:2b790db779a6e344ee6094b09b859e0ac50f523888edc3678cd8fb845d784865"): "Jetton Master",
Address("kQC40ScRg9_1ob5sjWsdScltrCGu0HARsUnOYQ1esc12588C"): "Jetton Wallet",
},
truncate_address=True,
)
pretty_print_trace_tx(result, named_func=named_func)
if __name__ == "__main__":
asyncio.run(main())
You may get the following output in the console:
Alan WalletV4R2 -> Jetton Master (Mint:1) [1.0 TON]
└── Jetton Master -> Jetton Wallet (0x178d4519) [0.955002 TON]
└── Jetton Wallet -> Alan WalletV4R2 (0xd53276db) [0.853806 TON]
Examples (V3)
- [Get Transaction Traces] - Waiting for TONCENTER to fix their api
- Decode Jetton Get Method Result
- Decode Custom Get Method Result
- Multicall
- Subscribe transactions for address
Examples (V2)
- Get Transaction Traces
- Decode Jetton Get Method Result
- Decode Custom Get Method Result
- Execute Parallelly
- Subscribe transactions for address
Examples (Address)
Development Guide
Please refer to the Development Guide for more information on how to contribute to this project.
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
Hashes for pytoncenter-0.0.9-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9acd25966bc0f1243383862d754a7577430375ab63179377991e7119f9023506 |
|
MD5 | 8174af94be668ef4e08daf20a7652fa8 |
|
BLAKE2b-256 | 2cf7bbbf36d5f387a03bc23122d888f44c90da54e713388b41be464d35541404 |