Skip to content

Commit

Permalink
fix(app): working balances query for evm
Browse files Browse the repository at this point in the history
  • Loading branch information
cor committed Jun 7, 2024
1 parent 8a5fd77 commit 7d83e8c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 16 deletions.
20 changes: 20 additions & 0 deletions app/src/lib/graphql/documents/transfers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { graphql } from "gql.tada"

export const transfersQuery = graphql(/* GraphQL */ `
query PacketsQuery($limit: Int = 100) {
v0_packets(limit: $limit, order_by: {destination_time: desc_nulls_last, source_time: desc_nulls_last}) {
from_chain_id
from_channel_id
source_port
source_block_hash
source_time
to_chain_id
to_channel_id
to_port_id
destination_block_hash
destination_time
source_data
status
}
}
`)
39 changes: 26 additions & 13 deletions app/src/lib/queries/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { createQuery } from "@tanstack/svelte-query"
import type { ChainId } from "$/lib/constants/assets.ts"
import { isValidEvmAddress } from "$lib/wallet/utilities/validate"
import { isValidCosmosAddress } from "$lib/wallet/utilities/validate";
import { raise } from "$lib/utilities/index.ts";

/**
* TODO:
Expand Down Expand Up @@ -66,7 +67,7 @@ export function evmBalancesQuery({
queryKey: ["balances", chainId, address],
enabled: isValidEvmAddress(address),
refetchOnWindowFocus: false,
refetchInterval: 1_000,
refetchInterval: 10_000,
queryFn: async () => {
const assetsToCheck =
"contractAddresses" in restParams && Array.isArray(restParams.contractAddresses)
Expand All @@ -75,18 +76,30 @@ export function evmBalancesQuery({
["erc20", "DEFAULT_TOKENS"].includes(restParams.tokenSpecification)
? restParams.tokenSpecification // if tokenSpecification is a string, use it
: "DEFAULT_TOKENS"
console.log(address, assetsToCheck)
const response = await fetch(`https://eth-sepolia.g.alchemy.com/v2/${KEY.RPC.ALCHEMY}`, {
method: "POST",
body: JSON.stringify({
id: 1,
jsonrpc: "2.0",
method: "alchemy_getTokenBalances",
params: [address, assetsToCheck]
})
})
const result = v.safeParse(evmBalancesResponseSchema, await response.json())
if (!result.success) throw new Error(`Error parsing result ${JSON.stringify(result.issues)}`);

let json: undefined | unknown;

try {
const response = await fetch(`https://eth-sepolia.g.alchemy.com/v2/${KEY.RPC.ALCHEMY}`, {
method: "POST",
body: JSON.stringify({
id: 1,
jsonrpc: "2.0",
method: "alchemy_getTokenBalances",
params: [address, assetsToCheck]
})
});
if (!response.ok) raise("error fetching from alchemy: non-200 status");
json = await response.json();
} catch(err) {
if (err instanceof Error) {
raise(`error fetching from alchemy: ${err.message}`);
}
raise(`unknown error while fetching from alchemy: ${JSON.stringify(err)}`);
}
const result = v.safeParse(evmBalancesResponseSchema, json)

if (!result.success) raise(`error parsing result ${JSON.stringify(result.issues)}`);

const tokensInfo = await getEvmTokensInfo(
result.output.result.tokenBalances.map(({ contractAddress }) => contractAddress)
Expand Down
28 changes: 28 additions & 0 deletions app/src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
<script lang="ts">
import * as Card from "$lib/components/ui/card/index.ts"
import { sepoliaStore } from "$lib/wallet/evm/config.ts"
import { cosmosStore } from "$lib/wallet/cosmos"
import AlphaNotice from "$lib/components/alpha-notice.svelte";
import { summarizeString } from "$lib/utilities/format";
</script>

<main class="flex flex-col items-center size-full p-4 mt-16">
<Card.Root class="max-w-lg">
<Card.Header>
<Card.Title>Welcome to Union</Card.Title>
</Card.Header>
<Card.Content class="flex flex-col gap-2">
<p>Connect an <b>EVM</b> and <b>Cosmos</b> wallet to begin bridging.</p>

<div>
{#if $sepoliaStore.address }
✅ EVM wallet <span class="font-mono">{summarizeString($sepoliaStore.address, 6)}</span> connected
{:else}
Connect EVM wallet
{/if}
</div>

<div>
{#if $cosmosStore.address }
✅Cosmos wallet connected
{:else}
Connect cosmos wallet
{/if}
</div>
</Card.Content>
</Card.Root>
</main>
4 changes: 1 addition & 3 deletions app/src/routes/balance/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
chainId: 'union-testnet-8',
address: $cosmosStore.address
})
</script>


Expand All @@ -26,7 +25,7 @@
{#if $evmBalances.isLoading}
Loading...
{:else if $evmBalances.isError}
{$evmBalances.error.message}
Error: {$evmBalances.error.message}
{:else if $evmBalances.isSuccess}
<div>
{#each $evmBalances.data as asset}
Expand All @@ -37,7 +36,6 @@
{:else}
<p>Connect your EVM wallet to continue</p>
{/if}

</div>


Expand Down

0 comments on commit 7d83e8c

Please sign in to comment.