通过 RPC 查询数据#

点击顶部栏的 🚀 -> Binder 在线运行此示例!

准备工作#

# preparation: import and init w3 instance
from conflux_web3 import Web3

w3 = Web3(Web3.HTTPProvider("https://test.confluxrpc.com"))

# prepare constants
transaction_hash = "0x10a721e2654523a7ff682c1c8d3f868a9fdf78a3be1858bbe1f06147128d6d94"
epoch = 98943500

通过 RPC 查询数据#

我们可以使用 SDK 调用 RPC 接口来查询区块链状态、交易数据、区块数据等信息。完整的 RPC 文档可以在这里找到。

查询区块链状态#

# get_status provides an overview of blockchain status
w3.cfx.get_status()
AttributeDict({'bestHash': HexBytes('0x39c4f587683f82827b5d039e06bcf19ce0ac64a5314d0b7d280d789b8ff318a5'),
 'chainId': 1,
 'ethereumSpaceChainId': 71,
 'networkId': 1,
 'epochNumber': 99720387,
 'blockNumber': 127806498,
 'pendingTxNumber': 0,
 'latestCheckpoint': 99640000,
 'latestConfirmed': 99720322,
 'latestState': 99720383,
 'latestFinalized': 99719940})

许多 RPC 方法都可以接受 epoch number 参数。epoch number 参数可以是:

  • 一个整数

  • 或者是字符串形式的 epoch 标签:latest_minedlatest_statelatest_confirmedlatest_finalized

Conflux 中的 epoch number 概念与其他区块链中的 block number 概念类似,但一个 epoch 可以包含一个或多个区块。更多信息请参考 https://developer.confluxnetwork.org/conflux-doc/docs/json_rpc#the-default-epochnumber-parameter。

# latest_mined epoch number
print(f"latest_mined epoch number: {w3.cfx.epoch_number}")
# get epoch number by tag
print(f'latest_state epoch numebr: {w3.cfx.epoch_number_by_tag("latest_state")}')
latest_mined epoch number: 99720387
latest_state epoch numebr: 99720383

查询交易#

# get transaction data by hash
# this RPC is usable after transaction is sent
w3.cfx.get_transaction_by_hash(transaction_hash)
AttributeDict({'hash': HexBytes('0x10a721e2654523a7ff682c1c8d3f868a9fdf78a3be1858bbe1f06147128d6d94'),
 'nonce': 2,
 'blockHash': HexBytes('0xf0ac5f8d757a7415876e322a245d5eff76349b656b3eadb10d5e09a4a3f7d59d'),
 'transactionIndex': 0,
 'from': 'cfxtest:aan2vyszrgz3sr3tug73ywb8k128y5wa7yhpjm1hn1',
 'to': 'cfxtest:acejjfa80vj06j2jgtz9pngkv423fhkuxj786kjr61',
 'value': 0 Drip,
 'gasPrice': 1000000000 Drip,
 'gas': 41633,
 'contractCreated': None,
 'data': HexBytes('0x5f3cc9f7'),
 'storageLimit': 0,
 'epochHeight': 87083312,
 'chainId': 1,
 'status': 0,
 'v': 1,
 'r': HexBytes('0x17b30481d63a49223092215a2aa503938519df5cac0d20cbff7ebf50569776cd'),
 's': HexBytes('0x5fc96a1e383879cf66c09d106b2df4907c674a263c9a7bba602078c90b0038b1')})
# get transaction receipt by hash
# the transaction receipt is available after it is executed
w3.cfx.get_transaction_receipt(transaction_hash)
AttributeDict({'transactionHash': HexBytes('0x10a721e2654523a7ff682c1c8d3f868a9fdf78a3be1858bbe1f06147128d6d94'),
 'index': 0,
 'blockHash': HexBytes('0xf0ac5f8d757a7415876e322a245d5eff76349b656b3eadb10d5e09a4a3f7d59d'),
 'epochNumber': 87083316,
 'from': 'cfxtest:aan2vyszrgz3sr3tug73ywb8k128y5wa7yhpjm1hn1',
 'to': 'cfxtest:acejjfa80vj06j2jgtz9pngkv423fhkuxj786kjr61',
 'gasUsed': 35480,
 'gasFee': 35480000000000 Drip,
 'contractCreated': None,
 'logs': [],
 'logsBloom': HexBytes('0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'),
 'stateRoot': HexBytes('0xdbfbe3f9b08aa5fb04ca5218ebc245b3941cce9d30409ab854456f3070b454f6'),
 'outcomeStatus': 0,
 'txExecErrorMsg': None,
 'gasCoveredBySponsor': True,
 'storageCoveredBySponsor': True,
 'storageCollateralized': 0,
 'storageReleased': []})

查询区块#

在 Conflux 中,每个 epoch 包含一个或多个区块,其中一个区块被称为”枢轴区块(pivot block)”。枢轴区块决定了每个 epoch 中包含哪些区块。

更多信息请参考 Epoch

# query blocks in specific epoch
block_hashes = w3.cfx.get_blocks_by_epoch(epoch)
print(f"blocks in epoch {epoch}:\n{block_hashes}")
# the last element of block_hashes is the pivot block
print(f"pivot block hash of epoch {epoch}: {block_hashes[-1].hex()}")
blocks in epoch 98943500:
[HexBytes('0xae622377e139608a9202504df4bf20570c4741370d36f668952ae91c57c5da69'), HexBytes('0x104a3549108c7c83a36c65d188e842f6deb9a770d3ca749a222fce628846c04b')]
pivot block hash of epoch 98943500: 0x104a3549108c7c83a36c65d188e842f6deb9a770d3ca749a222fce628846c04b
# if the block is a pivot block, we can get it using epoch number
# and of cource, we can query a block using its hash
assert w3.cfx.get_block_by_epoch_number(epoch) == w3.cfx.get_block_by_hash(block_hashes[-1])
# get_block_by_epoch_number also accepts an epoch number tag
w3.cfx.get_block_by_epoch_number("latest_state")
AttributeDict({'hash': HexBytes('0x71b6406338ecbd2f40d12bc2b91304c3567fbb04a165efe299301a9c3acab46a'),
 'parentHash': HexBytes('0x37ab44387ae132e3eea6c666bfb5cd58af50d0b172ab21b0be5ff85073bc5592'),
 'height': 99720384,
 'miner': 'cfxtest:aanpu16mtgc7dke5xhuktyfyef8f00pz8a2z5mc14g',
 'deferredStateRoot': HexBytes('0x413d1a352ecec910bc7e7c5dabba9b8e83e36395ef6485db7881f46e723660f5'),
 'deferredReceiptsRoot': HexBytes('0x09f8709ea9f344a810811a373b30861568f5686e649d6177fd92ea2db7477508'),
 'deferredLogsBloomHash': HexBytes('0xd397b3b043d87fcd6fad1291ff0bfd16401c274896d8c63a923727f077b8e0b5'),
 'blame': 0,
 'transactionsRoot': HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'),
 'epochNumber': 99720384,
 'blockNumber': 127806494,
 'gasLimit': 30000000,
 'gasUsed': 0,
 'timestamp': 1668507784,
 'difficulty': 58688104,
 'powQuality': HexBytes('0x039fc293'),
 'refereeHashes': [],
 'adaptive': False,
 'nonce': HexBytes('0x24d32abf7e0f4c67'),
 'size': 0,
 'custom': [HexBytes('0x02')],
 'posReference': HexBytes('0x11f970d63eb9eaf581315b67f2258c0c59ad13572be29a2187c900ac576d3003'),
 'transactions': []})
# get a block by block number is also viable 
w3.cfx.get_block_by_block_number(110979562)
AttributeDict({'hash': HexBytes('0xf0ac5f8d757a7415876e322a245d5eff76349b656b3eadb10d5e09a4a3f7d59d'),
 'parentHash': HexBytes('0xeaf2f6227c068ae27cb847657af7eb7e3cfd6696f5a795d2f38159074d20a8c9'),
 'height': 87083316,
 'miner': 'cfxtest:aaskvgxcfej371g4ecepx9an78ngrke5ay9f8jtbgg',
 'deferredStateRoot': HexBytes('0xa26347f02f312db0fba655c29b9fa853e8e6242449a67added74c62679122f62'),
 'deferredReceiptsRoot': HexBytes('0x09f8709ea9f344a810811a373b30861568f5686e649d6177fd92ea2db7477508'),
 'deferredLogsBloomHash': HexBytes('0xd397b3b043d87fcd6fad1291ff0bfd16401c274896d8c63a923727f077b8e0b5'),
 'blame': 0,
 'transactionsRoot': HexBytes('0xdc62bb0e7115a9727688811f4e090efdfd203a516953ff7829e21dea29e1f166'),
 'epochNumber': 87083316,
 'blockNumber': 110979562,
 'gasLimit': 30000000,
 'gasUsed': 35480,
 'timestamp': 1660093852,
 'difficulty': 54757256,
 'powQuality': HexBytes('0x0365f39d'),
 'refereeHashes': [],
 'adaptive': False,
 'nonce': HexBytes('0xe72421314ac723e6'),
 'size': 113,
 'custom': [HexBytes('0x01')],
 'posReference': HexBytes('0xe7c39fb3312519d78e5606ef4d9040b558b852e7af439a8fcad1632de776cc06'),
 'transactions': [HexBytes('0x10a721e2654523a7ff682c1c8d3f868a9fdf78a3be1858bbe1f06147128d6d94')]})

其他 RPC 接口#

除了上述接口外,conflux-web3 支持 cfx 命名空间下的所有 RPC 方法。您可以访问 https://developer.confluxnetwork.org/conflux-doc/docs/json_rpc#json-rpc-methods 获取更多信息。

下面是一些示例:

w3.cfx.gas_price
1000000000 Drip
len(w3.cfx.get_code("cfxtest:acejjfa80vj06j2jgtz9pngkv423fhkuxj786kjr61"))
5964