-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate BuildersCheckInCount to Server Component
- Remove client-side hooks in favor of viem's createPublicClient - Implement contract reading directly on server - Eliminate loading states thanks to SSR - Reduce client-side JavaScript bundle
- Loading branch information
1 parent
9a7e113
commit 9fb74d4
Showing
3 changed files
with
40 additions
and
48 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { CounterDisplay } from "./CounterDisplay"; | ||
import { useScaffoldReadContract } from "~~/hooks/scaffold-eth"; | ||
import { createPublicClient, getContract, http } from "viem"; | ||
import { optimism } from "viem/chains"; | ||
|
||
const CONTRACT_NAME = "BatchRegistry"; | ||
const CONTRACT_ADR = "0x72ccAbE6620fa94eC69569d17f18a3914BEFBFD6"; | ||
const FUNCTION_NAME = "checkedInCounter"; | ||
|
||
function BuildersCheckInCount() { | ||
// Get the result of the target function call from the deployed contract | ||
const { | ||
data: counter, | ||
isLoading, | ||
error, | ||
} = useScaffoldReadContract({ | ||
contractName: CONTRACT_NAME, | ||
functionName: FUNCTION_NAME, | ||
}); | ||
// Defining the ABI for just the function we need | ||
const BatchRegistryABI = [ | ||
{ | ||
name: FUNCTION_NAME, | ||
type: "function", | ||
stateMutability: "view", | ||
inputs: [], | ||
outputs: [{ type: "uint256" }], | ||
}, | ||
] as const; | ||
|
||
async function BuildersCheckInCount() { | ||
try { | ||
// A Client in the context of viem is similar to an Ethers.js Provider. | ||
const publicClient = createPublicClient({ | ||
chain: optimism, | ||
transport: http(), | ||
}); | ||
|
||
const contract = getContract({ | ||
address: CONTRACT_ADR, | ||
abi: BatchRegistryABI, | ||
client: publicClient, | ||
}); | ||
|
||
const counter = await contract.read.checkedInCounter(); | ||
|
||
return <CounterDisplay data={counter} isLoading={isLoading} error={error} />; | ||
return <CounterDisplay data={counter} error={null} />; | ||
} catch (error) { | ||
return <CounterDisplay data={undefined} error={error instanceof Error ? error : new Error("Unknown error")} />; | ||
} | ||
} | ||
|
||
export default BuildersCheckInCount; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,11 @@ | ||
import { displayTxResult } from "../debug/_components/contract"; | ||
import { ArrowPathIcon } from "@heroicons/react/16/solid"; | ||
|
||
export async function CounterDisplay({ | ||
data: counter, | ||
isLoading, | ||
error, | ||
}: { | ||
data?: bigint; | ||
isLoading: boolean; | ||
error: Error | null; | ||
}) { | ||
export async function CounterDisplay({ data: counter, error }: { data?: bigint; error: Error | null }) { | ||
return ( | ||
<div className="text-lg flex gap-2 justify-center items-center"> | ||
<span className="font-bold">Checked in builders count:</span> | ||
{isLoading || (!error && !counter) ? ( | ||
<ArrowPathIcon className="size-5 animate-spin" /> | ||
) : error ? ( | ||
<span className="text-error">Error!</span> | ||
) : ( | ||
<span>{displayTxResult(counter)}</span> | ||
)} | ||
|
||
{error && <span className="text-error">Error!</span>} | ||
|
||
{counter && <span>{counter.toString()}</span>} | ||
</div> | ||
); | ||
} |
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