Skip to content

Commit

Permalink
fix: add protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
xsteadybcgo committed Jun 23, 2024
1 parent 68fbd1f commit 16fc9ef
Show file tree
Hide file tree
Showing 75 changed files with 14,148 additions and 853 deletions.
2 changes: 2 additions & 0 deletions adapters/zklink/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
number,timestamp
3207030,1719141967
7 changes: 7 additions & 0 deletions adapters/zklink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
"author": "",
"license": "UNLICENSED",
"dependencies": {
"@real-wagmi/v3-sdk": "^1.3.8",
"big.js": "^6.2.1",
"csv-parser": "^3.0.0",
"decimal.js-light": "^2.5.1",
"ethers": "^6.12.0",
"fast-csv": "^5.0.1",
"iziswap-sdk": "^1.4.11",
"patch-package": "^8.0.0",
"tiny-invariant": "^1.3.3",
"toformat": "^2.0.0",
"web3": "^4.8.0"
},
"devDependencies": {
"@tsconfig/recommended": "^1.0.6",
"@types/big.js": "^6.2.2",
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
Expand Down
127 changes: 93 additions & 34 deletions adapters/zklink/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,100 @@
import { getUserBalanceSnapshotAtBlock } from "./sdk/lib";
import fs from 'fs';
import csv from 'csv-parser';
import { write } from 'fast-csv';

import { BlockData, OutputSchemaRow } from './sdk/types';
import {
getTimestampAtBlock,
getUserPositionsAtBlock,
} from './sdk/lib';


// const getData = async () => {
// const block = 1262060;
// const timestamp = await getTimestampAtBlock(block);
// const csvRows = await getUserTVLByBlock({
// blockNumber: block,
// blockTimestamp: timestamp,
// })

// // Write the CSV output to a file
// const ws = fs.createWriteStream('outputData.csv');
// write(csvRows, { headers: true })
// .pipe(ws)
// .on('finish', () => {
// console.log('CSV file has been written.');
// });
// };

// getData().then(() => {
// console.log('Done');
// });

export const getUserTVLByBlock = async ({
blockNumber,
blockTimestamp,
}: BlockData): Promise<OutputSchemaRow[]> => {
const res = await getUserPositionsAtBlock(blockNumber)
return res.map(item => ({ ...item, block_number: blockNumber, timestamp: blockTimestamp }))

export type OutputSchemaRow = {
block_number: number;
timestamp: number;
user_address: string;
token_address: string;
token_balance: bigint;
token_symbol?: string;
usd_price?: number;
};

interface BlockData {
blockNumber: number;
blockTimestamp: number;
}

export const getUserTVLByBlock = async (blocks: BlockData) => {
const { blockNumber, blockTimestamp } = blocks;
const snapshotBlocks: number[] = [blockNumber];

const csvRows: OutputSchemaRow[] = [];

for (const block of snapshotBlocks) {
let snapshots = await getUserBalanceSnapshotAtBlock(block);

for (const snapshot of snapshots) {
const csvRow: OutputSchemaRow = {
block_number: block,
timestamp: blockTimestamp,
user_address: snapshot.userAddress,
token_address: snapshot.tokenAddress,
token_balance: BigInt(snapshot.balance),
};
csvRows.push(csvRow);
}
}

console.log("Total rows:", csvRows.length);

return csvRows;
};

const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
const blocks: BlockData[] = [];

await new Promise<void>((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv()) // Specify the separator as '\t' for TSV files
.on('data', (row) => {

const blockNumber = parseInt(row.number, 10);
const blockTimestamp = parseInt(row.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[]) => {
const result = await getUserTVLByBlock(blocks[0]);
const groupByTokenAddress = (rows: OutputSchemaRow[]): { [key: string]: bigint } => {
return rows.reduce((acc, row) => {
if (!acc[row.token_address]) {
acc[row.token_address] = BigInt(0);
}
acc[row.token_address] += row.token_balance;
return acc;
}, {} as { [key: string]: bigint });
};

const groupedBalances = groupByTokenAddress(result);
console.log(groupedBalances);

await new Promise((resolve) => {
const ws = fs.createWriteStream(`outputData.csv`, { flags: 'w' });
write(result, { headers: true })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
resolve(true);
});
});

}).catch((err) => {
console.error('Error reading CSV file:', err);
});
Loading

0 comments on commit 16fc9ef

Please sign in to comment.