From ef17359419fa46c6ca279d556d87c46af29d1909 Mon Sep 17 00:00:00 2001 From: GabrielMartinezRodriguez Date: Mon, 5 May 2025 16:10:11 +0200 Subject: [PATCH 1/2] feat(faucet): wait tx inclusion --- apps/faucet/src/services/faucet.ts | 12 +++++++++++- .../components/interface/home/tabs/views/Faucet.tsx | 10 +++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/apps/faucet/src/services/faucet.ts b/apps/faucet/src/services/faucet.ts index 11d0871bfe..b445056406 100644 --- a/apps/faucet/src/services/faucet.ts +++ b/apps/faucet/src/services/faucet.ts @@ -1,5 +1,5 @@ +import { TransactionManager, TransactionStatus } from "@happy.tech/txm" import type { Address } from "@happy.tech/common" -import { TransactionManager } from "@happy.tech/txm" import { type Result, err, ok } from "neverthrow" import { env } from "../env" import { FaucetRateLimitError } from "../errors" @@ -44,6 +44,16 @@ export class FaucetService { await this.txm.sendTransactions([tx]) + const result = await tx.waitForFinalization(10_000) + + if (result.isErr()) { + return err(result.error) + } + + if (result.value.status !== TransactionStatus.Success) { + return err(new Error("Transaction failed")) + } + return ok(undefined) } } diff --git a/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx b/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx index e915e88062..4d663830da 100644 --- a/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx +++ b/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx @@ -4,12 +4,15 @@ import { Button } from "#src/components/primitives/button/Button" import { useTurnstile } from "#src/hooks/useTurnstile" import { userAtom } from "#src/state/user" import UserNotFoundWarning from "./UserNotFoundWarning" +import { getBalanceQueryKey } from 'wagmi/query' +import { useQueryClient } from '@tanstack/react-query' const TURNSTILE_SITEKEY = import.meta.env.VITE_TURNSTILE_SITEKEY! const FAUCET_ENDPOINT = import.meta.env.VITE_FAUCET_ENDPOINT! const FaucetView = () => { const user = useAtomValue(userAtom) + const queryClient = useQueryClient() const [status, setStatus] = useState<"idle" | "loading" | "success" | "error">("idle") const [message, setMessage] = useState("") const turnstileRef = useRef(null) @@ -37,6 +40,11 @@ const FaucetView = () => { if (!res.ok) throw new Error(data?.message || "Unknown error") setStatus("success") setMessage(data?.message || "Tokens sent!") + + queryClient.invalidateQueries({ + queryKey: getBalanceQueryKey({ address: user.address }), + }) + } catch (err: unknown) { if (err instanceof Error) { setStatus("error") @@ -47,7 +55,7 @@ const FaucetView = () => { } } }, - [getToken, user, turnstileLoading], + [getToken, user, turnstileLoading, queryClient], ) if (!user) return From 8a3cf6bf676b1c5f819b5b7b77e97471105fb471 Mon Sep 17 00:00:00 2001 From: GabrielMartinezRodriguez Date: Mon, 5 May 2025 16:11:32 +0200 Subject: [PATCH 2/2] chore: format --- .../src/components/interface/home/tabs/views/Faucet.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx b/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx index 4d663830da..46d16cce8e 100644 --- a/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx +++ b/apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx @@ -1,11 +1,11 @@ +import { useQueryClient } from "@tanstack/react-query" import { useAtomValue } from "jotai" import { useCallback, useRef, useState } from "react" +import { getBalanceQueryKey } from "wagmi/query" import { Button } from "#src/components/primitives/button/Button" import { useTurnstile } from "#src/hooks/useTurnstile" import { userAtom } from "#src/state/user" import UserNotFoundWarning from "./UserNotFoundWarning" -import { getBalanceQueryKey } from 'wagmi/query' -import { useQueryClient } from '@tanstack/react-query' const TURNSTILE_SITEKEY = import.meta.env.VITE_TURNSTILE_SITEKEY! const FAUCET_ENDPOINT = import.meta.env.VITE_FAUCET_ENDPOINT! @@ -44,7 +44,6 @@ const FaucetView = () => { queryClient.invalidateQueries({ queryKey: getBalanceQueryKey({ address: user.address }), }) - } catch (err: unknown) { if (err instanceof Error) { setStatus("error")