-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
120 lines (93 loc) · 4.09 KB
/
app.js
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
const puppeteer = require('puppeteer');
(async () => {
// Launch headful browser and go to swap.defillama.com
const browser = await puppeteer.launch(
{
headless: false,
defaultViewport: false,
});
const page = await browser.newPage();
await page.goto('https://swap.defillama.com');
const setChain = async () => {
try {
await page.waitForSelector('#react-select-2-input');
await page.type('#react-select-2-input', 'Arbitrum');
await page.keyboard.press('Enter');
} catch (error) {
console.error('Error occurred during setChain:', error.message);
}
};
const setYouSell = async () => {
try {
await page.focus('.css-lv0ed5');
// Press Ctrl+A to select the existing value
await page.keyboard.down('Control');
await page.keyboard.press('A');
await page.keyboard.up('Control');
// Press Backspace to delete the existing value
await page.keyboard.press('Backspace');
// Type the new value
await page.keyboard.type('12');
} catch (error) {
console.error('Error occurred during setYouSell:', error.message);
}
};
const selectSellToken = async () => {
try {
// There are two buttons of same class and here we have to click on first one.
const buttons = await page.$$('.css-qjhap')
const btn = buttons[0];
await btn.click();
// Then we will type token name in searchBox ,that we want to select.
await page.waitForSelector('div:nth-child(2) > input');
await page.type('div:nth-child(2) > input', 'WBTC');
//Then we will wait for result of tokens that we were typed.
await page.waitForTimeout(1000);
//Then we will click on the first result.
await page.waitForSelector('div.List > div > div:nth-child(1) > div');
await page.click('div.List > div > div:nth-child(1) > div');
} catch (error) {
console.error('Error occurred during selectSellToken:', error.message);
}
};
const selectBuyToken = async () => {
try {
// There are two buttons of same class and here we have to click on second one.
const buttons = await page.$$('.css-qjhap')
const btn = buttons[1];
await btn.click();
// Then we will type token name in searchBox ,that we want to select.
await page.waitForSelector('div:nth-child(2) > input');
await page.type('div:nth-child(2) > input', 'USD Coin');
//Then we will wait for result of tokens that we were typed.
await page.waitForTimeout(1000);
//Then we will click on the first result.
await page.waitForSelector('div.List > div > div:nth-child(2) > div');
await page.click('div.List > div > div:nth-child(2) > div');
//Then we will wait for the available routes.
await page.waitForTimeout(4000);
} catch (error) {
console.error('Error occurred during selectBuyToken:', error.message);
}
};
const selectRouteToSwap = async () => {
try {
//There are more routes of same class but we have to click on the second one.
const elementSelector = '.css-199sga5';
await page.waitForSelector(elementSelector, { visible: true });
const elements = await page.$$(elementSelector);
await elements[1].click();
} catch (error) {
console.error('Error occurred during selectRouteToSwap:', error.message);
}
};
await setChain();
await setYouSell();
await selectSellToken();
await selectBuyToken();
//Routes are changing their position dynamically but we have to select second route.so we will call selectRouteToSwap function every 0.2 second.
setInterval(async () => {
await selectRouteToSwap();
}, 200);
// Keep the browser window open
})();