From 8363889f6eda18587bd8b71f1eecf7166c305a74 Mon Sep 17 00:00:00 2001 From: alelliott Date: Tue, 18 Jun 2024 15:53:55 -0400 Subject: [PATCH] adjust subgraph query --- src/common/InfoDashboard/helpers.ts | 91 +++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 11 deletions(-) diff --git a/src/common/InfoDashboard/helpers.ts b/src/common/InfoDashboard/helpers.ts index fe1b0bf..3e29a60 100644 --- a/src/common/InfoDashboard/helpers.ts +++ b/src/common/InfoDashboard/helpers.ts @@ -1,19 +1,88 @@ -// helpers.ts -import { BigNumber } from 'ethers' -import { formatEther } from '@/utils' +import { Time, hexlify, BigNumber } from '@/utils' +import { gql } from '@apollo/client' +import { config } from '@config' +import { mapKeys, mapValues } from 'lodash' type ChartData = Record -export async function getChartData(): Promise { - // Static value - const totalDeposited = BigNumber.from('2226000000000000000000') +const ONE_DAY_TIMESTAMP = 24 * 60 * 60 - // Start of today in milliseconds - const today = new Date().setHours(0, 0, 0, 0,) +export async function getChartData( + poolId: number, + poolStartedAt: BigNumber, + month: number, +): Promise { + type QueryData = Record<`d${number}`, { totalStaked: string }[]> + const { data } = await config.apolloClient.query({ + query: _generateTotalStakedPerDayGraphqlQuery(poolId, poolStartedAt, month), + }) - const data: ChartData = { - [today]: totalDeposited + console.log('GraphQL response data:', data) + + return mapValues( + mapKeys(data, (_, key) => key.slice(1)), + value => BigNumber.from(value[0]?.totalStaked || 0), + ) +} + +function _generateTotalStakedPerDayGraphqlQuery( + poolId: number, + poolStartedAt: BigNumber, + month: number, +) { + const REQUEST_PATTERN = (date: number, timestamp: number) => ` + d${date}: poolInteractions( + first: 1, + orderDirection: desc, + where: { timestamp_lte: ${timestamp}, pool: "${hexlify(poolId)}" }, + orderBy: timestamp + ) { + totalStaked + }` + + const monthTime = new Time(String(month), 'M') + const currentTime = new Time() + const poolStartedAtTime = new Time(poolStartedAt.toNumber()) + + let startDate: number + let endDate: number + + if (currentTime.isBefore(poolStartedAtTime)) { + // If the current time is before the pool start date + startDate = monthTime.isSame(poolStartedAtTime, 'month') ? poolStartedAtTime.get('date') : 1 + endDate = monthTime.isSame(poolStartedAtTime, 'month') ? poolStartedAtTime.get('date') : monthTime.dayjs.daysInMonth() + } else { + // If the pool has already started + startDate = monthTime.isSame(poolStartedAtTime, 'month') ? poolStartedAtTime.get('date') : 1 + endDate = monthTime.isSame(currentTime, 'month') ? currentTime.get('date') : monthTime.dayjs.daysInMonth() } - return data + console.log('startDate:', startDate) + console.log('endDate:', endDate) + + const requests = [] + for (let date = startDate; date <= endDate; date++) { + let timestamp + if (date === startDate) { + // For the first day, set timestamp to the end of the day (12:00 AM GMT next day) + timestamp = poolStartedAtTime.timestamp + (24 * 60 * 60) - (poolStartedAtTime.timestamp % ONE_DAY_TIMESTAMP) + } else { + // For subsequent days, use 12:00 AM GMT + timestamp = poolStartedAtTime.timestamp + (date - startDate) * ONE_DAY_TIMESTAMP + } + + console.log(`Generating request for date ${date} with timestamp ${timestamp}`) + + const request = REQUEST_PATTERN(date, timestamp) + requests.push(request) + console.log('Request added:', request) + } + + const query = gql` + ${'{\n' + requests.join('\n') + '\n}'} + ` + + console.log('Generated GraphQL query:', query) + + return query }