追踪交易状态#
点击顶部栏的 🚀 -> Binder 在线运行此示例!
准备工作#
# 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()
Conflux 中的交易状态#
在 Conflux 中,交易发送后会经历以下几个阶段:
pending:交易已发送,但尚未被打包进区块mined:交易已被打包进区块,但可能尚未执行executed:交易已执行完成confirmed:交易已经通过 PoW 链的确认规则,这意味着除非 PoW 链遭受攻击,否则交易几乎不可能被回滚finalized:交易已被 PoS 链最终确认,这意味着交易不可能被回滚,但通常需要 4~6 分钟才能达到这个状态
追踪交易状态#
我们可以使用 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: # 4~6 minutes is needed
w3.cfx.wait_till_transaction_finalized(tx_hash)
语法糖#
我们经常需要追踪已发送交易的状态,因此 SDK 对 send_transaction 或 send_raw_transaction 返回的十六进制交易哈希进行了封装,使其能够方便地访问上述 API。
tx_hash.mined()
tx_hash.executed()
tx_hash.confirmed()
if False:
tx_hash.finalized()