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¶
Tron
¶
Tron(provider: HTTPProvider = None, *, network: str = 'mainnet', conf: dict = None)
The TRON API Client.
Parameters:
-
(provider¶HTTPProvider, default:None) –An
HTTPProviderobject, can be configured to use private node -
(network¶str, default:'mainnet') –Which network to connect, one of
"mainnet","shasta","nile", or"tronex"
Methods:
-
generate_address–Generate a random address.
-
generate_address_from_mnemonic–Generate address from a mnemonic.
-
generate_address_with_mnemonic–Create a new address and related mnemonic.
-
generate_zkey–Generate a random shielded address.
-
get_account–Get account info from an address.
-
get_account_asset_balance–Get TRC10 token balance of an account. Result is in raw amount.
-
get_account_asset_balances–Get all TRC10 token balances of an account.
-
get_account_balance–Get TRX balance of an account. Result in
TRX. -
get_account_permission–Get account's permission info from an address. Can be used in
account_permission_update. -
get_account_resource–Get resource info of an account.
-
get_address_from_passphrase–Get an address from a passphrase, compatiable with
wallet/createaddress. -
get_asset–Get TRC10(asset) info by asset's id or issuer.
-
get_asset_from_name–Get asset info from its abbr name, might fail if there're duplicates.
-
get_bandwidth–Query the bandwidth of the account
-
get_block–Get block from a block id or block number.
-
get_can_delegated_max_size–Query the amount of delegatable resources share of the specified resource type for an address
-
get_chain_parameters–List all chain parameters, values that can be changed via proposal.
-
get_contract–Get a contract object.
-
get_contract_as_shielded_trc20–Get a Shielded TRC20 Contract object.
-
get_contract_info–Queries a contract's information from the blockchain
-
get_delegated_resource_account_index_v2–Query the resource delegation index by an account
-
get_delegated_resource_v2–Query the amount of delegatable resources share of the specified resource type for an address
-
get_energy–Query the energy of the account
-
get_estimated_energy–Returns an estimated energy of calling a contract from the chain.
-
get_latest_block–Get latest block.
-
get_latest_block_id–Get latest block id in hex.
-
get_latest_block_number–Get latest block number. Implemented via
wallet/getnodeinfo, which is faster thanwallet/getnowblock. -
get_latest_solid_block_id–Get latest solid block id in hex.
-
get_latest_solid_block_number–Get latest solid block number. Implemented via
wallet/getnodeinfo, -
get_node_info–Get current API node' info.
-
get_solid_transaction_info–Get transaction receipt info from a transaction id, must be in solid block.
-
get_transaction–Get transaction from a transaction id.
-
get_transaction_info–Get transaction receipt info from a transaction id.
-
get_zkey_from_sk–Get the shielded address from sk(spending key) and d(diversifier).
-
list_assets–List all TRC10 tokens(assets).
-
list_nodes–List all nodes that current API node is connected to.
-
list_witnesses–List all witnesses, including SR, SRP, and SRC.
Attributes:
-
conf–The config dict.
-
is_address–Is object a TRON address, both hex format and base58check format.
-
is_base58check_address–Is object an address in base58check format.
-
is_hex_address–Is object an address in hex str format.
-
to_base58check_address–Convert address of any format to a base58check format.
-
to_canonical_address–Convert address of any format to a base58check format.
-
to_hex_address–Convert address of any format to a hex format.
-
trx(Trx) –Helper object to send various transactions.
Source code in tronpy/tron.py
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 | |
is_address
class-attribute
instance-attribute
¶
is_address = staticmethod(keys.is_address)
Is object a TRON address, both hex format and base58check format.
is_base58check_address
class-attribute
instance-attribute
¶
is_base58check_address = staticmethod(keys.is_base58check_address)
Is object an address in base58check format.
is_hex_address
class-attribute
instance-attribute
¶
is_hex_address = staticmethod(keys.is_hex_address)
Is object an address in hex str format.
to_base58check_address
class-attribute
instance-attribute
¶
to_base58check_address = staticmethod(keys.to_base58check_address)
Convert address of any format to a base58check format.
to_canonical_address
class-attribute
instance-attribute
¶
to_canonical_address = staticmethod(keys.to_base58check_address)
Convert address of any format to a base58check format.
to_hex_address
class-attribute
instance-attribute
¶
to_hex_address = staticmethod(keys.to_hex_address)
Convert address of any format to a hex format.
generate_address
¶
generate_address(priv_key=None) -> dict
Generate a random address.
Source code in tronpy/tron.py
636 637 638 639 640 641 642 643 644 645 | |
generate_address_from_mnemonic
¶
generate_address_from_mnemonic(
mnemonic: str, passphrase: str = "", account_path: str = TRON_DEFAULT_PATH
)
Generate address from a mnemonic.
Parameters:
-
(mnemonic¶str) –space-separated list of BIP39 mnemonic seed words
-
(passphrase¶str, default:'') –Optional passphrase used to encrypt the mnemonic
-
(account_path¶str, default:TRON_DEFAULT_PATH) –Specify an alternate HD path for deriving the seed using BIP32 HD wallet key derivation.
Source code in tronpy/tron.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 | |
generate_address_with_mnemonic
¶
generate_address_with_mnemonic(
passphrase: str = "",
num_words: int = 12,
language: str = "english",
account_path: str = TRON_DEFAULT_PATH,
)
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, default:'') –Extra passphrase to encrypt the seed phrase
-
(num_words¶int, default:12) –Number of words to use with seed phrase. Default is 12 words. Must be one of [12, 15, 18, 21, 24].
-
(language¶str, default:'english') –Language to use for BIP39 mnemonic seed phrase.
-
(account_path¶str, default:TRON_DEFAULT_PATH) –Specify an alternate HD path for deriving the seed using BIP32 HD wallet key derivation.
Source code in tronpy/tron.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
generate_zkey
¶
generate_zkey() -> dict
Generate a random shielded address.
Source code in tronpy/tron.py
689 690 691 | |
get_account
¶
get_account(addr: TAddress) -> dict
Get account info from an address.
Source code in tronpy/tron.py
731 732 733 734 735 736 737 | |
get_account_asset_balance
¶
get_account_asset_balance(addr: TAddress, token_id: int | str) -> int
Get TRC10 token balance of an account. Result is in raw amount.
Source code in tronpy/tron.py
778 779 780 781 782 783 784 | |
get_account_asset_balances
¶
get_account_asset_balances(addr: TAddress) -> dict
Get all TRC10 token balances of an account.
Source code in tronpy/tron.py
773 774 775 776 | |
get_account_balance
¶
get_account_balance(addr: TAddress) -> Decimal
Get TRX balance of an account. Result in TRX.
Source code in tronpy/tron.py
750 751 752 753 754 | |
get_account_permission
¶
get_account_permission(addr: TAddress) -> dict
Get account's permission info from an address. Can be used in account_permission_update.
Source code in tronpy/tron.py
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 | |
get_account_resource
¶
get_account_resource(addr: TAddress) -> dict
Get resource info of an account.
Source code in tronpy/tron.py
739 740 741 742 743 744 745 746 747 748 | |
get_address_from_passphrase
¶
get_address_from_passphrase(passphrase: str) -> dict
Get an address from a passphrase, compatiable with wallet/createaddress.
Source code in tronpy/tron.py
684 685 686 687 | |
get_asset
¶
get_asset(asset_id: int = None, issuer: TAddress = None) -> dict
Get TRC10(asset) info by asset's id or issuer.
Source code in tronpy/tron.py
982 983 984 985 986 987 988 989 990 991 | |
get_asset_from_name
¶
get_asset_from_name(name: str) -> dict
Get asset info from its abbr name, might fail if there're duplicates.
Source code in tronpy/tron.py
993 994 995 996 997 998 999 1000 | |
get_bandwidth
¶
get_bandwidth(addr: TAddress) -> int
Query the bandwidth of the account
Source code in tronpy/tron.py
756 757 758 759 760 761 762 763 764 | |
get_block
¶
Get block from a block id or block number.
Parameters:
-
(id_or_num¶None | str | int, default:None) –Block number, or Block hash(id), or
None(default) to get the latest block. -
(visible¶bool, default:True) –Use
visible=Falseto get non-base58check addresses and strings instead of hex strings.
Source code in tronpy/tron.py
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 | |
get_can_delegated_max_size
¶
get_can_delegated_max_size(address: TAddress, resource: str = 'ENERGY') -> dict
Query the amount of delegatable resources share of the specified resource type for an address
Args:
address (TAddress):
resource (str, optional): Resource type, can be "ENERGY" or "BANDWIDTH". Defaults to "ENERGY".
Returns: dict: Response data
Source code in tronpy/tron.py
835 836 837 838 839 840 841 842 843 844 845 846 847 848 | |
get_chain_parameters
¶
get_chain_parameters() -> dict
List all chain parameters, values that can be changed via proposal.
Source code in tronpy/tron.py
976 977 978 | |
get_contract
¶
get_contract(addr: TAddress) -> Contract
Get a contract object.
Source code in tronpy/tron.py
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 | |
get_contract_as_shielded_trc20
¶
get_contract_as_shielded_trc20(addr: TAddress) -> ShieldedTRC20
Get a Shielded TRC20 Contract object.
Source code in tronpy/tron.py
1058 1059 1060 1061 | |
get_contract_info
¶
get_contract_info(addr: TAddress) -> dict
Queries a contract's information from the blockchain
Source code in tronpy/tron.py
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 | |
get_delegated_resource_account_index_v2
¶
get_delegated_resource_account_index_v2(addr: TAddress) -> dict
Query the resource delegation index by an account
Source code in tronpy/tron.py
850 851 852 853 854 855 856 857 858 | |
get_delegated_resource_v2
¶
get_delegated_resource_v2(fromAddr: TAddress, toAddr: TAddress) -> dict
Query the amount of delegatable resources share of the specified resource type for an address
Source code in tronpy/tron.py
824 825 826 827 828 829 830 831 832 833 | |
get_energy
¶
get_energy(address: str) -> int
Query the energy of the account
Source code in tronpy/tron.py
766 767 768 769 770 771 | |
get_estimated_energy
¶
get_estimated_energy(
owner_address: TAddress,
contract_address: TAddress,
function_selector: str,
parameter: str,
) -> int
Returns an estimated energy of calling a contract from the chain.
Source code in tronpy/tron.py
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 | |
get_latest_block
¶
get_latest_block() -> dict
Get latest block.
Source code in tronpy/tron.py
877 878 879 | |
get_latest_block_id
¶
get_latest_block_id() -> str
Get latest block id in hex.
Source code in tronpy/tron.py
881 882 883 884 885 | |
get_latest_block_number
¶
get_latest_block_number() -> int
Get latest block number. Implemented via wallet/getnodeinfo, which is faster than wallet/getnowblock.
Source code in tronpy/tron.py
887 888 889 890 891 | |
get_latest_solid_block_id
¶
get_latest_solid_block_id() -> str
Get latest solid block id in hex.
Source code in tronpy/tron.py
865 866 867 868 869 | |
get_latest_solid_block_number
¶
get_latest_solid_block_number() -> int
Get latest solid block number. Implemented via wallet/getnodeinfo,
which is faster than walletsolidity/getnowblock.
Source code in tronpy/tron.py
871 872 873 874 875 | |
get_node_info
¶
get_node_info() -> dict
Get current API node' info.
Source code in tronpy/tron.py
971 972 973 974 | |
get_solid_transaction_info
¶
get_solid_transaction_info(txn_id: str) -> dict
Get transaction receipt info from a transaction id, must be in solid block.
Source code in tronpy/tron.py
939 940 941 942 943 944 945 946 947 948 949 | |
get_transaction
¶
get_transaction(txn_id: str) -> dict
Get transaction from a transaction id.
Source code in tronpy/tron.py
915 916 917 918 919 920 921 922 923 924 925 | |
get_transaction_info
¶
get_transaction_info(txn_id: str) -> dict
Get transaction receipt info from a transaction id.
Source code in tronpy/tron.py
927 928 929 930 931 932 933 934 935 936 937 | |
get_zkey_from_sk
¶
get_zkey_from_sk(sk: str, d: str = None) -> dict
Get the shielded address from sk(spending key) and d(diversifier).
Source code in tronpy/tron.py
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 | |
list_assets
¶
list_assets() -> list
List all TRC10 tokens(assets).
Source code in tronpy/tron.py
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 | |
list_nodes
¶
list_nodes() -> list
List all nodes that current API node is connected to.
Source code in tronpy/tron.py
962 963 964 965 966 967 968 969 | |
list_witnesses
¶
list_witnesses() -> list
List all witnesses, including SR, SRP, and SRC.
Source code in tronpy/tron.py
953 954 955 956 957 958 959 960 | |
HTTPProvider
¶
HTTPProvider(
endpoint_uri: str | dict = None,
timeout: float = DEFAULT_TIMEOUT,
api_key: str | list[str] = None,
jw_token: str = None,
)
An HTTP Provider for API request.
Parameters:
-
(endpoint_uri¶str | dict, default:None) –HTTP API URL base. Default value is
"https://api.trongrid.io/". Can also be configured via theTRONPY_HTTP_PROVIDER_URIenvironment variable. -
(timeout¶float, default:DEFAULT_TIMEOUT) –HTTP timeout in seconds.
-
(api_key¶str | list[str], default:None) –TronGRID API Key in str, or list of str.
-
(jw_token¶str, default:None) –TronGRID JWT Credentials in str.
Attributes:
-
timeout–Request timeout in second.
Source code in tronpy/providers/http.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |