diff --git a/app/api/contributions/validate/route.ts b/app/api/contributions/validate/route.ts new file mode 100644 index 00000000..3a58897d --- /dev/null +++ b/app/api/contributions/validate/route.ts @@ -0,0 +1,43 @@ +import { type NextRequest, NextResponse } from "next/server"; +import { http, createPublicClient } from "viem"; +import { sepolia } from "viem/chains"; + +export async function POST(req: NextRequest) { + try { + const { txId } = await req.json(); + if (!txId) { + return NextResponse.json( + { error: "Missing required fields" }, + { status: 400 }, + ); + } + + const viemClient = createPublicClient({ + chain: sepolia, + transport: http(), + }); + + // wait for the transaction to be included in a block + console.log( + `[Viem] waiting for tx ${txId} to be included in a block . . .`, + ); + const txReceipt = await viemClient.waitForTransactionReceipt({ + hash: txId, + }); + console.log(`[Viem] tx ${txId} included in block ${txReceipt.blockNumber}`); + + if (txReceipt.status === "reverted") { + console.log(`[Viem] tx ${txId} reverted`); + return NextResponse.json({ txStatus: "reverted" }, { status: 200 }); + } + return NextResponse.json({ txStatus: "ok" }, { status: 200 }); + } catch (error) { + let errorMessage = "An unknown error occurred"; + if (typeof error === "object" && error !== null) { + errorMessage = (error as { message?: string }).message ?? errorMessage; + } else if (typeof error === "string") { + errorMessage = error; + } + return NextResponse.json({ error: errorMessage }, { status: 500 }); + } +}