-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdip.py
346 lines (295 loc) · 13.1 KB
/
dip.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""Webdriver tests for DUAL DIP deposit"""
import logging
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import TimeoutException, ElementClickInterceptedException, ElementNotInteractableException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
SOL_TRADE_SIZE = .05
HEADLESS = True
def init_wallet(driver, phrase, password):
''' Init wallet'''
# add wallet to chrome
logging.info("Initializing wallet")
time.sleep(1)
logging.info("Switching to window")
driver.switch_to.window(driver.window_handles[1])
try:
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Use Secret Recovery Phrase')]")))
except TimeoutException:
driver.switch_to.window(driver.window_handles[0])
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Use Secret Recovery Phrase')]")))
time.sleep(1)
logging.info("Clicking on secret recovery phrase")
driver.find_element(
By.XPATH, "//button[contains(text(),'Use Secret Recovery Phrase')]").click()
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//textarea[@placeholder='Secret phrase']")))
logging.info("Typing secret recovery phrase")
driver.find_element(
By.XPATH, "//textarea[@placeholder='Secret phrase']").send_keys(phrase)
logging.info("Clicking submit")
driver.find_element(
By.XPATH, "//button[@class='sc-bdfBQB bzlPNH']").click()
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//input[@placeholder='Password']")))
logging.info("Typing password")
driver.find_element(
By.XPATH, "//input[@placeholder='Password']").send_keys(password)
logging.info("Confirming password")
driver.find_element(
By.XPATH, "//input[@placeholder='Confirm Password']").send_keys(password)
logging.info("Clicking checkbox")
driver.find_element(
By.XPATH, "//input[@type='checkbox']").click()
logging.info("Clicking submit")
driver.find_element(
By.XPATH, "//button[@type='submit']").click()
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Continue')]")))
continue_ = driver.find_element(
By.XPATH, "//button[contains(text(),'Continue')]")
logging.info("Clicking continue")
driver.execute_script("arguments[0].click();", continue_)
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Finish')]")))
logging.info("Clicking finish")
finish = driver.find_element(
By.XPATH, "//button[contains(text(),'Finish')]")
driver.execute_script("arguments[0].click();", finish)
main_window = driver.window_handles[0]
logging.info("Switching back to main window")
driver.switch_to.window(main_window)
return main_window
def select_wallet(driver, main_window):
''' Init wallet'''
# Sleep for the modal transitions so the elements are clickable
time.sleep(2)
logging.info("Selecting wallet")
# Accept the disclaimer
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Accept')]")))
accept_disclaimer = driver.find_element(
By.XPATH, "//button[contains(text(), 'Accept')]")
logging.info("Clicking disclaimer")
accept_disclaimer.click()
time.sleep(5)
# Skip the tutorial
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Skip')]")))
skip = driver.find_element(
By.XPATH, "//button[contains(text(), 'Skip')]")
logging.info("Clicking skip tutorial")
skip.click()
time.sleep(2)
# Select Wallet
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Select Wallet')]")))
select_wallet_button = driver.find_element(
By.XPATH, "//button[contains(text(), 'Select Wallet')]")
logging.info("Selecting wallet")
select_wallet_button.click()
# Choose Phantom
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(), 'Phantom')]")))
phantom = driver.find_element(
By.XPATH, "//button[contains(text(), 'Phantom')]")
logging.info("Clicking phantom")
time.sleep(2)
phantom.click()
original_window = driver.current_window_handle
WebDriverWait(driver, 60).until(EC.number_of_windows_to_be(2))
for window_handle in driver.window_handles:
if window_handle != original_window:
logging.info("Switching to window")
driver.switch_to.window(window_handle)
break
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Connect')]")))
popup_connect = driver.find_element(
By.XPATH, "//button[contains(text(),'Connect')]")
logging.info("Clicking connect")
popup_connect.click()
time.sleep(1)
logging.info("Switching back to main window")
driver.switch_to.window(main_window)
def deposit(values):
''' Deposit into a DIP '''
def select_dip(token="BTC"):
logging.info("Clicking Type Sort")
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//th/div/span[contains(text(), 'Type')]")))
time.sleep(1)
sort = driver.find_elements(
By.XPATH, "//th/div/span[contains(text(), 'Type')]")[0]
sort.click()
if token:
logging.info("Selecting DIP for %s", token)
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, f"//div[contains(text(), '{token}')]")))
stake = driver.find_element(
By.XPATH, f"//div[contains(text(), '{token}')]")
stake.click()
logging.info("Clicking Stake")
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[div[contains(text(), 'Stake')]]")))
# Wait for the wallet to connect
time.sleep(20)
stake = driver.find_elements(
By.XPATH, "//button[div[contains(text(), 'Stake')]]")[0]
stake.click()
logging.info("Waiting for modal load")
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//div/input")))
num_tokens = driver.find_element(
By.XPATH, "//div/input")
logging.info("Waiting")
time.sleep(1)
logging.info("Clicking on num tokens")
num_tokens.click()
logging.info("Typing num tokens")
if token == 'BTC':
num_tokens.send_keys('.000005')
else:
num_tokens.send_keys(str(SOL_TRADE_SIZE))
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//span[@class=\"ant-checkbox\"]")))
disclaimer = driver.find_element(
By.XPATH, "//span[@class=\"ant-checkbox\"]")
logging.info("Clicking checkbox")
time.sleep(1)
disclaimer.click()
WebDriverWait(driver, 60).until(EC.presence_of_element_located((
By.XPATH, "//div/button/div[contains(text(), 'Stake ')]"
)))
time.sleep(1)
stake = driver.find_element(
By.XPATH, "//div/button/div[contains(text(), 'Stake ')]"
)
logging.info("Clicking stake")
stake.click()
logging.info("Done clicking stake")
time.sleep(1)
original_window = driver.current_window_handle
WebDriverWait(driver, 60).until(EC.number_of_windows_to_be(2))
for window_handle in driver.window_handles:
if window_handle != original_window:
logging.info('Switching to approval window')
driver.switch_to.window(window_handle)
break
time.sleep(1)
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Approve')]")))
approve = driver.find_element(
By.XPATH, "//button[contains(text(),'Approve')]")
logging.info("Clicking approve")
approve.click()
logging.info("Done clicking approve")
time.sleep(1)
driver.switch_to.window(main_window)
logging.info("Switched back to main window")
time.sleep(1)
logging.info("Waiting for success toast")
# Wait for the success toast
WebDriverWait(driver, 120).until(EC.presence_of_element_located(
(By.XPATH, "//div[contains(text(),'Success')]")))
logging.info("Got the success toast")
options = webdriver.ChromeOptions()
options.add_extension("Phantom.crx")
options.add_argument("--disable-gpu")
if HEADLESS:
options.add_argument("--headless=new")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
service = Service()
driver = webdriver.Chrome(service=service, options=options)
logging.info("Successfully found chrome driver")
driver.get(values[0])
driver.maximize_window()
try:
logging.info('Initializing wallet')
main_window = init_wallet(driver, values[1], values[2])
logging.info('Selecting wallet')
select_wallet(driver, main_window)
logging.info('Selecting DIP')
token = values[3] if len(values) >= 4 else None
select_dip(token)
time.sleep(100)
driver.close()
except (TimeoutException, ElementClickInterceptedException, ElementNotInteractableException) as error:
logging.info('Error. Saving screenshot')
driver.save_screenshot('screenshot.png')
with open("failure.html", "w", encoding="utf-8") as source_file:
source_file.write(driver.page_source)
print(repr(error))
raise error
def withdraw(values):
''' Withdraw from the first vault '''
def do_withdraw():
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[div[contains(text(), 'WITHDRAW')]]")))
withdraw_button = driver.find_element(
By.XPATH, "//button[div[contains(text(), 'WITHDRAW')]]")
logging.info("Clicking Withdraw")
withdraw_button.click()
time.sleep(1)
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[div[contains(text(), 'Withdraw')]]")))
withdraw_modal_button = driver.find_element(
By.XPATH, "//button[div[contains(text(), 'Withdraw')]]")
logging.info("Clicking Withdraw modal")
withdraw_modal_button.click()
original_window = driver.current_window_handle
WebDriverWait(driver, 60).until(EC.number_of_windows_to_be(2))
for window_handle in driver.window_handles:
if window_handle != original_window:
logging.info('Switching to approval window')
driver.switch_to.window(window_handle)
break
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//button[contains(text(),'Approve')]")))
approve = driver.find_element(
By.XPATH, "//button[contains(text(),'Approve')]")
logging.info("Clicking approve")
approve.click()
driver.switch_to.window(main_window)
# Wait for the success toast
WebDriverWait(driver, 60).until(EC.presence_of_element_located(
(By.XPATH, "//span[contains(text(),'Success')]")))
logging.info("Got the success toast")
options = webdriver.ChromeOptions()
options.add_extension("Phantom.crx")
options.add_argument("--disable-gpu")
if HEADLESS:
options.add_argument("--headless=new")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
service = Service()
driver = webdriver.Chrome(service=service, options=options)
logging.info("Successfully found chrome driver")
driver.get(values[0])
driver.maximize_window()
try:
logging.info('Initializing wallet')
main_window = init_wallet(driver, values[1], values[2])
logging.info('Selecting wallet')
select_wallet(driver, main_window)
driver.get(values[0] + '/balance')
logging.info('Doing withdraw')
do_withdraw()
driver.close()
except (TimeoutException, ElementClickInterceptedException, ElementNotInteractableException) as error:
logging.info('Error. Saving screenshot')
driver.save_screenshot('screenshot.png')
with open("failure.html", "w", encoding="utf-8") as source_file:
source_file.write(driver.page_source)
print(repr(error))
raise error