Skip to content

Commit 4a65b81

Browse files
committed
Update vars
1 parent 3364960 commit 4a65b81

File tree

6 files changed

+205
-142
lines changed

6 files changed

+205
-142
lines changed
Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { Engine } from "@thirdweb-dev/engine";
22
import * 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

56
dotenv.config();
67

78
const 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

1011
console.log("Environment Variables:");
1112
console.log("CHAIN_ID:", CHAIN_ID);
1213
console.log("BACKEND_WALLET_ADDRESS:", BACKEND_WALLET_ADDRESS);
1314
console.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

1620
const 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

2125
interface MintResult {
@@ -27,7 +31,7 @@ interface MintResult {
2731
toAddress: string;
2832
amount: string;
2933
chainId: number;
30-
network: 'Base Sep';
34+
network: "Base Sep";
3135
}
3236

3337
export async function POST(req: NextRequest) {
@@ -39,15 +43,12 @@ export async function POST(req: NextRequest) {
3943
if (!Array.isArray(data)) {
4044
return NextResponse.json(
4145
{ error: "Invalid data format" },
42-
{ status: 400 }
46+
{ status: 400 },
4347
);
4448
}
4549

4650
if (data.length === 0) {
47-
return NextResponse.json(
48-
{ error: "Empty data array" },
49-
{ status: 400 }
50-
);
51+
return NextResponse.json({ error: "Empty data array" }, { status: 400 });
5152
}
5253

5354
console.log(`Attempting to mint batch to ${data.length} receivers`);
@@ -58,31 +59,42 @@ export async function POST(req: NextRequest) {
5859
contractAddress,
5960
BACKEND_WALLET_ADDRESS,
6061
{
61-
data: data.map(item => ({
62+
data: data.map((item) => ({
6263
toAddress: item.toAddress,
6364
amount: item.amount,
6465
})),
65-
}
66+
},
6667
);
6768

6869
console.log("Mint batch initiated, queue ID:", res.result.queueId);
6970
const result = await pollToMine(res.result.queueId, data[0]);
7071
return NextResponse.json([result]);
7172
} catch (error: unknown) {
7273
console.error("Error minting ERC20 tokens", error);
73-
return NextResponse.json([{
74-
queueId: "",
75-
status: "error",
76-
errorMessage: error instanceof Error ? error.message : "An unknown error occurred",
77-
toAddress: "",
78-
amount: "",
79-
chainId: parseInt(CHAIN_ID),
80-
network: "Base Sep"
81-
}], { status: 500 });
74+
return NextResponse.json(
75+
[
76+
{
77+
queueId: "",
78+
status: "error",
79+
errorMessage:
80+
error instanceof Error
81+
? error.message
82+
: "An unknown error occurred",
83+
toAddress: "",
84+
amount: "",
85+
chainId: Number.parseInt(CHAIN_ID),
86+
network: "Base Sep",
87+
},
88+
],
89+
{ status: 500 },
90+
);
8291
}
8392
}
8493

85-
async function pollToMine(queueId: string, firstItem: { toAddress: string, amount: string }): Promise<MintResult> {
94+
async function pollToMine(
95+
queueId: string,
96+
firstItem: { toAddress: string; amount: string },
97+
): Promise<MintResult> {
8698
let attempts = 0;
8799
const maxAttempts = 10;
88100

@@ -91,7 +103,10 @@ async function pollToMine(queueId: string, firstItem: { toAddress: string, amoun
91103
const status = await engine.transaction.status(queueId);
92104

93105
if (status.result.status === "mined") {
94-
console.log("Transaction mined! 🥳 ERC20 tokens have been minted", queueId);
106+
console.log(
107+
"Transaction mined! 🥳 ERC20 tokens have been minted",
108+
queueId,
109+
);
95110
const transactionHash = status.result.transactionHash;
96111
const blockExplorerUrl = `https://base-sepolia.blockscout.com/tx/${transactionHash}`;
97112
console.log("View transaction on the blockexplorer:", blockExplorerUrl);
@@ -102,10 +117,12 @@ async function pollToMine(queueId: string, firstItem: { toAddress: string, amoun
102117
blockExplorerUrl: blockExplorerUrl,
103118
toAddress: firstItem.toAddress,
104119
amount: firstItem.amount,
105-
chainId: parseInt(CHAIN_ID),
106-
network: "Base Sep"
120+
chainId: Number.parseInt(CHAIN_ID),
121+
network: "Base Sep",
107122
};
108-
} else if (status.result.status === "errored") {
123+
}
124+
125+
if (status.result.status === "errored") {
109126
console.error("Mint failed", queueId);
110127
console.error(status.result.errorMessage);
111128
return {
@@ -114,8 +131,8 @@ async function pollToMine(queueId: string, firstItem: { toAddress: string, amoun
114131
errorMessage: status.result.errorMessage ?? "Unknown error occurred",
115132
toAddress: firstItem.toAddress,
116133
amount: firstItem.amount,
117-
chainId: parseInt(CHAIN_ID),
118-
network: "Base Sep"
134+
chainId: Number.parseInt(CHAIN_ID),
135+
network: "Base Sep",
119136
};
120137
}
121138
} catch (error) {
@@ -132,7 +149,7 @@ async function pollToMine(queueId: string, firstItem: { toAddress: string, amoun
132149
errorMessage: "Transaction did not mine within the expected time",
133150
toAddress: firstItem.toAddress,
134151
amount: firstItem.amount,
135-
chainId: parseInt(CHAIN_ID),
136-
network: "Base Sep"
152+
chainId: Number.parseInt(CHAIN_ID),
153+
network: "Base Sep",
137154
};
138-
}
155+
}

apps/playground-web/src/app/api/claimTo/route.ts

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { Engine } from "@thirdweb-dev/engine";
22
import * 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

56
dotenv.config();
67

78
const 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

1011
console.log("Environment Variables:");
1112
console.log("CHAIN_ID:", BASESEP_CHAIN_ID);
1213
console.log("BACKEND_WALLET_ADDRESS:", BACKEND_WALLET_ADDRESS);
1314
console.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

1620
const 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

2125
type 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
3943
async 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(
8589
export 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(/^0x[a-fA-F0-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

127131
function 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
195205
export 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

Comments
 (0)