11import { Engine } from "@thirdweb-dev/engine" ;
22import * as dotenv from "dotenv" ;
3- import { NextRequest , NextResponse } from "next/server" ;
3+ import type { NextRequest } from "next/server" ;
4+ import { NextResponse } from "next/server" ;
45
56dotenv . config ( ) ;
67
78const BASESEP_CHAIN_ID = "84532" ;
8- const BACKEND_WALLET_ADDRESS = process . env . BACKEND_WALLET as string ;
9+ const BACKEND_WALLET_ADDRESS = process . env . ENGINE_BACKEND_WALLET as string ;
910
1011console . log ( "Environment Variables:" ) ;
1112console . log ( "CHAIN_ID:" , BASESEP_CHAIN_ID ) ;
1213console . log ( "BACKEND_WALLET_ADDRESS:" , BACKEND_WALLET_ADDRESS ) ;
1314console . log ( "ENGINE_URL:" , process . env . ENGINE_URL ) ;
14- console . log ( "ACCESS_TOKEN:" , process . env . ACCESS_TOKEN ? "Set" : "Not Set" ) ;
15+ console . log (
16+ "ACCESS_TOKEN:" ,
17+ process . env . ENGINE_ACCESS_TOKEN ? "Set" : "Not Set" ,
18+ ) ;
1519
1620const engine = new Engine ( {
1721 url : process . env . ENGINE_URL as string ,
18- accessToken : process . env . ACCESS_TOKEN as string ,
22+ accessToken : process . env . ENGINE_ACCESS_TOKEN as string ,
1923} ) ;
2024
2125type TransactionStatus = "Queued" | "Sent" | "Mined" | "error" ;
@@ -37,12 +41,12 @@ const pollingProcesses = new Map<string, NodeJS.Timeout>();
3741
3842// Helper function to make a single claim
3943async function makeClaimRequest (
40- chainId : string ,
41- contractAddress : string ,
42- data : {
43- recipient : string ,
44- quantity : number
45- }
44+ chainId : string ,
45+ contractAddress : string ,
46+ data : {
47+ recipient : string ;
48+ quantity : number ;
49+ } ,
4650) : Promise < ClaimResult > {
4751 try {
4852 // Validate the recipient address format
@@ -62,7 +66,7 @@ async function makeClaimRequest(
6266 maxFeePerGas : "1000000000" ,
6367 maxPriorityFeePerGas : "1000000000" ,
6468 } ,
65- }
69+ } ,
6670 ) ;
6771
6872 const initialResponse : ClaimResult = {
@@ -85,19 +89,19 @@ async function makeClaimRequest(
8589export async function POST ( req : NextRequest ) {
8690 try {
8791 const body = await req . json ( ) ;
88-
92+
8993 if ( ! body . receiver || ! body . quantity || ! body . contractAddress ) {
9094 return NextResponse . json (
9195 { error : "Missing receiver, quantity, or contract address" } ,
92- { status : 400 }
96+ { status : 400 } ,
9397 ) ;
9498 }
9599
96100 // Validate contract address format
97101 if ( ! body . contractAddress . match ( / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / ) ) {
98102 return NextResponse . json (
99103 { error : "Invalid contract address format" } ,
100- { status : 400 }
104+ { status : 400 } ,
101105 ) ;
102106 }
103107
@@ -106,28 +110,28 @@ export async function POST(req: NextRequest) {
106110 body . contractAddress ,
107111 {
108112 recipient : body . receiver ,
109- quantity : parseInt ( body . quantity )
110- }
113+ quantity : Number . parseInt ( body . quantity ) ,
114+ } ,
111115 ) ;
112116
113117 return NextResponse . json ( { result } ) ;
114-
115118 } catch ( error ) {
116119 console . error ( "API Error:" , error ) ;
117120 return NextResponse . json (
118- {
119- error : error instanceof Error ? error . message : "Unknown error occurred" ,
120- details : error instanceof Error ? error . stack : undefined
121+ {
122+ error :
123+ error instanceof Error ? error . message : "Unknown error occurred" ,
124+ details : error instanceof Error ? error . stack : undefined ,
121125 } ,
122- { status : 400 }
126+ { status : 400 } ,
123127 ) ;
124128 }
125129}
126130
127131function startPolling ( queueId : string ) {
128132 const maxPollingTime = 5 * 60 * 1000 ; // 5 minutes timeout
129133 const startTime = Date . now ( ) ;
130-
134+
131135 const pollingInterval = setInterval ( async ( ) => {
132136 try {
133137 // Check if we've exceeded the maximum polling time
@@ -166,18 +170,24 @@ async function pollToMine(queueId: string): Promise<ClaimResult> {
166170 case "sent" :
167171 console . log ( "Transaction is submitted to the network" ) ;
168172 return { queueId, status : "Sent" } ;
169- case "mined" :
170- console . log ( "Transaction mined! 🥳 ERC721 token has been claimed" , queueId ) ;
173+ case "mined" : {
174+ console . log (
175+ "Transaction mined! 🥳 ERC721 token has been claimed" ,
176+ queueId ,
177+ ) ;
171178 const transactionHash = status . result . transactionHash ;
172- const blockExplorerUrl = status . result . chainId === BASESEP_CHAIN_ID ?
173- `https://base-sepolia.blockscout.com/tx/${ transactionHash } ` : '' ;
179+ const blockExplorerUrl =
180+ status . result . chainId === BASESEP_CHAIN_ID
181+ ? `https://base-sepolia.blockscout.com/tx/${ transactionHash } `
182+ : "" ;
174183 console . log ( "View transaction on the blockexplorer:" , blockExplorerUrl ) ;
175184 return {
176185 queueId,
177186 status : "Mined" ,
178187 transactionHash : transactionHash ?? undefined ,
179188 blockExplorerUrl : blockExplorerUrl ,
180189 } ;
190+ }
181191 case "errored" :
182192 console . error ( "Claim failed" , queueId ) ;
183193 console . error ( status . result . errorMessage ) ;
@@ -194,7 +204,7 @@ async function pollToMine(queueId: string): Promise<ClaimResult> {
194204// Add a new endpoint to check the status
195205export async function GET ( req : NextRequest ) {
196206 const { searchParams } = new URL ( req . url ) ;
197- const queueId = searchParams . get ( ' queueId' ) ;
207+ const queueId = searchParams . get ( " queueId" ) ;
198208
199209 if ( ! queueId ) {
200210 return NextResponse . json ( { error : "Missing queueId" } , { status : 400 } ) ;
@@ -206,11 +216,11 @@ export async function GET(req: NextRequest) {
206216 } catch ( error ) {
207217 console . error ( "Error checking transaction status:" , error ) ;
208218 return NextResponse . json (
209- {
210- status : "error" as TransactionStatus ,
211- error : "Failed to check transaction status"
212- } ,
213- { status : 500 }
219+ {
220+ status : "error" as TransactionStatus ,
221+ error : "Failed to check transaction status" ,
222+ } ,
223+ { status : 500 } ,
214224 ) ;
215225 }
216- }
226+ }
0 commit comments