From eedf7695d64e4c00af3d178484900c7c57087ac0 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 15 Feb 2026 22:26:43 -0800 Subject: ark refresh --- lib/ark.ts | 25 ++++++++++++++++++------- lib/zmq.ts | 3 +++ 2 files changed, 21 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/ark.ts b/lib/ark.ts index 3522c9cf..2c7fbe2b 100644 --- a/lib/ark.ts +++ b/lib/ark.ts @@ -24,6 +24,8 @@ const getWallet = async () => { }; let refreshing = false; +let lastRefresh = 0; +const REFRESH_COOLDOWN = 30_000; const failedBoardingOutpoints = new Set(); const withTimeout = (promise: Promise, ms: number, label: string) => @@ -34,10 +36,12 @@ const withTimeout = (promise: Promise, ms: number, label: string) => ), ]); -export const refreshArkWallet = async () => { - if (refreshing) return; +export const refreshArkWallet = async (force = false) => { + const now = Date.now(); + if (refreshing || (!force && now - lastRefresh < REFRESH_COOLDOWN)) return; try { refreshing = true; + lastRefresh = now; const w = await getWallet(); const balance = await w.getBalance(); l( @@ -122,18 +126,25 @@ export const refreshArkWallet = async () => { } }; -// Check every 2 minutes -setInterval(refreshArkWallet, 2 * 60 * 1000); -// Initial check after 30s startup delay +// Initial check after 30s startup delay; ongoing checks triggered by ZMQ blocks setTimeout(refreshArkWallet, 30_000); export const sendArk = async (address: string, amount: number) => { const timeout = new Promise((_, reject) => - setTimeout(() => reject(new Error("Ark send timed out")), 45000), + setTimeout(() => reject(new Error("Ark send timed out")), 60000), ); const send = async () => { const w = await getWallet(); - return w.sendBitcoin({ address, amount }); + try { + return await w.sendBitcoin({ address, amount }); + } catch (e: any) { + if (/insufficient funds/i.test(e.message)) { + l("ark send got insufficient funds, refreshing wallet and retrying"); + await refreshArkWallet(true); + return w.sendBitcoin({ address, amount }); + } + throw e; + } }; return Promise.race([send(), timeout]); }; diff --git a/lib/zmq.ts b/lib/zmq.ts index b743a750..09f5a4ba 100644 --- a/lib/zmq.ts +++ b/lib/zmq.ts @@ -1,6 +1,7 @@ import net from "node:net"; import config from "$config"; import { btcNetwork } from "$lib/esplora"; +import { refreshArkWallet } from "$lib/ark"; import { err, l, warn } from "$lib/logging"; import { processWatchedTx } from "$lib/payments"; import { Transaction } from "@scure/btc-signer"; @@ -116,6 +117,8 @@ const handleRawBlock = async (raw: Uint8Array) => { warn("zmq block tx decode failed", i, e.message); } } + + refreshArkWallet().catch(() => {}); }; // ZMTP 3.0 NULL protocol -- cgit v1.2.3