-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjup-multiply.js
More file actions
72 lines (57 loc) · 2.07 KB
/
Copy pathjup-multiply.js
File metadata and controls
72 lines (57 loc) · 2.07 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
const fs = require("node:fs");
const puppeteer = require("puppeteer");
const AMOUNT_MULTIPLIERS = { "": 1, K: 1e3, M: 1e6, B: 1e9 };
function parseAmount(value) {
const match = value.match(/\$([\d,.]+)([KMB])?/);
if (!match) {
throw new Error(`Unable to parse amount from: ${value}`);
}
const [, amount, suffix = ""] = match;
return parseFloat(amount.replaceAll(",", "")) * AMOUNT_MULTIPLIERS[suffix];
}
function parsePercentage(value) {
const match = value.match(/[+-]?([\d,.]+)%/);
if (!match) {
throw new Error(`Unable to parse percentage from: ${value}`);
}
return parseFloat(match[0].replaceAll(",", "").replace("%", ""));
}
async function getEthenaMultiply(page) {
await page.goto("https://jup.ag/lend/ethena/market", { waitUntil: "domcontentloaded" });
const element = await page.waitForSelector(
`xpath/(//h2[normalize-space()='Multiply']/following::table[1]//tr[contains(., 'USDe') and contains(., 'USDG')])[1]`,
);
const cells = await page.evaluate(
(el) => Array.from(el.querySelectorAll("td")).map((cell) => cell.textContent.trim()),
element,
);
if (cells.length < 4) {
throw new Error(`Unable to parse Bitwise × Ethena Multiply data from: ${cells.join(" | ")}`);
}
return { rate: parsePercentage(cells[3]), tvl: parseAmount(cells[0]) };
}
(async () => {
const timestamp = new Date(process.env.SCRAPE_TIMESTAMP ?? Date.now());
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
try {
const ethenaPage = await browser.newPage();
await ethenaPage.setViewport({ width: 1920, height: 1080 });
const ethena = await getEthenaMultiply(ethenaPage);
const results = [
{
id: "jup-ethena-usde-multiply",
timestamp: timestamp.toISOString(),
protocol: "jup",
name: "USDe (Bitwise × Ethena Multiply)",
rate: ethena.rate,
tvl: ethena.tvl,
},
];
for (const result of results) {
console.log(result);
fs.writeFileSync(`${result.id}.json`, JSON.stringify(result) + "\n");
}
} finally {
await browser.close();
}
})();