Skip to content

Commit

Permalink
feat(app): reactive balance querying and error state handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cor committed Jun 7, 2024
1 parent e56617b commit f7a7d40
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
16 changes: 7 additions & 9 deletions app/src/lib/queries/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ export function evmBalancesQuery({
chainId: string
} & ({ contractAddresses: Array<string> } | { tokenSpecification: "erc20" | "DEFAULT_TOKENS" })) {
return createQuery({
queryKey: [address, "balances", chainId],
queryKey: ["balances", chainId, address],
enabled: isValidEvmAddress(address),
refetchOnWindowFocus: false,
refetchInterval: 1_000,
queryFn: async () => {
const assetsToCheck =
"contractAddresses" in restParams && Array.isArray(restParams.contractAddresses)
Expand All @@ -85,17 +86,14 @@ export function evmBalancesQuery({
})
})
const result = v.safeParse(evmBalancesResponseSchema, await response.json())
if (!result.success) return null
if (!result.success) return new Error(`Error parsing result ${JSON.stringify(result.issues)}`);

const tokensInfo = await getEvmTokensInfo(
result.output.result.tokenBalances.map(({ contractAddress }) => contractAddress)
)
return tokensInfo.map((token, index) => ({
...token,
balance: formatUnits(
BigInt(result.output.result.tokenBalances[index].tokenBalance),
token.decimals
)
balance: BigInt(result.output.result.tokenBalances[index].tokenBalance)
}))
}
})
Expand All @@ -109,15 +107,15 @@ export function cosmosBalancesQuery({
chainId: string
}) {
return createQuery({
queryKey: [address, "balances", chainId],
queryKey: ["balances", chainId, address],
enabled: isValidCosmosAddress(address),
refetchOnWindowFocus: false,
queryFn: async () => {
const restUrl = CHAIN_URLS[chainId].REST
const response = await fetch(
`${restUrl}/cosmos/bank/v1beta1/balances/${address}`,
)
if (!response.ok) return []
{});
if (!response.ok) return new Error("invalid response");
return (await response.json()).balances.map((x) => {
return {
address: x.denom,
Expand Down
58 changes: 42 additions & 16 deletions app/src/routes/balance/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
<script lang="ts">
import { createQuery } from '@tanstack/svelte-query'
import { cosmosBalancesQuery, evmBalancesQuery } from '$lib/queries/balance'
import { CHAIN_URLS } from '$lib/constants';
import { sepoliaStore } from "$lib/wallet/evm/config.ts"
import { cosmosStore } from "$lib/wallet/cosmos"
$: evmBalances = evmBalancesQuery({
chainId: '11155111',
address: $sepoliaStore.address,
tokenSpecification: 'erc20',
})
let evmBalances: null | ReturnType<typeof evmBalancesQuery>;
$: if($sepoliaStore.address) evmBalances = evmBalancesQuery({
chainId: '11155111',
address: $sepoliaStore.address,
tokenSpecification: 'erc20',
})
$: cosmosBalances = cosmosBalancesQuery({
let cosmosBalances: null | ReturnType<typeof cosmosBalancesQuery>;
$: if ($cosmosStore.address) cosmosBalances = cosmosBalancesQuery({
chainId: 'union-testnet-8',
address: $cosmosStore.address
address: $cosmosStore.address
})
$: data1 = $cosmosBalances?.data || []
$: data2 = $evmBalances?.data || []
</script>

<main>
<pre>{JSON.stringify(data1, undefined, 2)}</pre>
<pre>{JSON.stringify(data2, undefined, 2)}</pre>
</main>

<div>
<h2>Sepolia</h2>
{#if $evmBalances}
{#if $evmBalances.isLoading}
Loading...
{:else if $evmBalances.isError}
{$evmBalances.error.message}
{:else if $evmBalances.isSuccess}
<pre>{JSON.stringify($evmBalances.data, null, 2)}</pre>
{/if}
{:else}
<p>Connect your EVM wallet to continue</p>
{/if}

</div>


<div>
<h2>Cosmos</h2>
{#if $cosmosBalances}
{#if $cosmosBalances.isLoading}
Loading...
{:else if $cosmosBalances.isError}
{$cosmosBalances.error.message}
{:else if $cosmosBalances.isSuccess}
<pre>{JSON.stringify($cosmosBalances.data)}</pre>
{/if}
{:else}
<p>Connect your cosmos wallet to continue</p>
{/if}
</div>

0 comments on commit f7a7d40

Please sign in to comment.