41
loading...
This website collects cookies to deliver better user experience
https://mainnet.infura.io/v3/your_project_id
CREATE
DATABASE eth;
USE
eth;
CREATE BLOCKCHAIN blocks HISTORICAL (
time TIMESTAMP,
number UINT64,
hash STRING SIZE=66,
parent_hash STRING SIZE=66,
nonce STRING SIZE=42,
sha3_uncles STRING SIZE=66,
logs_bloom STRING PACKED SIZE=18000,
transactions_root STRING SIZE=66,
state_root STRING SIZE=66,
receipts_root STRING SIZE=66,
miner STRING SIZE=42,
difficulty FLOAT64,
size_of_block INT64,
extra_data STRING PACKED,
gas_limit INT64,
transaction_count INT64,
base_fee_per_gas INT64
);
infru = "https://mainnet.infura.io/v3/your_project_id" #change me
web3 = Web3(Web3.HTTPProvider(infru))
conn = mdb_bp.driver.connect(
username="your username", #change me
password="your password", #change me
connection_protocol="tcp",
server_address="server address", #change me
server_port=5461,
database_name="eth",
parameters={"interpolateParams": True},
)
def main():
block_filter = web3.eth.filter('latest')
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(
asyncio.gather(
log_loop(block_filter, 2)))
finally:
# close loop to free up system resources
loop.close()
if __name__ == '__main__':
main()
handle_event
function.async def log_loop(event_filter, poll_interval):
while True:
for PairCreated in event_filter.get_new_entries():
handle_event(web3.eth.get_block(PairCreated))
await asyncio.sleep(poll_interval)
def handle_event(block):
print(block['number'])
conn.exec("INSERT blocks VALUES (" +
"\"" + str(datetime.datetime.utcfromtimestamp(block['timestamp'])) + "\"," +
str(block['number']) + "," +
"\"" + str(block['hash'].hex()) + "\"," +
"\"" + str(block['parentHash'].hex()) + "\"," +
"\"" + str(block['nonce'].hex()) + "\"," +
"\"" + str(block['sha3Uncles'].hex()) + "\"," +
"\"" + str(block['logsBloom'].hex()) + "\"," +
"\"" + str(block['transactionsRoot'].hex()) + "\"," +
"\"" + str(block['stateRoot'].hex()) + "\"," +
"\"" + str(block['receiptsRoot'].hex()) + "\"," +
"\"" + str(block['miner']) + "\"," +
str(block['difficulty']) + "," +
str(block['size']) + "," +
"\"" + str(block['extraData'].hex()) + "\"," +
str(block['gasLimit']) + "," +
str(len(block['transactions'])) + "," +
str(block['baseFeePerGas']) + ")")