Skip to content

Commit 7846f69

Browse files
committed
fix: check mempool fees before starting LDK
1 parent 59cfcdf commit 7846f69

File tree

7 files changed

+39
-30
lines changed

7 files changed

+39
-30
lines changed

src/navigation/bottom-sheet/SendNavigation.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const SendNavigation = (): ReactElement => {
117117

118118
const onOpen = async (): Promise<void> => {
119119
if (!transaction?.lightningInvoice) {
120-
await updateOnchainFeeEstimates({ selectedNetwork, forceUpdate: true });
120+
await updateOnchainFeeEstimates({ forceUpdate: true });
121121
if (!transaction?.inputs.length) {
122122
await setupOnChainTransaction();
123123
}

src/screens/Settings/AddressViewer/index.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,13 +750,8 @@ const AddressViewer = ({
750750
// Switching networks requires us to reset LDK.
751751
await setupLdk({ selectedWallet, selectedNetwork });
752752
// Start wallet services with the newly selected network.
753-
await startWalletServices({
754-
selectedNetwork: config.selectedNetwork,
755-
});
756-
await updateOnchainFeeEstimates({
757-
selectedNetwork: config.selectedNetwork,
758-
forceUpdate: true,
759-
});
753+
await startWalletServices({ selectedNetwork: config.selectedNetwork });
754+
await updateOnchainFeeEstimates({ forceUpdate: true });
760755
updateActivityList();
761756
await syncLedger();
762757
}

src/store/actions/wallet.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,6 @@ export const setWalletData = async <K extends keyof IWalletData>(
663663
case 'feeEstimates': {
664664
const feeEstimates = data2 as IWalletData[typeof value];
665665
updateOnchainFeeEstimates({
666-
selectedNetwork: getNetworkFromBeignet(network),
667666
feeEstimates,
668667
forceUpdate: true,
669668
});

src/store/utils/fees.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
1-
import { ok, err, Result } from '@synonymdev/result';
1+
import { IOnchainFees } from 'beignet';
2+
import { Result, err, ok } from '@synonymdev/result';
23

4+
import { getOnChainWalletAsync } from '../../utils/wallet';
35
import { dispatch, getFeesStore } from '../helpers';
46
import { updateOnchainFees } from '../slices/fees';
5-
import { getFeeEstimates } from '../../utils/wallet/transactions';
6-
import { EAvailableNetwork } from '../../utils/networks';
7-
import { getOnChainWalletAsync, getSelectedNetwork } from '../../utils/wallet';
8-
import { IOnchainFees } from 'beignet';
9-
10-
export const REFRESH_INTERVAL = 60 * 30; // in seconds, 30 minutes
117

128
export const updateOnchainFeeEstimates = async ({
13-
selectedNetwork = getSelectedNetwork(),
149
forceUpdate = false,
1510
feeEstimates,
1611
}: {
17-
selectedNetwork: EAvailableNetwork;
1812
forceUpdate?: boolean;
1913
feeEstimates?: IOnchainFees;
2014
}): Promise<Result<string>> => {
@@ -24,12 +18,7 @@ export const updateOnchainFeeEstimates = async ({
2418
}
2519

2620
if (!feeEstimates) {
27-
const timestamp = feesStore.onchain.timestamp;
28-
const difference = Math.floor((Date.now() - timestamp) / 1000);
29-
if (!forceUpdate && difference < REFRESH_INTERVAL) {
30-
return ok('On-chain fee estimates are up to date.');
31-
}
32-
const feeEstimatesRes = await getFeeEstimates(selectedNetwork);
21+
const feeEstimatesRes = await refreshOnchainFeeEstimates({ forceUpdate });
3322
if (feeEstimatesRes.isErr()) {
3423
return err(feeEstimatesRes.error);
3524
}

src/utils/helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
55

66
import { i18nTime } from '../utils/i18n';
77

8+
/**
9+
* Returns the result of a promise, or an error if the promise takes too long to resolve.
10+
* @param {number} ms The time to wait in milliseconds.
11+
* @param {Promise<any>} promise The promise to resolve.
12+
* @returns {Promise<T>}
13+
*/
814
export const promiseTimeout = <T>(
915
ms: number,
1016
promise: Promise<any>,

src/utils/lightning/index.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as bitcoin from 'bitcoinjs-lib';
44
import ecc from '@bitcoinerlab/secp256k1';
55
import RNFS from 'react-native-fs';
66
import { err, ok, Result } from '@synonymdev/result';
7-
import { EPaymentType, TGetAddressHistory } from 'beignet';
7+
import { EPaymentType, IOnchainFees, TGetAddressHistory } from 'beignet';
88
import lm, {
99
ldk,
1010
defaultUserConfig,
@@ -234,7 +234,12 @@ const getScriptPubKeyHistory = async (
234234
};
235235

236236
const getFees: TGetFees = async () => {
237-
const fees = getFeesStore().onchain;
237+
const res = await promiseTimeout<Result<IOnchainFees>>(
238+
2000,
239+
refreshOnchainFeeEstimates({}),
240+
);
241+
const fees = res.isOk() ? res.value : getFeesStore().onchain;
242+
238243
return {
239244
//https://github.com/lightningdevkit/rust-lightning/blob/main/CHANGELOG.md#api-updates
240245
onChainSweep: fees.fast,
@@ -335,6 +340,20 @@ export const setupLdk = async ({
335340
return err(backupRes.error);
336341
}
337342

343+
// force fetch fees if they are older than 20 minutes
344+
let attempts = 0;
345+
while (
346+
Date.now() - getFeesStore().onchain.timestamp > 20 * 60 * 1000 &&
347+
!getFeesStore().override
348+
) {
349+
if (attempts > 4) {
350+
return err('Failed to fetch on-chain fees');
351+
}
352+
await refreshOnchainFeeEstimates({});
353+
await sleep(100); // give Beignet time to propagate the new fees
354+
attempts++;
355+
}
356+
338357
const lmStart = await lm.start({
339358
account: account.value,
340359
getFees,

src/utils/wallet/transactions.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ export const broadcastBoost = async ({
887887
*/
888888
export const getFeeEstimates = async (
889889
selectedNetwork: EAvailableNetwork = getSelectedNetwork(),
890+
forceUpdate: boolean,
890891
): Promise<Result<IOnchainFees>> => {
891892
try {
892893
if (__E2E__) {
@@ -904,11 +905,11 @@ export const getFeeEstimates = async (
904905
}
905906

906907
const wallet = await getOnChainWalletAsync();
907-
const feeRes = await wallet.getFeeEstimates();
908-
if (!feeRes) {
909-
return err('Unable to get fee estimates.');
908+
const feeRes = await wallet.updateFeeEstimates(forceUpdate);
909+
if (feeRes.isErr()) {
910+
return err(feeRes.error);
910911
}
911-
return ok(feeRes);
912+
return ok(feeRes.value);
912913
} catch (e) {
913914
return err(e);
914915
}

0 commit comments

Comments
 (0)