|
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 { |
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"; |
| 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" |
26 | 20 |
|
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 |
| - >; |
39 |
| -
|
40 |
| - let stargazeAddress = derived(cosmosStore, ($cosmosStore) => |
41 |
| - $cosmosStore.address |
42 |
| - ? bech32ToBech32Address({ |
43 |
| - address: $cosmosStore.address, |
44 |
| - toPrefix: "stars", |
45 |
| - }) |
46 |
| - : "", |
47 |
| - ); |
48 |
| -
|
49 |
| - let stargazeFaucetState: Writable<DydxFaucetState> = writable({ |
50 |
| - kind: "IDLE", |
51 |
| - }); |
52 |
| - let turnstileToken = ""; |
53 |
| - let resetTurnstile: () => void; |
54 |
| - let showTurnstile = false; |
| 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 | +> |
55 | 33 |
|
56 |
| - const verifyWithTurnstile = () => { |
57 |
| - if ($stargazeFaucetState.kind === "IDLE") { |
58 |
| - showTurnstile = true; |
59 |
| - stargazeFaucetState.set({ kind: "VERIFYING" }); |
60 |
| - resetTurnstile?.(); |
61 |
| - } |
62 |
| - }; |
| 34 | +let stargazeAddress = derived(cosmosStore, $cosmosStore => |
| 35 | + $cosmosStore.address |
| 36 | + ? bech32ToBech32Address({ |
| 37 | + address: $cosmosStore.address, |
| 38 | + toPrefix: "stars" |
| 39 | + }) |
| 40 | + : "" |
| 41 | +) |
63 | 42 |
|
64 |
| - const requestStarsFromFaucet = async () => { |
65 |
| - console.info("stargazeAddress: ", $stargazeAddress); |
| 43 | +let stargazeFaucetState: Writable<DydxFaucetState> = writable({ |
| 44 | + kind: "IDLE" |
| 45 | +}) |
| 46 | +let turnstileToken = "" |
| 47 | +let resetTurnstile: () => void |
| 48 | +let showTurnstile = false |
66 | 49 |
|
67 |
| - if ($stargazeFaucetState.kind === "VERIFIED") { |
68 |
| - stargazeFaucetState.set({ |
69 |
| - kind: "SUBMITTING", |
70 |
| - captchaToken: turnstileToken, |
71 |
| - }); |
72 |
| - } |
| 50 | +const verifyWithTurnstile = () => { |
| 51 | + if ($stargazeFaucetState.kind === "IDLE") { |
| 52 | + showTurnstile = true |
| 53 | + stargazeFaucetState.set({ kind: "VERIFYING" }) |
| 54 | + resetTurnstile?.() |
| 55 | + } |
| 56 | +} |
73 | 57 |
|
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 |
| - }); |
| 58 | +const requestStarsFromFaucet = async () => { |
| 59 | + console.info("stargazeAddress: ", $stargazeAddress) |
82 | 60 |
|
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 |
| - } |
| 61 | + if ($stargazeFaucetState.kind === "VERIFIED") { |
| 62 | + stargazeFaucetState.set({ |
| 63 | + kind: "SUBMITTING", |
| 64 | + captchaToken: turnstileToken |
| 65 | + }) |
| 66 | + } |
92 | 67 |
|
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 |
| - } |
| 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 | + }) |
105 | 76 |
|
| 77 | + if (!result.send) { |
106 | 78 | stargazeFaucetState.set({
|
107 |
| - kind: "RESULT_OK", |
108 |
| - message: result.send, |
109 |
| - }); |
110 |
| - turnstileToken = ""; |
111 |
| - showTurnstile = false; |
112 |
| - } catch (error) { |
113 |
| - console.error(error); |
| 79 | + kind: "RESULT_ERR", |
| 80 | + error: "Empty faucet response" |
| 81 | + }) |
| 82 | + turnstileToken = "" |
| 83 | + showTurnstile = false |
| 84 | + return |
| 85 | + } |
| 86 | +
|
| 87 | + if (result.send.startsWith("ERROR")) { |
| 88 | + console.error(result.send) |
114 | 89 | stargazeFaucetState.set({
|
115 | 90 | kind: "RESULT_ERR",
|
116 |
| - error: `Faucet error: ${error}`, |
117 |
| - }); |
118 |
| - turnstileToken = ""; |
119 |
| - showTurnstile = false; |
| 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 |
120 | 98 | }
|
121 |
| - } |
122 |
| - }; |
123 | 99 |
|
124 |
| - const resetVerification = () => { |
125 |
| - if ($stargazeFaucetState.kind === "VERIFICATION_FAILED") { |
126 |
| - turnstileToken = ""; |
127 |
| - showTurnstile = false; |
128 |
| - stargazeFaucetState.set({ kind: "IDLE" }); |
| 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 |
129 | 114 | }
|
130 |
| - }; |
| 115 | + } |
| 116 | +} |
131 | 117 |
|
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 |
| - }; |
| 118 | +const resetVerification = () => { |
| 119 | + if ($stargazeFaucetState.kind === "VERIFICATION_FAILED") { |
| 120 | + turnstileToken = "" |
| 121 | + showTurnstile = false |
| 122 | + stargazeFaucetState.set({ kind: "IDLE" }) |
| 123 | + } |
| 124 | +} |
140 | 125 |
|
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 |
| - }; |
| 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 | +} |
| 134 | +
|
| 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 | +} |
149 | 143 |
|
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 |
| - ); |
| 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 | +) |
165 | 158 | </script>
|
166 | 159 |
|
167 | 160 | <!-- stargaze faucet -->
|
|
0 commit comments