Skip to content

Commit

Permalink
Fix date for reporting
Browse files Browse the repository at this point in the history
Date should always be date before submitting to subquery
  • Loading branch information
kattylucy committed Aug 20, 2024
1 parent 2289ee4 commit a4174d5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 20 additions & 5 deletions centrifuge-js/src/modules/pools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 } }
}>(
Expand Down Expand Up @@ -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,
}
)
Expand All @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions centrifuge-js/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit a4174d5

Please sign in to comment.