-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.ts
143 lines (125 loc) · 3.95 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
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
import csv from 'csv-parser';
import { write } from 'fast-csv';
import fs from 'fs';
import { AMM_TYPES, CHAINS, PROTOCOLS } from "./sdk/config";
import {
getLPValueByUserAndPoolFromPositions,
getPositionDetailsFromPosition,
getPositionsForAddressByPoolAtBlock,
getTokenPriceFromPositions,
} from "./sdk/subgraphDetails";
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};
interface BlockData {
blockNumber: number;
blockTimestamp: number;
}
interface OutputDataSchemaRow {
block_number: number;
timestamp: number;
user_address: string;
token_address: string;
token_balance: bigint;
token_symbol: string;
usd_price: number;
}
export const getUserTVLByBlock = async (blocks: BlockData) => {
const { blockNumber, blockTimestamp } = blocks
const positions = await getPositionsForAddressByPoolAtBlock(
blockNumber,
"",
"",
CHAINS.LINEA,
PROTOCOLS.DODOEX,
AMM_TYPES.DODO
);
console.log(`Block: ${blockNumber}`);
console.log("Positions: ", positions.length);
// Assuming this part of the logic remains the same
let positionsWithUSDValue = [];
// Add price to token
await getTokenPriceFromPositions(positions, "linea");
for (let position of positions) {
const res = await getPositionDetailsFromPosition(position);
positionsWithUSDValue.push(res);
}
let lpValueByUsers = await getLPValueByUserAndPoolFromPositions(
positionsWithUSDValue
);
const csvRows: OutputDataSchemaRow[] = [];
lpValueByUsers.forEach((value, owner) => {
value.forEach((tokenBalance, tokenAddress) => {
csvRows.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: owner,
token_address: tokenAddress,
token_symbol: tokenBalance.tokenSymbol,
token_balance: tokenBalance.tokenBalance,
usd_price: tokenBalance.usdPrice,
});
});
});
return csvRows
}
const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
const blocks: BlockData[] = [];
await new Promise<void>((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv({ separator: '\t' })) // Specify the separator as '\t' for TSV files
.on('data', (row) => {
const blockNumber = parseInt(row.number, 10);
const blockTimestamp = parseInt(row.block_timestamp, 10);
if (!isNaN(blockNumber) && blockTimestamp) {
blocks.push({ blockNumber: blockNumber, blockTimestamp });
}
})
.on('end', () => {
resolve();
})
.on('error', (err) => {
reject(err);
});
});
return blocks;
};
readBlocksFromCSV('hourly_blocks.csv').then(async (blocks: any[]) => {
console.log(blocks);
const allCsvRows: any[] = [];
const pageSize = 10; // Size of batch to trigger writing to the file
// Write the CSV output to a file
const writeCsv = function (data: any[], first: boolean): Promise<void> {
return new Promise<void>((resolve, reject) => {
const ws = fs.createWriteStream(`outputData.csv`, { flags: first ? 'w' : 'a' });
write(data, { headers: first ? true : false })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
resolve();
})
.on('error', (err) => {
reject(err);
});
// Clear the accumulated CSV rows
allCsvRows.length = 0;
});
}
for (let i = 0; i < blocks.length; i++) {
try {
const result = await getUserTVLByBlock(blocks[i]);
// Accumulate CSV rows for all blocks
allCsvRows.push(...result);
console.log(`Processed block ${i}`);
// Write to file when batch size is reached or at the end of loop
if (i % pageSize === 0 || i === blocks.length - 1) {
await writeCsv(allCsvRows, i === pageSize);
}
} catch (error) {
console.error(`An error occurred for block ${blocks[i]}:`, error);
}
}
})
.catch((err) => {
console.error('Error reading CSV file:', err);
});