-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
43 lines (35 loc) · 1.27 KB
/
index.ts
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
import fs from "fs";
import { write } from "fast-csv";
import { getUserTVLByBlock, readBlocksFromCSV } from "./sdk";
import path from "path";
const filePath = path.join(process.cwd(), "hourly_blocks.csv");
async function processBlocks() {
try {
const blocks = await readBlocksFromCSV(filePath);
console.log(`Read ${blocks.length} blocks from CSV.`);
const allCsvRows: any[] = [];
for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);
allCsvRows.push(...result); // Assuming result is an array of objects
} catch (error) {
console.error(`An error occurred for block ${block.blockNumber}:`, error);
}
}
const ws = fs.createWriteStream(path.join(process.cwd(), "outputData.csv"), { flags: "w" });
write(allCsvRows, { headers: true }).pipe(ws);
await new Promise((resolve, reject) => {
ws.on("finish", () => {
console.log(`CSV file has been written.`);
});
ws.on("error", (err) => {
console.error("Error writing CSV file:", err);
reject(err); // Reject the promise if there's an error
});
});
} catch (err) {
console.error("Error reading or processing CSV file:", err);
}
}
// Invoke the main processing function
processBlocks();