Skip to content

Commit

Permalink
Merge pull request #201 from miler012/main
Browse files Browse the repository at this point in the history
satori:fix the block_number match the time
  • Loading branch information
0xroll authored Jun 3, 2024
2 parents 41ccaf4 + e1df85c commit 5476e6c
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions adapters/satori/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,51 @@ 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))
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;
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;
}
} catch (error) {
console.error(`An error occurred for block ${blockNumber}:`, error);
}
}
// Array.from(new Map(snapshots.map(obj => [obj.user_address + '|' + obj.block_number, obj])).values());
snapshots = Array.from(new Map(snapshots.map(obj => [obj.user_address + '|' + obj.block_number, obj])).values());
let groupedSnapshots: { [user_address: string]: OutputDataSchemaRow } = {};
snapshots.forEach(obj => {
const key = obj.user_address;
if (!groupedSnapshots[key] || obj.block_number > groupedSnapshots[key].block_number) {
groupedSnapshots[key] = obj;
}
});
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 = 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;
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;

}
} catch (error) {
console.error(`An error occurred for block ${blockNumber}:`, error);
}
}
// Array.from(new Map(snapshots.map(obj => [obj.user_address + '|' + obj.block_number, obj])).values());
snapshots = Array.from(new Map(snapshots.map(obj => [obj.user_address + '|' + obj.block_number + '|' + obj.token_address, obj])).values());
let groupedSnapshots: { [user_address: string]: OutputDataSchemaRow } = {};
snapshots.forEach(obj => {
const key = obj.user_address + obj.token_address;
if (!groupedSnapshots[key] || obj.block_number > groupedSnapshots[key].block_number) {
groupedSnapshots[key] = obj;
}
});

return groupedSnapshots;
}

Expand Down

0 comments on commit 5476e6c

Please sign in to comment.