-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_bridge.py
119 lines (104 loc) · 3.48 KB
/
main_bridge.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
from typing import Awaitable
import random
import copy
from asyncio import (
create_task,
gather,
sleep,
get_event_loop,
Semaphore,
AbstractEventLoop,
)
from loguru import logger
from tqdm import tqdm
from config import *
from src.utils.mappings import bridge_handlers
from src.utils.helper import (
metamask_addresses,
metamask_private_keys,
argentx_addresses,
argentx_private_keys,
active_bridge_modules,
)
async def process_private_key(metamask_address: str,
metamask_private_key: str,
argentx_address: str,
argentx_private_key: str,
thread_num: int,
semaphore: Semaphore) -> bool:
tasks = []
if RANDOMIZE is True:
modules = copy.copy(active_bridge_modules)
random.shuffle(modules)
else:
modules = active_bridge_modules
async with semaphore:
for pattern in modules:
task = create_task(bridge_handlers[pattern](
metamask_address,
metamask_private_key,
argentx_address,
argentx_private_key,
))
tasks.append(task)
time_to_sleep = random.randint(MIN_PAUSE, MAX_PAUSE)
logger.info(f'Thread {thread_num}: Sleeping {time_to_sleep} seconds...')
await sleep(time_to_sleep)
await task
await gather(*tasks)
return all(task.done() for task in tasks)
async def main(loop: AbstractEventLoop) -> None:
if len(metamask_addresses) != len(argentx_addresses):
print(len(metamask_addresses), len(argentx_addresses))
logger.error('Number of metamask wallets does not match number of argentx wallets')
return
num_threads = min(NUM_THREADS, len(argentx_addresses))
semaphore = Semaphore(num_threads)
if not RUN_FOREVER:
tasks = []
thread_num = 1
for metamask_addr, metamask_key, argentx_addr, argentx_key in zip(
metamask_addresses,
metamask_private_keys,
argentx_addresses,
argentx_private_keys,
):
task = loop.create_task(process_private_key(
metamask_addr,
metamask_key,
argentx_addr,
argentx_key,
thread_num,
semaphore,
))
tasks.append(task)
thread_num += 1
await gather(*tasks)
while RUN_FOREVER:
tasks = []
thread_num = 1
for metamask_addr, metamask_key, argentx_addr, argentx_key in zip(
metamask_addresses,
metamask_private_keys,
argentx_addresses,
argentx_private_keys,
):
task = loop.create_task(process_private_key(
metamask_addr,
metamask_key,
argentx_addr,
argentx_key,
thread_num,
semaphore,
))
tasks.append(task)
thread_num += 1
await gather(*tasks)
def start_event_loop(awaitable: Awaitable[object], loop: AbstractEventLoop) -> None:
loop.run_until_complete(awaitable)
if __name__ == '__main__':
with tqdm(total=len(argentx_addresses)) as pbar:
async def tracked_main():
await main(get_event_loop())
start_event_loop(tracked_main(), get_event_loop())
pbar.close()