persist dashboard prices and balances in stores

This commit is contained in:
2026-08-02 18:44:53 +08:00
parent c61fea938a
commit c0f6417925
3 changed files with 84 additions and 84 deletions
@@ -56,6 +56,8 @@ export const MoneroWallet = () => {
let refreshInFlight: Promise<void> | null = null
let syncing = $state(false)
let syncRetryTimer: ReturnType<typeof setTimeout> | null = null
let price = $state<string | null>(null)
let priceInFlight = false
const rpcTimeout = <T>(promise: Promise<T>, ms: number, label: string): Promise<T> =>
new Promise<T>((resolve, reject) => {
@@ -478,6 +480,18 @@ export const MoneroWallet = () => {
return refreshInFlight
}
const fetchPrice = async () => {
const rpc = electrobun.rpc
if (!rpc || priceInFlight) return
priceInFlight = true
try {
const [p] = await tryCatch(rpc.request.fetchMoneroPrice({}))
if (p) price = p.usd
} finally {
priceInFlight = false
}
}
const send = async (
address: string,
amountAtomic: string,
@@ -597,6 +611,9 @@ export const MoneroWallet = () => {
get syncing() {
return syncing
},
get price() {
return price
},
get error() {
return error
},
@@ -646,6 +663,7 @@ export const MoneroWallet = () => {
autoUnlock,
unlockWallet,
refresh,
fetchPrice,
send,
sendAll,
createAccount,
@@ -29,6 +29,9 @@ export const XrpWallet = () => {
let loading = $state(false)
let error = $state('')
let wallets = $state<XrpWalletInfo[]>([])
let balances = $state<Record<string, { balance: number; funded: boolean }>>({})
let balancesLoading = $state(false)
let fetchBalancesInFlight = false
let currentWalletId = $state<string | null>(null)
let currentPasswordHash = $state<string | null>(null)
@@ -319,6 +322,34 @@ export const XrpWallet = () => {
})()
}
const fetchBalances = async () => {
const rpc = electrobun.rpc
if (!rpc || fetchBalancesInFlight) return
if (wallets.length === 0) return
fetchBalancesInFlight = true
balancesLoading = true
try {
const [p] = await tryCatch(rpc.request.fetchXrpPrice({}))
if (p) price = p.usd
await Promise.allSettled(
wallets.map(async (w) => {
const [res] = await tryCatch(
rpc.request.xrpGetBalance({ address: w.address }),
)
if (res) {
balances[w.address] = {
balance: parseFloat(res.balance),
funded: res.funded,
}
}
}),
)
} finally {
balancesLoading = false
fetchBalancesInFlight = false
}
}
const send = async (
to: string,
amount: string,
@@ -399,6 +430,12 @@ export const XrpWallet = () => {
get wallets() {
return wallets
},
get balances() {
return balances
},
get balancesLoading() {
return balancesLoading
},
get currentWalletId() {
return currentWalletId
},
@@ -424,6 +461,7 @@ export const XrpWallet = () => {
login,
logout,
refresh,
fetchBalances,
createWallet,
importWallet,
lock,