Skip to content

Commit

Permalink
Merge pull request #427 from poap-xyz/release/v1.20.0
Browse files Browse the repository at this point in the history
Release v1.20.0
  • Loading branch information
jm42 authored Dec 9, 2024
2 parents 5ef3184 + 99a146f commit 81f345c
Show file tree
Hide file tree
Showing 15 changed files with 331 additions and 328 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poap-xyz/poap-family",
"version": "1.19.0",
"version": "1.20.0",
"author": {
"name": "POAP",
"url": "https://poap.xyz"
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useAddressTokens.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useState } from 'react'
import { scanAddress } from 'loaders/poap'
import { fetchCollectorPOAPs } from 'loaders/collector'
import { POAP } from 'models/poap'

function useAddressTokens(address: string): {
Expand All @@ -16,7 +16,7 @@ function useAddressTokens(address: string): {
() => {
const controller = new AbortController()
setLoading(true)
scanAddress(address, controller.signal).then(
fetchCollectorPOAPs(address, controller.signal).then(
(foundPOAPs) => {
setLoading(false)
setPOAPs(foundPOAPs)
Expand Down
49 changes: 22 additions & 27 deletions src/hooks/useEventInCommon.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useCallback, useEffect, useState } from 'react'
import { AbortedError } from 'models/error'
import { Drop } from 'models/drop'
import { POAP } from 'models/poap'
import { CountProgress, DownloadProgress } from 'models/http'
import { InCommon } from 'models/api'
import { filterInCommon } from 'models/in-common'
import { getInCommonEventsWithEvents, getInCommonEventsWithProgress } from 'loaders/api'
import { scanAddress } from 'loaders/poap'
import { fetchCollectorDrops } from 'loaders/collector'

function useEventInCommon(
eventId: number,
Expand Down Expand Up @@ -82,38 +81,34 @@ function useEventInCommon(

const fetchAddressInCommon = useCallback(
async (address: string, abortSignal: AbortSignal) => {
let tokens: POAP[]
let addressDrops: Drop[]
try {
tokens = await scanAddress(address, abortSignal)
addressDrops = await fetchCollectorDrops(address, abortSignal)
} catch (err: unknown) {
addError(address, err)
return
}
for (const token of tokens) {
const event = token.event
if (event != null) {
const eventId = event.id
setInCommon((prevInCommon) => {
if (prevInCommon == null) {
return {
[eventId]: [address],
}
for (const addressDrop of addressDrops) {
const addressDropId = addressDrop.id
setInCommon((prevInCommon) => {
if (prevInCommon == null) {
return {
[addressDropId]: [address],
}
if (eventId in prevInCommon) {
if (!prevInCommon[eventId].includes(address)) {
prevInCommon[eventId].push(address)
}
} else {
prevInCommon[eventId] = [address]
}
if (addressDropId in prevInCommon) {
if (!prevInCommon[addressDropId].includes(address)) {
prevInCommon[addressDropId].push(address)
}
return prevInCommon
})
setEvents((prevEvents) => ({ ...prevEvents, [eventId]: event }))
} else {
const error = new Error(`Could not find POAP ${token.id}`)
addError(address, error)
return
}
} else {
prevInCommon[addressDropId] = [address]
}
return prevInCommon
})
setEvents((prevEvents) => ({
...prevEvents,
[addressDropId]: addressDrop,
}))
}
setLoadedOwners((prevLoadedCount) => prevLoadedCount + 1)
},
Expand Down
22 changes: 6 additions & 16 deletions src/hooks/useEventsInCommon.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useCallback, useEffect, useState } from 'react'
import { getInCommonEventsWithEvents, getInCommonEventsWithProgress } from 'loaders/api'
import { scanAddress } from 'loaders/poap'
import { fetchCollectorDrops } from 'loaders/collector'
import { AbortedError } from 'models/error'
import { POAP } from 'models/poap'
import { Drop } from 'models/drop'
import { CountProgress, DownloadProgress } from 'models/http'
import { EventsInCommon, InCommon } from 'models/api'
Expand Down Expand Up @@ -414,9 +413,9 @@ function useEventsInCommon(
const processEventAddress = useCallback(
async (eventId: number, address: string, abortSignal: AbortSignal) => {
removeError(eventId, address)
let ownerTokens: POAP[]
let ownerDrops: Drop[]
try {
ownerTokens = await scanAddress(address, abortSignal)
ownerDrops = await fetchCollectorDrops(address, abortSignal)
} catch (err: unknown) {
if (!(err instanceof AbortedError)) {
addError(
Expand All @@ -429,19 +428,10 @@ function useEventsInCommon(
}
return
}
for (const ownerToken of ownerTokens) {
if (ownerToken.event) {
updateInCommonEvent(eventId, address, ownerToken.event)
} else {
addError(
eventId,
address,
new Error(`Missing token drop for ${address}`)
)
return
}
}
incrLoadedCount(eventId)
for (const ownerDrop of ownerDrops) {
updateInCommonEvent(eventId, address, ownerDrop)
}
},
[]
)
Expand Down
22 changes: 11 additions & 11 deletions src/hooks/useEventsOwnersAndMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useState } from 'react'
import { filterInvalidOwners } from 'models/address'
import { AbortedError } from 'models/error'
import { EventAndOwners, InCommon } from 'models/api'
import { fetchPOAPs } from 'loaders/poap'
import { fetchDropCollectors } from 'loaders/collector'
import {
getEventAndOwners,
getEventMetrics,
Expand Down Expand Up @@ -156,11 +156,11 @@ function useEventsOwnersAndMetrics(eventIds: number[], expiryDates: Record<numbe
async (eventId: number, abortSignal: AbortSignal) => {
removeError(eventId)
addLoading(eventId)
let eventOwnerTokensResult
let eventMetricsResult
let eventCollectorsResult: PromiseSettledResult<Awaited<ReturnType<typeof fetchDropCollectors>>>
let eventMetricsResult: PromiseSettledResult<Awaited<ReturnType<typeof getEventMetrics>>>
try {
[eventOwnerTokensResult, eventMetricsResult] = await Promise.allSettled([
fetchPOAPs(eventId, abortSignal),
[eventCollectorsResult, eventMetricsResult] = await Promise.allSettled([
fetchDropCollectors([eventId], abortSignal),
getEventMetrics(eventId, abortSignal, force),
])
} catch (err: unknown) {
Expand All @@ -176,18 +176,18 @@ function useEventsOwnersAndMetrics(eventIds: number[], expiryDates: Record<numbe
return
}
removeLoading(eventId)
if (eventOwnerTokensResult.status === 'fulfilled') {
if (eventCollectorsResult.status === 'fulfilled') {
updateEventOwners(
eventId,
eventOwnerTokensResult.value.map((token) => token.owner)
eventCollectorsResult.value
)
} else {
if (!(eventOwnerTokensResult.reason instanceof AbortedError)) {
console.error(eventOwnerTokensResult.reason)
if (!(eventCollectorsResult.reason instanceof AbortedError)) {
console.error(eventCollectorsResult.reason)
addError(
eventId,
new Error(`Tokens for drop '${eventId}' failed to fetch`, {
cause: eventOwnerTokensResult.reason,
new Error(`Collectors for drop '${eventId}' failed to fetch`, {
cause: eventCollectorsResult.reason,
})
)
}
Expand Down
2 changes: 0 additions & 2 deletions src/loaders/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export async function findEventsCollections(
},
'offset',
limit,
undefined,
abortSignal
),
eventIds.length < 2 ? Promise.resolve([]) : queryAllCompass(
Expand Down Expand Up @@ -94,7 +93,6 @@ export async function findEventsCollections(
},
'offset',
limit,
undefined,
abortSignal
)
])
Expand Down
153 changes: 153 additions & 0 deletions src/loaders/collector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { IGNORED_OWNERS } from 'models/address'
import { DEFAULT_COLLECTOR_LIMIT, parseColectorDrop, parseCollector } from 'models/collector'
import { DEFAULT_DROP_LIMIT } from 'models/event'
import { DEFAULT_COMPASS_LIMIT } from 'models/compass'
import { DEFAULT_POAP_LIMIT, parsePOAP, POAP } from 'models/poap'
import { Drop } from 'models/drop'
import { queryAllCompass } from 'loaders/compass'

export async function fetchDropCollectors(
dropIds: number[],
abortSignal?: AbortSignal,
limit = Math.min(DEFAULT_COLLECTOR_LIMIT, DEFAULT_COMPASS_LIMIT),
): Promise<string[]> {
return await queryAllCompass(
`poaps`,
parseCollector,
`
query FetchDropCollectors(
$dropIds: [bigint!]
$offset: Int!
$limit: Int!
) {
poaps(
where: {
drop_id: { _in: $dropIds }
collector_address: {
_nin: ["${IGNORED_OWNERS.join('", "')}"]
}
}
distinct_on: collector_address
offset: $offset
limit: $limit
) {
collector_address
}
}
`,
{
dropIds,
limit,
},
'offset',
limit,
abortSignal
)
}

export async function fetchCollectorDrops(
address: string,
abortSignal?: AbortSignal,
limit = Math.min(DEFAULT_DROP_LIMIT, DEFAULT_COMPASS_LIMIT),
): Promise<Drop[]> {
return await queryAllCompass(
`poaps`,
(data: unknown) => parseColectorDrop(data, /*includeDescription*/false),
`
query FetchCollectorDrops(
$address: String!
$offset: Int!
$limit: Int!
) {
poaps(
where: {
collector_address: { _eq: $address }
}
distinct_on: drop_id
offset: $offset
limit: $limit
) {
drop {
id
name
description
image_url
city
country
start_date
end_date
expiry_date
drop_image {
gateways {
type
url
}
}
}
}
}
`,
{
address: address.toLowerCase(),
limit,
},
'offset',
limit,
abortSignal,
)
}

export async function fetchCollectorPOAPs(
address: string,
abortSignal?: AbortSignal,
limit = Math.min(DEFAULT_POAP_LIMIT, DEFAULT_COMPASS_LIMIT),
): Promise<POAP[]> {
return await queryAllCompass(
'poaps',
parsePOAP,
`
query FetchCollectorPOAPs(
$address: String!
$offset: Int!
$limit: Int!
) {
poaps(
where: {
collector_address: { _eq: $address }
}
offset: $offset
limit: $limit
) {
id
collector_address
minted_on
drop {
id
name
description
image_url
city
country
start_date
end_date
expiry_date
drop_image {
gateways {
type
url
}
}
}
}
}
`,
{
address: address.toLowerCase(),
limit,
},
'offset',
limit,
abortSignal
)
}
5 changes: 1 addition & 4 deletions src/loaders/compass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ export async function queryAllCompass<T>(
variables: Record<string, unknown>,
offsetKey: string,
limit: number,
total: number,
abortSignal?: AbortSignal
): Promise<T[]> {
let results: T[] = []
Expand Down Expand Up @@ -216,9 +215,7 @@ export async function queryAllCompass<T>(

results = [...results, ...pageResults]
pageCount = pageResults.length
} while (
(total && pageCount >= total) || (!total && pageCount > 0)
)
} while (pageCount > 0)

return results
}
Expand Down
6 changes: 5 additions & 1 deletion src/loaders/drop.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { DEFAULT_COMPASS_LIMIT } from 'models/compass'
import { DEFAULT_DROP_LIMIT, DEFAULT_SEARCH_LIMIT } from 'models/event'
import { Drop, parseDrop } from 'models/drop'
import { queryAggregateCountCompass, queryFirstCompass, queryManyCompass } from 'loaders/compass'
import { HttpError } from 'models/error'
import {
queryAggregateCountCompass,
queryFirstCompass,
queryManyCompass,
} from 'loaders/compass'

export async function searchDrops(
query: string,
Expand Down
Loading

0 comments on commit 81f345c

Please sign in to comment.