|
| 1 | +"use client"; |
| 2 | +import { WalletAvatar } from "@/components/blocks/wallet-address"; |
| 3 | +import { Input } from "@/components/ui/input"; |
| 4 | +import { |
| 5 | + Select, |
| 6 | + SelectContent, |
| 7 | + SelectItem, |
| 8 | + SelectTrigger, |
| 9 | + SelectValue, |
| 10 | +} from "@/components/ui/select"; |
| 11 | +import { THIRDWEB_ENGINE_CLOUD_URL } from "@/constants/env"; |
| 12 | +import { useThirdwebClient } from "@/constants/thirdweb.client"; |
| 13 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 14 | +import { useMutation } from "@tanstack/react-query"; |
| 15 | +import { ChevronDown, Loader2 } from "lucide-react"; |
| 16 | +import { useState } from "react"; |
| 17 | +import { useForm } from "react-hook-form"; |
| 18 | +import { toast } from "sonner"; |
| 19 | +import { shortenAddress } from "thirdweb/utils"; |
| 20 | +import * as z from "zod"; |
| 21 | +import { SingleNetworkSelector } from "../../../../../../@/components/blocks/NetworkSelectors"; |
| 22 | +import { Button } from "../../../../../../@/components/ui/button"; |
| 23 | +import type { Wallet } from "../server-wallets/wallet-table/types"; |
| 24 | + |
| 25 | +const formSchema = z.object({ |
| 26 | + projectSecretKey: z.string().min(1, "Project secret key is required"), |
| 27 | + accessToken: z.string().min(1, "Access token is required"), |
| 28 | + walletIndex: z.string(), |
| 29 | + chainId: z.number(), |
| 30 | +}); |
| 31 | + |
| 32 | +type FormValues = z.infer<typeof formSchema>; |
| 33 | + |
| 34 | +export function SendTestTransaction(props: { |
| 35 | + wallets?: Wallet[]; |
| 36 | +}) { |
| 37 | + const [isOpen, setIsOpen] = useState(false); |
| 38 | + const thirdwebClient = useThirdwebClient(); |
| 39 | + |
| 40 | + const form = useForm<FormValues>({ |
| 41 | + resolver: zodResolver(formSchema), |
| 42 | + defaultValues: { |
| 43 | + projectSecretKey: "", |
| 44 | + accessToken: "", |
| 45 | + walletIndex: "0", |
| 46 | + chainId: 84532, |
| 47 | + }, |
| 48 | + }); |
| 49 | + |
| 50 | + const selectedWalletIndex = Number(form.watch("walletIndex")); |
| 51 | + const selectedWallet = props.wallets?.[selectedWalletIndex]; |
| 52 | + |
| 53 | + const sendDummyTxMutation = useMutation({ |
| 54 | + mutationFn: async (args: { |
| 55 | + walletAddress: string; |
| 56 | + accessToken: string; |
| 57 | + chainId: number; |
| 58 | + }) => { |
| 59 | + const response = await fetch( |
| 60 | + `${THIRDWEB_ENGINE_CLOUD_URL}/write/transaction`, |
| 61 | + { |
| 62 | + method: "POST", |
| 63 | + headers: { |
| 64 | + "Content-Type": "application/json", |
| 65 | + "x-secret-key": form.getValues("projectSecretKey"), |
| 66 | + }, |
| 67 | + body: JSON.stringify({ |
| 68 | + executionOptions: { |
| 69 | + type: "AA", |
| 70 | + signerAddress: args.walletAddress, |
| 71 | + }, |
| 72 | + transactionParams: [ |
| 73 | + { |
| 74 | + to: args.walletAddress, |
| 75 | + value: "0", |
| 76 | + }, |
| 77 | + ], |
| 78 | + vaultAccessToken: args.accessToken, |
| 79 | + chainId: args.chainId.toString(), |
| 80 | + }), |
| 81 | + }, |
| 82 | + ); |
| 83 | + const result = await response.json(); |
| 84 | + if (!response.ok) { |
| 85 | + const errorMsg = result?.error?.message || "Failed to send transaction"; |
| 86 | + throw new Error(errorMsg); |
| 87 | + } |
| 88 | + return result; |
| 89 | + }, |
| 90 | + onSuccess: () => { |
| 91 | + toast.success("Test transaction sent successfully!"); |
| 92 | + }, |
| 93 | + onError: (error) => { |
| 94 | + toast.error(error.message); |
| 95 | + }, |
| 96 | + }); |
| 97 | + |
| 98 | + const isLoading = sendDummyTxMutation.isPending; |
| 99 | + |
| 100 | + // Early return in render phase |
| 101 | + if (!props.wallets || props.wallets.length === 0 || !selectedWallet) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + const onSubmit = (data: FormValues) => { |
| 106 | + sendDummyTxMutation.mutate({ |
| 107 | + walletAddress: selectedWallet.address, |
| 108 | + accessToken: data.accessToken, |
| 109 | + chainId: data.chainId, |
| 110 | + }); |
| 111 | + }; |
| 112 | + |
| 113 | + return ( |
| 114 | + <div className="w-full space-y-2 rounded-md border p-3"> |
| 115 | + {/* Trigger Area */} |
| 116 | + <div |
| 117 | + role="button" |
| 118 | + tabIndex={0} |
| 119 | + className="flex cursor-pointer items-center justify-between p-3" |
| 120 | + onClick={() => setIsOpen(!isOpen)} |
| 121 | + onKeyDown={(e) => { |
| 122 | + if (e.key === "Enter" || e.key === " ") { |
| 123 | + setIsOpen(!isOpen); |
| 124 | + } |
| 125 | + }} |
| 126 | + > |
| 127 | + <h2 className="font-semibold text-xl tracking-tight"> |
| 128 | + Send Test Transaction |
| 129 | + </h2> |
| 130 | + <ChevronDown |
| 131 | + className={`h-4 w-4 transform transition-transform duration-200 ${isOpen ? "rotate-180" : ""}`} |
| 132 | + /> |
| 133 | + </div> |
| 134 | + |
| 135 | + {/* Content Area (conditional) */} |
| 136 | + {isOpen && ( |
| 137 | + <form |
| 138 | + onSubmit={form.handleSubmit(onSubmit)} |
| 139 | + className="space-y-4 px-3 pb-3" |
| 140 | + > |
| 141 | + <p className="text-sm text-warning-text"> |
| 142 | + 🔐 This action requires a project secret key and a vault access |
| 143 | + token. |
| 144 | + </p> |
| 145 | + {/* Responsive container */} |
| 146 | + <div className="flex flex-col gap-2 md:flex-row md:items-end md:gap-2"> |
| 147 | + <div className="flex-grow"> |
| 148 | + <div className="flex flex-col gap-2"> |
| 149 | + <p className="text-sm">Project Secret Key</p> |
| 150 | + <Input |
| 151 | + id="secret-key" |
| 152 | + type="password" |
| 153 | + placeholder="Project Secret Key" |
| 154 | + {...form.register("projectSecretKey")} |
| 155 | + disabled={isLoading} |
| 156 | + className="text-xs" |
| 157 | + /> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + <div className="flex-grow"> |
| 161 | + <div className="flex flex-col gap-2"> |
| 162 | + <p className="text-sm">Vault Access Token</p> |
| 163 | + <Input |
| 164 | + id="access-token" |
| 165 | + type="password" |
| 166 | + placeholder="Vault Access Token" |
| 167 | + {...form.register("accessToken")} |
| 168 | + disabled={isLoading} |
| 169 | + className="text-xs" |
| 170 | + /> |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + {/* Wallet Selector */} |
| 175 | + <div className="flex flex-col gap-2"> |
| 176 | + <div className="flex flex-col gap-2 md:flex-row md:items-end md:gap-2"> |
| 177 | + <div className="flex flex-1 flex-col gap-2"> |
| 178 | + <p className="text-sm">Signer</p> |
| 179 | + <Select |
| 180 | + value={form.watch("walletIndex")} |
| 181 | + onValueChange={(value) => form.setValue("walletIndex", value)} |
| 182 | + > |
| 183 | + <SelectTrigger className="w-full"> |
| 184 | + <SelectValue> |
| 185 | + <div className="flex items-center gap-2"> |
| 186 | + <WalletAvatar |
| 187 | + address={selectedWallet.address} |
| 188 | + profiles={[]} |
| 189 | + thirdwebClient={thirdwebClient} |
| 190 | + iconClassName="h-5 w-5" |
| 191 | + /> |
| 192 | + <span className="font-mono text-sm"> |
| 193 | + {shortenAddress(selectedWallet.address)} |
| 194 | + </span> |
| 195 | + </div> |
| 196 | + </SelectValue> |
| 197 | + </SelectTrigger> |
| 198 | + <SelectContent> |
| 199 | + {props.wallets.map((wallet, index) => ( |
| 200 | + <SelectItem key={wallet.address} value={index.toString()}> |
| 201 | + <div className="flex items-center gap-2"> |
| 202 | + <WalletAvatar |
| 203 | + address={wallet.address} |
| 204 | + profiles={[]} |
| 205 | + thirdwebClient={thirdwebClient} |
| 206 | + iconClassName="h-5 w-5" |
| 207 | + /> |
| 208 | + <span className="font-mono text-sm"> |
| 209 | + {shortenAddress(wallet.address)} |
| 210 | + </span> |
| 211 | + </div> |
| 212 | + </SelectItem> |
| 213 | + ))} |
| 214 | + </SelectContent> |
| 215 | + </Select> |
| 216 | + </div> |
| 217 | + <div className="flex flex-1 flex-col gap-2"> |
| 218 | + <p className="text-sm">Network</p> |
| 219 | + <SingleNetworkSelector |
| 220 | + chainId={form.watch("chainId")} |
| 221 | + onChange={(chainId) => { |
| 222 | + form.setValue("chainId", chainId); |
| 223 | + }} |
| 224 | + /> |
| 225 | + </div> |
| 226 | + </div> |
| 227 | + </div> |
| 228 | + <div className="flex justify-end"> |
| 229 | + <Button |
| 230 | + type="submit" |
| 231 | + variant="primary" |
| 232 | + disabled={isLoading} |
| 233 | + size="sm" |
| 234 | + className="w-full min-w-[200px] md:w-auto" |
| 235 | + > |
| 236 | + {isLoading ? ( |
| 237 | + <> |
| 238 | + <Loader2 className="mr-2 h-4 w-4 animate-spin" /> |
| 239 | + Sending... |
| 240 | + </> |
| 241 | + ) : ( |
| 242 | + "Send Transaction" |
| 243 | + )} |
| 244 | + </Button> |
| 245 | + </div> |
| 246 | + </form> |
| 247 | + )} |
| 248 | + </div> |
| 249 | + ); |
| 250 | +} |
0 commit comments