如何获取以太坊合约和账户信息(使用geth和web3.py)
Posted
技术标签:
【中文标题】如何获取以太坊合约和账户信息(使用geth和web3.py)【英文标题】:How to get ethereum contract and account information (using geth and web3.py) 【发布时间】:2021-09-29 13:48:16 【问题描述】:我想获取以太坊数据,所以我使用 geth 和 web3.py。 另外,我目前使用的是geth light模式。
我通过下面的代码获取交易数据...
from web3 import Web3
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:8545'))
block = w3.eth.get_block('latest')
transactions = block.transactions
for tx in transactions:
info = w3.eth.get_transaction(tx)
print(info)
AttributeDict('blockHash': HexBytes('0x6e04c6c9151cfa458966d4ec4d10903c2d91558282b51344df64fc2bdae3e0b8'), 'blockNumber': 12875217, 'from': '0x4919F5DA960Edaf34b36473c05a543d4C18Bfc15', 'gas': 155808, 'gasPrice': 23000000000, 'hash': HexBytes('0x4015ec12e9482c668f6470844c5022041a215a59917ad4f2a6973a5688f09ac3'), 'input': '0x7ff36ab50000000000000000000000000000000000000000000068b6f1fa997190b6e8c300000000000000000000000000000000000000000000000000000000000000800000000000000000000000004919f5da960edaf34b36473c05a543d4c18bfc150000000000000000000000000000000000000000000000000000000060f937a80000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000008f8221afbb33998d8584a2b05749ba73c37a938a', 'nonce': 249, 'to': '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', 'transactionIndex': 0, 'value': 12000000000000000000, 'type': '0x0', 'v': 38, 'r': HexBytes('0x4475f064437911a03040e68b2ff1dd670f219affad9a20a20e167c873a6743ae'), 's': HexBytes('0x01f7489fe57ed19c3898e57db5a8b18e19c60281b84f9a3b3bdc9991ade0d42b'))
AttributeDict('blockHash': HexBytes('0x6e04c6c9151cfa458966d4ec4d10903c2d91558282b51344df64fc2bdae3e0b8'), 'blockNumber': 12875217, 'from': '0x744bc1d963E8f54395Dfe504E343fC3F2fe8FC8B', 'gas': 600000, 'gasPrice': 0, 'hash': HexBytes('0xcc2e940d993ed1f8dc165b600d14328476f065efc429b5f2912e1fec46f61647'), 'input': '0x0000000200c475d1027adb368141248d6c50b614aee3b6a9ef9f8c20bd4a7d4be868e0b811ea804faf0d3a325c3a29a9ad8f8221afbb33998d8584a2b05749ba73c37a938a00000000000022272d97e7c80db80000000015be8d50fce354ed377b00000000000023175a7257e16e1e03004b22004b23004b24', 'nonce': 11172, 'to': '0x0000000000d41C96294CCdaC8612Bdfe29C641aF', 'transactionIndex': 1, 'value': 64086484616915290, 'type': '0x1', 'accessList': [AttributeDict('address': '0x8594446b3ff3f0e5e2f5577948ab8980ba13b0ea', 'storageKeys': []), AttributeDict('address': '0x5aba0395ad0f4d699ba117df7c064985fc7eece1', 'storageKeys': []), AttributeDict('address': '0xe813ce6019d39752f9cecff40916af87de65cb1a', 'storageKeys': []), AttributeDict('address': '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', 'storageKeys': []), AttributeDict('address': '0x8f8221afbb33998d8584a2b05749ba73c37a938a', 'storageKeys': []), AttributeDict('address': '0x7adb368141248d6c50b614aee3b6a9ef9f8c20bd', 'storageKeys': ['0x0000000000000000000000000000000000000000000000000000000000000006', '0x0000000000000000000000000000000000000000000000000000000000000007', '0x0000000000000000000000000000000000000000000000000000000000000008', '0x000000000000000000000000000000000000000000000000000000000000000c']), AttributeDict('address': '0x4a7d4be868e0b811ea804faf0d3a325c3a29a9ad', 'storageKeys': ['0x0000000000000000000000000000000000000000000000000000000000000006', '0x0000000000000000000000000000000000000000000000000000000000000007', '0x0000000000000000000000000000000000000000000000000000000000000008', '0x000000000000000000000000000000000000000000000000000000000000000c'])], 'chainId': '0x1', 'v': 0, 'r': HexBytes('0x53f80b7c54e64167d76aa195b256836d2b7d013caa0cf8838a6952e3cf7607a8'), 's': HexBytes('0x5f9dc8a13651b29fca884947d2fe4566424e2cb2ce5e9d517530a1abb32b311b'))
.
.
.
我想接收特定区块中使用的合约信息和账户信息(ex number = 12875255),我该怎么做?
和 ethescan 一样,我想在一个块中读取各种信息。 https://etherscan.io/block/12875255
【问题讨论】:
我投票结束这个问题,因为它属于 ethereum.stackexchange.com 【参考方案1】:要获取特定区块号的区块数据,可以使用get_block() 方法。
例子:
web3.eth.get_block(2000000, true)
第一个参数是块号。第二个参数决定是显示交易详情 (true
) 还是仅显示其哈希值 (false
)。
【讨论】:
谢谢!我还有其他问题。我想获取帐户信息和合同信息...我该怎么做?? 我要获取账户和合约数据..!如果我知道地址,您能从该地址获取 TX 或历史记录吗?我希望它表现得像下面的 Etherscan ..! etherscan.io/address/0x2354f932573a728436f519238706c2582c6db92e 您可以使用其他 web3 方法(例如 current balance 或 contract bytecode)获取一些数据。其中一些聚合在区块链浏览器的单独数据库中。以上是关于如何获取以太坊合约和账户信息(使用geth和web3.py)的主要内容,如果未能解决你的问题,请参考以下文章