Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(counter): Add builders check-in count implementation #18

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions packages/nextjs/app/_components/BuildersCheckInCount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { CounterDisplay } from "./CounterDisplay";
import { createPublicClient, getContract, http } from "viem";
import { optimism } from "viem/chains";

const CONTRACT_ADR = "0x72ccAbE6620fa94eC69569d17f18a3914BEFBFD6";
const FUNCTION_NAME = "checkedInCounter";

// 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} error={null} />;
} catch (error) {
return <CounterDisplay data={undefined} error={error instanceof Error ? error : new Error("Unknown error")} />;
}
}

export default BuildersCheckInCount;
11 changes: 11 additions & 0 deletions packages/nextjs/app/_components/CounterDisplay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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>

{error && <span className="text-error">Error!</span>}

{counter && <span>{counter.toString()}</span>}
</div>
);
}
8 changes: 2 additions & 6 deletions packages/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";

import Link from "next/link";
import BuildersCheckInCount from "./_components/BuildersCheckInCount";
import type { NextPage } from "next";
import { BugAntIcon, MagnifyingGlassIcon } from "@heroicons/react/24/outline";

Expand All @@ -14,10 +13,7 @@ const Home: NextPage = () => {
<span className="block text-4xl font-bold">Batch 12</span>
</h1>
<p className="text-center text-lg">Get started by taking a look at your batch GitHub repository.</p>
<p className="text-lg flex gap-2 justify-center">
<span className="font-bold">Checked in builders count:</span>
<span>To Be Implemented</span>
</p>
<BuildersCheckInCount />
</div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the way you factored out the logic to keep things organized 👌


<div className="flex-grow bg-base-300 w-full mt-16 px-8 py-12">
Expand Down
Loading