Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
baumstern committed Mar 28, 2024
1 parent 60a4feb commit 04f7942
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions app/api/contributions/validate/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}

0 comments on commit 04f7942

Please sign in to comment.