Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clip.finance #134

Merged
merged 7 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions adapters/clip-finance/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
number,timestamp
4572185,1
32 changes: 32 additions & 0 deletions adapters/clip-finance/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "clip-finance",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/big.js": "^6.2.2",
"big.js": "^6.2.1",
"bignumber.js": "^9.1.2",
"csv-parser": "^3.0.0",
"decimal.js-light": "^2.5.1",
"fast-csv": "^5.0.1",
"jsbi": "^4.3.0",
"tiny-invariant": "^1.3.1",
"toformat": "^2.0.0",
"viem": "^2.8.13"
},
"devDependencies": {
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
}
111 changes: 111 additions & 0 deletions adapters/clip-finance/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { getTimestampAtBlock, getUserBalanceSnapshotAtBlock } from "./sdk/subgraphDetails";
import fs from 'fs';
import csv from 'csv-parser';
import { write } from 'fast-csv';

interface CSVRow {
block_number: number;
timestamp: number;
user_address: string;
token_address: string;
token_symbol: string;
token_balance: bigint;
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: CSVRow[] = [];

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

const timestamp = await getTimestampAtBlock(block);

for (const snapshot of snapshots) {
const csvRow: CSVRow = {
block_number: block,
timestamp: timestamp,
user_address: snapshot.id,
token_address: snapshot.token,
token_symbol: snapshot.tokenSymbol,
token_balance: BigInt(snapshot.balance.toString()),
usd_price: 0
};
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({ separator: ',' })) // Specify the separator as ',' for csv 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) => {
const allCsvRows: any[] = []; // Array to accumulate CSV rows for all blocks
const batchSize = 10; // Size of batch to trigger writing to the file
let i = 0;

for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);

// Accumulate CSV rows for all blocks
allCsvRows.push(...result);

i++;
console.log(`Processed block ${i}`);

// Write to file when batch size is reached or at the end of loop
if (i % batchSize === 0 || i === blocks.length) {
const ws = fs.createWriteStream(`outputData.csv`, { flags: i === batchSize ? 'w' : 'a' });
write(allCsvRows, { headers: i === batchSize ? true : false })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
});

// Clear the accumulated CSV rows
allCsvRows.length = 0;
}
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
}

}
})
.catch((err) => {
console.error('Error reading CSV file:', err);
});
19 changes: 19 additions & 0 deletions adapters/clip-finance/src/sdk/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const enum CHAINS {
LINEA = 59144,
}

export const SUBGRAPH_URLS = {
[CHAINS.LINEA]:
"https://api.goldsky.com/api/public/project_cltzfe75l0y4u01s98n3c7fmu/subgraphs/clip-finance-shares-token/v2.4/gn",
};

export const RESERVE_SUBGRAPH_URLS = {
[CHAINS.LINEA]:
"https://api.goldsky.com/api/public/project_cltzfe75l0y4u01s98n3c7fmu/subgraphs/clip-finance-shares-token/v2.5/gn",

}

export const RPC_URLS = {
[CHAINS.LINEA]:
"https://rpc.linea.build",
};
Loading
Loading