Skip to content

Commit 63ae06a

Browse files
committed
fix: add fallback logic for fee caching
1 parent d841f52 commit 63ae06a

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

src/store/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const persistConfig = {
3434
// increase version after store shape changes
3535
version: 54,
3636
stateReconciler: autoMergeLevel2,
37-
blacklist: ['receive', 'ui', 'fees'],
37+
blacklist: ['receive', 'ui'],
3838
migrate: createMigrate(migrations, { debug: __ENABLE_MIGRATION_DEBUG__ }),
3939
};
4040

src/utils/lightning/index.ts

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -318,32 +318,51 @@ export const getFees: TGetFees = async () => {
318318
} else if (getSelectedNetwork() !== 'bitcoin') {
319319
fees = initialFeesState.onchain;
320320
} else {
321-
fees = await new Promise<IOnchainFees>((resolve, reject) => {
322-
// try twice
323-
const mpPromise = Promise.race([
324-
fetchMp().catch(fetchMp),
325-
throwTimeout(10000),
326-
]);
327-
const btPromise = Promise.race([
328-
fetchBt().catch(fetchBt),
329-
throwTimeout(10000),
330-
]).catch(() => null); // Prevent unhandled rejection
331-
332-
// prioritize mempool.space over blocktank
333-
mpPromise.then(resolve).catch(() => {
334-
btPromise
335-
.then((btFees) => {
336-
if (btFees !== null) {
337-
resolve(btFees);
338-
} else {
339-
reject(new Error('Failed to fetch fees'));
340-
}
341-
})
342-
.catch(reject);
321+
try {
322+
fees = await new Promise<IOnchainFees>((resolve, reject) => {
323+
// try twice
324+
const mpPromise = Promise.race([
325+
fetchMp().catch(fetchMp),
326+
throwTimeout(10000),
327+
]);
328+
const btPromise = Promise.race([
329+
fetchBt().catch(fetchBt),
330+
throwTimeout(10000),
331+
]).catch(() => null); // Prevent unhandled rejection
332+
333+
// prioritize mempool.space over blocktank
334+
mpPromise.then(resolve).catch(() => {
335+
btPromise
336+
.then((btFees) => {
337+
if (btFees !== null) {
338+
resolve(btFees);
339+
} else {
340+
reject(new Error('Failed to fetch fees'));
341+
}
342+
})
343+
.catch(reject);
344+
});
343345
});
344-
});
345346

346-
updateOnchainFeeEstimates({ feeEstimates: fees });
347+
updateOnchainFeeEstimates({ feeEstimates: fees });
348+
} catch (error) {
349+
// FALLBACK: Use cached fees if network fetch fails
350+
const cachedFees = getFeesStore().onchain;
351+
const feeAge = Date.now() - cachedFees.timestamp;
352+
const MAX_FEE_AGE = 60 * 60 * 1000; // 1 hour
353+
354+
if (feeAge < MAX_FEE_AGE) {
355+
console.warn(
356+
`getFees: Network fetch failed, using cached fees (age: ${Math.round(feeAge / 1000 / 60)} minutes)`,
357+
);
358+
fees = cachedFees;
359+
} else {
360+
console.warn(
361+
`getFees: Network fetch failed and cached fees too old (age: ${Math.round(feeAge / 1000 / 60)} minutes), using initial state`,
362+
);
363+
fees = initialFeesState.onchain;
364+
}
365+
}
347366
}
348367

349368
return {

0 commit comments

Comments
 (0)