-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
124 lines (104 loc) · 3.72 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
import { CHAINS, PROTOCOLS, AMM_TYPES } from "./sdk/config";
import { getLPValueByUserAndPoolFromPositions, getPositionAtBlock, getPositionDetailsFromPosition, getPositionsForAddressByPoolAtBlock } from "./sdk/subgraphDetails";
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};
import csv from 'csv-parser';
import fs from 'fs';
import { write } from 'fast-csv';
import path from "path";
//Uncomment the following lines to test the getPositionAtBlock function
// const position = getPositionAtBlock(
// 0, // block number 0 for latest block
// 2, // position id
// CHAINS.MODE, // chain id
// PROTOCOLS.SUPSWAP, // protocol
// AMM_TYPES.UNISWAPV3 // amm type
// );
// position.then((position) => {
// // print response
// const result = getPositionDetailsFromPosition(position);
// console.log(`${JSON.stringify(result,null, 4)}
// `)
// });
interface LPValueDetails {
pool: string;
lpValue: string;
}
interface UserLPData {
totalLP: string;
pools: LPValueDetails[];
}
// Define an object type that can be indexed with string keys, where each key points to a UserLPData object
interface OutputData {
[key: string]: UserLPData;
}
interface CSVRow {
user: string;
pool: string;
block: number;
position: number;
lpvalue: string;
}
const readBlocksFromCSV = async (filePath: string): Promise<number[]> => {
return new Promise((resolve, reject) => {
const blocks: number[] = [];
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (row) => {
for (let key in row) {
const blockNumber = parseInt(row[key]);
if (!isNaN(blockNumber)) { // Ensure it's a valid number before pushing
blocks.push(blockNumber);
}
}
})
.on('end', () => {
console.log('CSV file successfully processed.');
resolve(blocks); // Resolve the promise with the blocks array
})
.on('error', (error) => {
reject(error); // Reject the promise if an error occurs
});
});
};
const getData = async () => {
const csvFilePath = path.resolve(__dirname, '../../../../data/mode_supswapv3_hourly_blocks.csv');
const snapshotBlocks = await readBlocksFromCSV(csvFilePath);
const csvRows: CSVRow[] = [];
for (let block of snapshotBlocks) {
const positions = await getPositionsForAddressByPoolAtBlock(
block, "", "", CHAINS.MODE, PROTOCOLS.SUPSWAP, AMM_TYPES.UNISWAPV3
);
console.log(`Block: ${block}`);
console.log("Positions: ", positions.length);
// Assuming this part of the logic remains the same
let positionsWithUSDValue = positions.map(getPositionDetailsFromPosition);
let lpValueByUsers = getLPValueByUserAndPoolFromPositions(positionsWithUSDValue);
lpValueByUsers.forEach((value, key) => {
let positionIndex = 0; // Define how you track position index
value.forEach((lpValue, poolKey) => {
const lpValueStr = lpValue.toString();
// Accumulate CSV row data
csvRows.push({
user: key,
pool: poolKey,
block,
position: positions.length, // Adjust if you have a specific way to identify positions
lpvalue: lpValueStr,
});
});
});
}
// Write the CSV output to a file
const outputPath = path.resolve(__dirname, '../../../../data/mode_supswapv3_tvl_snapshot.csv');
const ws = fs.createWriteStream(outputPath);
write(csvRows, { headers: true }).pipe(ws).on('finish', () => {
console.log("CSV file has been written.");
});
};
getData().then(() => {
console.log("Done");
});
// getPrice(new BigNumber('1579427897588720602142863095414958'), 6, 18); //Uniswap
// getPrice(new BigNumber('3968729022398277600000000'), 18, 6); //SupSwap