Sending Transaction¶
A Transaction in TRON is a system contract call, including TRX transfer, TRC10 transfer, contract call, create contract, vote witness, create new TRC10 token, and so on. The system contract call will be saved on the blockchain(in a block as a element in transactions list).
The routine for sending¶
A normal routine for sending a transaction is:
Create -> Sign -> Broadcast -> (wait) -> Lookup and get receipt
TronPy chooses the method chaining approach to create,
sign, and broadcast any transaction. All type of transactions can be created via Tron.trx
object.
from tronpy import Tron
from tronpy.keys import PrivateKey
client = Tron(network='nile')
priv_key = PrivateKey(bytes.fromhex("8888888888888888888888888888888888888888888888888888888888888888"))
txn = (
client.trx.transfer("TJzXt1sZautjqXnpjQT4xSCBHNSYgBkDr3", "TVjsyZ7fYF3qLF6BQgPmTEZy1xrNNyVAAA", 1_000)
.memo("test memo")
.build()
.sign(priv_key)
)
print(txn.txid)
print(txn.broadcast().wait())
In TronPy, the routine is:
- Create a
TRON API client
- Create a
TransactionBuilder
viaclient.trx.foo
functions - Optionally set
fee_limit()
,memo()
, orpermission_id()
- Call
builder.
build()
, create aTransaction
object - Call
transaction.
sign()
, to sign the transaction with aprivate key
. Multiple calling of sign means multi-sign - Call
transaction.
broadcast()
, to broadcast the transaction object - Optionally call
ret.
wait()
, to wait and query the transaction receipt
The above steps are made easy through following APIs.
API reference¶
-
class
tronpy.tron.
Trx
¶ The Trx(transaction) API.
-
account_permission_update
(owner: str, perm: dict) → tronpy.tron.TransactionBuilder¶ Update account permission.
Parameters: - owner –
- perm – Permission dict from
get_account_permission()
-
account_update
(owner: str, name: str) → tronpy.tron.TransactionBuilder¶ 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.tron.TransactionBuilder¶ Issue a TRC10 token.
Almost all parameters have resonable defaults.
-
asset_transfer
(from_: str, to: str, amount: int, token_id: int) → tronpy.tron.TransactionBuilder¶ Transfer TRC10 tokens.
-
create_witness
(owner: str, url: str) → tronpy.tron.TransactionBuilder¶ Create a new witness, will consume 1_000 TRX.
-
delegate_resource
(owner: str, receiver: str, balance: int, resource: str = 'BANDWIDTH', lock: bool = False) → tronpy.tron.TransactionBuilder¶ 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.contract.Contract) → tronpy.tron.TransactionBuilder¶ Deploy a new contract on chain.
-
freeze_balance
(owner: str, amount: int, resource: str = 'ENERGY') → tronpy.tron.TransactionBuilder¶ Freeze balance to get energy or bandwidth, for 3 days.
Parameters: - owner –
- amount –
- resource – Resource type, can be
"ENERGY"
or"BANDWIDTH"
-
transfer
(from_: str, to: str, amount: int) → tronpy.tron.TransactionBuilder¶ Transfer TRX.
amount
in SUN.
-
undelegate_resource
(owner: str, receiver: str, balance: int, resource: str = 'BANDWIDTH') → tronpy.tron.TransactionBuilder¶ 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.tron.TransactionBuilder¶ Unfreeze balance to get TRX back.
Parameters: - owner –
- resource – Resource type, can be
"ENERGY"
or"BANDWIDTH"
- unfreeze_balance –
-
unfreeze_balance_legacy
(owner: str, resource: str = 'ENERGY', receiver: str = None) → tronpy.tron.TransactionBuilder¶ Unfreeze Stake 1.0 balance to get TRX back.
Parameters: - owner –
- resource – Resource type, can be
"ENERGY"
or"BANDWIDTH"
- receiver –
-
vote_witness
(owner: str, *votes) → tronpy.tron.TransactionBuilder¶ Vote for witnesses. Empty
votes
to clean voted.
-
withdraw_rewards
(owner: str) → tronpy.tron.TransactionBuilder¶ Withdraw voting rewards.
-
-
class
tronpy.tron.
TransactionBuilder
¶ TransactionBuilder, to build a
Transaction
object.-
build
(options=None, **kwargs) → tronpy.tron.Transaction¶ Build the transaction.
-
fee_limit
(value: int) → tronpy.tron.TransactionBuilder¶ Set fee_limit of the transaction, in SUN.
-
memo
(memo: Union[str, bytes]) → tronpy.tron.TransactionBuilder¶ Set memo of the transaction.
-
permission_id
(perm_id: int) → tronpy.tron.TransactionBuilder¶ Set permission_id of the transaction.
-
with_owner
(addr: str) → tronpy.tron.TransactionBuilder¶ Set owner of the transaction.
-
-
class
tronpy.tron.
Transaction
¶ The Transaction object, signed or unsigned.
-
broadcast
() → tronpy.tron.TransactionRet¶ Broadcast the transaction to TRON network.
-
set_signature
(signature: list) → tronpy.tron.Transaction¶ set transaction signature
-
sign
(priv_key: tronpy.keys.PrivateKey) → tronpy.tron.Transaction¶ Sign the transaction with a private key.
-
txid
= None¶ The transaction id in hex.
-
update
()¶ update Transaction, change ref_block and txID, remove all signature
-
-
class
tronpy.tron.
TransactionRet
¶ -
result
(timeout=30, interval=1.6, solid=False) → dict¶ Wait the contract calling result.
Returns: Result of contract method
-
txid
¶ The transaction id in hex.
-
wait
(timeout=30, interval=1.6, solid=False) → dict¶ Wait the transaction to be on chain.
Returns: TransactionInfo
-