From 86149a32d7e954f474747ac98252edea900745ff Mon Sep 17 00:00:00 2001 From: Lukas Date: Wed, 8 Jan 2025 15:17:03 +0100 Subject: [PATCH] fix(app): save store reactivity progress --- .../lib/components/TransferFrom/index.svelte | 15 ++++++- .../TransferFrom/transfer/balances.ts | 17 +++++++ .../TransferFrom/transfer/context.ts | 45 +++++++------------ .../components/TransferFrom/transfer/index.ts | 9 ++-- app/src/routes/transfer/+page.svelte | 4 +- 5 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 app/src/lib/components/TransferFrom/transfer/balances.ts diff --git a/app/src/lib/components/TransferFrom/index.svelte b/app/src/lib/components/TransferFrom/index.svelte index 8eb48a83a7..7468c19c94 100644 --- a/app/src/lib/components/TransferFrom/index.svelte +++ b/app/src/lib/components/TransferFrom/index.svelte @@ -7,8 +7,21 @@ import Chains from "$lib/components/TransferFrom/components/Cube/faces/Chains.sv import Assets from "$lib/components/TransferFrom/components/Cube/faces/Assets.svelte" import Transfer from "$lib/components/TransferFrom/components/Cube/faces/Transfer.svelte" import Cube from "$lib/components/TransferFrom/components/Cube/index.svelte" +import type {Chain, UserAddresses} from "$lib/types.ts"; +import {userBalancesQuery} from "$lib/queries/balance"; +import {balanceStore, userAddress} from "$lib/components/TransferFrom/transfer/balances.ts"; -const stores = createTransferStore() +export let chains: Array + +$: userBalancesQuery({ + chains, + userAddr: $userAddress, + connected: true +}).subscribe(x => { + balanceStore.set(x) +}) + +const stores = createTransferStore(chains) diff --git a/app/src/lib/components/TransferFrom/transfer/balances.ts b/app/src/lib/components/TransferFrom/transfer/balances.ts new file mode 100644 index 0000000000..c821e58404 --- /dev/null +++ b/app/src/lib/components/TransferFrom/transfer/balances.ts @@ -0,0 +1,17 @@ +import {derived, type Readable, writable} from "svelte/store"; +import type {UserAddresses} from "$lib/types.ts"; +import {userAddrCosmos} from "$lib/wallet/cosmos"; +import {userAddrEvm} from "$lib/wallet/evm"; +import {userAddressAptos} from "$lib/wallet/aptos"; +import type {QueryObserverResult} from "@tanstack/query-core"; + +export let userAddress: Readable = derived( + [userAddrCosmos, userAddrEvm, userAddressAptos], + ([$userAddrCosmos, $userAddrEvm, $userAddressAptos]) => ({ + evm: $userAddrEvm, + cosmos: $userAddrCosmos, + aptos: $userAddressAptos + }) +) + +export let balanceStore = writable[]>([]) \ No newline at end of file diff --git a/app/src/lib/components/TransferFrom/transfer/context.ts b/app/src/lib/components/TransferFrom/transfer/context.ts index 713b377b27..24215c19da 100644 --- a/app/src/lib/components/TransferFrom/transfer/context.ts +++ b/app/src/lib/components/TransferFrom/transfer/context.ts @@ -1,11 +1,7 @@ -import { derived, get, type Readable } from "svelte/store" -import { userAddrCosmos } from "$lib/wallet/cosmos" -import { userAddrEvm } from "$lib/wallet/evm" -import { userAddressAptos } from "$lib/wallet/aptos" -import { userBalancesQuery } from "$lib/queries/balance" +import { derived, type Readable } from "svelte/store" import type { Chain, UserAddresses } from "$lib/types" -import { useQueryClient } from "@tanstack/svelte-query" import type { Address } from "$lib/wallet/types" +import { balanceStore, userAddress } from "./balances" export type BalanceRecord = { balance: bigint @@ -27,27 +23,17 @@ export interface ContextStore { balances: BalancesList } -export function createContextStore(): Readable { - const queryClient = useQueryClient() - - const queryData = >( - key: Array, - filter?: (value: T[number]) => boolean - ): T => { - const data = queryClient.getQueryData(key) ?? [] - return (filter ? data.filter(filter) : data) as T - } - - const chains = queryData>(["chains"], chain => chain.enabled_staging) - - const userAddress = derived( - [userAddrCosmos, userAddrEvm, userAddressAptos], - ([cosmos, evm, aptos]) => ({ evm, aptos, cosmos }) - ) as Readable - +export function createContextStore(chains: Array): Readable { const balances = derived( - [userAddress, userBalancesQuery({ chains, connected: true, userAddr: get(userAddress) })], - ([_, $rawBalances]) => { + balanceStore, + ($rawBalances) => { + if (!$rawBalances?.length) { + return chains.map(chain => ({ + chainId: chain.chain_id, + balances: [] + })) + } + return chains.map((chain, chainIndex) => { const balanceResult = $rawBalances[chainIndex] @@ -63,7 +49,10 @@ export function createContextStore(): Readable { chainId: chain.chain_id, balances: balanceResult.data.map(balance => ({ ...balance, - balance: BigInt(balance.balance) + balance: BigInt(balance.balance), + gasToken: balance.gasToken ?? false, + address: balance.address as Address, + symbol: balance.symbol || balance.address })) } }) @@ -75,4 +64,4 @@ export function createContextStore(): Readable { userAddress: $userAddress, balances: $balances })) -} +} \ No newline at end of file diff --git a/app/src/lib/components/TransferFrom/transfer/index.ts b/app/src/lib/components/TransferFrom/transfer/index.ts index 4decf4f45c..e28da5c6b7 100644 --- a/app/src/lib/components/TransferFrom/transfer/index.ts +++ b/app/src/lib/components/TransferFrom/transfer/index.ts @@ -12,6 +12,9 @@ import { createValidationStore, type ValidationStore } from "$lib/components/TransferFrom/transfer/validation.ts" +import type { Chain, UserAddresses } from "$lib/types" +import type { QueryObserverResult } from "@tanstack/svelte-query" + export interface TransferStore { rawIntents: RawIntentsStore @@ -20,9 +23,9 @@ export interface TransferStore { validation: Readable } -export function createTransferStore(): TransferStore { +export function createTransferStore(chains: Array): TransferStore { const rawIntents = createRawIntentsStore() - const context = createContextStore() + const context = createContextStore(chains) const intents = createIntentStore(rawIntents, context) const validation = createValidationStore(rawIntents, intents, context) @@ -32,4 +35,4 @@ export function createTransferStore(): TransferStore { intents, validation } -} +} \ No newline at end of file diff --git a/app/src/routes/transfer/+page.svelte b/app/src/routes/transfer/+page.svelte index a4f16c8e8e..48a76f4eba 100644 --- a/app/src/routes/transfer/+page.svelte +++ b/app/src/routes/transfer/+page.svelte @@ -7,8 +7,8 @@ import TransferFrom from "$lib/components/TransferFrom/index.svelte" Union | Send - +
- +