Skip to content

Commit

Permalink
fix: Expiration queries
Browse files Browse the repository at this point in the history
  • Loading branch information
fzavalia committed Jul 21, 2023
1 parent 8845d62 commit 185e078
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 27 deletions.
5 changes: 4 additions & 1 deletion src/logic/expiration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export function isOrderExpired(expiresAt: string) {
return BigInt(expiresAt) * 1000n < Date.now()
let expiresAtNum =
expiresAt.length === 13 ? Number(expiresAt) : Number(expiresAt) * 1000

return expiresAtNum < Date.now()
}
28 changes: 21 additions & 7 deletions src/logic/prices/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,17 @@ export function marketplacePricesQuery(filters: PriceFilters) {
category: filters.category as NFTCategory,
})
const categories = getNFTCategoryFromPriceCategory(filters.category)

const innerWhere = `
searchOrderPrice_gt:0,
searchOrderStatus: open,
category_in: [${categories}],
searchEstateSize_gt: 0,
${additionalWheres.join('\n')}
`
return `query NFTPrices(
$expiresAt: String
$expiresAtSec: String
$wearableCategory: String
$isWearableHead: Boolean
$isWearableAccessory: Boolean
Expand All @@ -98,13 +107,18 @@ export function marketplacePricesQuery(filters: PriceFilters) {
first: ${MAX_RESULTS},
orderBy: tokenId,
orderDirection: asc,
where: {
searchOrderPrice_gt:0,
searchOrderStatus: open,
searchOrderExpiresAt_gt: $expiresAt,
category_in: [${categories}],
searchEstateSize_gt: 0,
${additionalWheres.join('\n')}
where: {
or:[
{
${innerWhere}
searchOrderExpiresAt_gt: $expiresAtSec
searchOrderExpiresAt_lt: "1000000000000"
},
{
${innerWhere}
searchOrderExpiresAt_gt: $expiresAt
}
]
}) {
...priceFragment
}
Expand Down
1 change: 1 addition & 0 deletions src/ports/nfts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type QueryVariables = Omit<NFTFilters, 'sortBy' | 'rentalDays'> & {
orderBy: string
orderDirection: 'asc' | 'desc'
expiresAt: string
expiresAtSec: string
}

export interface INFTsComponent {
Expand Down
38 changes: 32 additions & 6 deletions src/ports/nfts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const NFTS_FILTERS = `
$orderBy: String
$orderDirection: String
$expiresAt: String
$expiresAtSec: String
$owner: String
$wearableCategory: String
$isWearableHead: Boolean
Expand Down Expand Up @@ -44,11 +45,14 @@ export function getQueryVariables<T>(
const { sortBy, ...variables } = options
const orderBy = getOrderBy(sortBy) as string
const orderDirection = getOrderDirection(sortBy)
const expiresAt = Date.now()
const expiresAtSec = Math.trunc(expiresAt / 1000)
return {
...variables,
orderBy,
orderDirection,
expiresAt: Date.now().toString(),
expiresAt: expiresAt.toString(),
expiresAtSec: expiresAtSec.toString(),
}
}

Expand Down Expand Up @@ -183,6 +187,7 @@ export function getFetchQuery(
isCount = false
) {
const where: string[] = []
let wrapWhere = false

if (filters.owner) {
where.push('owner: $owner')
Expand All @@ -194,7 +199,7 @@ export function getFetchQuery(
filters.sortBy === NFTSortBy.RECENTLY_LISTED
) {
where.push('searchOrderStatus: open')
where.push('searchOrderExpiresAt_gt: $expiresAt')
wrapWhere = true
}

if (filters.search) {
Expand Down Expand Up @@ -247,15 +252,36 @@ export function getFetchQuery(
: filters.first
: max

let wrappedWhere = `{
${where.join('\n')}
${getExtraWhere ? getExtraWhere(filters).join('\n') : ''}
}`

if (wrapWhere) {
wrappedWhere = `{
or:[
{
${[
...where,
`searchOrderExpiresAt_gt: $expiresAtSec`,
`searchOrderExpiresAt_lt: "1000000000000"`,
].join('\n')}
${getExtraWhere ? getExtraWhere(filters).join('\n') : ''}
},
{
${[...where, `searchOrderExpiresAt_gt: $expiresAt`].join('\n')}
${getExtraWhere ? getExtraWhere(filters).join('\n') : ''}
}
]
}`
}

const query = `query NFTs(
${NFTS_FILTERS}
${getExtraVariables ? getExtraVariables(filters).join('\n') : ''}
) {
nfts(
where: {
${where.join('\n')}
${getExtraWhere ? getExtraWhere(filters).join('\n') : ''}
}${getArguments(total)})
where: ${wrappedWhere}${getArguments(total)})
{
${isCount ? 'id' : `...${fragmentName}`}
}
Expand Down
32 changes: 28 additions & 4 deletions src/ports/orders/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const getOrdersQuery = (
} = filters

const where: string[] = []
let wrapWhere = false

if (marketplaceAddress) {
where.push(`marketplaceAddress : "${marketplaceAddress}"`)
Expand Down Expand Up @@ -122,7 +123,7 @@ export const getOrdersQuery = (

if (status) {
if (status === ListingStatus.OPEN) {
where.push(`expiresAt_gt: "${Math.round(Date.now() / 1000)}"`)
wrapWhere = true
}
where.push(`status: ${status}`)
}
Expand Down Expand Up @@ -169,15 +170,38 @@ export const getOrdersQuery = (
orderDirection = 'desc'
}

let wrappedWhere = `{
${where.join('\n')}
}`

if (wrapWhere) {
const expiresAt = Date.now()
const expiresAtSec = Math.trunc(expiresAt / 1000)

wrappedWhere = `{
or:[
{
${[
...where,
`expiresAt_gt: "${expiresAtSec}"`,
`expiresAt_lt: "1000000000000"`,
].join('\n')}
},
{
${[...where, `expiresAt_gt: "${expiresAt}"`].join('\n')}
}
]
}`
}

return `
query Orders {
orders(
first: ${total},
orderBy: ${orderBy},
orderDirection: ${orderDirection},
where: {
${where.join('\n')}
})
where: ${wrappedWhere}
)
{ ${isCount ? 'id' : `...orderFragment`} }
}
${isCount ? '' : getOrderFragment(getOrderFields)}
Expand Down
5 changes: 4 additions & 1 deletion src/ports/prices/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,13 @@ export function createPricesComponent(options: {
let priceFragments: PriceFragment[] = []
while (true) {
const query = getPricesQuery(queryGetter, filters, lastId)
const expiresAt = Date.now()
const expiresAtSec = Math.trunc(expiresAt / 1000)
const queryVariables = {
...filters,
lastId,
expiresAt: Date.now().toString(),
expiresAt: expiresAt.toString(),
expiresAtSec: expiresAtSec.toString(),
}
const { prices: fragments } = await subgraph.query<{
prices: PriceFragment[]
Expand Down
5 changes: 4 additions & 1 deletion src/ports/stats/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ export function createStatsComponent(options: {
let sizes: FetchEstateSizesQueryFragment[] = []
while (true) {
const query = getEstatesSizesQuery(filters)
const expiresAt = Date.now()
const expiresAtSec = Math.trunc(expiresAt / 1000)
const queryVariables = {
lastId,
expiresAt: Date.now().toString(),
expiresAt: expiresAt.toString(),
expiresAtSec: expiresAtSec.toString(),
}
const { nfts: fragments } = await subgraph.query<{
nfts: FetchEstateSizesQueryFragment[]
Expand Down
37 changes: 30 additions & 7 deletions src/ports/stats/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export const MAX_RESULTS = 1000

export function getEstatesSizesQuery(filters: StatsEstateFilters) {
const extraWhere = []
let wrapWhere = false

if (filters.isOnSale) {
extraWhere.push('searchOrderStatus: open')
extraWhere.push('searchOrderExpiresAt_gt: $expiresAt')
wrapWhere = true
}
if (filters.maxPrice) {
extraWhere.push(`searchOrderPrice_lte: "${filters.maxPrice}"`)
Expand All @@ -16,16 +18,37 @@ export function getEstatesSizesQuery(filters: StatsEstateFilters) {
if (filters.minPrice) {
extraWhere.push(`searchOrderPrice_gte: "${filters.minPrice}"`)
}

addLandFilters(filters, extraWhere)

let wrappedWhere = `
category: estate,
searchEstateSize_gt:0,
id_gt: $lastId,
${extraWhere.join('\n')}
`

if (wrapWhere) {
wrappedWhere = `
or:[
{
${wrappedWhere}
searchOrderExpiresAt_gt: $expiresAtSec
searchOrderExpiresAt_lt: "1000000000000"
},
{
${wrappedWhere}
searchOrderExpiresAt_gt: $expiresAt
}
]`
}

return `
query EstateSizesQuery($lastId: ID, $expiresAt: String ) {
query EstateSizesQuery($lastId: ID, $expiresAt: String, $expiresAtSec: String) {
nfts (
first: ${MAX_RESULTS},
where: {
category: estate,
searchEstateSize_gt:0,
id_gt: $lastId,
${extraWhere.join('\n')}
where: {
${wrappedWhere}
},
orderBy: id,
orderDirection: asc)
Expand Down

0 comments on commit 185e078

Please sign in to comment.