-
Notifications
You must be signed in to change notification settings - Fork 91
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
fix: update SyncSwap adapter #51 #59
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<number[]> => { | ||
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"); | ||
// }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @amedrontadora is the usage of arbitrum network intentional here. We need the liquidity on Linea. Please confirm if that will be received from this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the address provided by the graph service provider, which can obtain linea data. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@amedrontadora can you please change the timestamp field to number in the schema of returned object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, updated