Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon committed May 16, 2024
2 parents d5c09b7 + 5b640a2 commit d138163
Show file tree
Hide file tree
Showing 12 changed files with 646 additions and 65 deletions.
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
4333348 1715043599
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"
}
}
100 changes: 100 additions & 0 deletions adapters/clip-finance/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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_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: 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: blockTimestamp,
user_address: snapshot.id,
token_address: snapshot.token,
token_balance: BigInt(snapshot.balance.toString()),
token_symbol: snapshot.tokenSymbol,
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()) // 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[]) => {
console.log(blocks);
const allCsvRows: any[] = [];

for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);
allCsvRows.push(...result);
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
}
}
await new Promise((resolve, reject) => {
const ws = fs.createWriteStream(`outputData.csv`, { flags: 'w' });
write(allCsvRows, { headers: true })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
resolve;
});
});

}).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",
};

0 comments on commit d138163

Please sign in to comment.