-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdb_handler.py
More file actions
172 lines (148 loc) · 6.4 KB
/
Copy pathdb_handler.py
File metadata and controls
172 lines (148 loc) · 6.4 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import sqlite3
import time
from datetime import datetime
import requests
from future.moves import configparser
class DBHandler:
def __init__(self):
self.wallets_file = "wallets.db"
config = configparser.ConfigParser()
config.read('config.ini')
self.eth_api_key = config.get("ETH", "token")
self.bsc_api_key = config.get("BSC", "token")
def create_connection(self):
return sqlite3.connect(self.wallets_file)
def load_wallets(self):
connection = self.create_connection()
cursor = connection.cursor()
wallets = {}
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
table_names = cursor.fetchall()
for table_name in table_names:
table_name = table_name[0]
cursor.execute(f"SELECT * FROM {table_name}")
rows = cursor.fetchall()
for row in rows:
network = table_name
chat_id, address, last_check = row
if chat_id not in wallets:
wallets[chat_id] = {}
if network not in wallets[chat_id]:
wallets[chat_id][network] = []
wallets[chat_id][network].append(address)
connection.close()
return wallets
def save_wallet(self, chat_id, network, address):
if network == "wax":
timestamp = self.getCurrentTimeWax()
elif network == "eth":
timestamp = self.getCurrentTimeETH()
elif network == "bsc":
timestamp = self.getCurrentTimeBSC()
else:
return
with self.create_connection() as connection:
cursor = connection.cursor()
cursor.execute(f"CREATE TABLE IF NOT EXISTS {network} (chat_id INTEGER, address TEXT, last_check LONG)")
cursor.execute(f"SELECT * FROM {network} WHERE chat_id = ? AND address = ?", (chat_id, address))
existing_row = cursor.fetchone()
if existing_row is None:
cursor.execute(f"INSERT INTO {network} (chat_id, address, last_check) VALUES (?, ?, ?)",
(chat_id, address, timestamp))
return True
else:
return False
def remove_wallet(self, chat_id, network, address):
connection = self.create_connection()
cursor = connection.cursor()
cursor.execute(f"DELETE FROM {network} WHERE chat_id = ? AND address = ?",
(chat_id, address))
connection.commit()
connection.close()
def load_wallets_by_blockchain(self, blockchain):
connection = self.create_connection()
cursor = connection.cursor()
cursor.execute(f"SELECT * FROM {blockchain}")
rows = cursor.fetchall()
wallets = {}
for row in rows:
chat_id, address, last_check = row
if chat_id not in wallets:
wallets[chat_id] = []
wallets[chat_id].append({address: last_check})
connection.close()
return wallets
def update_last_check(self, blockchain, chat_id, address, new_last_check):
connection = self.create_connection()
cursor = connection.cursor()
cursor.execute(f"UPDATE {blockchain} SET last_check = ? WHERE chat_id = ? AND address = ?",
(new_last_check, chat_id, address))
connection.commit()
connection.close()
def getCurrentTimeWax(self):
url = f"https://api.waxsweden.org:443/v1/chain/get_info"
response = requests.post(url)
data = response.json()
head_block_time = data['head_block_time']
time_stamp = self.convert_to_timestamp(head_block_time)
return time_stamp
def convert_to_timestamp(self, timestamp):
dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f")
timestamp = int(dt.timestamp())
return timestamp
def getCurrentTimeETH(self):
url = f"https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey={self.eth_api_key}"
response = requests.get(url)
data = response.json()
block_number = int(data['result'], 16)
print(block_number)
while True:
timestamp = requests.get(
f"https://api.etherscan.io/api?module=block&action=getblockreward&blockno={block_number}&apikey={self.eth_api_key}").json()
print(timestamp)
if timestamp['result']['timeStamp'] is None:
time.sleep(2)
else:
return timestamp['result']['timeStamp']
def getCurrentTimeBSC(self):
url = f"https://api.bscscan.com/api?module=proxy&action=eth_blockNumber&apikey={self.bsc_api_key}"
response = requests.get(url)
data = response.json()
block_number = int(data['result'], 16)
print(block_number)
while True:
timestamp = requests.get(
f"https://api.bscscan.com/api?module=block&action=getblockreward&blockno={block_number}&apikey={self.bsc_api_key}").json()
print(timestamp)
if timestamp['result']['timeStamp'] is None:
time.sleep(2)
else:
return timestamp['result']['timeStamp']
def resetTimestamps(self):
connection = self.create_connection()
cursor = connection.cursor()
# Update last_check for eth
timestamp_eth = self.getCurrentTimeETH()
try:
cursor.execute("UPDATE eth SET last_check = ?", (timestamp_eth,))
connection.commit()
print(f"Updated last_check for ETH: {timestamp_eth}")
except Exception as e:
print(f"Error updating last_check for ETH: {e}")
# Update last_check for bsc
timestamp_bsc = self.getCurrentTimeBSC()
try:
cursor.execute("UPDATE bsc SET last_check = ?", (timestamp_bsc,))
connection.commit()
print(f"Updated last_check for BSC: {timestamp_bsc}")
except Exception as e:
print(f"Error updating last_check for BSC: {e}")
# Update last_check for wax
timestamp_wax = self.getCurrentTimeWax()
try:
cursor.execute("UPDATE wax SET last_check = ?", (timestamp_wax,))
connection.commit()
print(f"Updated last_check for WAX: {timestamp_wax}")
except Exception as e:
print(f"Error updating last_check for WAX: {e}")
connection.close()