Skip to content

Commit 5ba2f4a

Browse files
feat(faucet): wait tx inclusion
1 parent e9c007f commit 5ba2f4a

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

apps/faucet/src/services/faucet.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TransactionManager } from "@happy.tech/txm"
1+
import { TransactionManager, TransactionStatus } from "@happy.tech/txm"
22
import { type Result, err, ok } from "neverthrow"
33
import type { Address } from "viem"
44
import { env } from "../env"
@@ -44,6 +44,16 @@ export class FaucetService {
4444

4545
await this.txm.sendTransactions([tx])
4646

47+
const result = await tx.waitForFinalization(10_000)
48+
49+
if (result.isErr()) {
50+
return err(result.error)
51+
}
52+
53+
if (result.value.status !== TransactionStatus.Success) {
54+
return err(new Error("Transaction failed"))
55+
}
56+
4757
return ok(undefined)
4858
}
4959
}

apps/iframe/src/components/interface/home/tabs/views/Faucet.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@ import { Button } from "#src/components/primitives/button/Button"
44
import { useTurnstile } from "#src/hooks/useTurnstile"
55
import { userAtom } from "#src/state/user"
66
import UserNotFoundWarning from "./UserNotFoundWarning"
7+
import { getBalanceQueryKey } from 'wagmi/query'
8+
import { useQueryClient } from '@tanstack/react-query'
79

810
const TURNSTILE_SITEKEY = import.meta.env.VITE_TURNSTILE_SITEKEY!
911
const FAUCET_ENDPOINT = import.meta.env.VITE_FAUCET_ENDPOINT!
1012

1113
const FaucetView = () => {
1214
const user = useAtomValue(userAtom)
15+
const queryClient = useQueryClient()
1316
const [status, setStatus] = useState<"idle" | "loading" | "success" | "error">("idle")
1417
const [message, setMessage] = useState("")
1518
const turnstileRef = useRef<HTMLDivElement | null>(null)
@@ -37,6 +40,11 @@ const FaucetView = () => {
3740
if (!res.ok) throw new Error(data?.message || "Unknown error")
3841
setStatus("success")
3942
setMessage(data?.message || "Tokens sent!")
43+
44+
queryClient.invalidateQueries({
45+
queryKey: getBalanceQueryKey({ address: user.address }),
46+
})
47+
4048
} catch (err: unknown) {
4149
if (err instanceof Error) {
4250
setStatus("error")
@@ -47,7 +55,7 @@ const FaucetView = () => {
4755
}
4856
}
4957
},
50-
[getToken, user, turnstileLoading],
58+
[getToken, user, turnstileLoading, queryClient],
5159
)
5260

5361
if (!user) return <UserNotFoundWarning />

0 commit comments

Comments
 (0)