|
1 | 1 | <script lang="ts">
|
2 |
| -import { URLS } from "$lib/constants" |
3 |
| -import request from "graphql-request" |
4 |
| -import { cn } from "$lib/utilities/shadcn.ts" |
5 |
| -import { Label } from "$lib/components/ui/label" |
6 |
| -import { createQuery } from "@tanstack/svelte-query" |
7 |
| -import Truncate from "$lib/components/truncate.svelte" |
8 |
| -import * as Card from "$lib/components/ui/card/index.ts" |
9 |
| -import { Input } from "$lib/components/ui/input/index.ts" |
10 |
| -import { Button } from "$lib/components/ui/button/index.ts" |
11 |
| -import SpinnerSVG from "$lib/components/spinner-svg.svelte" |
12 |
| -import { cosmosStore } from "$/lib/wallet/cosmos/config.ts" |
13 |
| -import { derived, writable, type Writable } from "svelte/store" |
14 |
| -import { getCosmosChainBalances } from "$lib/queries/balance/cosmos" |
15 |
| -import { createCosmosSdkAddressRegex } from "$lib/utilities/address.ts" |
16 |
| -import { bech32ToBech32Address, isValidBech32Address } from "@unionlabs/client" |
17 |
| -import type { AwaitedReturnType, DiscriminatedUnion } from "$lib/utilities/types.ts" |
18 |
| -import { faucetUnoMutation2 } from "$lib/graphql/queries/faucet.ts" |
19 |
| -import { Turnstile } from "svelte-turnstile" |
| 2 | + import { URLS } from "$lib/constants"; |
| 3 | + import request from "graphql-request"; |
| 4 | + import { cn } from "$lib/utilities/shadcn.ts"; |
| 5 | + import { Label } from "$lib/components/ui/label"; |
| 6 | + import { createQuery } from "@tanstack/svelte-query"; |
| 7 | + import Truncate from "$lib/components/truncate.svelte"; |
| 8 | + import * as Card from "$lib/components/ui/card/index.ts"; |
| 9 | + import { Input } from "$lib/components/ui/input/index.ts"; |
| 10 | + import { Button } from "$lib/components/ui/button/index.ts"; |
| 11 | + import SpinnerSVG from "$lib/components/spinner-svg.svelte"; |
| 12 | + import { cosmosStore } from "$/lib/wallet/cosmos/config.ts"; |
| 13 | + import { derived, writable, type Writable } from "svelte/store"; |
| 14 | + import { getCosmosChainBalances } from "$lib/queries/balance/cosmos"; |
| 15 | + import { createCosmosSdkAddressRegex } from "$lib/utilities/address.ts"; |
| 16 | + import { |
| 17 | + bech32ToBech32Address, |
| 18 | + isValidBech32Address, |
| 19 | + } from "@unionlabs/client"; |
| 20 | + import type { |
| 21 | + AwaitedReturnType, |
| 22 | + DiscriminatedUnion, |
| 23 | + } from "$lib/utilities/types.ts"; |
| 24 | + import { faucetUnoMutation2 } from "$lib/graphql/queries/faucet.ts"; |
| 25 | + import { Turnstile } from "svelte-turnstile"; |
20 | 26 |
|
21 |
| -type DydxFaucetState = DiscriminatedUnion< |
22 |
| - "kind", |
23 |
| - { |
24 |
| - IDLE: {} |
25 |
| - VERIFYING: {} |
26 |
| - VERIFIED: {} |
27 |
| - SUBMITTING: { captchaToken: string } |
28 |
| - RESULT_OK: { message: string } |
29 |
| - RESULT_ERR: { error: string } |
30 |
| - VERIFICATION_FAILED: { error: string } |
31 |
| - } |
32 |
| -> |
| 27 | + type DydxFaucetState = DiscriminatedUnion< |
| 28 | + "kind", |
| 29 | + { |
| 30 | + IDLE: {}; |
| 31 | + VERIFYING: {}; |
| 32 | + VERIFIED: {}; |
| 33 | + SUBMITTING: { captchaToken: string }; |
| 34 | + RESULT_OK: { message: string }; |
| 35 | + RESULT_ERR: { error: string }; |
| 36 | + VERIFICATION_FAILED: { error: string }; |
| 37 | + } |
| 38 | + >; |
33 | 39 |
|
34 |
| -let stargazeAddress = derived(cosmosStore, $cosmosStore => |
35 |
| - $cosmosStore.address |
36 |
| - ? bech32ToBech32Address({ |
37 |
| - address: $cosmosStore.address, |
38 |
| - toPrefix: "stars" |
39 |
| - }) |
40 |
| - : "" |
41 |
| -) |
| 40 | + let stargazeAddress = derived(cosmosStore, ($cosmosStore) => |
| 41 | + $cosmosStore.address |
| 42 | + ? bech32ToBech32Address({ |
| 43 | + address: $cosmosStore.address, |
| 44 | + toPrefix: "stars", |
| 45 | + }) |
| 46 | + : "", |
| 47 | + ); |
42 | 48 |
|
43 |
| -let stargazeFaucetState: Writable<DydxFaucetState> = writable({ |
44 |
| - kind: "IDLE" |
45 |
| -}) |
46 |
| -let turnstileToken = "" |
47 |
| -let resetTurnstile: () => void |
48 |
| -let showTurnstile = false |
| 49 | + let stargazeFaucetState: Writable<DydxFaucetState> = writable({ |
| 50 | + kind: "IDLE", |
| 51 | + }); |
| 52 | + let turnstileToken = ""; |
| 53 | + let resetTurnstile: () => void; |
| 54 | + let showTurnstile = false; |
49 | 55 |
|
50 |
| -const verifyWithTurnstile = () => { |
51 |
| - if ($stargazeFaucetState.kind === "IDLE") { |
52 |
| - showTurnstile = true |
53 |
| - stargazeFaucetState.set({ kind: "VERIFYING" }) |
54 |
| - resetTurnstile?.() |
55 |
| - } |
56 |
| -} |
| 56 | + const verifyWithTurnstile = () => { |
| 57 | + if ($stargazeFaucetState.kind === "IDLE") { |
| 58 | + showTurnstile = true; |
| 59 | + stargazeFaucetState.set({ kind: "VERIFYING" }); |
| 60 | + resetTurnstile?.(); |
| 61 | + } |
| 62 | + }; |
57 | 63 |
|
58 |
| -const requestStarsFromFaucet = async () => { |
59 |
| - console.info("stargazeAddress: ", $stargazeAddress) |
| 64 | + const requestStarsFromFaucet = async () => { |
| 65 | + console.info("stargazeAddress: ", $stargazeAddress); |
60 | 66 |
|
61 |
| - if ($stargazeFaucetState.kind === "VERIFIED") { |
62 |
| - stargazeFaucetState.set({ |
63 |
| - kind: "SUBMITTING", |
64 |
| - captchaToken: turnstileToken |
65 |
| - }) |
66 |
| - } |
| 67 | + if ($stargazeFaucetState.kind === "VERIFIED") { |
| 68 | + stargazeFaucetState.set({ |
| 69 | + kind: "SUBMITTING", |
| 70 | + captchaToken: turnstileToken, |
| 71 | + }); |
| 72 | + } |
67 | 73 |
|
68 |
| - if ($stargazeFaucetState.kind === "SUBMITTING") { |
69 |
| - try { |
70 |
| - const result = await request(URLS().GRAPHQL, faucetUnoMutation2, { |
71 |
| - chainId: "elgafar-1", |
72 |
| - denom: "ustars", |
73 |
| - address: $stargazeAddress, |
74 |
| - captchaToken: $stargazeFaucetState.captchaToken |
75 |
| - }) |
| 74 | + if ($stargazeFaucetState.kind === "SUBMITTING") { |
| 75 | + try { |
| 76 | + const result = await request(URLS().GRAPHQL, faucetUnoMutation2, { |
| 77 | + chainId: "elgafar-1", |
| 78 | + denom: "ustars", |
| 79 | + address: $stargazeAddress, |
| 80 | + captchaToken: $stargazeFaucetState.captchaToken, |
| 81 | + }); |
76 | 82 |
|
77 |
| - if (!result.send) { |
78 |
| - stargazeFaucetState.set({ |
79 |
| - kind: "RESULT_ERR", |
80 |
| - error: "Empty faucet response" |
81 |
| - }) |
82 |
| - turnstileToken = "" |
83 |
| - showTurnstile = false |
84 |
| - return |
85 |
| - } |
| 83 | + if (!result.send) { |
| 84 | + stargazeFaucetState.set({ |
| 85 | + kind: "RESULT_ERR", |
| 86 | + error: "Empty faucet response", |
| 87 | + }); |
| 88 | + turnstileToken = ""; |
| 89 | + showTurnstile = false; |
| 90 | + return; |
| 91 | + } |
86 | 92 |
|
87 |
| - if (result.send.startsWith("ERROR")) { |
88 |
| - console.error(result.send) |
| 93 | + if (result.send.startsWith("ERROR")) { |
| 94 | + console.error(result.send); |
| 95 | + stargazeFaucetState.set({ |
| 96 | + kind: "RESULT_ERR", |
| 97 | + error: result.send.endsWith("ratelimited") |
| 98 | + ? "You already got USTARS from the faucet today. Try again in 24 hours." |
| 99 | + : "Error from faucet", |
| 100 | + }); |
| 101 | + turnstileToken = ""; |
| 102 | + showTurnstile = false; |
| 103 | + return; |
| 104 | + } |
| 105 | +
|
| 106 | + stargazeFaucetState.set({ |
| 107 | + kind: "RESULT_OK", |
| 108 | + message: result.send, |
| 109 | + }); |
| 110 | + turnstileToken = ""; |
| 111 | + showTurnstile = false; |
| 112 | + } catch (error) { |
| 113 | + console.error(error); |
89 | 114 | stargazeFaucetState.set({
|
90 | 115 | kind: "RESULT_ERR",
|
91 |
| - error: result.send.endsWith("ratelimited") |
92 |
| - ? "You already got USTARS from the faucet today. Try again in 24 hours." |
93 |
| - : "Error from faucet" |
94 |
| - }) |
95 |
| - turnstileToken = "" |
96 |
| - showTurnstile = false |
97 |
| - return |
| 116 | + error: `Faucet error: ${error}`, |
| 117 | + }); |
| 118 | + turnstileToken = ""; |
| 119 | + showTurnstile = false; |
98 | 120 | }
|
99 |
| -
|
100 |
| - stargazeFaucetState.set({ |
101 |
| - kind: "RESULT_OK", |
102 |
| - message: result.send |
103 |
| - }) |
104 |
| - turnstileToken = "" |
105 |
| - showTurnstile = false |
106 |
| - } catch (error) { |
107 |
| - console.error(error) |
108 |
| - stargazeFaucetState.set({ |
109 |
| - kind: "RESULT_ERR", |
110 |
| - error: `Faucet error: ${error}` |
111 |
| - }) |
112 |
| - turnstileToken = "" |
113 |
| - showTurnstile = false |
114 | 121 | }
|
115 |
| - } |
116 |
| -} |
| 122 | + }; |
117 | 123 |
|
118 |
| -const resetVerification = () => { |
119 |
| - if ($stargazeFaucetState.kind === "VERIFICATION_FAILED") { |
120 |
| - turnstileToken = "" |
121 |
| - showTurnstile = false |
122 |
| - stargazeFaucetState.set({ kind: "IDLE" }) |
123 |
| - } |
124 |
| -} |
| 124 | + const resetVerification = () => { |
| 125 | + if ($stargazeFaucetState.kind === "VERIFICATION_FAILED") { |
| 126 | + turnstileToken = ""; |
| 127 | + showTurnstile = false; |
| 128 | + stargazeFaucetState.set({ kind: "IDLE" }); |
| 129 | + } |
| 130 | + }; |
125 | 131 |
|
126 |
| -const handleTurnstileCallback = ( |
127 |
| - e: CustomEvent<{ token: string; preClearanceObtained: boolean }> |
128 |
| -) => { |
129 |
| - turnstileToken = e.detail.token |
130 |
| - if ($stargazeFaucetState.kind === "VERIFYING") { |
131 |
| - stargazeFaucetState.set({ kind: "VERIFIED" }) |
132 |
| - } |
133 |
| -} |
| 132 | + const handleTurnstileCallback = ( |
| 133 | + e: CustomEvent<{ token: string; preClearanceObtained: boolean }>, |
| 134 | + ) => { |
| 135 | + turnstileToken = e.detail.token; |
| 136 | + if ($stargazeFaucetState.kind === "VERIFYING") { |
| 137 | + stargazeFaucetState.set({ kind: "VERIFIED" }); |
| 138 | + } |
| 139 | + }; |
134 | 140 |
|
135 |
| -const handleTurnstileError = (e: CustomEvent<{ code: string }>) => { |
136 |
| - if ($stargazeFaucetState.kind === "VERIFYING") { |
137 |
| - stargazeFaucetState.set({ |
138 |
| - kind: "VERIFICATION_FAILED", |
139 |
| - error: `Verification error: ${e.detail.code}` |
140 |
| - }) |
141 |
| - } |
142 |
| -} |
| 141 | + const handleTurnstileError = (e: CustomEvent<{ code: string }>) => { |
| 142 | + if ($stargazeFaucetState.kind === "VERIFYING") { |
| 143 | + stargazeFaucetState.set({ |
| 144 | + kind: "VERIFICATION_FAILED", |
| 145 | + error: `Verification error: ${e.detail.code}`, |
| 146 | + }); |
| 147 | + } |
| 148 | + }; |
143 | 149 |
|
144 |
| -let stargazeBalance = createQuery( |
145 |
| - derived(stargazeAddress, $stargazeAddress => ({ |
146 |
| - queryKey: ["stargaze-balance", $stargazeAddress], |
147 |
| - enabled: $stargazeAddress?.indexOf("stars") === 0, |
148 |
| - refetchInterval: () => ($stargazeAddress?.indexOf("stars") === 0 ? 5_000 : false), |
149 |
| - queryFn: async () => |
150 |
| - await getCosmosChainBalances({ |
151 |
| - walletAddress: `${$stargazeAddress}`, |
152 |
| - url: "https://stargaze-testnet-api.polkachu.com" |
153 |
| - }), |
154 |
| - select: (data: AwaitedReturnType<typeof getCosmosChainBalances>) => |
155 |
| - data?.find(balance => balance?.symbol === "ustars") |
156 |
| - })) |
157 |
| -) |
| 150 | + let stargazeBalance = createQuery( |
| 151 | + derived(stargazeAddress, ($stargazeAddress) => ({ |
| 152 | + queryKey: ["stargaze-balance", $stargazeAddress], |
| 153 | + enabled: $stargazeAddress?.indexOf("stars") === 0, |
| 154 | + refetchInterval: () => |
| 155 | + $stargazeAddress?.indexOf("stars") === 0 ? 5_000 : false, |
| 156 | + queryFn: async () => |
| 157 | + await getCosmosChainBalances({ |
| 158 | + walletAddress: `${$stargazeAddress}`, |
| 159 | + url: "https://stargaze-testnet-api.polkachu.com", |
| 160 | + }), |
| 161 | + select: (data: AwaitedReturnType<typeof getCosmosChainBalances>) => |
| 162 | + data?.find((balance) => balance?.symbol === "ustars"), |
| 163 | + })), |
| 164 | + ); |
158 | 165 | </script>
|
159 | 166 |
|
160 | 167 | <!-- stargaze faucet -->
|
161 | 168 | <Card.Root
|
162 | 169 | class={cn(
|
163 | 170 | "w-full max-w-lg rounded-lg font-sans",
|
164 |
| - "bg-[url('/images/backgrounds/stride-background.png')]", |
165 | 171 | "bg-[#181825] text-[#99f0cf] dark:bg-[#2D2D44]/50 dark:text-[#99f0cf]",
|
166 | 172 | )}
|
167 | 173 | >
|
|
0 commit comments