diff --git a/adapters/satori/hourly_blocks.csv b/adapters/satori/hourly_blocks.csv new file mode 100644 index 00000000..be798269 --- /dev/null +++ b/adapters/satori/hourly_blocks.csv @@ -0,0 +1,3 @@ +number,timestamp +4457308,1715394711 +4457309,1715394712 \ No newline at end of file diff --git a/adapters/satori/src/index.ts b/adapters/satori/src/index.ts index 549b5c79..95959760 100644 --- a/adapters/satori/src/index.ts +++ b/adapters/satori/src/index.ts @@ -1,6 +1,6 @@ import fs from 'fs'; -import { write } from 'fast-csv'; -import { OutputDataSchemaRow,queryUserTVLByBlock } from './sdk/subgraphDetails'; +import {write} from 'fast-csv'; +import {OutputDataSchemaRow, queryUserTVLByBlock} from './sdk/subgraphDetails'; import csv from 'csv-parser'; import * as swapindex from './sdk/swap/swapindex' @@ -13,12 +13,12 @@ interface BlockData { async function getUserTvlFromPerpetual(blocks: BlockData[]) { let snapshots: OutputDataSchemaRow[] = []; let groupedSnapshots: { [user_address: string]: OutputDataSchemaRow } = {}; - for (const { blockNumber, blockTimestamp } of blocks) { - try { - snapshots = snapshots.concat(await queryUserTVLByBlock(blockNumber,blockTimestamp)) + for (const {blockNumber, blockTimestamp} of blocks) { + try { + snapshots = snapshots.concat(await queryUserTVLByBlock(blockNumber, blockTimestamp)) snapshots = Array.from(new Map(snapshots.map(obj => [obj.user_address + '|' + obj.block_number, obj])).values()); snapshots.forEach(obj => { - const key = obj.user_address; + const key = obj.user_address + obj.token_address; if (!groupedSnapshots[key] || obj.block_number > groupedSnapshots[key].block_number) { groupedSnapshots[key] = obj; } @@ -26,12 +26,15 @@ async function getUserTvlFromPerpetual(blocks: BlockData[]) { for (const key in groupedSnapshots) { groupedSnapshots[key].block_number = blockNumber; groupedSnapshots[key].timestamp = blockTimestamp; + if(groupedSnapshots[key].token_balance <= 0){ + delete groupedSnapshots[key]; + } } } catch (error) { console.error(`An error occurred for block ${blockNumber}:`, error); } - } - return groupedSnapshots; + } + return groupedSnapshots; } async function getUserTvlFromSwap(blocks: BlockData[]) { @@ -39,7 +42,10 @@ async function getUserTvlFromSwap(blocks: BlockData[]) { let groupedSnapshots: { [user_address: string]: OutputDataSchemaRow } = {}; for (const {blockNumber, blockTimestamp} of blocks) { try { - snapshots = snapshots.concat(await swapindex.getUserTVLByBlock({blockNumber:blockNumber, blockTimestamp:blockTimestamp})) + snapshots = snapshots.concat(await swapindex.getUserTVLByBlock({ + blockNumber: blockNumber, + blockTimestamp: blockTimestamp + })) snapshots = Array.from(new Map(snapshots.map(obj => [obj.user_address + '|' + obj.block_number + '|' + obj.token_address, obj])).values()); snapshots.forEach(obj => { const key = obj.user_address + obj.token_address; @@ -60,14 +66,14 @@ async function getUserTvlFromSwap(blocks: BlockData[]) { return groupedSnapshots; } -async function mergeTvl(from: {[p: string]: OutputDataSchemaRow}, to: { [p: string]: OutputDataSchemaRow }) { +async function mergeTvl(from: { [p: string]: OutputDataSchemaRow }, to: { [p: string]: OutputDataSchemaRow }) { for (let s2UserAddr in from) { let toRow = to[s2UserAddr]; let fromRow = from[s2UserAddr]; if (toRow == null) { to[s2UserAddr] = fromRow; } else { - toRow.token_balance = toRow.token_balance + fromRow.token_balance; + toRow.token_balance = BigInt(toRow.token_balance) + BigInt(fromRow.token_balance); } } return to; @@ -91,8 +97,8 @@ export const queryAllByBloks = async (blocks: BlockData[]) => { }; // 4457308 -export const getUserTVLByBlock = async(blocks: BlockData) =>{ - const { blockNumber, blockTimestamp } = blocks +export const getUserTVLByBlock = async (blocks: BlockData) => { + const {blockNumber, blockTimestamp} = blocks return await queryAllByBloks([blocks]) } @@ -101,44 +107,44 @@ const readBlocksFromCSV = async (filePath: string): Promise => { await new Promise((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); - }); + .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); - let allCsvRows: any[] = []; + let allCsvRows: any[] = []; let csvRows: OutputDataSchemaRow[] = await queryAllByBloks(blocks); console.log(`length:---${csvRows.length}`); await new Promise((resolve, reject) => { - const ws = fs.createWriteStream(`outputData.csv`, { flags: 'w' }); - write(csvRows, { headers: true }) + const ws = fs.createWriteStream(`outputData.csv`, {flags: 'w'}); + write(csvRows, {headers: true}) .pipe(ws) .on("finish", () => { - console.log(`CSV file has been written.`); - resolve; + console.log(`CSV file has been written.`); + resolve; }); }); - }).catch((err) => { +}).catch((err) => { console.error('Error reading CSV file:', err); - }); +}); // main([{blockNumber:669512,blockTimestamp:1715394711}]).then(() => { // console.log("Done"); diff --git a/adapters/satori/src/sdk/config.ts b/adapters/satori/src/sdk/config.ts index b891e3c7..3d0c0fb3 100644 --- a/adapters/satori/src/sdk/config.ts +++ b/adapters/satori/src/sdk/config.ts @@ -1,5 +1,5 @@ export const SUBGRAPH_URL = "https://api.goldsky.com/api/private/project_clw1so6mrsn6o01uafow40xlo/subgraphs/satori-linea-perpet/1.0.0/gn" -export const ASSET = "0x176211869cA2b568f2A7D4EE941E073a821EE1ff" +export const ASSET = "0x176211869ca2b568f2a7d4ee941e073a821ee1ff" export const SYMBOL = "USDC" export const PRICE = 1 export const KEY = "Bearer clw1sqzpysmqg01x5h17y1jvq"