From cdfcc35926ee5264bfcc2809d93d6943aa4f70ed Mon Sep 17 00:00:00 2001 From: Amedrontadora Date: Wed, 17 Apr 2024 22:38:44 +0800 Subject: [PATCH 1/2] fix: update SyncSwap adapter #51 --- adapters/syncswap/src/index.ts | 91 +++++++++++++---------------- adapters/syncswap/src/sdk/config.ts | 2 +- 2 files changed, 42 insertions(+), 51 deletions(-) diff --git a/adapters/syncswap/src/index.ts b/adapters/syncswap/src/index.ts index cc1fbdc7..fbdfea03 100644 --- a/adapters/syncswap/src/index.ts +++ b/adapters/syncswap/src/index.ts @@ -1,66 +1,57 @@ import { getPositionsForAddressByPoolAtBlock as getSyncSwapPositionsForAddressByPoolAtBlock} from "./sdk/positionSnapshots" -import { promisify } from 'util'; -import stream from 'stream'; -import csv from 'csv-parser'; import fs from 'fs'; import { write } from 'fast-csv'; -interface CSVRow { - block_number: number - timestamp: string - user_address: string - token_address: string - token_symbol: string - token_balance: string - usd_price: string -} -const pipeline = promisify(stream.pipeline); -// Assuming you have the following functions and constants already defined -// getPositionsForAddressByPoolAtBlock, CHAINS, PROTOCOLS, AMM_TYPES, getPositionDetailsFromPosition, getLPValueByUserAndPoolFromPositions, BigNumber +interface BlockData { + blockNumber: number; + blockTimestamp: number; +} -const readBlocksFromCSV = async (filePath: string): Promise => { - const blocks: number[] = []; - await pipeline( - fs.createReadStream(filePath), - csv(), - async function* (source) { - for await (const chunk of source) { - // Assuming each row in the CSV has a column 'block' with the block number - if (chunk.block) blocks.push(parseInt(chunk.block, 10)); +export const main = async (blocks: BlockData[]) => { + 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 { blockNumber, blockTimestamp } of blocks) { + try { + // Retrieve data using block number and timestamp + const csvRows = await getSyncSwapPositionsForAddressByPoolAtBlock(blockNumber) + + // Accumulate CSV rows for all blocks + allCsvRows.push(...csvRows); + + 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 ${blockNumber}:`, error); } - ); - return blocks; -}; - - -const getData = async () => { - const snapshotBlocks = [ - 296496,330000 - // Add more blocks as needed - ]; //await readBlocksFromCSV('src/sdk/mode_chain_daily_blocks.csv'); - - const csvRows: CSVRow[] = []; - - for (let block of snapshotBlocks) { - // SyncSwap Linea position snapshot - const rows = await getSyncSwapPositionsForAddressByPoolAtBlock(block) - rows.forEach((row) => csvRows.push(row as CSVRow)) } - - // 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 (blocks: BlockData) => { + const { blockNumber, blockTimestamp } = blocks + return await getSyncSwapPositionsForAddressByPoolAtBlock(blockNumber) +} + +// main().then(() => { +// console.log("Done"); +// }); diff --git a/adapters/syncswap/src/sdk/config.ts b/adapters/syncswap/src/sdk/config.ts index 3f43cc87..bf646530 100644 --- a/adapters/syncswap/src/sdk/config.ts +++ b/adapters/syncswap/src/sdk/config.ts @@ -3,5 +3,5 @@ export const enum CHAINS{ } export const SUBGRAPH_URLS = { - [CHAINS.LINEA]: "https://api.studio.thegraph.com/query/62864/syncswap-graph-linea/v1.4.1.4" + [CHAINS.LINEA]: "https://gateway-arbitrum.network.thegraph.com/api/ce0ba3625ebbbd3c4b5a2af394dc8e47/subgraphs/id/3xpZFx5YNWzqemwdtRhyaTXVidKNnjY19XAWoHtvR6Lh" } From 8948a57e2be0a70897a845487bd786b991a88a0e Mon Sep 17 00:00:00 2001 From: Amedrontadora Date: Tue, 23 Apr 2024 07:54:18 +0800 Subject: [PATCH 2/2] chore: SyncSwap change the timestamp field to number #51 --- adapters/syncswap/src/sdk/positionSnapshots.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adapters/syncswap/src/sdk/positionSnapshots.ts b/adapters/syncswap/src/sdk/positionSnapshots.ts index 7ef0fe9e..3b99f221 100644 --- a/adapters/syncswap/src/sdk/positionSnapshots.ts +++ b/adapters/syncswap/src/sdk/positionSnapshots.ts @@ -23,7 +23,7 @@ interface SubgraphResponse { interface UserPositionSnapshotsAtBlockData { block_number: number - timestamp: string + timestamp: number user_address: string token_address: string token_symbol: string @@ -102,7 +102,7 @@ export const getPositionsForAddressByPoolAtBlock = async ( } userPositionSnapshotsAtBlockData.push({ user_address: positionSnapshot.account, - timestamp: new Date(positionSnapshot.timestamp * 1000).toISOString(), + timestamp: Math.floor(positionSnapshot.timestamp), token_address: positionSnapshot.pair.id, block_number: snapshotBlockNumber, token_symbol: `${positionSnapshot.pair.token0.symbol}/${positionSnapshot.pair.token1.symbol} cSLP`,