Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/containers/ChainOverview/useFetchChainChartData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions src/containers/DimensionAdapters/ChainChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down
12 changes: 6 additions & 6 deletions src/containers/DimensionAdapters/ProtocolChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 = {}
Expand Down
22 changes: 11 additions & 11 deletions src/containers/ProtocolOverview/Chart/ProtocolChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/containers/ProtocolOverview/queries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1202,25 +1202,25 @@ export async function getProtocolIncomeStatement({ metadata }: { metadata: IProt
const monthDates = new Set<number>()

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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/pages/etfs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/lst.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
}
Expand Down
24 changes: 18 additions & 6 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
Expand Down