diff --git a/src/containers/ChainOverview/useFetchChainChartData.tsx b/src/containers/ChainOverview/useFetchChainChartData.tsx index a95d043360..37803904af 100644 --- a/src/containers/ChainOverview/useFetchChainChartData.tsx +++ b/src/containers/ChainOverview/useFetchChainChartData.tsx @@ -637,9 +637,9 @@ const formatBarChart = ({ const isCumulative = groupBy === 'cumulative' for (const [date, value] of data) { const dateKey = isWeekly - ? lastDayOfWeek(dateInMs ? +date : +date * 1e3) + ? lastDayOfWeek(dateInMs ? +date / 1e3 : +date) : isMonthly - ? firstDayOfMonth(dateInMs ? +date : +date * 1e3) + ? firstDayOfMonth(dateInMs ? +date / 1e3 : +date) : dateInMs ? +date / 1e3 : +date @@ -690,9 +690,9 @@ const formatLineChart = ({ const isMonthly = groupBy === 'monthly' for (const [date, value] of data) { const dateKey = isWeekly - ? lastDayOfWeek(dateInMs ? +date : +date * 1e3) + ? lastDayOfWeek(dateInMs ? +date / 1e3 : +date) : isMonthly - ? firstDayOfMonth(dateInMs ? +date : +date * 1e3) + ? firstDayOfMonth(dateInMs ? +date / 1e3 : +date) : dateInMs ? +date / 1e3 : +date diff --git a/src/containers/DimensionAdapters/ChainChart.tsx b/src/containers/DimensionAdapters/ChainChart.tsx index 4f2a6875e0..32a71e2400 100644 --- a/src/containers/DimensionAdapters/ChainChart.tsx +++ b/src/containers/DimensionAdapters/ChainChart.tsx @@ -69,14 +69,18 @@ export const AdapterByChainChart = ({ let cumulative = 0 for (const [date, value] of chartData) { - const finalDate = - chartInterval === 'Weekly' - ? Number(lastDayOfWeek(date)) * 1e3 - : chartInterval === 'Monthly' - ? Number(firstDayOfMonth(date)) * 1e3 - : date - data[finalDate] = data[finalDate] || 0 - data[finalDate] += value + const dateInSec = date / 1e3 + let normalizedDateInSec: number + if (chartInterval === 'Weekly') { + normalizedDateInSec = lastDayOfWeek(dateInSec) + } else if (chartInterval === 'Monthly') { + normalizedDateInSec = firstDayOfMonth(dateInSec) + } else { + normalizedDateInSec = dateInSec + } + + const finalDate = normalizedDateInSec * 1e3 + data[finalDate] = (data[finalDate] || 0) + value if (chartInterval === 'Cumulative') { data[finalDate] += cumulative @@ -352,9 +356,9 @@ const getChartDataByChainAndInterval = ({ for (const date in chartData) { const finalDate = chartInterval === 'Weekly' - ? lastDayOfWeek(+date * 1e3) * 1e3 + ? lastDayOfWeek(+date) * 1e3 : chartInterval === 'Monthly' - ? firstDayOfMonth(+date * 1e3) * 1e3 + ? firstDayOfMonth(+date) * 1e3 : +date * 1e3 const topByDate = {} diff --git a/src/containers/DimensionAdapters/ProtocolChart.tsx b/src/containers/DimensionAdapters/ProtocolChart.tsx index f1ace1e4a8..b31a4c92e5 100644 --- a/src/containers/DimensionAdapters/ProtocolChart.tsx +++ b/src/containers/DimensionAdapters/ProtocolChart.tsx @@ -29,9 +29,9 @@ export const DimensionProtocolOverviewChart = ({ const mainChartData = React.useMemo(() => { const formatDate = (date) => chartInterval === 'Weekly' - ? lastDayOfWeek(+date * 1e3) * 1e3 + ? lastDayOfWeek(+date) * 1e3 : chartInterval === 'Monthly' - ? firstDayOfMonth(+date * 1e3) * 1e3 + ? firstDayOfMonth(+date) * 1e3 : +date * 1e3 if (totalDataChart[1].includes('Fees')) { @@ -355,9 +355,9 @@ const ChartByType = ({ for (const [date, chains] of totalDataChartBreakdown) { const finalDate = chartInterval === 'Weekly' - ? lastDayOfWeek(+date * 1e3) * 1e3 + ? lastDayOfWeek(+date) * 1e3 : chartInterval === 'Monthly' - ? firstDayOfMonth(+date * 1e3) * 1e3 + ? firstDayOfMonth(+date) * 1e3 : +date * 1e3 const dataByVersion = {} @@ -387,9 +387,9 @@ const ChartByType = ({ for (const [date, chains] of totalDataChartBreakdown) { const finalDate = chartInterval === 'Weekly' - ? lastDayOfWeek(+date * 1e3) * 1e3 + ? lastDayOfWeek(+date) * 1e3 : chartInterval === 'Monthly' - ? firstDayOfMonth(+date * 1e3) * 1e3 + ? firstDayOfMonth(+date) * 1e3 : +date * 1e3 const dataByChain = {} diff --git a/src/containers/ProtocolOverview/Chart/ProtocolChart.tsx b/src/containers/ProtocolOverview/Chart/ProtocolChart.tsx index c27edd0aab..cbffd75b10 100644 --- a/src/containers/ProtocolOverview/Chart/ProtocolChart.tsx +++ b/src/containers/ProtocolOverview/Chart/ProtocolChart.tsx @@ -508,7 +508,7 @@ export const useFetchAndFormatChartData = ({ const isWeekly = groupBy === 'weekly' const isMonthly = groupBy === 'monthly' for (const [date, value] of tvlChartData) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1e3) : isMonthly ? firstDayOfMonth(+date * 1e3) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date store[dateKey] = value + extraTvls.reduce((acc, curr) => acc + (extraTvlCharts?.[curr]?.[dateKey] ?? 0), 0) } const finalChart = [] @@ -1070,7 +1070,7 @@ export const useFetchAndFormatChartData = ({ if (feesData) { let total = 0 for (const [date, value] of feesData.totalDataChart) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1e3) : isMonthly ? firstDayOfMonth(+date * 1e3) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date const finalValue = denominationPriceHistory ? denominationPriceHistory[String(+date * 1e3)] ? value / denominationPriceHistory[String(+date * 1e3)] @@ -1086,7 +1086,7 @@ export const useFetchAndFormatChartData = ({ if (revenueData) { let total = 0 for (const [date, value] of revenueData.totalDataChart) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1e3) : isMonthly ? firstDayOfMonth(+date * 1e3) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date const finalValue = denominationPriceHistory ? denominationPriceHistory[String(+date * 1e3)] ? value / denominationPriceHistory[String(+date * 1e3)] @@ -1102,7 +1102,7 @@ export const useFetchAndFormatChartData = ({ if (holdersRevenueData) { let total = 0 for (const [date, value] of holdersRevenueData.totalDataChart) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1e3) : isMonthly ? firstDayOfMonth(+date * 1e3) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date const finalValue = denominationPriceHistory ? denominationPriceHistory[String(+date * 1e3)] ? value / denominationPriceHistory[String(+date * 1e3)] @@ -1118,7 +1118,7 @@ export const useFetchAndFormatChartData = ({ if (bribesData) { let total = 0 for (const [date, value] of bribesData.totalDataChart) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1e3) : isMonthly ? firstDayOfMonth(+date * 1e3) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date const finalValue = denominationPriceHistory ? denominationPriceHistory[String(+date * 1e3)] ? value / denominationPriceHistory[String(+date * 1e3)] @@ -1143,7 +1143,7 @@ export const useFetchAndFormatChartData = ({ if (tokenTaxesData) { let total = 0 for (const [date, value] of tokenTaxesData.totalDataChart) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1e3) : isMonthly ? firstDayOfMonth(+date * 1e3) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date const finalValue = denominationPriceHistory ? denominationPriceHistory[String(+date * 1e3)] ? value / denominationPriceHistory[String(+date * 1e3)] @@ -1260,7 +1260,7 @@ export const useFetchAndFormatChartData = ({ const isMonthly = groupBy === 'monthly' const store = {} for (const { date, ...rest } of unlocksAndIncentivesData.chartData.documented) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1000) : isMonthly ? firstDayOfMonth(+date * 1000) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date let total = 0 for (const label in rest) { total += rest[label] @@ -1556,9 +1556,9 @@ const formatBarChart = ({ const isCumulative = groupBy === 'cumulative' for (const [date, value] of data) { const dateKey = isWeekly - ? lastDayOfWeek(dateInMs ? +date : +date * 1e3) + ? lastDayOfWeek(dateInMs ? +date / 1000 : +date) : isMonthly - ? firstDayOfMonth(dateInMs ? +date : +date * 1e3) + ? firstDayOfMonth(dateInMs ? +date / 1000 : +date) : dateInMs ? +date / 1e3 : +date @@ -1609,9 +1609,9 @@ const formatLineChart = ({ const isMonthly = groupBy === 'monthly' for (const [date, value] of data) { const dateKey = isWeekly - ? lastDayOfWeek(dateInMs ? +date : +date * 1e3) + ? lastDayOfWeek(dateInMs ? +date / 1000 : +date) : isMonthly - ? firstDayOfMonth(dateInMs ? +date : +date * 1e3) + ? firstDayOfMonth(dateInMs ? +date / 1000 : +date) : dateInMs ? +date / 1e3 : +date diff --git a/src/containers/ProtocolOverview/queries.tsx b/src/containers/ProtocolOverview/queries.tsx index 4e305d5a7f..0825ce4331 100644 --- a/src/containers/ProtocolOverview/queries.tsx +++ b/src/containers/ProtocolOverview/queries.tsx @@ -1202,25 +1202,25 @@ export async function getProtocolIncomeStatement({ metadata }: { metadata: IProt const monthDates = new Set() for (const [date, value] of fees.totalDataChart ?? []) { - const dateKey = +firstDayOfMonth(+date * 1e3) * 1e3 + const dateKey = +firstDayOfMonth(+date) * 1e3 feesByMonth[dateKey] = (feesByMonth[dateKey] ?? 0) + value monthDates.add(dateKey) } for (const [date, value] of revenue.totalDataChart ?? []) { - const dateKey = +firstDayOfMonth(+date * 1e3) * 1e3 + const dateKey = +firstDayOfMonth(+date) * 1e3 revenueByMonth[dateKey] = (revenueByMonth[dateKey] ?? 0) + value monthDates.add(dateKey) } for (const [date, value] of holdersRevenue?.totalDataChart ?? []) { - const dateKey = +firstDayOfMonth(+date * 1e3) * 1e3 + const dateKey = +firstDayOfMonth(+date) * 1e3 holdersRevenueByMonth[dateKey] = (holdersRevenueByMonth[dateKey] ?? 0) + value monthDates.add(dateKey) } for (const [date, value] of incentives ?? []) { - const dateKey = +firstDayOfMonth(+date * 1e3) * 1e3 + const dateKey = +firstDayOfMonth(+date) * 1e3 incentivesByMonth[dateKey] = (incentivesByMonth[dateKey] ?? 0) + value monthDates.add(dateKey) } diff --git a/src/pages/etfs.tsx b/src/pages/etfs.tsx index a41a7560b4..8cb34bae2f 100644 --- a/src/pages/etfs.tsx +++ b/src/pages/etfs.tsx @@ -90,8 +90,8 @@ const PageView = ({ snapshot, flows, totalsByAsset, lastUpdated }: PageViewProps const date = ['daily', 'cumulative'].includes(groupBy) ? flowDate : groupBy === 'weekly' - ? lastDayOfWeek(+flowDate * 1000) - : firstDayOfMonth(+flowDate * 1000) + ? lastDayOfWeek(+flowDate) + : firstDayOfMonth(+flowDate) bitcoin[date] = (bitcoin[date] || 0) + (flows[flowDate]['Bitcoin'] ?? 0) + totalBitcoin if (flows[flowDate]['Ethereum']) { diff --git a/src/pages/lst.tsx b/src/pages/lst.tsx index ae7d810c3d..16c97193f3 100644 --- a/src/pages/lst.tsx +++ b/src/pages/lst.tsx @@ -51,7 +51,7 @@ const PageView = ({ const totalByToken = {} for (const date in inflowsChartData) { for (const token in inflowsChartData[date]) { - const dateKey = isWeekly ? lastDayOfWeek(+date * 1e3) : isMonthly ? firstDayOfMonth(+date * 1e3) : date + const dateKey = isWeekly ? lastDayOfWeek(+date) : isMonthly ? firstDayOfMonth(+date) : date if (!store[dateKey]) { store[dateKey] = {} } diff --git a/src/utils/index.js b/src/utils/index.js index 49de8c386d..8a54bb9280 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -537,9 +537,14 @@ export function nearestUtcZeroHour(dateString) { : Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()) } -// TODO params & return value should be in seconds -export function firstDayOfMonth(dateString) { - const date = new Date(dateString) +/** + * Returns the Unix timestamp (in seconds) for the first day of the month. + * + * @param {number} dateInSeconds - Unix timestamp in seconds. + * @returns {number} Unix timestamp (in seconds) for the first day of the month. + */ +export function firstDayOfMonth(dateInSeconds) { + const date = new Date(dateInSeconds * 1000) return Math.trunc(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1) / 1000) } @@ -549,9 +554,16 @@ export function firstDayOfQuarter(dateString) { const quarterStartMonth = Math.floor(month / 3) * 3 return Math.trunc(Date.UTC(date.getUTCFullYear(), quarterStartMonth, 1) / 1000) } -// TODO params & return value should be in seconds -export function lastDayOfWeek(dateString) { - const date = new Date(dateString) + +/** + * Returns the Unix timestamp (in seconds) for the last day of the week (Sunday), + * based on a given date in seconds. + * + * @param {number} dateInSeconds - Unix timestamp in seconds. + * @returns {number} Unix timestamp (in seconds) for the last day of the week. + */ +export function lastDayOfWeek(dateInSeconds) { + const date = new Date(dateInSeconds * 1000) const weekDay = date.getUTCDay() // Calculate days to add to get to the end of the week (Sunday) const daysToAdd = weekDay === 0 ? 0 : 7 - weekDay