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
TransactionBuildervia client.trx.foo functions - Optionally set
fee_limit,memo, orpermission_id - Call
builder.build, create aTransactionobject - Call
transaction.sign, to sign the transaction with a private key. Multiple calling ofsignmeansmulti-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¶
Trx
¶
Trx(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 Stake 1.0 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/tron.py
301 302 | |
account_permission_update
¶
account_permission_update(owner: TAddress, perm: dict) -> TransactionBuilder
Update account permission.
Parameters:
-
(owner¶TAddress) – -
(perm¶dict) –Permission dict from
get_account_permission
Source code in tronpy/tron.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
account_update
¶
account_update(owner: TAddress, name: str) -> TransactionBuilder
Update account name. An account can only set name once.
Source code in tronpy/tron.py
417 418 419 420 421 422 | |
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,
) -> TransactionBuilder
Issue a TRC10 token.
Almost all parameters have resonable defaults.
Source code in tronpy/tron.py
338 339 340 341 342 343 344 345 346 347 348 349 350 351 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 | |
asset_transfer
¶
asset_transfer(
from_: TAddress, to: TAddress, amount: int, token_id: int
) -> TransactionBuilder
Transfer TRC10 tokens.
Source code in tronpy/tron.py
326 327 328 329 330 331 332 333 334 335 336 | |
create_witness
¶
create_witness(owner: TAddress, url: str) -> TransactionBuilder
Create a new witness, will consume 1_000 TRX.
Source code in tronpy/tron.py
532 533 534 535 | |
delegate_resource
¶
delegate_resource(
owner: TAddress,
receiver: TAddress,
balance: int,
resource: str = "BANDWIDTH",
lock: bool = False,
lock_period: int = None,
) -> TransactionBuilder
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/tron.py
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | |
deploy_contract
¶
deploy_contract(owner: TAddress, contract: Contract) -> TransactionBuilder
Deploy a new contract on chain.
Source code in tronpy/tron.py
550 551 552 553 554 555 556 557 | |
freeze_balance
¶
freeze_balance(
owner: TAddress, amount: int, resource: str = "ENERGY"
) -> TransactionBuilder
Freeze balance to get energy or bandwidth, for 3 days.
Parameters:
-
(owner¶TAddress) – -
(amount¶int) – -
(resource¶str, default:'ENERGY') –Resource type, can be
"ENERGY"or"BANDWIDTH"
Source code in tronpy/tron.py
424 425 426 427 428 429 430 431 432 433 434 435 436 | |
transfer
¶
transfer(from_: TAddress, to: TAddress, amount: int) -> TransactionBuilder
Transfer TRX. amount in SUN.
Source code in tronpy/tron.py
317 318 319 320 321 322 | |
undelegate_resource
¶
undelegate_resource(
owner: TAddress, receiver: TAddress, balance: int, resource: str = "BANDWIDTH"
) -> TransactionBuilder
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/tron.py
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
unfreeze_balance
¶
unfreeze_balance(
owner: TAddress, resource: str = "ENERGY", *, unfreeze_balance: int
) -> TransactionBuilder
Unfreeze balance to get TRX back.
Parameters:
-
(owner¶TAddress) – -
(resource¶str, default:'ENERGY') –Resource type, can be
"ENERGY"or"BANDWIDTH" -
(unfreeze_balance¶int) –
Source code in tronpy/tron.py
448 449 450 451 452 453 454 455 456 457 458 459 460 | |
unfreeze_balance_legacy
¶
unfreeze_balance_legacy(
owner: TAddress, resource: str = "ENERGY", receiver: TAddress = None
) -> TransactionBuilder
Unfreeze Stake 1.0 balance to get TRX back.
Parameters:
-
(owner¶TAddress) – -
(resource¶str, default:'ENERGY') –Resource type, can be
"ENERGY"or"BANDWIDTH" -
(receiver¶TAddress, default:None) –
Source code in tronpy/tron.py
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
vote_witness
¶
vote_witness(owner: TAddress, *votes: tuple[TAddress, int]) -> TransactionBuilder
Vote for witnesses. Empty votes to clean voted.
Source code in tronpy/tron.py
537 538 539 540 541 | |
withdraw_rewards
¶
withdraw_rewards(owner: TAddress) -> TransactionBuilder
Withdraw voting rewards.
Source code in tronpy/tron.py
543 544 545 546 | |
withdraw_stake_balance
¶
withdraw_stake_balance(owner: TAddress) -> TransactionBuilder
Withdraw all stake v2 balance after waiting for 14 days since unfreeze_balance call.
Parameters:
-
(owner¶TAddress) –
Source code in tronpy/tron.py
438 439 440 441 442 443 444 445 446 | |
TransactionBuilder
¶
TransactionBuilder(inner: dict, client: Tron, method: ContractMethod = None)
TransactionBuilder, to build a Transaction object.
Methods:
-
build–Build the transaction.
-
fee_limit–Set fee_limit of the transaction, in
SUN. -
memo–Set memo of the transaction.
-
permission_id–Set permission_id of the transaction.
-
with_owner–Set owner of the transaction.
Source code in tronpy/tron.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
build
¶
build(*, offline: bool = False, ref_block_id: str = None, **kwargs) -> Transaction
Build the transaction.
Source code in tronpy/tron.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
fee_limit
¶
fee_limit(value: int) -> TransactionBuilder
Set fee_limit of the transaction, in SUN.
Source code in tronpy/tron.py
270 271 272 273 | |
memo
¶
memo(memo: str | bytes) -> TransactionBuilder
Set memo of the transaction.
Source code in tronpy/tron.py
260 261 262 263 264 | |
permission_id
¶
permission_id(perm_id: int) -> TransactionBuilder
Set permission_id of the transaction.
Source code in tronpy/tron.py
255 256 257 258 | |
with_owner
¶
with_owner(addr: TAddress) -> TransactionBuilder
Set owner of the transaction.
Source code in tronpy/tron.py
247 248 249 250 251 252 253 | |
Transaction
¶
Transaction(
raw_data: dict,
client: Tron = None,
method: ContractMethod = None,
txid: str = "",
permission: dict = EMPTY,
signature: list = None,
)
The Transaction object, signed or unsigned.
Methods:
-
broadcast–Broadcast the transaction to TRON network.
-
set_signature–set transaction signature
-
sign–Sign the transaction with a private key.
-
update–update Transaction, change ref_block and txID, remove all signature
Attributes:
-
txid(str) –The transaction id in hex.
Source code in tronpy/tron.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
broadcast
¶
broadcast() -> TransactionRet
Broadcast the transaction to TRON network.
Source code in tronpy/tron.py
188 189 190 | |
set_signature
¶
set_signature(signature: list) -> Transaction
set transaction signature
Source code in tronpy/tron.py
192 193 194 195 | |
sign
¶
sign(priv_key: PrivateKey) -> Transaction
Sign the transaction with a private key.
Source code in tronpy/tron.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
update
¶
update()
update Transaction, change ref_block and txID, remove all signature
Source code in tronpy/tron.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
TransactionRet
¶
TransactionRet(iterable, client: Tron, method: ContractMethod = None)
flowchart TD
tronpy.tron.TransactionRet[TransactionRet]
click tronpy.tron.TransactionRet href "" "tronpy.tron.TransactionRet"
Methods:
Attributes:
-
txid–The transaction id in hex.
Source code in tronpy/tron.py
50 51 52 53 54 55 | |
result
¶
result(timeout=30, interval=1.6, solid=False) -> dict
Wait the contract calling result.
Returns:
-
dict–Result of contract method
Source code in tronpy/tron.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
wait
¶
wait(timeout=30, interval=1.6, solid=False) -> dict
Wait the transaction to be on chain.
Returns:
-
dict–TransactionInfo
Source code in tronpy/tron.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |