Skip to content

Commit

Permalink
filter times before reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
darthgus committed Dec 11, 2024
1 parent 058c432 commit f6a080a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions dexs/plunderswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,25 @@ interface IVolumeall {
const fetch = async (timestamp: number, _: ChainBlocks, { createBalances, startOfDay, }: FetchOptions) => {
const dailyVolume = createBalances()
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date(timestamp * 1000))
const nextDayTimestamp = dayTimestamp + 24 * 60 * 60 // Add 24 hours in seconds
const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint));

// Find the entry closest to the start of day
const closestEntry = historicalVolume
.reduce((closest, current) => {
// First filter entries within the day
const dayEntries = historicalVolume.filter(entry => {
const entryTime = new Date(entry.time).getTime() / 1000;
return entryTime >= dayTimestamp && entryTime < nextDayTimestamp;
});

// Then find the closest entry to start of day from filtered entries
if (dayEntries.length > 0) {
const closestEntry = dayEntries.reduce((closest, current) => {
const currentTime = new Date(current.time).getTime() / 1000;
const closestTime = new Date(closest.time).getTime() / 1000;
return Math.abs(currentTime - dayTimestamp) < Math.abs(closestTime - dayTimestamp)
? current
: closest;
});

if (closestEntry) {
dailyVolume.addCGToken("zilliqa", Number(closestEntry.value));
}

Expand Down

0 comments on commit f6a080a

Please sign in to comment.