Skip to content

Commit

Permalink
feat: add whitelisted addresses to .env
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspang committed Aug 2, 2023
1 parent fe46ee7 commit 7a8700f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ DEVLINK_AUTH_TOKEN=
MONGODB_URI=
BACKEND_URL=https://api.radardao.xyz/launch
PAPER_API_KEY=
WHITELISTED_ADDRESSES=
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const nextConfig = {
VITE_WEB3AUTH_CLIENT_ID: process.env.VITE_WEB3AUTH_CLIENT_ID,
VITE_INFURA_KEY: process.env.VITE_INFURA_KEY,
BACKEND_URL: process.env.BACKEND_URL,
WHITELISTED_ADDRESSES: process.env.WHITELISTED_ADDRESSES
},
};

Expand Down
31 changes: 20 additions & 11 deletions pages/project/create/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { format, isBefore } from "date-fns";
import { CalendarIcon } from "lucide-react";
import { useForm } from "react-hook-form";
import { useAccount, useMutation, useNetwork, useQuery } from "wagmi";
import { useAccount, useEnsAddress, useMutation, useNetwork, useQuery } from "wagmi";
import * as z from "zod";
import { TeamFields } from "../../../components/TeamFields";
import {
Expand All @@ -54,10 +54,11 @@ import {
import { useRouter } from "next/router";
import { useContext, useEffect } from "react";
import { AuthContext } from "@/components/AuthProvider";
import { isAddress, isAddressEqual } from "viem";
import { Address, isAddress, isAddressEqual } from "viem";
import { retrieveYoutubeId } from "../../../lib/retrieveYoutubeId";
import { YOUTUBE_REGEX } from "../../../constants/regex";
import { WHITELISTED_ADDRESSES } from "@/constants/whitelist";
import isTestnet from "@/lib/utils/isTestnet";
import { chains } from "@/components/Web3Provider";

async function createProject(
idToken: string,
Expand Down Expand Up @@ -145,7 +146,7 @@ const formSchema = z.object({
admin_address: z
.string()
.min(1, { message: "Admin address is required" })
.refine(isAddress, { message: "Invalid address" }),
.refine(address => isAddress(address) || address.endsWith('.eth'), { message: "Invalid address" }),
});

export default function ProjectForm() {
Expand Down Expand Up @@ -173,7 +174,6 @@ export default function ProjectForm() {
edition_price: 0,
mint_end_date: new Date(),
benefits: [],
// @ts-expect-error "" is not of type Address
admin_address: address || "",
},
});
Expand All @@ -187,18 +187,27 @@ export default function ProjectForm() {
const admin_address = watch("admin_address");
const video_image = watch("video_image");


const { data: ensAddressData } = useEnsAddress({
name: admin_address,
chainId: chains[0].id,
enabled: admin_address.endsWith('.eth')
})
const {
data: createProjectData,
mutateAsync,
isLoading: isSubmitLoading,
isSuccess: isSubmitSuccess,
} = useMutation(["submit-project"], () => {
const values = form.getValues();
return createProject(idToken, { ...values, video_image: retrieveYoutubeId(values.video_image) })
}
);
let admin_address = values.admin_address
if (admin_address.endsWith('.eth')) {
admin_address = ensAddressData || admin_address
}
return createProject(idToken, { ...values, video_image: retrieveYoutubeId(values.video_image), admin_address })
});
const { data: checkoutLink, isLoading: isCheckoutLinkLoading } = useQuery(
["checkout-link", fee, createProjectData?._id, admin_address],
["checkout-link", fee, createProjectData?._id, ensAddressData || admin_address],
() => getCheckoutLink(fee, admin_address, createProjectData._id),
{ enabled: Boolean(createProjectData?._id) }
);
Expand Down Expand Up @@ -235,7 +244,7 @@ export default function ProjectForm() {
}, [isSubmitSuccess, createProjectData, toast, router, checkoutLink]);

useEffect(() => {
if (address !== undefined && !WHITELISTED_ADDRESSES.some(addr => isAddressEqual(address, addr))) {
if (address !== undefined && !(process.env.WHITELISTED_ADDRESSES?.split(" ") || []).some(addr => isAddressEqual(address, addr as Address))) {
router.back()
}
}, [address, router])
Expand Down Expand Up @@ -641,7 +650,7 @@ export default function ProjectForm() {
<Input {...field} placeholder="Your ETH / ENS address" />
</FormControl>
<FormDescription>
This should start with 0x... or end with .ens
This should start with 0x... or end with .eth
</FormDescription>
<FormMessage />
</FormItem>
Expand Down

0 comments on commit 7a8700f

Please sign in to comment.