|
| 1 | +import { Address, createPublicClient, http } from "viem"; |
| 2 | +import { abi as abi_queue_handler } from "../../../backend/ignition/deployments/chain-11155111/artifacts/QueueHandlerModule#QueueHandler.json" |
| 3 | +import deployed_addresses from "../../../backend/ignition/deployments/chain-11155111/deployed_addresses.json" |
| 4 | +import LoadingIndicator from "@/components/LoadingIndicator"; |
| 5 | +import { sepolia } from "viem/chains"; |
| 6 | +import { useEffect, useState } from "react"; |
| 7 | +import Link from "next/link"; |
| 8 | + |
| 9 | +export default function PairedSponsorship({ distributionQueueNumber }: { distributionQueueNumber: number }) { |
| 10 | + console.debug("PairedSponsorship") |
| 11 | + |
| 12 | + console.debug("distributionQueueNumber:", distributionQueueNumber) |
| 13 | + |
| 14 | + const deploymentAddress: Address = deployed_addresses["QueueHandlerModule#QueueHandler"] as `0x${string}` |
| 15 | + console.debug("deploymentAddress:", deploymentAddress) |
| 16 | + |
| 17 | + const publicClient = createPublicClient({ |
| 18 | + chain: sepolia, |
| 19 | + transport: http("https://0xrpc.io/sep") |
| 20 | + }) |
| 21 | + |
| 22 | + const [events, setEvents] = useState(Array(0)) |
| 23 | + useEffect(() => { |
| 24 | + async function fetchContractEvents() { |
| 25 | + const logs = await publicClient.getContractEvents({ |
| 26 | + abi: abi_queue_handler, |
| 27 | + address: deploymentAddress, |
| 28 | + fromBlock: BigInt(9_760_219), // https://sepolia.etherscan.io/tx/0x01ea5a02fc320619f391757c038666722837e908c5094bef55b8a16824fb96e5 |
| 29 | + eventName: "QueuePairProcessed" |
| 30 | + }) |
| 31 | + console.debug("logs:", logs) |
| 32 | + setEvents(logs) |
| 33 | + } |
| 34 | + fetchContractEvents() |
| 35 | + }, []) |
| 36 | + console.debug("events.length:", events.length) |
| 37 | + console.debug("events:", events) |
| 38 | + |
| 39 | + if (events.length == 0) { |
| 40 | + return <LoadingIndicator /> |
| 41 | + } |
| 42 | + |
| 43 | + // Iterate processed queue pairs and look for matching queue number |
| 44 | + const matchingEvent = events.find( |
| 45 | + event => event.args.distributionQueueNumber == distributionQueueNumber |
| 46 | + ) |
| 47 | + const sponsorshipQueueNumber = matchingEvent?.args.sponsorshipQueueNumber |
| 48 | + console.debug("sponsorshipQueueNumber:", sponsorshipQueueNumber) |
| 49 | + |
| 50 | + if (!sponsorshipQueueNumber) { |
| 51 | + return ( |
| 52 | + <span className="p-2 text-sm text-gray-300 border-gray-400 bg-gray-600 border-2 rounded-xl"> |
| 53 | + Pairing not yet <Link href="/process" className="text-purple-300" id="processLink">processed</Link> |
| 54 | + </span> |
| 55 | + ) |
| 56 | + } else { |
| 57 | + return ( |
| 58 | + <Link href={`/sponsorships/${sponsorshipQueueNumber}`} className="text-indigo-600"> |
| 59 | + <div className="skew-y-3 p-4 text-2xl bg-indigo-200 dark:bg-indigo-950 rounded-lg border-indigo-400 border-r-4 border-b-4 hover:border-r-8 hover:border-b-8 hover:-translate-y-1"> |
| 60 | + <div className="mb-4 text-center text-4xl"> |
| 61 | + 🛵💨 |
| 62 | + </div> |
| 63 | + Sponsorship #{sponsorshipQueueNumber} |
| 64 | + </div> |
| 65 | + </Link> |
| 66 | + ) |
| 67 | + } |
| 68 | +} |
0 commit comments