diff --git a/adapters/clip-finance/hourly_blocks.csv b/adapters/clip-finance/hourly_blocks.csv index f64777e7..8145066b 100644 --- a/adapters/clip-finance/hourly_blocks.csv +++ b/adapters/clip-finance/hourly_blocks.csv @@ -1,2 +1,2 @@ -number,timestamp -4572185,1 \ No newline at end of file +number timestamp +4333348 1715043599 \ No newline at end of file diff --git a/adapters/clip-finance/src/index.ts b/adapters/clip-finance/src/index.ts index e3ab8070..dd5fb2b8 100644 --- a/adapters/clip-finance/src/index.ts +++ b/adapters/clip-finance/src/index.ts @@ -8,8 +8,8 @@ interface CSVRow { timestamp: number; user_address: string; token_address: string; - token_symbol: string; token_balance: bigint; + token_symbol: string; usd_price: number } @@ -27,16 +27,16 @@ export const getUserTVLByBlock = async (blocks: BlockData) => { for (const block of snapshotBlocks) { let snapshots = await getUserBalanceSnapshotAtBlock(block, ""); - const timestamp = await getTimestampAtBlock(block); + // const timestamp = await getTimestampAtBlock(block); for (const snapshot of snapshots) { const csvRow: CSVRow = { block_number: block, - timestamp: timestamp, + timestamp: blockTimestamp, user_address: snapshot.id, token_address: snapshot.token, - token_symbol: snapshot.tokenSymbol, token_balance: BigInt(snapshot.balance.toString()), + token_symbol: snapshot.tokenSymbol, usd_price: 0 }; csvRows.push(csvRow); @@ -52,60 +52,49 @@ const readBlocksFromCSV = async (filePath: string): Promise => { const blocks: BlockData[] = []; await new Promise((resolve, reject) => { - fs.createReadStream(filePath) - .pipe(csv({ separator: ',' })) // Specify the separator as ',' for csv files + 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) { + + const blockNumber = parseInt(row.number, 10); + const blockTimestamp = parseInt(row.timestamp, 10); + if (!isNaN(blockNumber) && blockTimestamp) { blocks.push({ blockNumber: blockNumber, blockTimestamp }); - } + } }) .on('end', () => { - resolve(); + resolve(); }) .on('error', (err) => { - reject(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) { +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); - - // 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; - } + const result = await getUserTVLByBlock(block); + allCsvRows.push(...result); } catch (error) { - console.error(`An error occurred for block ${block}:`, error); + console.error(`An error occurred for block ${block}:`, error); } - - } - }) - .catch((err) => { - console.error('Error reading CSV file:', err); + } + 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); + }); \ No newline at end of file diff --git a/adapters/clip-finance/src/sdk/subgraphDetails.ts b/adapters/clip-finance/src/sdk/subgraphDetails.ts index ac402529..a4a0975e 100644 --- a/adapters/clip-finance/src/sdk/subgraphDetails.ts +++ b/adapters/clip-finance/src/sdk/subgraphDetails.ts @@ -113,8 +113,7 @@ export const getUserBalanceSnapshotAtBlock = async ( const strategyRouterSharesMap = new Map(); let strategyRouterBalance = new Map(); const addBalance = (balance: UserBalanceSnapshot, share: UserBalanceSnapshot) => { - const contract = "0x".concat(share.id.substring(42)); - const user = share.id.substring(0, 42); + const user= share.id.substring(0, 42); const key = user.concat(balance.token); if (user == "0xa663f143055254a503467ff8b18aa9e70b9455b6") { strategyRouterBalance.set(key.concat(balance.token), balance); @@ -165,9 +164,9 @@ export const getUserBalanceSnapshotAtBlock = async ( }; if (sharePrice.price0.gt(0)) { userBalanceSnapshot = { - id: user, + id: user.toLowerCase(), balance: Big(Math.round(Big(snapshot.shares0).mul(sharePrice.price0).div(1e18).toNumber())), - token : sharePrice.token0, + token : sharePrice.token0.toLowerCase(), tokenSymbol: sharePrice.token0Symbol, } @@ -175,9 +174,9 @@ export const getUserBalanceSnapshotAtBlock = async ( } if (sharePrice.price01.gt(0)) { userBalanceSnapshot = { - id: user, + id: user.toLowerCase(), balance: Big(Math.round(Big(snapshot.shares0).mul(sharePrice.price01).div(1e18).toNumber())), - token : sharePrice.token1, + token : sharePrice.token1.toLowerCase(), tokenSymbol: sharePrice.token1Symbol, } @@ -185,18 +184,18 @@ export const getUserBalanceSnapshotAtBlock = async ( } if (sharePrice.price1.gt(0)) { userBalanceSnapshot = { - id: user, + id: user.toLowerCase(), balance: Big(Math.round(Big(snapshot.shares1).mul(sharePrice.price1).div(1e18).toNumber())), - token : sharePrice.token1, + token : sharePrice.token1.toLowerCase(), tokenSymbol: sharePrice.token1Symbol, } addBalance(userBalanceSnapshot, snapshot); } if (sharePrice.price10.gt(0)) { userBalanceSnapshot = { - id: user, + id: user.toLowerCase(), balance: Big(Math.round(Big(snapshot.shares1).mul(sharePrice.price10).div(1e18).toNumber())), - token : sharePrice.token0, + token : sharePrice.token0.toLowerCase(), tokenSymbol: sharePrice.token0Symbol, } @@ -240,15 +239,15 @@ export const getUserBalanceSnapshotAtBlock = async ( if (strategyRouterTotalShares.gt(0)) { let checkBalance = Big(0); strategyRouterSharesMap.forEach((share: UserSharesSnapshot, id: string)=> { - const user = "0x".concat(share.id.substring(0, 42)); + const user = share.id.substring(0, 42); for (const srbKey of strategyRouterBalance.keys()) { const balance = strategyRouterBalance.get(srbKey); if (balance) { countedTotalShares = countedTotalShares.plus(Big(share.shares0)); const userBalance : UserBalanceSnapshot = { - id: user, + id: user.toLowerCase(), balance: Big(Math.round(Big(share.shares0).mul(balance.balance).div(strategyRouterTotalShares).toNumber())), - token : balance.token, + token : balance.token.toLowerCase(), tokenSymbol: balance.tokenSymbol }