Trace Transaction Status#
Run this example online by clicking 🚀 -> Binder on the top bar!
Preparation#
# preparation: init w3 instance and prepare transaction hash object tx_hash
from conflux_web3 import Web3
w3 = Web3(Web3.HTTPProvider("https://test.confluxrpc.com"))
acct = w3.account.create()
w3.cfx.default_account = acct
faucet = w3.cfx.contract(name="Faucet")
# get a tx_hash
tx_hash = faucet.functions.claimCfx().transact()
Status of a Transaction in Conflux#
In Conflux, transaction will witness several periods after it is sent:
pending: the transaction is sent, but not contained in any blockmined: the transaction is already contained in a block, but might not be executedexecuted: the transaction is executedconfirmed: the transaction is confirmed under PoW chain confirmation rule, which means it is extremly unlikely to be reverted unless the PoW chain is under attack.finalized: the transaction is finalized by PoS chain, which means the transaction is impossible to revert, but it would take 4~6 minutes before a transaction is finalized.
Trace the Status of a Transaction#
We can use w3.cfx.wait_till_transaction_mined, w3.cfx.wait_till_transaction_executed, w3.cfx.wait_till_transaction_confirmed, w3.cfx.wait_till_transaction_finalized to wait until transaction reached specific status.
w3.cfx.wait_till_transaction_mined(tx_hash)
w3.cfx.wait_till_transaction_executed(tx_hash)
w3.cfx.wait_till_transaction_confirmed(tx_hash)
if False: # 4~6 minutes is needed
w3.cfx.wait_till_transaction_finalized(tx_hash)
Syntactic Sugar#
We often need to trace the status of the sent transaction, so SDK wraps the returned hex transaction hash from send_transacation or send_raw_transaction to visit the above APIs easily.
tx_hash.mined()
tx_hash.executed()
tx_hash.confirmed()
if False:
tx_hash.finalized()