|
1 | 1 | /* eslint-disable @next/next/no-img-element */ |
2 | 2 | import { cn } from "@/lib/utils"; |
3 | 3 | import type { ChainMetadata } from "thirdweb/chains"; |
4 | | -import { getBaseUrl } from "../../lib/getBaseUrl"; |
| 4 | + |
| 5 | +type ApiResponseType = { |
| 6 | + data: Record<string, { service: string; enabled: boolean }[]>; |
| 7 | +}; |
5 | 8 |
|
6 | 9 | async function getChains(): Promise<ChainMetadata[]> { |
7 | 10 | try { |
8 | | - const chains = await fetch(`${getBaseUrl()}/api/aa-chains`); |
9 | | - if (!chains.ok) { |
10 | | - return []; |
11 | | - } |
12 | | - const result = (await chains.json()) as { data: ChainMetadata[] }; |
13 | | - return result.data; |
| 11 | + const [chainsWithServices, allChains] = await Promise.all([ |
| 12 | + fetch("https://api.thirdweb.com/v1/chains/services", { |
| 13 | + headers: { |
| 14 | + "Content-Type": "application/json", |
| 15 | + }, |
| 16 | + }).then((res) => res.json() as Promise<ApiResponseType>), |
| 17 | + fetch("https://api.thirdweb.com/v1/chains", { |
| 18 | + headers: { |
| 19 | + "Content-Type": "application/json", |
| 20 | + }, |
| 21 | + }).then((res) => res.json() as Promise<{ data: ChainMetadata[] }>), |
| 22 | + ]); |
| 23 | + |
| 24 | + const aaChains = Object.entries(chainsWithServices.data) |
| 25 | + .filter(([, services]) => |
| 26 | + services.some( |
| 27 | + (service) => |
| 28 | + service.service === "account-abstraction" && service.enabled, |
| 29 | + ), |
| 30 | + ) |
| 31 | + .map(([chainId]) => Number(chainId)); |
| 32 | + |
| 33 | + const intersectedChains = allChains.data |
| 34 | + .filter((chain) => |
| 35 | + aaChains.some((aaChainId) => aaChainId === chain.chainId), |
| 36 | + ) |
| 37 | + .filter((c) => c.name) |
| 38 | + .sort((a, b) => a.name.localeCompare(b.name)); |
| 39 | + return intersectedChains; |
14 | 40 | } catch (error) { |
15 | | - console.error(error); |
16 | | - return []; |
| 41 | + console.error("Failed to fetch chains", error); |
| 42 | + throw error; |
17 | 43 | } |
18 | 44 | } |
19 | 45 |
|
|
0 commit comments