|
| 1 | +import "server-only"; |
| 2 | + |
| 3 | +import { ResultOf, graphql, readFragment } from "@/lib/graphql"; |
| 4 | + |
| 5 | +import request from "graphql-request"; |
| 6 | +import { HYPERCERTS_API_URL_GRAPH } from "@/configs/hypercerts"; |
| 7 | +import { getHypercertMetadata } from "@/hypercerts/actions/getHypercertMetadata"; |
| 8 | +import { UnclaimedFraction } from "@/components/profile/unclaimed-hypercerts-list"; |
| 9 | + |
| 10 | +export const AllowListRecordFragment = graphql(` |
| 11 | + fragment AllowListRecordFragment on AllowlistRecord { |
| 12 | + id |
| 13 | + hypercert_id |
| 14 | + token_id |
| 15 | + root |
| 16 | + leaf |
| 17 | + entry |
| 18 | + user_address |
| 19 | + claimed |
| 20 | + proof |
| 21 | + units |
| 22 | + total_units |
| 23 | + } |
| 24 | +`); |
| 25 | +export type AllowListRecord = ResultOf<typeof AllowListRecordFragment>; |
| 26 | + |
| 27 | +const query = graphql( |
| 28 | + ` |
| 29 | + query allowlistRecords($address: String, $claimed: Boolean) { |
| 30 | + allowlistRecords( |
| 31 | + where: { user_address: { eq: $address }, claimed: { eq: $claimed } } |
| 32 | + ) { |
| 33 | + count |
| 34 | + data { |
| 35 | + ...AllowListRecordFragment |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + `, |
| 40 | + [AllowListRecordFragment], |
| 41 | +); |
| 42 | + |
| 43 | +const requestMap = new Map<string, Promise<any>>(); |
| 44 | + |
| 45 | +async function getMetadataWithDeduping(hypercertId: string) { |
| 46 | + if (requestMap.has(hypercertId)) { |
| 47 | + return requestMap.get(hypercertId); |
| 48 | + } |
| 49 | + |
| 50 | + const requestPromise = getHypercertMetadata(hypercertId); |
| 51 | + requestMap.set(hypercertId, requestPromise); |
| 52 | + |
| 53 | + return await requestPromise; |
| 54 | +} |
| 55 | + |
| 56 | +export async function getAllowListRecordsForAddressByClaimedWithMetadata({ |
| 57 | + address, |
| 58 | + claimed, |
| 59 | +}: { |
| 60 | + address: string; |
| 61 | + claimed: boolean; |
| 62 | +}): Promise< |
| 63 | + { data: UnclaimedFraction[] | null; count: number | null } | undefined |
| 64 | +> { |
| 65 | + const res = await request(HYPERCERTS_API_URL_GRAPH, query, { |
| 66 | + address, |
| 67 | + claimed, |
| 68 | + }); |
| 69 | + |
| 70 | + const allowlistRecords = res.allowlistRecords.data; |
| 71 | + if (!allowlistRecords) { |
| 72 | + return undefined; |
| 73 | + } |
| 74 | + const allowlistRecordsRead = readFragment( |
| 75 | + AllowListRecordFragment, |
| 76 | + allowlistRecords, |
| 77 | + ); |
| 78 | + |
| 79 | + const count = res.allowlistRecords.count; |
| 80 | + |
| 81 | + if (allowlistRecordsRead && allowlistRecordsRead.length > 0) { |
| 82 | + const processedData = await Promise.all( |
| 83 | + allowlistRecordsRead.map(async (fraction) => { |
| 84 | + const metadata = fraction.hypercert_id |
| 85 | + ? await getMetadataWithDeduping(fraction.hypercert_id) |
| 86 | + : null; |
| 87 | + return { ...fraction, metadata: metadata?.data ?? null }; |
| 88 | + }), |
| 89 | + ); |
| 90 | + |
| 91 | + return { |
| 92 | + count, |
| 93 | + data: processedData, |
| 94 | + }; |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + count, |
| 99 | + data: [...allowlistRecordsRead], |
| 100 | + }; |
| 101 | +} |
0 commit comments