Last active
July 20, 2024 12:17
-
-
Save Yuripetusko/8a90338fd42f0f5babdfd9b470d171aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Address } from "viem"; | |
export const BASE_REGISTRY_API_BASE_URL = "https://base.org/api/registry"; | |
export const BASE_REGISTRY_API_ENDPOINTS = { | |
entries: "entries", | |
featured: "featured", | |
}; | |
export const BASE_REGISTRY_API_ENTRIES_QUERY_PARAMS = { | |
//The page number (default 1) | |
page: "page", | |
//The number of entries per page (default 10) | |
limit: "limit", | |
//The category or categories of the entries of interest | |
category: "category", | |
//The entry’s level of curation | |
curation: "curation", | |
}; | |
//(Options: Games, Social, Creators, Finance, Media) | |
export const BASE_REGISTRY_CATEGORIES = { | |
games: "Games", | |
social: "Social", | |
creators: "Creators", | |
finance: "Finance", | |
media: "Media", | |
} as const; | |
//(Options: Featured, Curated, Community) | |
export const BASE_REGISTRY_CURATION_TYPES = { | |
featured: "Featured", | |
curated: "Curated", | |
community: "Community", | |
} as const; | |
export type BaseRegistryCategory = (typeof BASE_REGISTRY_CATEGORIES)[keyof typeof BASE_REGISTRY_CATEGORIES]; | |
export type BaseRegistryCurationType = (typeof BASE_REGISTRY_CURATION_TYPES)[keyof typeof BASE_REGISTRY_CURATION_TYPES]; | |
export type BaseRegistryEntryResponseItem = { | |
id: string; | |
category: BaseRegistryCategory; | |
content: { | |
title: string; | |
short_description: string; | |
full_description: string; | |
image_url: string; | |
target_url: string; | |
cta_text: string; | |
function_signature: string; | |
contract_address: Address; | |
token_id: string; | |
token_amount: string; | |
curation: BaseRegistryCurationType; | |
creator_name: string; | |
creator_image_url: string; | |
}; | |
}; | |
export type BaseRegistryEntriesResponse = { | |
data: BaseRegistryEntryResponseItem[]; | |
}; | |
const getBaseRegistryEntries = async ({ | |
urlInstance, | |
pageNumber, | |
}: { | |
urlInstance: URL; | |
pageNumber: number; | |
}) => { | |
urlInstance.searchParams.set( | |
BASE_REGISTRY_API_ENTRIES_QUERY_PARAMS.page, | |
pageNumber.toString() | |
); | |
const response = await fetch(urlInstance.toString()); | |
const entriesResponse = (await response.json()) as BaseRegistryEntriesResponse; | |
return entriesResponse.data; | |
}; | |
//https://docs.base.org/docs/tools/registry-api/ | |
const fetchAllBaseRegistryEntriesBatched = async () => { | |
const baseRegistryEntriesApiUrl = new URL( | |
`${BASE_REGISTRY_API_BASE_URL}/${BASE_REGISTRY_API_ENDPOINTS.entries}` | |
); | |
const maxEntriesPerPage = 200; | |
let page = 1; | |
const results: BaseRegistryEntryResponseItem[][] = []; | |
baseRegistryEntriesApiUrl.searchParams.set( | |
BASE_REGISTRY_API_ENTRIES_QUERY_PARAMS.limit, | |
maxEntriesPerPage.toString() | |
); | |
let lastBaseEntriesBatch = []; | |
do { | |
lastBaseEntriesBatch = await getBaseRegistryEntries({ | |
urlInstance: baseRegistryEntriesApiUrl, | |
pageNumber: page, | |
}); | |
results.push(...lastBaseEntriesBatch); | |
page++; | |
} while (lastBaseEntriesBatch.length >= maxEntriesPerPage); | |
return results; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment