Skip to content

Commit 720b5ff

Browse files
committed
feat(profile): render warning banner on the server
This caches the contract check on the server, so that our users don't have to run the same checks individually.
1 parent 63ef9fd commit 720b5ff

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

components/profile/contract-accounts-banner.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
"use client";
1+
import { isContract } from "@/lib/isContract";
22

3-
import { useIsContract } from "@/hooks/useIsContract";
3+
export async function ContractAccountBanner({ address }: { address: string }) {
4+
const isContractAddress = await isContract(address);
45

5-
export function ContractAccountBanner({ address }: { address: string }) {
6-
const { isContract, isLoading } = useIsContract(address);
7-
8-
if (!isContract || isLoading) return null;
6+
if (!isContractAddress) return null;
97

108
return (
119
<div className="bg-yellow-50 border-l-4 border-yellow-400 p-4 mb-4">

lib/isContract.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { ChainFactory } from "./chainFactory";
2+
import { EvmClientFactory } from "./evmClient";
3+
import { unstable_cache } from "next/cache";
4+
5+
export const isContract = unstable_cache(
6+
async (address: string) => {
7+
const supportedChains = ChainFactory.getSupportedChains();
8+
const clients = supportedChains.map((chainId) =>
9+
EvmClientFactory.createClient(chainId),
10+
);
11+
12+
const results = await Promise.allSettled(
13+
clients.map((client) =>
14+
client.getCode({ address: address as `0x${string}` }),
15+
),
16+
);
17+
18+
return results.some(
19+
(result) =>
20+
result.status === "fulfilled" &&
21+
result.value !== undefined &&
22+
result.value !== "0x",
23+
);
24+
},
25+
["isContract"],
26+
{ revalidate: 604800 }, // 1 week
27+
);

0 commit comments

Comments
 (0)