Skip to content

Commit

Permalink
fix(app): fix faucet (#2077)
Browse files Browse the repository at this point in the history
Things left to do

- [ ] Fix balance formating
- [ ] Fix toasts and texts
- [ ] Check if claimed / Limit claims
- [ ] Show TX status (success, waiting, fail)

(I clearly have issues with my env, got biome installed but not
formatting according to config)
  • Loading branch information
cor authored Jun 8, 2024
2 parents f7122b3 + da985a5 commit 44a0080
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 219 deletions.
29 changes: 29 additions & 0 deletions app/src/lib/graphql/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { MutationCache, QueryClient } from "@tanstack/svelte-query"
import { createSyncStoragePersister } from "@tanstack/query-sync-storage-persister"
import { browser } from "$app/environment"

export function createQueryClient() {
const queryClient: QueryClient = new QueryClient({
defaultOptions: {
queries: {
enabled: browser,
gcTime: 1000 * 60 * 60 * 24,
refetchOnReconnect: () => !queryClient.isMutating()
}
},
mutationCache: new MutationCache({
onSettled: () => {
if (queryClient.isMutating() === 1) {
return queryClient.invalidateQueries()
}
}
})
})

const localStoragePersister = createSyncStoragePersister({
key: "SVELTE_QUERY",
storage: typeof window !== "undefined" ? window.localStorage : undefined // Use local storage if in browser
})

return { queryClient, localStoragePersister }
}
6 changes: 6 additions & 0 deletions app/src/lib/graphql/documents/faucet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const faucetUnoMutation = `
mutation FaucetUnoMutation($address: Address!) {
faucet {
send(input: {toAddress: $address})
}
}`
30 changes: 17 additions & 13 deletions app/src/lib/mutations/faucet.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
import { faucetUnoMutation } from "$lib/graphql/documents/faucet.ts"
import { URLS } from "$lib/constants"

export async function getUnoFromFaucet(address: string) {
const response = await fetch("https://graphql.union.build/v1/graphql", {
const response = await fetch(URLS.GRAPHQL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: /* GraphQL */ `
mutation GetUno($address: Address!) {
faucet { send(input: { toAddress: $address }) }
}
`,
query: faucetUnoMutation,
variables: { address },
operationName: "GetUno"
operationName: "FaucetUnoMutation"
})
})

if (!response.ok) return { error: await response.text(), status: response.status }
if (!response.ok) {
const errorText = await response.text()
console.error("Fetch error:", errorText)
return { error: errorText, status: response.status }
}

const responseJson = (await response.json()) as {
data?: { union: { send: string } }
data?: { faucet: { send: string } }
errors?: Array<{ message: string }>
}
if ("errors" in responseJson) {
const [error] = responseJson.errors || []
console.error(error)
return { error: error.message, status: response.status }

if (responseJson.errors && responseJson.errors.length > 0) {
const errorMessage = responseJson.errors.map(e => e.message).join("; ")
console.error("GraphQL error:", errorMessage)
return { error: errorMessage, status: response.status }
}

return { data: responseJson.data, status: response.status }
Expand Down
27 changes: 4 additions & 23 deletions app/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import OnlineStatus from "$lib/components/online-status.svelte"
import { partytownSnippet } from "@builder.io/partytown/integration"
import { SvelteQueryDevtools } from "@tanstack/svelte-query-devtools"
import PreloadingIndicator from "$lib/components/preloading-indicator.svelte"
import { QueryClient, MutationCache, notifyManager } from "@tanstack/svelte-query"
import { notifyManager } from "@tanstack/svelte-query"
import { PersistQueryClientProvider } from "@tanstack/svelte-query-persist-client"
import { createSyncStoragePersister } from "@tanstack/query-sync-storage-persister"
import { createQueryClient } from "$lib/graphql/client.ts"
const { queryClient, localStoragePersister } = createQueryClient()
if (browser) notifyManager.setScheduler(window.requestAnimationFrame)
$: updateTheme({ path: $page.url.pathname, activeTheme: "dark" })
Expand All @@ -45,27 +46,7 @@ onMount(() => {
else if (window?.leap) cosmosStore.connect("leap")
})
const queryClient: QueryClient = new QueryClient({
defaultOptions: {
queries: {
enabled: browser,
gcTime: 1_000 * 60 * 60 * 24, // 24 hours
refetchOnReconnect: () => !queryClient.isMutating()
}
},
mutationCache: new MutationCache({
onSettled: () => {
if (queryClient.isMutating() === 1) {
return queryClient.invalidateQueries()
}
}
})
})
const localStoragePersister = createSyncStoragePersister({
key: "SVELTE_QUERY",
storage: browser ? window.localStorage : undefined // or window.sessionStorage
})
onNavigate(navigation => console.info("Navigating to", navigation.to?.route.id))
</script>

<svelte:head>
Expand Down
Loading

0 comments on commit 44a0080

Please sign in to comment.