跟踪交易状态
内容
跟踪交易状态¶
单击顶部栏上的🚀
-> Binder
在线运行此示例!
准备¶
# 准备阶段:初始化 web3 对象并提供交易哈希对象 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()
Conflux 中的交易状态¶
在 Conflux 中,交易在发送后会经历几个阶段:
pending
:交易已发送,但不包含在任何区块中。mined
:交易已经包含在一个区块中,但可能尚未被执行。executed
:交易被执行。confirmed
:交易在 PoW 链确认规则下被确认,这意味着除非 PoW 链受到攻击,否则它极不可能被逆转。finalized
:交易已被 PoS 链最终确认,这意味着该交易不可能被逆转。但需要 5~10 分钟交易才会被最终确认。
跟踪交易的状态¶
我们可以使用w3.cfx.wait_till_transaction_mined
、 w3.cfx.wait_till_transaction_executed
、 w3.cfx.wait_till_transaction_confirmed
、 w3.cfx.wait_till_transaction_finalized
等待交易达到特定状态。
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: # 需要 5~10 分钟
w3.cfx.wait_till_transaction_finalized(tx_hash)
语法糖¶
我们经常需要跟踪已发送交易的状态,因此SDK 将send_transacation
或send_raw_transaction
返回的交易哈希进行了封装,以便访问上述API。
tx_hash.mined()
tx_hash.executed()
tx_hash.confirmed()
if False:
tx_hash.finalized()