Skip to content

Commit 225ed27

Browse files
committed
update
1 parent f40d247 commit 225ed27

File tree

7 files changed

+183
-38
lines changed

7 files changed

+183
-38
lines changed

apps/dashboard/.env.example

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,5 @@ API_SERVER_SECRET=""
9191

9292
# Used for the Faucet page (/<chain_id>)
9393
NEXT_PUBLIC_TURNSTILE_SITE_KEY=""
94-
TURNSTILE_SECRET_KEY=""
94+
TURNSTILE_SECRET_KEY=""
95+
REDIS_URL=""

apps/dashboard/src/@/constants/env.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ export const IPFS_GATEWAY_URL =
88
(process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL as string) ||
99
"https://{clientId}.ipfscdn.io/ipfs/{cid}/{path}";
1010

11-
export const THIRDWEB_ENGINE_FAUCET_WALLET =
12-
process.env.NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET || "";
13-
1411
export const isProd =
1512
(process.env.VERCEL_ENV || process.env.NEXT_PUBLIC_VERCEL_ENV) ===
1613
"production";
@@ -24,5 +21,15 @@ export const DASHBOARD_STORAGE_URL =
2421
export const API_SERVER_URL =
2522
process.env.NEXT_PUBLIC_THIRDWEB_API_HOST || "https://api.thirdweb.com";
2623

24+
/**
25+
* Faucet stuff
26+
*/
27+
// Cloudflare Turnstile Site key
2728
export const TURNSTILE_SITE_KEY =
2829
process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY || "";
30+
export const THIRDWEB_ENGINE_FAUCET_WALLET =
31+
process.env.NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET || "";
32+
export const THIRDWEB_ENGINE_URL = process.env.THIRDWEB_ENGINE_URL;
33+
export const THIRDWEB_ACCESS_TOKEN = process.env.THIRDWEB_ACCESS_TOKEN;
34+
// Comma-separated list of chain IDs to disable faucet for.
35+
export const DISABLE_FAUCET_CHAIN_IDS = process.env.DISABLE_FAUCET_CHAIN_IDS;

apps/dashboard/src/app/(dashboard)/(chain)/[chain_id]/(chainPage)/components/client/FaucetButton.tsx

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use client";
22

3+
import { ChakraProviderSetup } from "@/components/ChakraProviderSetup";
34
import { Spinner } from "@/components/ui/Spinner/Spinner";
45
import { Button } from "@/components/ui/button";
56
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
@@ -9,11 +10,15 @@ import {
910
} from "@/constants/env";
1011
import { useThirdwebClient } from "@/constants/thirdweb.client";
1112
import { CustomConnectWallet } from "@3rdweb-sdk/react/components/connect-wallet";
13+
import { useAccount } from "@3rdweb-sdk/react/hooks/useApi";
14+
import { useLoggedInUser } from "@3rdweb-sdk/react/hooks/useLoggedInUser";
1215
import { Turnstile } from "@marsidev/react-turnstile";
1316
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
1417
import type { CanClaimResponseType } from "app/api/testnet-faucet/can-claim/CanClaimResponseType";
18+
import { Onboarding } from "components/onboarding";
1519
import { mapV4ChainToV5Chain } from "contexts/map-chains";
1620
import { useTrack } from "hooks/analytics/useTrack";
21+
import { useState } from "react";
1722
import { useForm } from "react-hook-form";
1823
import { toast } from "sonner";
1924
import { toUnits } from "thirdweb";
@@ -113,6 +118,10 @@ export function FaucetButton({
113118
},
114119
});
115120

121+
const accountQuery = useAccount();
122+
const userQuery = useLoggedInUser();
123+
const [showOnboarding, setShowOnBoarding] = useState(false);
124+
116125
const canClaimFaucetQuery = useQuery({
117126
queryKey: ["testnet-faucet-can-claim", chainId],
118127
queryFn: async () => {
@@ -133,6 +142,32 @@ export function FaucetButton({
133142

134143
const form = useForm<z.infer<typeof claimFaucetSchema>>();
135144

145+
// Force users to log in to claim the faucet
146+
if (!address || !userQuery.user) {
147+
return (
148+
<CustomConnectWallet
149+
loginRequired={true}
150+
connectButtonClassName="!w-full !rounded !bg-primary !text-primary-foreground !px-4 !py-2 !text-sm"
151+
/>
152+
);
153+
}
154+
155+
if (accountQuery.isPending) {
156+
return (
157+
<Button variant="outline" className="w-full gap-2">
158+
Loading account <Spinner className="size-3" />
159+
</Button>
160+
);
161+
}
162+
163+
if (!accountQuery.data) {
164+
return (
165+
<Button variant="outline" className="w-full gap-2" disabled>
166+
Failed to load account
167+
</Button>
168+
);
169+
}
170+
136171
// loading state
137172
if (faucetWalletBalanceQuery.isPending || canClaimFaucetQuery.isPending) {
138173
return (
@@ -145,7 +180,7 @@ export function FaucetButton({
145180
// faucet is empty
146181
if (isFaucetEmpty) {
147182
return (
148-
<Button variant="outline" disabled className="!opacity-100 w-full ">
183+
<Button variant="outline" disabled className="!opacity-100 w-full">
149184
Faucet is empty right now
150185
</Button>
151186
);
@@ -168,12 +203,24 @@ export function FaucetButton({
168203
);
169204
}
170205

171-
if (!address) {
206+
// Email verification is required to claim from the faucet
207+
if (accountQuery.data.status === "noCustomer") {
172208
return (
173-
<CustomConnectWallet
174-
loginRequired={false}
175-
connectButtonClassName="!w-full !rounded !bg-primary !text-primary-foreground !px-4 !py-2 !text-sm"
176-
/>
209+
<>
210+
<Button
211+
variant="outline"
212+
className="!opacity-100 w-full"
213+
onClick={() => setShowOnBoarding(true)}
214+
>
215+
Verify your email
216+
</Button>
217+
{/* We will show the modal only if the user click on it, because this is a public page */}
218+
{showOnboarding && (
219+
<ChakraProviderSetup>
220+
<Onboarding onOpenChange={setShowOnBoarding} />
221+
</ChakraProviderSetup>
222+
)}
223+
</>
177224
);
178225
}
179226

apps/dashboard/src/app/api/testnet-faucet/can-claim/route.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1+
import {
2+
DISABLE_FAUCET_CHAIN_IDS,
3+
THIRDWEB_ACCESS_TOKEN,
4+
THIRDWEB_ENGINE_FAUCET_WALLET,
5+
THIRDWEB_ENGINE_URL,
6+
} from "@/constants/env";
17
import { cacheTtl } from "lib/redis";
28
import { NextResponse } from "next/server";
39
import type { NextRequest } from "next/server";
410
import type { CanClaimResponseType } from "./CanClaimResponseType";
511

6-
const THIRDWEB_ENGINE_URL = process.env.THIRDWEB_ENGINE_URL;
7-
const NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET =
8-
process.env.NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET;
9-
const THIRDWEB_ACCESS_TOKEN = process.env.THIRDWEB_ACCESS_TOKEN;
10-
11-
// Comma-separated list of chain IDs to disable faucet for.
12-
const DISABLE_FAUCET_CHAIN_IDS = process.env.DISABLE_FAUCET_CHAIN_IDS;
13-
1412
// Note: This handler cannot use "edge" runtime because of Redis usage.
1513
export const GET = async (req: NextRequest) => {
1614
const searchParams = req.nextUrl.searchParams;
@@ -49,7 +47,7 @@ export const GET = async (req: NextRequest) => {
4947

5048
if (
5149
!THIRDWEB_ENGINE_URL ||
52-
!NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET ||
50+
!THIRDWEB_ENGINE_FAUCET_WALLET ||
5351
!THIRDWEB_ACCESS_TOKEN ||
5452
isFaucetDisabled
5553
) {

apps/dashboard/src/app/api/testnet-faucet/claim/route.ts

Lines changed: 86 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie";
2+
import {
3+
API_SERVER_URL,
4+
THIRDWEB_ACCESS_TOKEN,
5+
THIRDWEB_ENGINE_FAUCET_WALLET,
6+
THIRDWEB_ENGINE_URL,
7+
} from "@/constants/env";
8+
import type { Account } from "@3rdweb-sdk/react/hooks/useApi";
19
import { startOfToday } from "date-fns";
210
import { cacheGet, cacheSet } from "lib/redis";
311
import { type NextRequest, NextResponse } from "next/server";
4-
import { ZERO_ADDRESS } from "thirdweb";
12+
import { ZERO_ADDRESS, getAddress } from "thirdweb";
513
import { getFaucetClaimAmount } from "./claim-amount";
614

7-
const THIRDWEB_ENGINE_URL = process.env.THIRDWEB_ENGINE_URL;
8-
const NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET =
9-
process.env.NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET;
10-
const THIRDWEB_ACCESS_TOKEN = process.env.THIRDWEB_ACCESS_TOKEN;
11-
1215
interface RequestTestnetFundsPayload {
1316
chainId: number;
1417
toAddress: string;
@@ -17,8 +20,68 @@ interface RequestTestnetFundsPayload {
1720
turnstileToken: string;
1821
}
1922

20-
// Note: This handler cannot use "edge" runtime because of Redis usage.
23+
/**
24+
* How this endpoint works:
25+
* Only users who have signed in to thirdweb.com with an account that is email-verified can claim.
26+
* Those who satisfy the requirement above can claim once per 24 hours for every account
27+
*
28+
* Note: This handler cannot use "edge" runtime because of Redis usage.
29+
*/
2130
export const POST = async (req: NextRequest) => {
31+
// Make sure user's connected to the site
32+
const activeAccount = req.cookies.get(COOKIE_ACTIVE_ACCOUNT)?.value;
33+
34+
if (!activeAccount) {
35+
return NextResponse.json(
36+
{
37+
error: "No wallet detected",
38+
},
39+
{ status: 400 },
40+
);
41+
}
42+
const authCookieName = COOKIE_PREFIX_TOKEN + getAddress(activeAccount);
43+
44+
const authCookie = req.cookies.get(authCookieName);
45+
46+
if (!authCookie) {
47+
return NextResponse.json(
48+
{
49+
error: "No wallet connected",
50+
},
51+
{ status: 400 },
52+
);
53+
}
54+
55+
// Make sure the connected wallet has a thirdweb account
56+
const accountRes = await fetch(`${API_SERVER_URL}/v1/account/me`, {
57+
method: "GET",
58+
headers: {
59+
Authorization: `Bearer ${authCookie.value}`,
60+
},
61+
});
62+
63+
if (accountRes.status !== 200) {
64+
// Account not found on this connected address
65+
return NextResponse.json(
66+
{
67+
error: "thirdweb account not found",
68+
},
69+
{ status: 400 },
70+
);
71+
}
72+
73+
const account: { data: Account } = await accountRes.json();
74+
75+
// Make sure the logged-in account has verified its email
76+
if (account.data.status === "noCustomer") {
77+
return NextResponse.json(
78+
{
79+
error: "Account owner hasn't verified email",
80+
},
81+
{ status: 400 },
82+
);
83+
}
84+
2285
const requestBody = (await req.json()) as RequestTestnetFundsPayload;
2386
const { chainId, toAddress, turnstileToken } = requestBody;
2487
if (Number.isNaN(chainId)) {
@@ -27,7 +90,7 @@ export const POST = async (req: NextRequest) => {
2790

2891
if (
2992
!THIRDWEB_ENGINE_URL ||
30-
!NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET ||
93+
!THIRDWEB_ENGINE_FAUCET_WALLET ||
3194
!THIRDWEB_ACCESS_TOKEN
3295
) {
3396
return NextResponse.json(
@@ -88,15 +151,23 @@ export const POST = async (req: NextRequest) => {
88151

89152
const ipCacheKey = `testnet-faucet:${chainId}:${ipAddress}`;
90153
const addressCacheKey = `testnet-faucet:${chainId}:${toAddress}`;
154+
const accountCacheKey = `testnet-faucet:${chainId}:${account.data.id}`;
91155

92156
// Assert 1 request per IP/chain every 24 hours.
93157
// get the cached value
94-
const [ipCacheValue, addressCache] = await Promise.all([
95-
cacheGet(ipCacheKey),
96-
cacheGet(addressCacheKey),
97-
]);
158+
const [ipCacheValue, accountCacheValue, addressCacheValue] =
159+
await Promise.all([
160+
cacheGet(ipCacheKey),
161+
cacheGet(accountCacheKey),
162+
cacheGet(addressCacheKey),
163+
]);
164+
98165
// if we have a cached value, return an error
99-
if (ipCacheValue !== null || addressCache !== null) {
166+
if (
167+
ipCacheValue !== null ||
168+
accountCacheValue !== null ||
169+
addressCacheValue !== null
170+
) {
100171
return NextResponse.json(
101172
{ error: "Already requested funds on this chain in the past 24 hours." },
102173
{ status: 429 },
@@ -116,6 +187,7 @@ export const POST = async (req: NextRequest) => {
116187
// Store the claim request for 24 hours.
117188
await Promise.all([
118189
cacheSet(ipCacheKey, "claimed", 24 * 60 * 60),
190+
cacheSet(accountCacheKey, "claimed", 24 * 60 * 60),
119191
cacheSet(addressCacheKey, "claimed", 24 * 60 * 60),
120192
]);
121193
// then actually transfer the funds
@@ -125,7 +197,7 @@ export const POST = async (req: NextRequest) => {
125197
headers: {
126198
"Content-Type": "application/json",
127199
"x-idempotency-key": idempotencyKey,
128-
"x-backend-wallet-address": NEXT_PUBLIC_THIRDWEB_ENGINE_FAUCET_WALLET,
200+
"x-backend-wallet-address": THIRDWEB_ENGINE_FAUCET_WALLET,
129201
Authorization: `Bearer ${THIRDWEB_ACCESS_TOKEN}`,
130202
},
131203
body: JSON.stringify({

apps/dashboard/src/components/onboarding/Modal.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
import { Dialog, DialogContent } from "@/components/ui/dialog";
22
import { cn } from "@/lib/utils";
33
import { IconLogo } from "components/logo";
4+
import type { Dispatch, SetStateAction } from "react";
45
import type { ComponentWithChildren } from "types/component-with-children";
56

67
interface OnboardingModalProps {
78
isOpen: boolean;
89
wide?: boolean;
10+
isClosable?: boolean;
11+
12+
// Pass this props to make the modal closable (it will enable backdrop + the "x" icon)
13+
onOpenChange?: Dispatch<SetStateAction<boolean>>;
914
}
1015

1116
export const OnboardingModal: ComponentWithChildren<OnboardingModalProps> = ({
1217
children,
1318
isOpen,
1419
wide,
20+
onOpenChange,
1521
}) => {
1622
return (
17-
<Dialog open={isOpen}>
23+
<Dialog open={isOpen} onOpenChange={onOpenChange}>
1824
<DialogContent
1925
dialogOverlayClassName="z-[10000]"
2026
className={cn("z-[10001] max-h-[90vh] overflow-auto", {
2127
"!max-w-[768px]": wide,
2228
})}
23-
dialogCloseClassName="hidden"
29+
dialogCloseClassName={onOpenChange !== undefined ? "" : "hidden"}
2430
>
2531
<div className="flex flex-col gap-4">
2632
<div className="w-[40px]">

apps/dashboard/src/components/onboarding/index.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import {
55
useAccount,
66
} from "@3rdweb-sdk/react/hooks/useApi";
77
import { useLoggedInUser } from "@3rdweb-sdk/react/hooks/useLoggedInUser";
8-
import { Suspense, lazy, useEffect, useState } from "react";
8+
import {
9+
type Dispatch,
10+
type SetStateAction,
11+
Suspense,
12+
lazy,
13+
useEffect,
14+
useState,
15+
} from "react";
916
import { useActiveWallet } from "thirdweb/react";
1017
import { useTrack } from "../../hooks/analytics/useTrack";
1118
import { LazyOnboardingBilling } from "./LazyOnboardingBilling";
@@ -42,7 +49,10 @@ type OnboardingState =
4249
| "skipped"
4350
| undefined;
4451

45-
export const Onboarding: React.FC = () => {
52+
export const Onboarding: React.FC<{
53+
// Pass this props to make the modal closable (it will enable backdrop + the "x" icon)
54+
onOpenChange?: Dispatch<SetStateAction<boolean>>;
55+
}> = ({ onOpenChange }) => {
4656
const meQuery = useAccount();
4757

4858
const { isLoggedIn } = useLoggedInUser();
@@ -202,7 +212,11 @@ export const Onboarding: React.FC = () => {
202212
}
203213

204214
return (
205-
<OnboardingModal isOpen={!!state} wide={state === "plan"}>
215+
<OnboardingModal
216+
isOpen={!!state}
217+
wide={state === "plan"}
218+
onOpenChange={onOpenChange}
219+
>
206220
{state === "onboarding" && (
207221
<Suspense fallback={<Loading />}>
208222
<OnboardingGeneral

0 commit comments

Comments
 (0)