Skip to content

Commit

Permalink
[Chore] Remove unused 'amount' field from FaucetButton component and …
Browse files Browse the repository at this point in the history
…claim route (#4090)

## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## PR-Codex overview
The focus of this PR is to update the testnet faucet claim route to fix issues related to the amount handling.

### Detailed summary
- Removed `amount` from request payload
- Updated amount handling in the faucet claim route to always transfer 0.01
- Improved error handling and response consistency

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}`

<!-- end pr-codex -->
  • Loading branch information
jnsdls committed Aug 13, 2024
1 parent 9d3fa73 commit 814024a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export function FaucetButton({
body: JSON.stringify({
chainId: chainId,
toAddress: address,
amount,
}),
});

Expand Down
68 changes: 29 additions & 39 deletions apps/dashboard/src/app/api/testnet-faucet/claim/route.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { getIpAddress } from "lib/ip";
import { cacheDeleteKey, cacheExists, cacheSet } from "lib/redis";
import { cacheExists, cacheSet } from "lib/redis";
import { NextResponse } from "next/server";
import { ZERO_ADDRESS } from "thirdweb";

interface RequestTestnetFundsPayload {
chainId: number;
toAddress: string;
amount: string;
}

// Note: This handler cannot use "edge" runtime because of Redis usage.
export const POST = async (req: Request) => {
const requestBody = (await req.json()) as RequestTestnetFundsPayload;
const { chainId, toAddress, amount } = requestBody;
const { chainId, toAddress } = requestBody;
if (Number.isNaN(chainId)) {
throw new Error("Invalid chain ID.");
}
Expand Down Expand Up @@ -44,44 +43,35 @@ export const POST = async (req: Request) => {
);
}

// Assert max amount.
if (Number.parseFloat(amount) > 0.01) {
return NextResponse.json({ error: "Invalid amount." }, { status: 400 });
}

// Store the claim request for 24 hours.
await cacheSet(cacheKey, "claimed", 24 * 60 * 60);

if (amount !== "0") {
try {
const url = `${THIRDWEB_ENGINE_URL}/backend-wallet/${chainId}/transfer`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-backend-wallet-address": NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET,
Authorization: `Bearer ${THIRDWEB_ACCESS_TOKEN}`,
},
body: JSON.stringify({
to: toAddress,
currencyAddress: ZERO_ADDRESS,
amount,
}),
});
try {
// Store the claim request for 24 hours.
await cacheSet(cacheKey, "claimed", 24 * 60 * 60);
// then actually transfer the funds
const url = `${THIRDWEB_ENGINE_URL}/backend-wallet/${chainId}/transfer`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-backend-wallet-address": NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET,
Authorization: `Bearer ${THIRDWEB_ACCESS_TOKEN}`,
},
body: JSON.stringify({
to: toAddress,
currencyAddress: ZERO_ADDRESS,
amount: "0.01",
}),
});

if (!response.ok) {
const error = await response.json();
throw error.error;
}
} catch (error) {
// Remove user from cache for retry on transfer failure.
cacheDeleteKey(cacheKey);
return NextResponse.json(
{ error: `${(error as Error)?.message}` },
{ status: 500 },
);
if (!response.ok) {
const error = await response.json();
throw error.error;
}
} catch (error) {
return NextResponse.json(
{ error: `${(error as Error)?.message}` },
{ status: 500 },
);
}

return NextResponse.json({ amount: amount.toString() }, { status: 200 });
return NextResponse.json({ amount: "0.01" }, { status: 200 });
};

0 comments on commit 814024a

Please sign in to comment.