From a4174d584bdff1145815fcd20c3c61d3dc8c0204 Mon Sep 17 00:00:00 2001 From: katty barroso Date: Tue, 20 Aug 2024 14:33:53 -0500 Subject: [PATCH] Fix date for reporting Date should always be date before submitting to subquery --- centrifuge-js/src/modules/pools.ts | 25 ++++++++++++++++++++----- centrifuge-js/src/utils/index.ts | 6 ++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/centrifuge-js/src/modules/pools.ts b/centrifuge-js/src/modules/pools.ts index d1752dcdb5..f0a039eec7 100644 --- a/centrifuge-js/src/modules/pools.ts +++ b/centrifuge-js/src/modules/pools.ts @@ -36,6 +36,7 @@ import { getDateYearsFromNow, getRandomUint, isSameAddress, + isValidDate, } from '../utils' import { CurrencyBalance, Perquintill, Price, Rate, TokenBalance } from '../utils/BN' import { Dec } from '../utils/Decimal' @@ -2292,8 +2293,16 @@ export function getPoolsModule(inst: Centrifuge) { } ) } - + // fix function getPoolSnapshotsWithCursor(poolId: string, endCursor: string | null, from?: Date, to?: Date) { + // Default values for invalid dates + const defaultFrom = getDateYearsFromNow(-10).toISOString() + const defaultTo = getDateYearsFromNow(10).toISOString() + + // Use valid dates or default values + const validFrom = isValidDate(from) ? from?.toISOString() : defaultFrom + const validTo = isValidDate(to) ? to?.toISOString() : defaultTo + return inst.getSubqueryObservable<{ poolSnapshots: { nodes: SubqueryPoolSnapshot[]; pageInfo: { hasNextPage: boolean; endCursor: string } } }>( @@ -2341,8 +2350,8 @@ export function getPoolsModule(inst: Centrifuge) { `, { poolId, - from: from ? from.toISOString() : getDateYearsFromNow(-10).toISOString(), - to: to ? to.toISOString() : getDateYearsFromNow(10).toISOString(), + from: validFrom, + to: validTo, poolCursor: endCursor, } ) @@ -2354,10 +2363,16 @@ export function getPoolsModule(inst: Centrifuge) { from?: Date, to?: Date ) { + const defaultFrom = getDateYearsFromNow(-10).toISOString() + const defaultTo = getDateYearsFromNow(10).toISOString() + + const validFrom = isValidDate(from) ? from?.toISOString() : defaultFrom + const validTo = isValidDate(to) ? to?.toISOString() : defaultTo + const filter: any = { timestamp: { - greaterThan: from ? from.toISOString() : getDateYearsFromNow(-10).toISOString(), - lessThan: to ? to.toISOString() : getDateYearsFromNow(10).toISOString(), + greaterThan: validFrom, + lessThan: validTo, }, } if ('poolId' in filterBy) { diff --git a/centrifuge-js/src/utils/index.ts b/centrifuge-js/src/utils/index.ts index 7950752077..a1e316970b 100644 --- a/centrifuge-js/src/utils/index.ts +++ b/centrifuge-js/src/utils/index.ts @@ -151,3 +151,9 @@ export function isEvm(address: string) { const suffix = '45564d00' return address.length === 66 && address.endsWith(suffix) } + +export function isValidDate(value: string | Date | undefined) { + if (value === undefined) return false + const date = new Date(value) + return !isNaN(date.getTime()) +}