HTTP API Client¶
TronPy uses TRON HTTP API to communicate with TRON nodes. The main client class is Tron.
Creating client¶
Refer Quickstart for creating clients.
Calling query APIs¶
The Tron wraps many query APIs and utility functions. You can query the chain using a instance.
>>> from tronpy import Tron
>>> client = Tron()
>>> client.get_latest_block_number()
20746184
>>> client.get_latest_block_id()
'00000000013c8fc9a74d2ef9111d2216b9bec4b0fbf44e2e77711dde0e535f8e'
>>> client.get_account_balance('TTzPiwbBedv7E8p4FkyPyeqq4RVoqRL3TW')
Decimal('286820.078511')
>>> client.get_account_asset_balance('TCrahg7N9cB1SwN21WzVMqxCptbRdvQata', 1002928)
989937719235000000
>>> client.generate_address()
{'base58check_address': 'TU7r98aQ3XTHuM9NLwnyVmdCH7gAZDYqxt',
'hex_address': '41c71498123f6de4698410712e5f5c96ae42978776',
'private_key': '.................omitted.....................',
'public_key': '..................omitted.....................'}
Using TronGrid API Key¶
You can register an API Key at TronGrid.
from tronpy import Tron
from tronpy.providers import HTTPProvider
client = Tron(HTTPProvider(api_key="Your api_key here")) # Use mainnet(trongrid) with a single api_key
# or
client = Tron(HTTPProvider(api_key=["key1", "key2", "key3"])) # Use mainnet(trongrid) with multiple api_key
Use HTTP Proxy¶
from tronpy import Tron
from tronpy.providers import HTTPProvider
provider = HTTPProvider(api_key="API KEY HERE")
provider.sess.proxies = {"https": "....", "http": "..."}
client = Tron(provider)
API reference¶
-
class
tronpy.Tron(provider: tronpy.providers.http.HTTPProvider = None, *, network: str = 'mainnet', conf: dict = None)¶ The TRON API Client.
Parameters: - provider – An
HTTPProviderobject, 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) → int¶ 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=Falseto 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.contract.Contract¶ 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_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.providers.HTTPProvider(endpoint_uri: Union[str, dict] = None, timeout: float = 10.0, api_key: Union[str, List[str]] = None, jw_token: str = None)¶ An HTTP Provider for API request.
Parameters: - endpoint_uri – HTTP API URL base. Default value is
"https://api.trongrid.io/". Can also be configured via theTRONPY_HTTP_PROVIDER_URIenvironment variable. - timeout – HTTP timeout in seconds.
- api_key – TronGRID API Key in str, or list of str.
- jw_token – TronGRID JWT Credentials in str.
-
timeout= None¶ Request timeout in second.
- endpoint_uri – HTTP API URL base. Default value is