diff options
| author | Adam Soltys <[email protected]> | 2026-03-03 21:40:35 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-03-03 21:40:35 -0800 |
| commit | e66a08d18f2bef2eccfe84042a2674141a73d1c2 (patch) | |
| tree | 171389b57782b7ff2abb570ac69f130f0705ecf5 /src | |
| parent | 632358158c76ceaee5db2b1879487c136289766e (diff) | |
| download | coinos-ui-e66a08d18f2bef2eccfe84042a2674141a73d1c2.tar.gz coinos-ui-e66a08d18f2bef2eccfe84042a2674141a73d1c2.zip | |
bitcoin sync routine
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/bitcoinSync.ts | 13 | ||||
| -rw-r--r-- | src/routes/(app)/+layout.svelte | 45 |
2 files changed, 58 insertions, 0 deletions
diff --git a/src/lib/bitcoinSync.ts b/src/lib/bitcoinSync.ts new file mode 100644 index 00000000..9061e2fc --- /dev/null +++ b/src/lib/bitcoinSync.ts @@ -0,0 +1,13 @@ +import { post } from "$lib/utils"; + +let syncing = false; + +export const syncBitcoinVault = async (aid: string) => { + if (syncing) return; + syncing = true; + try { + return await post("/post/bitcoin/sync", { aid }); + } finally { + syncing = false; + } +}; diff --git a/src/routes/(app)/+layout.svelte b/src/routes/(app)/+layout.svelte index 6cbcad2b..f4d036f2 100644 --- a/src/routes/(app)/+layout.svelte +++ b/src/routes/(app)/+layout.svelte @@ -1,5 +1,6 @@ <script lang="ts"> import { getWallet, subscribeToAsp, syncTransactions, settle, refresh, arkkey, arkaid, sendArk, arkSending, vaultForwarding, autoUnlockArk, getServiceWorkerWallet, cleanupServiceWorkerWallet } from "$lib/ark"; + import { syncBitcoinVault } from "$lib/bitcoinSync"; import { SvelteToast } from "@zerodevx/svelte-toast"; import { onDestroy, onMount } from "svelte"; import { close, connect, send, socket } from "$lib/socket"; @@ -17,6 +18,7 @@ fiat, rate as rateStore, cachedUser, + cachedAccounts, offline, } from "$lib/store"; import { page } from "$app/stores"; @@ -48,9 +50,41 @@ if (user) $cachedUser = user; }); + const getBtcVaultAid = () => { + const accounts = $cachedAccounts; + if (!accounts?.length) return null; + const aid = getCookie("aid") || user?.id; + const active = accounts.find((a: any) => a.id === aid); + if (active?.pubkey && active?.fingerprint && active?.type !== "ark") return active.id; + return null; + }; + + let btcSyncTimer: ReturnType<typeof setInterval> | undefined; + + const btcSync = async () => { + const aid = getBtcVaultAid(); + if (!aid) return; + const result = await syncBitcoinVault(aid); + if (!result) return; + + if (result.received > 0) { + const paidPayment = result.payments?.find((p: any) => p.iid && p.amount > 0); + if (paidPayment) { + goto(`/invoice/${paidPayment.iid}`); + } else if ($fiat && $rateStore && user?.currency) { + success(`Received ${f(toFiat(result.received, $rateStore as number), user.currency)}!`); + } else { + success(`Received ${s(result.received)}!`); + } + } + + invalidate("app:payments"); + }; + afterNavigate(() => { document.cookie = `pathname=${$page.url.pathname}; path=/; max-age=86400`; if (user && $arkkey) arkSync("afterNavigate"); + if (user) btcSync(); }); let arkSyncing = false; @@ -167,6 +201,16 @@ $effect(() => { if ($paymentSignal && $arkkey) arkSync("paymentSignal"); + if ($paymentSignal) btcSync(); + }); + + $effect(() => { + if (browser && user && getBtcVaultAid()) { + btcSyncTimer = setInterval(btcSync, 30_000); + return () => { + if (btcSyncTimer) clearInterval(btcSyncTimer); + }; + } }); @@ -253,6 +297,7 @@ close(); clearTimeout(checkTimer); if (arkRefreshTimer) clearInterval(arkRefreshTimer); + if (btcSyncTimer) clearInterval(btcSyncTimer); aspStopFunc?.(); cleanupServiceWorkerWallet(); } |
