Skip to content

Commit

Permalink
Added caching
Browse files Browse the repository at this point in the history
  • Loading branch information
mvpoyatt authored and mvpoyatt committed Oct 23, 2024
1 parent add6c14 commit 9a10eb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
9 changes: 8 additions & 1 deletion app/api/registry/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
import { unstable_cache } from 'next/cache';
import { getChainId } from 'utils/chains/id-maps';
import { fetchRegistry } from './fetch-registry';

Expand Down Expand Up @@ -31,7 +32,7 @@ export async function GET(request: NextRequest) {
}

async function getExplorerUrl(chainId: number) {
const registry = await fetchRegistry();
const registry = await getRegistryWithCache();

let explorerUrl = '';
const chainData = registry[chainId];
Expand All @@ -45,3 +46,9 @@ async function getExplorerUrl(chainId: number) {

return NextResponse.json({ explorerUrl }, { status: 200 });
}

const getRegistryWithCache = unstable_cache(
async () => fetchRegistry(),
['registry'],
{ revalidate: 180 }
)
21 changes: 10 additions & 11 deletions app/utils/chains/id-maps.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
export const getChainId = (chainName: string, env: string): number => {
chainName = chainName.toLowerCase();

if (env === 'mainnet') {
if (!(chainName in mainnetIdMap)) {
throw new Error(`Invalid chain name: ${chainName}`);
}
return mainnetIdMap[chainName as ChainName];
let chainMap: Record<string, number>;
if (env === 'sepolia') {
chainMap = sepoliaIdMap;
} else if (env === 'mainnet') {
chainMap = mainnetIdMap;
} else {
throw new Error(`Invalid environment: ${env}`);
}

else if (env === 'sepolia') {
if (!(chainName in sepoliaIdMap)) {
throw new Error(`Invalid chain name: ${chainName}`);
}
return sepoliaIdMap[chainName as ChainName];
if (!(chainName in chainMap)) {
throw new Error(`Invalid chain name: ${chainName}`);
}

throw new Error(`Invalid environment: ${env}`);
return chainMap[chainName as ChainName];
}

export type ChainName = 'optimism' | 'base';
Expand Down

0 comments on commit 9a10eb8

Please sign in to comment.