Skip to content

Commit

Permalink
fix: prioritize mintscan explorer links [FRE-585]
Browse files Browse the repository at this point in the history
  • Loading branch information
grikomsn committed Jan 30, 2024
1 parent 16e656a commit 25c9306
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/pages/api/explorer/[chainId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,50 @@ import { NextRequest } from "next/server";

import { explorersRecord } from "@/chains/explorers";
import { ExplorerResponse } from "@/schemas/api";
import { raise } from "@/utils/assert";

export const config: PageConfig = {
runtime: "edge",
};

export default async function handler(req: NextRequest) {
let baseUrl: string | undefined;
const chainId = req.nextUrl.searchParams.get("chainId") || "";

const parsedIntId = parseInt(chainId);
const isEvmChain = typeof parsedIntId === "number" && !isNaN(parsedIntId);

if (isEvmChain) {
const { EVM_CHAINS } = await import("@/constants/wagmi");
const chain = EVM_CHAINS.find((chain) => chain.id === parseInt(chainId));
if (chain?.blockExplorers?.default.url) {
baseUrl = chain.blockExplorers!.default.url;
try {
let baseUrl: string | undefined;
const chainId = req.nextUrl.searchParams.get("chainId") || raise("/api/explorer error: 'chainId' is required");

const parsedIntId = parseInt(chainId);
const isEvmChain = typeof parsedIntId === "number" && !isNaN(parsedIntId);

if (isEvmChain) {
const { EVM_CHAINS } = await import("@/constants/wagmi");
const chain = EVM_CHAINS.find((chain) => chain.id === parseInt(chainId));
if (chain?.blockExplorers?.default.url) {
baseUrl = chain.blockExplorers!.default.url;
}
} else {
const explorers = explorersRecord[chainId] || [];

baseUrl ||= explorers.find((explorer) => explorer.kind === "mintscan")?.tx_page;
baseUrl ||= explorers[0]?.tx_page;
}
}

const explorers = explorersRecord[chainId] || [];

const mintscan = explorers.find((explorer) => explorer.kind === "mintscan");
if (mintscan && mintscan.tx_page) {
baseUrl = mintscan.tx_page;
}

if (explorers[0]?.tx_page) {
baseUrl = explorers[0].tx_page;
}
if (!baseUrl) {
return new Response(null, { status: 404 }); // Not Found
}

if (!baseUrl) {
return new Response(null, { status: 404 }); // Not Found
const payload: ExplorerResponse = {
evm: isEvmChain,
explorer: baseUrl,
};

return new Response(JSON.stringify(payload), {
headers: {
"cache-control": "public, max-age=604800, immutable", // 1 week
"content-type": "application/json",
},
});
} catch (error) {
console.error(error);
return new Response(null, { status: 500 }); // Internal Server Error
}

const payload: ExplorerResponse = {
evm: isEvmChain,
explorer: baseUrl,
};

return new Response(JSON.stringify(payload), {
headers: {
"cache-control": "public, max-age=604800, immutable", // 1 week
"content-type": "application/json",
},
});
}

0 comments on commit 25c9306

Please sign in to comment.