forked from delta-hq/l2-lxp-liquidity-reward
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request delta-hq#245 from kyle-layerzero/fix/stargate-stream
stargate : bridge : use readable stream to avoid stack overflow
- Loading branch information
Showing
3 changed files
with
78 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
number,timestamp | ||
5154879,1717513197 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,60 @@ | ||
import fs from "fs"; | ||
import { write } from "fast-csv"; | ||
import csv from "csv-parser"; | ||
import path from "path"; | ||
|
||
import { BlockData, OutputSchemaRow } from "./sdk/types"; | ||
import { getTimestampAtBlock, getUserBalancesAtBlock } from "./sdk/lib"; | ||
|
||
const getData = async () => { | ||
const blocks = [3676829]; | ||
const csvRows: OutputSchemaRow[] = []; | ||
|
||
for (const block of blocks) { | ||
const timestamp = await getTimestampAtBlock(block); | ||
|
||
const userBalances = await getUserTVLByBlock({ | ||
blockNumber: block, | ||
blockTimestamp: timestamp, | ||
}); | ||
|
||
csvRows.push(...userBalances); | ||
} | ||
|
||
const ws = fs.createWriteStream("outputData.csv"); | ||
write(csvRows, { headers: true }) | ||
.pipe(ws) | ||
.on("finish", () => { | ||
console.log("CSV file has been written."); | ||
}); | ||
}; | ||
|
||
const WHITELISTED_TOKEN_ADDRESS = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' | ||
|
||
export const getUserTVLByBlock = async ({ | ||
blockNumber, | ||
blockTimestamp, | ||
}: BlockData): Promise<OutputSchemaRow[]> => { | ||
const positions = await getUserBalancesAtBlock(blockNumber); | ||
|
||
return positions.map((position) => ({ | ||
block_number: blockNumber, | ||
timestamp: blockTimestamp, | ||
user_address: position.user, | ||
token_address: WHITELISTED_TOKEN_ADDRESS, | ||
token_balance: BigInt(position.balance), | ||
token_symbol: "", | ||
usd_price: 0, | ||
})); | ||
}; | ||
|
||
// getData().then(() => { | ||
// console.log("Done"); | ||
// }); | ||
import { BlockData } from "./sdk/types"; | ||
import { PositionsStream } from "./sdk/lib"; | ||
|
||
const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => { | ||
const blocks: BlockData[] = []; | ||
//console.log(`Reading: ${filePath}`); | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
fs.createReadStream(filePath) | ||
.pipe(csv({ separator: "," })) // Specify the separator as '\t' for TSV files | ||
.on("data", (row) => { | ||
//console.log(row); | ||
const blockNumber = parseInt(row.number, 10); | ||
const blockTimestamp = parseInt(row.timestamp, 10); | ||
//console.log(`Maybe Data ${blockNumber} ${blockTimestamp}`); | ||
if (!isNaN(blockNumber) && blockTimestamp) { | ||
//console.log(`Valid Data`); | ||
blocks.push({ blockNumber: blockNumber, blockTimestamp }); | ||
} | ||
}) | ||
.on("end", () => { | ||
resolve(); | ||
}) | ||
.on("error", (err) => { | ||
reject(err); | ||
}); | ||
fs.createReadStream(filePath) | ||
.pipe(csv({ separator: "," })) // Specify the separator as '\t' for TSV files | ||
.on("data", (row) => { | ||
//console.log(row); | ||
const blockNumber = parseInt(row.number, 10); | ||
const blockTimestamp = parseInt(row.timestamp, 10); | ||
//console.log(`Maybe Data ${blockNumber} ${blockTimestamp}`); | ||
if (!isNaN(blockNumber) && blockTimestamp) { | ||
//console.log(`Valid Data`); | ||
blocks.push({ blockNumber: blockNumber, blockTimestamp }); | ||
} | ||
}) | ||
.on("end", () => { | ||
resolve(); | ||
}) | ||
.on("error", (err) => { | ||
reject(err); | ||
}); | ||
}); | ||
|
||
//console.log(`blocks: ${blocks.length}`); | ||
return blocks; | ||
}; | ||
|
||
readBlocksFromCSV(path.resolve(__dirname, "../hourly_blocks.csv")) | ||
.then(async (blocks) => { | ||
console.log(blocks); | ||
const allCsvRows: any[] = []; // Array to accumulate CSV rows for all blocks | ||
const csvWriteStream = fs.createWriteStream(`outputData.csv`, { | ||
flags: "w", | ||
}); | ||
|
||
csvWriteStream.write( | ||
"block_number,timestamp,user_address,token_address,token_balance,token_symbol,usd_price\n" | ||
); | ||
|
||
for (const block of blocks) { | ||
try { | ||
const result = await getUserTVLByBlock(block); | ||
for (const block of blocks) { | ||
try { | ||
const poisitionsStream = new PositionsStream(block); | ||
|
||
// Accumulate CSV rows for all blocks | ||
allCsvRows.push(...result); | ||
} catch (error) { | ||
console.error(`An error occurred for block ${block}:`, error); | ||
} | ||
poisitionsStream.pipe(csvWriteStream); | ||
} catch (error) { | ||
console.error( | ||
`An error occurred for block ${block.blockNumber}:`, | ||
error | ||
); | ||
} | ||
const ws = fs.createWriteStream(`outputData.csv`, { | ||
flags: "w", | ||
}); | ||
write(allCsvRows, {headers: true}) | ||
.pipe(ws).on("finish", () => { | ||
console.log(`CSV file has been written.`); | ||
}); | ||
} | ||
}) | ||
.catch((err) => { | ||
console.error("Error reading CSV file:", err); | ||
}); | ||
console.error("Error reading CSV file:", err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters