Skip to content

Commit

Permalink
Merge pull request #205 from miler012/main
Browse files Browse the repository at this point in the history
satori: fix all request
  • Loading branch information
0xroll authored Jun 4, 2024
2 parents 9d588d6 + dc4d533 commit f20f866
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
3 changes: 3 additions & 0 deletions adapters/satori/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
number,timestamp
4457308,1715394711
4457309,1715394712
76 changes: 41 additions & 35 deletions adapters/satori/src/index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -13,33 +13,39 @@ 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;
}
});
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[]) {
let snapshots: OutputDataSchemaRow[] = [];
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;
Expand All @@ -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;
Expand All @@ -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])
}

Expand All @@ -101,44 +107,44 @@ const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {

await new Promise<void>((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");
Expand Down
2 changes: 1 addition & 1 deletion adapters/satori/src/sdk/config.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit f20f866

Please sign in to comment.