-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 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 |
---|---|---|
@@ -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 }); | ||
} | ||
} |