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¶
AsyncTron
¶
The Async TRON API Client.
Parameters:
-
(provider¶AsyncHTTPProvider, 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–Get transaction from a transaction id, must be in solid block.
-
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(AsyncTrx) –Helper object to send various transactions.
Source code in tronpy/async_tron.py
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
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/async_tron.py
644 645 646 647 648 649 650 651 652 653 | |
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/async_tron.py
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | |
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/async_tron.py
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
generate_zkey
async
¶
generate_zkey() -> dict
Generate a random shielded address.
Source code in tronpy/async_tron.py
697 698 699 | |
get_account
async
¶
get_account(addr: TAddress) -> dict
Get account info from an address.
Source code in tronpy/async_tron.py
738 739 740 741 742 743 744 745 746 | |
get_account_asset_balance
async
¶
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/async_tron.py
789 790 791 792 793 794 795 | |
get_account_asset_balances
async
¶
get_account_asset_balances(addr: TAddress) -> dict
Get all TRC10 token balances of an account.
Source code in tronpy/async_tron.py
784 785 786 787 | |
get_account_balance
async
¶
get_account_balance(addr: TAddress) -> Decimal
Get TRX balance of an account. Result in TRX.
Source code in tronpy/async_tron.py
778 779 780 781 782 | |
get_account_permission
async
¶
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/async_tron.py
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 823 824 825 826 827 828 829 830 831 832 833 | |
get_account_resource
async
¶
get_account_resource(addr: TAddress) -> dict
Get resource info of an account.
Source code in tronpy/async_tron.py
767 768 769 770 771 772 773 774 775 776 | |
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/async_tron.py
692 693 694 695 | |
get_asset
async
¶
get_asset(asset_id: int = None, issuer: TAddress = None) -> dict
Get TRC10(asset) info by asset's id or issuer.
Source code in tronpy/async_tron.py
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 | |
get_asset_from_name
async
¶
get_asset_from_name(name: str) -> dict
Get asset info from its abbr name, might fail if there're duplicates.
Source code in tronpy/async_tron.py
1022 1023 1024 1025 1026 1027 1028 1029 | |
get_bandwidth
async
¶
get_bandwidth(addr: TAddress) -> int
Query the bandwidth of the account
Source code in tronpy/async_tron.py
749 750 751 752 753 754 755 756 757 758 | |
get_block
async
¶
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/async_tron.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 | |
get_can_delegated_max_size
async
¶
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/async_tron.py
846 847 848 849 850 851 852 853 854 855 856 857 858 859 | |
get_chain_parameters
async
¶
get_chain_parameters() -> dict
List all chain parameters, values that can be changed via proposal.
Source code in tronpy/async_tron.py
1004 1005 1006 1007 | |
get_contract
async
¶
get_contract(addr: TAddress) -> AsyncContract
Get a contract object.
Source code in tronpy/async_tron.py
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 | |
get_contract_as_shielded_trc20
async
¶
get_contract_as_shielded_trc20(addr: TAddress) -> ShieldedTRC20
Get a Shielded TRC20 Contract object.
Source code in tronpy/async_tron.py
1087 1088 1089 1090 | |
get_contract_info
async
¶
get_contract_info(addr: TAddress) -> dict
Queries a contract's information from the blockchain
Source code in tronpy/async_tron.py
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 | |
get_delegated_resource_account_index_v2
async
¶
get_delegated_resource_account_index_v2(addr: TAddress) -> dict
Query the resource delegation index by an account
Source code in tronpy/async_tron.py
861 862 863 864 865 866 867 868 869 | |
get_delegated_resource_v2
async
¶
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/async_tron.py
835 836 837 838 839 840 841 842 843 844 | |
get_energy
async
¶
get_energy(address: str) -> int
Query the energy of the account
Source code in tronpy/async_tron.py
760 761 762 763 764 765 | |
get_estimated_energy
async
¶
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/async_tron.py
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 | |
get_latest_block
async
¶
get_latest_block() -> dict
Get latest block.
Source code in tronpy/async_tron.py
892 893 894 | |
get_latest_block_id
async
¶
get_latest_block_id() -> str
Get latest block id in hex.
Source code in tronpy/async_tron.py
896 897 898 899 900 | |
get_latest_block_number
async
¶
get_latest_block_number() -> int
Get latest block number. Implemented via wallet/getnodeinfo, which is faster than wallet/getnowblock.
Source code in tronpy/async_tron.py
902 903 904 905 906 | |
get_latest_solid_block_id
async
¶
get_latest_solid_block_id() -> str
Get latest solid block id in hex.
Source code in tronpy/async_tron.py
876 877 878 879 880 881 882 883 884 | |
get_latest_solid_block_number
async
¶
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/async_tron.py
886 887 888 889 890 | |
get_node_info
async
¶
get_node_info() -> dict
Get current API node' info.
Source code in tronpy/async_tron.py
999 1000 1001 1002 | |
get_solid_transaction
async
¶
get_solid_transaction(txn_id: str) -> dict
Get transaction from a transaction id, must be in solid block.
Source code in tronpy/async_tron.py
942 943 944 945 946 947 948 949 950 951 952 | |
get_solid_transaction_info
async
¶
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/async_tron.py
966 967 968 969 970 971 972 973 974 975 976 | |
get_transaction
async
¶
get_transaction(txn_id: str) -> dict
Get transaction from a transaction id.
Source code in tronpy/async_tron.py
930 931 932 933 934 935 936 937 938 939 940 | |
get_transaction_info
async
¶
get_transaction_info(txn_id: str) -> dict
Get transaction receipt info from a transaction id.
Source code in tronpy/async_tron.py
954 955 956 957 958 959 960 961 962 963 964 | |
get_zkey_from_sk
async
¶
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/async_tron.py
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 728 729 730 731 732 733 734 735 | |
list_assets
async
¶
list_assets() -> list
List all TRC10 tokens(assets).
Source code in tronpy/async_tron.py
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 | |
list_nodes
async
¶
list_nodes() -> list
List all nodes that current API node is connected to.
Source code in tronpy/async_tron.py
990 991 992 993 994 995 996 997 | |
list_witnesses
async
¶
list_witnesses() -> list
List all witnesses, including SR, SRP, and SRC.
Source code in tronpy/async_tron.py
980 981 982 983 984 985 986 987 988 | |
AsyncTrx
¶
AsyncTrx(tron)
The Trx(transaction) API.
Methods:
-
account_permission_update–Update account permission.
-
account_update–Update account name. An account can only set name once.
-
asset_issue–Issue a TRC10 token.
-
asset_transfer–Transfer TRC10 tokens.
-
create_witness–Create a new witness, will consume 1_000 TRX.
-
delegate_resource–Delegate bandwidth or energy resources to other accounts in Stake2.0.
-
deploy_contract–Deploy a new contract on chain.
-
freeze_balance–Freeze balance to get energy or bandwidth, for 3 days.
-
transfer–Transfer TRX.
amountinSUN. -
undelegate_resource–Cancel the delegation of bandwidth or energy resources to other accounts in Stake2.0
-
unfreeze_balance–Unfreeze balance to get TRX back.
-
unfreeze_balance_legacy–Unfreeze balance to get TRX back.
-
vote_witness–Vote for witnesses. Empty
votesto clean voted. -
withdraw_rewards–Withdraw voting rewards.
-
withdraw_stake_balance–Withdraw all stake v2 balance after waiting for 14 days since unfreeze_balance call.
Source code in tronpy/async_tron.py
315 316 | |
account_permission_update
¶
Update account permission.
Parameters:
-
(owner¶TAddress) –Address of owner
-
(perm¶dict) –Permission dict from
get_account_permission
Source code in tronpy/async_tron.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | |
account_update
¶
account_update(owner: TAddress, name: str) -> AsyncTransactionBuilder
Update account name. An account can only set name once.
Source code in tronpy/async_tron.py
431 432 433 434 435 436 | |
asset_issue
¶
asset_issue(
owner: TAddress,
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,
) -> AsyncTransactionBuilder
Issue a TRC10 token.
Almost all parameters have resonable defaults.
Source code in tronpy/async_tron.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | |
asset_transfer
¶
asset_transfer(
from_: TAddress, to: TAddress, amount: int, token_id: int
) -> AsyncTransactionBuilder
Transfer TRC10 tokens.
Source code in tronpy/async_tron.py
340 341 342 343 344 345 346 347 348 349 350 | |
create_witness
¶
create_witness(owner: TAddress, url: str) -> AsyncTransactionBuilder
Create a new witness, will consume 1_000 TRX.
Source code in tronpy/async_tron.py
542 543 544 545 | |
delegate_resource
¶
delegate_resource(
owner: TAddress,
receiver: TAddress,
balance: int,
resource: str = "BANDWIDTH",
lock: bool = False,
lock_period: int = None,
) -> AsyncTransactionBuilder
Delegate bandwidth or energy resources to other accounts in Stake2.0.
Parameters:
-
(owner¶TAddress) – -
(receiver¶TAddress) – -
(balance¶int) – -
(resource¶str, default:'BANDWIDTH') –Resource type, can be
"ENERGY"or"BANDWIDTH" -
(lock¶bool, default:False) –Optionally lock delegated resources for 3 days.
-
(lock_period¶int, default:None) –Optionally lock delegated resources for a specific period. Default: 3 days.
Source code in tronpy/async_tron.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | |
deploy_contract
¶
deploy_contract(owner: TAddress, contract: AsyncContract) -> AsyncTransactionBuilder
Deploy a new contract on chain.
Source code in tronpy/async_tron.py
560 561 562 563 564 565 566 567 | |
freeze_balance
¶
freeze_balance(
owner: TAddress, amount: int, resource: str = "ENERGY"
) -> AsyncTransactionBuilder
Freeze balance to get energy or bandwidth, for 3 days.
Parameters:
-
(resource¶str, default:'ENERGY') –Resource type, can be
"ENERGY"or"BANDWIDTH"
Source code in tronpy/async_tron.py
438 439 440 441 442 443 444 445 446 447 448 | |
transfer
¶
transfer(from_: TAddress, to: TAddress, amount: int) -> AsyncTransactionBuilder
Transfer TRX. amount in SUN.
Source code in tronpy/async_tron.py
331 332 333 334 335 336 | |
undelegate_resource
¶
undelegate_resource(
owner: TAddress, receiver: TAddress, balance: int, resource: str = "BANDWIDTH"
) -> AsyncTransactionBuilder
Cancel the delegation of bandwidth or energy resources to other accounts in Stake2.0
Parameters:
-
(owner¶TAddress) – -
(receiver¶TAddress) – -
(balance¶int) – -
(resource¶str, default:'BANDWIDTH') –Resource type, can be
"ENERGY"or"BANDWIDTH"
Source code in tronpy/async_tron.py
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
unfreeze_balance
¶
unfreeze_balance(
owner: TAddress, resource: str = "ENERGY", *, unfreeze_balance: int
) -> AsyncTransactionBuilder
Unfreeze balance to get TRX back.
Parameters:
-
(resource¶str, default:'ENERGY') –Resource type, can be
"ENERGY"or"BANDWIDTH"
Source code in tronpy/async_tron.py
460 461 462 463 464 465 466 467 468 469 470 471 472 | |
unfreeze_balance_legacy
¶
unfreeze_balance_legacy(
owner: TAddress, resource: str = "ENERGY", receiver: TAddress = None
) -> AsyncTransactionBuilder
Unfreeze balance to get TRX back.
Parameters:
-
(resource¶str, default:'ENERGY') –Resource type, can be
"ENERGY"or"BANDWIDTH"
Source code in tronpy/async_tron.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
vote_witness
¶
vote_witness(owner: TAddress, *votes: tuple[TAddress, int]) -> AsyncTransactionBuilder
Vote for witnesses. Empty votes to clean voted.
Source code in tronpy/async_tron.py
547 548 549 550 551 | |
withdraw_rewards
¶
withdraw_rewards(owner: TAddress) -> AsyncTransactionBuilder
Withdraw voting rewards.
Source code in tronpy/async_tron.py
553 554 555 556 | |
withdraw_stake_balance
¶
withdraw_stake_balance(owner: TAddress) -> AsyncTransactionBuilder
Withdraw all stake v2 balance after waiting for 14 days since unfreeze_balance call.
Parameters:
-
(owner¶TAddress) –
Source code in tronpy/async_tron.py
450 451 452 453 454 455 456 457 458 | |