-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
300 lines (235 loc) · 10.2 KB
/
utils.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import asyncio
import logging
import time
import requests
from eth_account import Account
from eth_account.messages import encode_defunct
from typing import Optional
from datetime import datetime
from headers import headers
import json
try:
# Load data from the JSON file
with open('config.json', "r") as file:
data = json.load(file)
except FileNotFoundError:
raise FileNotFoundError(f"config.json file does not exist. Create one!")
except json.JSONDecodeError:
raise ValueError(f"The config file is not a valid JSON file.")
def get_response(url: str, payload: Optional[dict] = None):
"""Make request with optional proxy support from config"""
try:
proxies = data.get('proxies')
request_params = {
'url': url,
'headers': headers,
'timeout': (3.05, 20),
**({"proxies": {"http": proxies, "https": proxies}} if proxies else {})
}
if payload:
response = requests.post(**request_params, json=payload)
else:
response = requests.get(**request_params)
if response.status_code == 200:
return response.json()
else:
raise Exception(response.text)
except Exception as e:
raise e
def get_ip():
url = 'https://api.ipify.org?format=json'
json_response = get_response(url)
proxy_ip = json_response.get('ip')
no_proxies_response = requests.get(url)
# Extract relevant information
real_ip = no_proxies_response.json().get('ip')
if proxy_ip == real_ip:
protected = False
else:
protected = True
return proxy_ip, protected
def short_address(wallet_address):
address = f"{''.join(wallet_address[:5])}..{''.join(wallet_address[-5:])}"
return address
def get_wallet_details(wallet_address):
url = f"https://referralapi.layeredge.io/api/referral/wallet-details/{wallet_address}"
wallet_info = get_response(url)['data']
return wallet_info
def get_node_status(wallet_address):
url = f"https://referralapi.layeredge.io/api/light-node/node-status/{wallet_address}"
node_status = get_response(url)['data']
return node_status
def register_wallet(wallet_address, referral_code):
url = f"https://referralapi.layeredge.io/api/referral/register-wallet/{referral_code}"
payload = {
"walletAddress": f"{wallet_address}"
}
response = get_response(url, payload=payload)
resp_message = response['message']
logging.info(f"User {short_address(wallet_address)}: {resp_message}")
return response
def sign_message(private_key: str, message: str) -> str:
"""
Sign a message using an Ethereum private key.
Args:
private_key (str): The private key to sign with
message (str): The message to sign
Returns:
str: The signature in hexadecimal format with 0x prefix
"""
encoded_message = message.encode('utf-8')
account = Account.from_key(private_key)
signed_message = account.sign_message(encode_defunct(encoded_message))
# Convert to proper hex format with 0x prefix
return '0x' + signed_message.signature.hex()
def start_node(private_key):
account = Account.from_key(private_key)
wallet_address = str(account.address)
timestamp = int(time.time() * 1000)
message = f'Node activation request for {wallet_address} at {timestamp}'
signature = sign_message(private_key, message)
url = f"https://referralapi.layeredge.io/api/light-node/node-action/{wallet_address}/start"
payload = {
"sign": signature,
"timestamp": timestamp
}
response = get_response(url, payload=payload)
message = response['message']
logging.info(f"User {short_address(wallet_address)}: {message}")
start_timestamp = response['data']['startTimestamp']
return start_timestamp
def claim_node_points(private_key):
account = Account.from_key(private_key)
wallet_address = str(account.address)
timestamp = int(time.time() * 1000)
message = f'I am claiming my daily node point for {wallet_address} at {timestamp}'
signature = sign_message(private_key, message)
url = "https://referralapi.layeredge.io/api/light-node/claim-node-points"
payload = {
"sign": signature,
"timestamp": timestamp,
"walletAddress": wallet_address
}
response = get_response(url, payload=payload)
claim_msg = response['message']
logging.info(f"User {short_address(wallet_address)}: {claim_msg}")
return response
def get_time_left(target_timestamp):
current_timestamp = int(datetime.now().timestamp())
return target_timestamp - current_timestamp
def get_time_spent(timestamp: int):
current_timestamp = int(datetime.now().timestamp())
return current_timestamp - timestamp
def convert_time_left(time_left):
"""
convert to human-readable time
:param time_left:
:return:
"""
hours = int(time_left / 3600)
minutes = int((time_left % 3600) / 60)
seconds = int(time_left % 60)
display_time_left = f"{hours} Hours {minutes} Mins" if hours > 0 else \
f"{minutes} Minutes {seconds} Secs" if minutes > 0 else \
f"{seconds} Seconds"
return display_time_left
def verify_user(wallet_address, referral_code):
try:
user_details = get_wallet_details(wallet_address)
# logging.info(f"User {short_address(wallet_address)}: User already registered!")
except Exception as e:
if 'user not found' in str(e):
logging.warning(
f"User {short_address(wallet_address)}: User NOT found. Registering user with ref code...")
response = verify_referral_code(referral_code)
ref_valid = response["data"]["valid"]
message = response["message"]
if ref_valid:
logging.info(f"User {short_address(wallet_address)}: {message}")
register_wallet(wallet_address, referral_code)
else:
raise Exception(f"User {short_address(wallet_address)}: {message}")
else:
raise e
def display_dashboard_logs(wallet_address):
user_details = get_wallet_details(wallet_address)
node_points = user_details['nodePoints']
# get current point based on time
status = get_node_status(wallet_address)
start_timestamp = status['startTimestamp']
if start_timestamp:
time_spent_secs = get_time_spent(start_timestamp)
# current node points are calculated as such
node_points = node_points + int(time_spent_secs / 3.6)
daily_streak = user_details['dailyStreak']
referralCode = user_details['referralCode']
total_points_ref = user_details['totalPoints']
confirmedReferralPoints = user_details['confirmedReferralPoints']
confirmed_referrals = int(confirmedReferralPoints / 500)
pending_referrals_points = total_points_ref - confirmedReferralPoints
pending_ref = int(pending_referrals_points/ 500)
logging.info(
f"User {short_address(wallet_address)} | Node points: {node_points} | Streak: {daily_streak}/7 | Ref Code: {referralCode} | "
f"Refs: {confirmed_referrals} | Earnings: {confirmedReferralPoints} | Pending - Refs: {pending_ref}, Points: {pending_referrals_points}"
)
def verify_referral_code(referral_code: str):
url = "https://referralapi.layeredge.io/api/referral/verify-referral-code"
payload = {"invite_code": referral_code}
response = get_response(url, payload)
return response
async def run_node(private_key, referral_code):
# proxy check
try:
wallet_address = Account.from_key(private_key).address
ip, protected = get_ip()
if protected:
logging.info(f"User {short_address(wallet_address)}: IP address protected: {ip}")
else:
logging.warning(f"User {short_address(wallet_address)}: No IP Protection. User could be marked as bot {ip}")
except Exception as e:
logging.error(f"User {short_address(wallet_address)}: Error during IP Check {e}")
while True:
account = Account.from_key(private_key)
wallet_address = str(account.address)
try:
# check if the wallet account has been registered
verify_user(wallet_address, referral_code)
# grab user details and display points
display_dashboard_logs(wallet_address)
# check if the light node has started running
status = get_node_status(wallet_address)
start_timestamp = status['startTimestamp']
if start_timestamp:
logging.info(f"User {short_address(wallet_address)}: Node already running")
else:
logging.info(f"User {short_address(wallet_address)}: Starting node...")
start_node(private_key)
logging.info(f"User {short_address(wallet_address)}: Node running...")
logging.info(f"User {short_address(wallet_address)}: Waiting 1hr till next ping...")
await asyncio.sleep(60 * 60 * 1)
except Exception as e:
logging.error(f"User {short_address(wallet_address)}: An Error occurred!\n{e}")
await asyncio.sleep(10)
async def claim_daily_node_points(private_key):
# claim streak node points
while True:
try:
wallet_address = Account.from_key(private_key).address
logging.info(f"User {short_address(wallet_address)}: Claiming node points...")
claim_node_points(private_key)
await asyncio.sleep(60 * 60 * 6)
except Exception as e:
wallet_address = Account.from_key(private_key).address
if 'can not claim node points twice in 24 hours, come back after 24 hours!' in str(e):
logging.warning(f"User {short_address(wallet_address)}: You have already claimed today's reward!")
else:
logging.error(f"User {short_address(wallet_address)}: {e}")
await asyncio.sleep(60 * 60 * 6)
async def main(private_keys: list, referral_code):
# Create tasks for both functions for each private key
tasks = []
for private_key in private_keys:
tasks.append(run_node(private_key, referral_code))
tasks.append(claim_daily_node_points(private_key))
# Run all tasks concurrently
await asyncio.gather(*tasks)