|
| 1 | +import { Address } from "viem"; |
| 2 | +import { getAction } from "viem/utils"; |
| 3 | +import { waitForTransactionReceipt } from "viem/actions"; |
| 4 | +import { useAccount, useClient, useSwitchChain, useWriteContract } from "wagmi"; |
| 5 | +import { twMerge } from "tailwind-merge"; |
| 6 | +import { useMutation } from "@tanstack/react-query"; |
| 7 | +import { getPaymaster } from "../../getPaymaster"; |
| 8 | +import { paymasterAbi } from "../../quarry/common"; |
| 9 | +import { useEntryKitConfig } from "../../EntryKitConfigProvider"; |
| 10 | +import { useQueryClient } from "@tanstack/react-query"; |
| 11 | +import { useShowQueryError } from "../../errors/useShowQueryError"; |
| 12 | +import { useBalance } from "./useBalance"; |
| 13 | +import { PendingIcon } from "../../icons/PendingIcon"; |
| 14 | + |
| 15 | +export type Props = { |
| 16 | + userAddress: Address; |
| 17 | +}; |
| 18 | + |
| 19 | +export function WithdrawGasBalanceButton({ userAddress }: Props) { |
| 20 | + const { writeContractAsync } = useWriteContract(); |
| 21 | + const { switchChain } = useSwitchChain(); |
| 22 | + const { chain, chainId } = useEntryKitConfig(); |
| 23 | + const { chainId: userChainId } = useAccount(); |
| 24 | + const queryClient = useQueryClient(); |
| 25 | + const client = useClient({ chainId }); |
| 26 | + const paymaster = getPaymaster(chain); |
| 27 | + const balance = useShowQueryError(useBalance(userAddress)); |
| 28 | + const shouldSwitchChain = chainId != null && chainId !== userChainId; |
| 29 | + |
| 30 | + const withdraw = useMutation({ |
| 31 | + mutationKey: ["withdraw", userAddress], |
| 32 | + mutationFn: async () => { |
| 33 | + if (!client) throw new Error("Client not ready."); |
| 34 | + if (!paymaster) throw new Error("Paymaster not found"); |
| 35 | + if (!balance.data) throw new Error("No gas balance to withdraw."); |
| 36 | + |
| 37 | + try { |
| 38 | + const hash = await writeContractAsync({ |
| 39 | + address: paymaster.address, |
| 40 | + abi: paymasterAbi, |
| 41 | + functionName: "withdrawTo", |
| 42 | + args: [userAddress, balance.data], |
| 43 | + chainId, |
| 44 | + }); |
| 45 | + await getAction(client, waitForTransactionReceipt, "waitForTransactionReceipt")({ hash }); |
| 46 | + |
| 47 | + await Promise.all([ |
| 48 | + queryClient.invalidateQueries({ queryKey: ["balance"] }), |
| 49 | + queryClient.invalidateQueries({ queryKey: ["getPrerequisites"] }), |
| 50 | + ]); |
| 51 | + } catch (error) { |
| 52 | + console.error("Error while withdrawing", error); |
| 53 | + throw error; |
| 54 | + } |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + if (balance.data == null || balance.data === 0n) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <button |
| 64 | + onClick={() => { |
| 65 | + if (shouldSwitchChain) { |
| 66 | + return switchChain({ chainId }); |
| 67 | + } |
| 68 | + withdraw.mutate(); |
| 69 | + }} |
| 70 | + className={twMerge( |
| 71 | + "text-sm font-medium text-white/50 group whitespace-nowrap", |
| 72 | + withdraw.isPending ? "opacity-50 pointer-events-none" : "cursor-pointer hover:text-white", |
| 73 | + )} |
| 74 | + disabled={withdraw.isPending} |
| 75 | + > |
| 76 | + <span className="inline-block"> |
| 77 | + <span |
| 78 | + className={twMerge( |
| 79 | + "inline-flex items-center gap-1 underline decoration-neutral-500 underline-offset-4", |
| 80 | + !withdraw.isPending && "hover:decoration-orange-500", |
| 81 | + shouldSwitchChain && "group-hover:hidden", |
| 82 | + )} |
| 83 | + > |
| 84 | + {withdraw.isPending && <PendingIcon className="w-3 h-3" />} |
| 85 | + Withdraw |
| 86 | + </span> |
| 87 | + |
| 88 | + {shouldSwitchChain && ( |
| 89 | + <span className="hidden group-hover:inline-block underline decoration-neutral-500 underline-offset-4 hover:decoration-orange-500"> |
| 90 | + Switch chain |
| 91 | + </span> |
| 92 | + )} |
| 93 | + </span> |
| 94 | + </button> |
| 95 | + ); |
| 96 | +} |
0 commit comments