From e1df85cdd4f8085fb4e270b6ae0c442c1fa0cfb1 Mon Sep 17 00:00:00 2001 From: kinglee Date: Mon, 3 Jun 2024 19:07:37 +0800 Subject: [PATCH] fix the block_number match the time --- adapters/satori/src/index.ts | 50 +++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/adapters/satori/src/index.ts b/adapters/satori/src/index.ts index 2173f0a8..549b5c79 100644 --- a/adapters/satori/src/index.ts +++ b/adapters/satori/src/index.ts @@ -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; }