The Async API¶
TronPy offers a standard synchronous API by default, but also introduces an async client via httpx.
The async client is AsyncTron
. It almost uses the same API as the synchronous client Tron
.
import asyncio
from tronpy import AsyncTron
from tronpy.keys import PrivateKey
# private key of TMisHYBVvFHwKXHPYTqo8DhrRPTbWeAM6z
priv_key = PrivateKey(bytes.fromhex("8888888888888888888888888888888888888888888888888888888888888888"))
async def transfer():
async with AsyncTron(network='nile') as client:
print(client)
txb = (
client.trx.transfer("TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3", "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA", 1_000)
.memo("test memo")
.fee_limit(100_000_000)
)
txn = await txb.build()
print(txn)
txn_ret = await txn.sign(priv_key).broadcast()
print(txn_ret)
# > {'result': True, 'txid': 'edc2a625752b9c71fdd0d68117802860c6adb1a45c19fd631a41757fa334d72b'}
print(await txn_ret.wait())
# > {'id': 'edc2a625752b9c71fdd0d68117802860c6adb1a45c19fd631a41757fa334d72b', 'blockNumber': 10163821,
# > 'blockTimeStamp': 1603368072000, 'contractResult': [''], 'receipt': {'net_usage': 283}}
if __name__ == '__main__':
asyncio.run(transfer())
API reference¶
-
class
tronpy.
AsyncTron
(provider: tronpy.providers.async_http.AsyncHTTPProvider = None, *, network: str = 'mainnet', conf: dict = None)¶ The Async TRON API Client.
Parameters: - provider – An
HTTPProvider
object, can be configured to use private node - network – Which network to connect, one of
"mainnet"
,"shasta"
,"nile"
, or"tronex"
-
conf
= None¶ The config dict.
-
generate_address
(priv_key=None) → dict¶ Generate a random address.
-
generate_address_from_mnemonic
(mnemonic: str, passphrase: str = '', account_path: str = "m/44'/195'/0'/0/0")¶ Generate address from a mnemonic.
Parameters: - mnemonic (str) – space-separated list of BIP39 mnemonic seed words
- passphrase (str) – Optional passphrase used to encrypt the mnemonic
- account_path (str) – Specify an alternate HD path for deriving the seed using BIP32 HD wallet key derivation.
-
generate_address_with_mnemonic
(passphrase: str = '', num_words: int = 12, language: str = 'english', account_path: str = "m/44'/195'/0'/0/0")¶ Create a new address and related mnemonic.
Creates a new address, and returns it alongside the mnemonic that can be used to regenerate it using any BIP39-compatible wallet.
Parameters: - passphrase (str) – Extra passphrase to encrypt the seed phrase
- num_words (int) – Number of words to use with seed phrase. Default is 12 words. Must be one of [12, 15, 18, 21, 24].
- language (str) – Language to use for BIP39 mnemonic seed phrase.
- account_path (str) – Specify an alternate HD path for deriving the seed using BIP32 HD wallet key derivation.
-
generate_zkey
() → dict¶ Generate a random shielded address.
-
get_account
(addr: str) → dict¶ Get account info from an address.
-
get_account_asset_balance
(addr: str, token_id: Union[int, str]) → int¶ Get TRC10 token balance of an account. Result is in raw amount.
-
get_account_asset_balances
(addr: str) → dict¶ Get all TRC10 token balances of an account.
-
get_account_balance
(addr: str) → decimal.Decimal¶ Get TRX balance of an account. Result in TRX.
-
get_account_permission
(addr: str) → dict¶ Get account’s permission info from an address. Can be used in account_permission_update.
-
get_account_resource
(addr: str) → dict¶ Get resource info of an account.
-
get_address_from_passphrase
(passphrase: str) → dict¶ Get an address from a passphrase, compatiable with wallet/createaddress.
-
get_asset
(id: int = None, issuer: str = None) → dict¶ Get TRC10(asset) info by asset’s id or issuer.
-
get_asset_from_name
(name: str) → dict¶ Get asset info from its abbr name, might fail if there’re duplicates.
-
get_bandwidth
(addr: str) → dict¶ Query the bandwidth of the account
-
get_block
(id_or_num: Union[None, str, int] = None, *, visible: bool = True) → dict¶ Get block from a block id or block number.
Parameters: - id_or_num – Block number, or Block hash(id), or
None
(default) to get the latest block. - visible – Use
visible=False
to get non-base58check addresses and strings instead of hex strings.
- id_or_num – Block number, or Block hash(id), or
-
get_chain_parameters
() → dict¶ List all chain parameters, values that can be changed via proposal.
-
get_contract
(addr: str) → tronpy.async_contract.AsyncContract¶ Get a contract object.
-
get_contract_as_shielded_trc20
(addr: str) → tronpy.contract.ShieldedTRC20¶ Get a Shielded TRC20 Contract object.
-
get_delegated_resource_v2
(fromAddr: str, toAddr: str) → dict¶ Query the amount of delegatable resources share of the specified resource type for an address
-
get_estimated_energy
(owner_address: str, contract_address: str, function_selector: str, parameter: str) → int¶ Returns an estimated energy of calling a contract from the chain.
-
get_latest_block
() → dict¶ Get latest block.
-
get_latest_block_id
() → str¶ Get latest block id in hex.
-
get_latest_block_number
() → int¶ Get latest block number. Implemented via wallet/getnodeinfo, which is faster than wallet/getnowblock.
-
get_latest_solid_block_id
() → str¶ Get latest solid block id in hex.
-
get_latest_solid_block_number
() → int¶ Get latest solid block number. Implemented via wallet/getnodeinfo, which is faster than walletsolidity/getnowblock.
-
get_node_info
() → dict¶ Get current API node’ info.
-
get_solid_transaction
(txn_id: str) → dict¶ Get transaction from a transaction id, must be in solid block.
-
get_solid_transaction_info
(txn_id: str) → dict¶ Get transaction receipt info from a transaction id, must be in solid block.
-
get_transaction
(txn_id: str) → dict¶ Get transaction from a transaction id.
-
get_transaction_info
(txn_id: str) → dict¶ Get transaction receipt info from a transaction id.
-
get_zkey_from_sk
(sk: str, d: str = None) → dict¶ Get the shielded address from sk(spending key) and d(diversifier).
-
static
is_address
(value: str) → bool¶ Is object a TRON address, both hex format and base58check format.
-
static
is_base58check_address
(value: str) → bool¶ Is object an address in base58check format.
-
static
is_hex_address
(value: str) → bool¶ Is object an address in hex str format.
-
list_assets
() → list¶ List all TRC10 tokens(assets).
-
list_nodes
() → list¶ List all nodes that current API node is connected to.
-
list_witnesses
() → list¶ List all witnesses, including SR, SRP, and SRC.
-
static
to_base58check_address
(raw_addr: Union[str, bytes]) → str¶ Convert address of any format to a base58check format.
-
static
to_canonical_address
(raw_addr: Union[str, bytes]) → str¶ Convert hex address or base58check address to base58check address(and verify it).
-
static
to_hex_address
(raw_addr: Union[str, bytes]) → str¶ Convert address of any format to a hex format.
- provider – An
-
class
tronpy.async_tron.
AsyncTrx
(tron)¶ The Trx(transaction) API.
-
account_permission_update
(owner: str, perm: dict) → tronpy.async_tron.AsyncTransactionBuilder¶ Update account permission.
Parameters: - owner – Address of owner
- perm – Permission dict from
get_account_permission()
-
account_update
(owner: str, name: str) → tronpy.async_tron.AsyncTransactionBuilder¶ Update account name. An account can only set name once.
-
asset_issue
(owner: str, abbr: str, total_supply: int, *, url: str, name: str = None, description: str = '', start_time: int = None, end_time: int = None, precision: int = 6, frozen_supply: list = None, trx_num: int = 1, num: int = 1) → tronpy.async_tron.AsyncTransactionBuilder¶ Issue a TRC10 token.
Almost all parameters have resonable defaults.
-
asset_transfer
(from_: str, to: str, amount: int, token_id: int) → tronpy.async_tron.AsyncTransactionBuilder¶ Transfer TRC10 tokens.
-
create_witness
(owner: str, url: str) → tronpy.async_tron.AsyncTransactionBuilder¶ Create a new witness, will consume 1_000 TRX.
-
delegate_resource
(owner: str, receiver: str, balance: int, resource: str = 'BANDWIDTH', lock: bool = False) → tronpy.async_tron.AsyncTransactionBuilder¶ Delegate bandwidth or energy resources to other accounts in Stake2.0.
Parameters: - owner –
- receiver –
- balance –
- resource – Resource type, can be
"ENERGY"
or"BANDWIDTH"
- lock – Optionally lock delegated resources for 3 days.
-
deploy_contract
(owner: str, contract: tronpy.async_contract.AsyncContract) → tronpy.async_tron.AsyncTransactionBuilder¶ Deploy a new contract on chain.
-
freeze_balance
(owner: str, amount: int, resource: str = 'ENERGY') → tronpy.async_tron.AsyncTransactionBuilder¶ Freeze balance to get energy or bandwidth, for 3 days.
Parameters: resource – Resource type, can be "ENERGY"
or"BANDWIDTH"
-
transfer
(from_: str, to: str, amount: int) → tronpy.async_tron.AsyncTransactionBuilder¶ Transfer TRX.
amount
in SUN.
-
undelegate_resource
(owner: str, receiver: str, balance: int, resource: str = 'BANDWIDTH') → tronpy.async_tron.AsyncTransactionBuilder¶ Cancel the delegation of bandwidth or energy resources to other accounts in Stake2.0
Parameters: - owner –
- receiver –
- balance –
- resource – Resource type, can be
"ENERGY"
or"BANDWIDTH"
-
unfreeze_balance
(owner: str, resource: str = 'ENERGY', *, unfreeze_balance: int) → tronpy.async_tron.AsyncTransactionBuilder¶ Unfreeze balance to get TRX back.
Parameters: resource – Resource type, can be "ENERGY"
or"BANDWIDTH"
-
unfreeze_balance_legacy
(owner: str, resource: str = 'ENERGY', receiver: str = None) → tronpy.async_tron.AsyncTransactionBuilder¶ Unfreeze balance to get TRX back.
Parameters: resource – Resource type, can be "ENERGY"
or"BANDWIDTH"
-
vote_witness
(owner: str, *votes) → tronpy.async_tron.AsyncTransactionBuilder¶ Vote for witnesses. Empty
votes
to clean voted.
-
withdraw_rewards
(owner: str) → tronpy.async_tron.AsyncTransactionBuilder¶ Withdraw voting rewards.
-