-
Notifications
You must be signed in to change notification settings - Fork 2
/
web3_get_historical_tx.py
82 lines (61 loc) · 2.52 KB
/
web3_get_historical_tx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import sys
import json
import sqlite3
import getopt
import pandas as pd
from web3 import Web3
provider = Web3.WebsocketProvider('ws://127.0.0.1:8546')
w3 = Web3(provider)
class DBController:
def __init__(self, db_name):
self.db_name = db_name
self.conn = sqlite3.connect(self.db_name)
self.db_cursor = self.conn.cursor()
def write_data(self, df, table, index=False, if_table_exists='replace'):
df.to_sql(name=table, con=self.conn, if_exists=if_table_exists, index=index)
self.conn.commit()
def read_data(self, sql_command):
df = pd.read_sql(sql_command, con=self.conn)
return df
def execute_sql(self, sql_command):
query = self.db_cursor.execute(sql_command)
self.conn.commit()
return query
def close_conn(self):
self.conn.commit()
self.conn.close()
def restart_conn(self):
self.conn.commit()
self.conn.close()
self.conn = sqlite3.connect(self.db_name)
def connectedToNode():
return w3.isConnected()
def getHistoricalSample(start, end, increment):
if not connectedToNode():
sys.exit('not connected to node.')
DB = DBController('Transactions.db')
first_run = True
for block_counter in range(start, end, increment):
block = w3.eth.get_block(block_counter)
tx_df = pd.DataFrame(columns=['blockHash', 'blockNumber', 'from', 'gas', 'gasPrice', 'hash', 'input', 'nonce', 'to', 'transactionIndex', 'value', 'type', 'v', 'r', 's'])
for tx in block.transactions:
tx_raw = w3.eth.get_transaction(tx)
tx_json = Web3.toJSON(tx_raw)
tx_df.loc[len(tx_df)] = dict(json.loads(tx_json))
if first_run:
DB.write_data(tx_df.astype(str), 'transactions', if_table_exists='replace') # cast all values as strings b/c sqlite does not support large integers
else:
DB.write_data(tx_df.astype(str), 'transactions', if_table_exists='append') # cast all values as strings b/c sqlite does not support large integers
print(f'Read block number {block_counter:,} with {len(tx_df):,} txs')
first_run = False
argv = sys.argv[1:]
start, end, increment = 0, 0, 0
args, values = getopt.getopt(argv, 's:e:i:')
for currentArgument, currentValue in args:
if currentArgument in ['-s']:
start = int(currentValue)
elif currentArgument in ['-e']:
end = int(currentValue)
elif currentArgument in ['-i']:
increment = int(currentValue)
getHistoricalSample(start, end, increment)