Skip to content

Commit 744f898

Browse files
committed
fix: async beignet wallet
1 parent a37d15b commit 744f898

File tree

16 files changed

+181
-129
lines changed

16 files changed

+181
-129
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"@synonymdev/slashtags-widget-price-feed": "1.1.0",
6464
"@synonymdev/web-relay": "1.0.7",
6565
"bech32": "2.0.0",
66-
"beignet": "0.0.46",
66+
"beignet": "0.0.48",
6767
"bip21": "2.0.3",
6868
"bip32": "4.0.0",
6969
"bip39": "3.1.0",

src/AppOnboarded.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import RootNavigator from './navigation/root/RootNavigator';
77
import InactivityTracker from './components/InactivityTracker';
88
import { showToast } from './utils/notifications';
99
import { startWalletServices } from './utils/startup';
10-
import { getOnChainWalletElectrum } from './utils/wallet';
10+
import { getOnChainWalletElectrumAsync } from './utils/wallet';
1111
import { unsubscribeFromLightningSubscriptions } from './utils/lightning';
1212
import { useAppSelector } from './hooks/redux';
1313
import { dispatch } from './store/helpers';
@@ -25,8 +25,6 @@ import {
2525
import { updateSettings } from './store/slices/settings';
2626
// import { updateExchangeRates } from './store/actions/wallet';
2727

28-
const electrum = getOnChainWalletElectrum();
29-
3028
const AppOnboarded = (): ReactElement => {
3129
const { t } = useTranslation('other');
3230
const appState = useRef(AppState.currentState);
@@ -59,24 +57,24 @@ const AppOnboarded = (): ReactElement => {
5957
// on AppState change
6058
const appStateSubscription = AppState.addEventListener(
6159
'change',
62-
(nextAppState) => {
60+
async (nextAppState) => {
6361
dispatch(updateUi({ appState: nextAppState }));
64-
62+
const electrum = await getOnChainWalletElectrumAsync();
6563
// on App to foreground
6664
if (
6765
appState.current.match(/inactive|background/) &&
6866
nextAppState === 'active'
6967
) {
7068
// resubscribe to electrum connection changes
71-
electrum?.startConnectionPolling();
69+
electrum.startConnectionPolling();
7270
}
7371

7472
// on App to background
7573
if (
7674
appState.current.match(/active|inactive/) &&
7775
nextAppState === 'background'
7876
) {
79-
electrum?.stopConnectionPolling();
77+
electrum.stopConnectionPolling();
8078
}
8179

8280
appState.current = nextAppState;

src/hooks/wallet.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Wallet as TWallet } from 'beignet';
2+
import { useEffect, useState } from 'react';
13
import { useTranslation } from 'react-i18next';
24

35
import { useAppDispatch, useAppSelector } from '../hooks/redux';
@@ -9,6 +11,7 @@ import { ignoreSwitchUnitToast } from '../store/slices/user';
911
import { EUnit } from '../store/types/wallet';
1012
import i18n from '../utils/i18n';
1113
import { showToast } from '../utils/notifications';
14+
import { getOnChainWalletAsync } from '../utils/wallet';
1215
import { useCurrency } from './displayValues';
1316

1417
/**
@@ -63,3 +66,23 @@ export const useSwitchUnitAnnounced = (): (() => void) => {
6366

6467
return switchUnitAnnounced;
6568
};
69+
70+
/**
71+
* Wait for the onchain wallet to be loaded.
72+
*/
73+
export const useOnchainWallet = (): { wallet: TWallet | null } => {
74+
const [wallet, setWallet] = useState<TWallet | null>(null);
75+
76+
useEffect(() => {
77+
const getWallet = async (): Promise<void> => {
78+
const w = await getOnChainWalletAsync();
79+
setWallet(w);
80+
};
81+
82+
getWallet();
83+
}, []);
84+
85+
return {
86+
wallet,
87+
};
88+
};

src/screens/Activity/ActivityDetail.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ import type {
9595
RootStackScreenProps,
9696
} from '../../navigation/types';
9797
import { i18nTime } from '../../utils/i18n';
98-
import { useSwitchUnit } from '../../hooks/wallet';
98+
import { useOnchainWallet, useSwitchUnit } from '../../hooks/wallet';
9999
import { contactsSelector } from '../../store/reselect/slashtags';
100100
import { ETransferStatus } from '../../store/types/wallet';
101101

@@ -157,6 +157,7 @@ const OnchainActivityDetail = ({
157157
const isSend = txType === EPaymentType.sent;
158158
const total = isSend ? fee + value : value;
159159

160+
const { wallet } = useOnchainWallet();
160161
const { t } = useTranslation('wallet');
161162
const { t: tTime } = useTranslation('intl', { i18n: i18nTime });
162163
const switchUnit = useSwitchUnit();
@@ -210,17 +211,21 @@ const OnchainActivityDetail = ({
210211
}, [confirmed, isBoosted, txId]);
211212

212213
const boostedParents = useMemo(() => {
214+
if (!wallet) {
215+
return [];
216+
}
213217
return getBoostedTransactionParents({
218+
wallet,
214219
txId,
215220
boostedTransactions,
216221
});
217-
}, [boostedTransactions, txId]);
222+
}, [boostedTransactions, txId, wallet]);
218223

219224
const hasBoostedParents = useMemo(() => {
220225
return boostedParents.length > 0;
221226
}, [boostedParents.length]);
222227

223-
const handleBoostParentPress = (parentTxId): void => {
228+
const handleBoostParentPress = (parentTxId: string): void => {
224229
const activityItem = activityItems.find((i) => {
225230
return i.activityType === EActivityType.onchain && i.txId === parentTxId;
226231
});
@@ -301,6 +306,10 @@ const OnchainActivityDetail = ({
301306
return <View />;
302307
}, [txDetails]);
303308

309+
if (!wallet) {
310+
return <ActivityIndicator />;
311+
}
312+
304313
let fees = fee;
305314
let paymentAmount = value;
306315
let status = (

src/screens/Settings/GapLimit/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { gapLimitOptionsSelector } from '../../../store/reselect/wallet';
1212
import { ScrollView, TextInput, View } from '../../../styles/components';
1313
import { Caption13Up } from '../../../styles/text';
1414
import { showToast } from '../../../utils/notifications';
15-
import { getOnChainWallet, refreshWallet } from '../../../utils/wallet';
15+
import { getOnChainWalletAsync, refreshWallet } from '../../../utils/wallet';
1616

1717
const GapLimit = ({}: SettingsScreenProps<'GapLimit'>): ReactElement => {
1818
const { t } = useTranslation('settings');
@@ -68,7 +68,7 @@ const GapLimit = ({}: SettingsScreenProps<'GapLimit'>): ReactElement => {
6868

6969
const saveGapLimit = async (): Promise<void> => {
7070
setLoading(true);
71-
const wallet = getOnChainWallet();
71+
const wallet = await getOnChainWalletAsync();
7272
const res = wallet.updateGapLimit({
7373
lookAhead: Number(lookAhead),
7474
lookBehind: Number(lookBehind),

src/store/actions/wallet.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ import {
2525
createDefaultWallet,
2626
getCurrentWallet,
2727
getOnChainWallet,
28+
getOnChainWalletAsync,
2829
getOnChainWalletTransaction,
30+
getOnChainWalletTransactionAsync,
2931
getSelectedNetwork,
3032
getSelectedWallet,
3133
refreshWallet,
32-
waitForWallet,
3334
} from '../../utils/wallet';
3435
import {
3536
dispatch,
@@ -38,7 +39,7 @@ import {
3839
getWalletStore,
3940
} from '../helpers';
4041
import { EAvailableNetwork } from '../../utils/networks';
41-
import { removeKeyFromObject } from '../../utils/helpers';
42+
import { removeKeyFromObject, sleep } from '../../utils/helpers';
4243
import { TGetImpactedAddressesRes } from '../types/checks';
4344
import { updateActivityList } from '../utils/activity';
4445
import { ETransactionSpeed } from '../types/settings';
@@ -94,6 +95,7 @@ export const createWalletThunk = async ({
9495
return err(response.error.message);
9596
}
9697
dispatch(createWallet(response.value));
98+
await sleep(1000); // give Beignet some time to propagate the data
9799
dispatch(setWalletExits());
98100
return ok('');
99101
} catch (e) {
@@ -125,7 +127,7 @@ export const generateNewReceiveAddress = async ({
125127
keyDerivationPath?: IKeyDerivationPath;
126128
}): Promise<Result<IAddress>> => {
127129
try {
128-
const wallet = getOnChainWallet();
130+
const wallet = await getOnChainWalletAsync();
129131
return wallet.generateNewReceiveAddress({ addressType, keyDerivationPath });
130132
} catch (e) {
131133
console.log(e);
@@ -138,23 +140,10 @@ export const generateNewReceiveAddress = async ({
138140
* @returns {Promise<string>}
139141
*/
140142
export const clearUtxos = async (): Promise<string> => {
141-
const wallet = getOnChainWallet();
143+
const wallet = await getOnChainWalletAsync();
142144
return await wallet.clearUtxos();
143145
};
144146

145-
export const updateWalletBalance = ({
146-
balance,
147-
}: {
148-
balance: number;
149-
}): Result<string> => {
150-
try {
151-
const wallet = getOnChainWallet();
152-
return wallet.updateWalletBalance({ balance });
153-
} catch (e) {
154-
return err(e);
155-
}
156-
};
157-
158147
/**
159148
* Parses and adds unconfirmed transactions to the store.
160149
* @param {TWalletName} [selectedWallet]
@@ -216,7 +205,7 @@ export const injectFakeTransaction = (
216205
// scanAllAddresses?: boolean;
217206
// replaceStoredTransactions?: boolean;
218207
// }): Promise<Result<string | undefined>> => {
219-
// const wallet = getOnChainWallet();
208+
// const wallet = async getOnChainWalletAsync();
220209
// return await wallet.updateTransactions({
221210
// scanAllAddresses,
222211
// replaceStoredTransactions,
@@ -233,7 +222,7 @@ export const deleteOnChainTransactionById = async ({
233222
}: {
234223
txid: string;
235224
}): Promise<void> => {
236-
const wallet = getOnChainWallet();
225+
const wallet = await getOnChainWalletAsync();
237226
return await wallet.deleteOnChainTransactionById({ txid });
238227
};
239228

@@ -255,7 +244,7 @@ export const addBoostedTransaction = async ({
255244
type?: EBoostType;
256245
fee: number;
257246
}): Promise<Result<IBoostedTransaction>> => {
258-
const wallet = getOnChainWallet();
247+
const wallet = await getOnChainWalletAsync();
259248
return await wallet.addBoostedTransaction({
260249
newTxId,
261250
oldTxId,
@@ -290,7 +279,7 @@ export const setupOnChainTransaction = async ({
290279
outputs?: IOutput[]; // Used to pre-specify outputs to use.
291280
} = {}): Promise<TSetupTransactionResponse> => {
292281
rbf = rbf ?? getSettingsStore().rbf;
293-
const transaction = getOnChainWalletTransaction();
282+
const transaction = await getOnChainWalletTransactionAsync();
294283
return await transaction.setupTransaction({
295284
inputTxHashes,
296285
utxos,
@@ -310,7 +299,7 @@ export const getChangeAddress = async ({
310299
}: {
311300
addressType?: EAddressType;
312301
}): Promise<Result<IAddress>> => {
313-
const wallet = getOnChainWallet();
302+
const wallet = await getOnChainWalletAsync();
314303
return await wallet.getChangeAddress(addressType);
315304
};
316305

@@ -331,8 +320,7 @@ export const updateSendTransaction = (
331320
* @returns {Result<string>}
332321
*/
333322
export const resetSendTransaction = async (): Promise<Result<string>> => {
334-
await waitForWallet();
335-
const transaction = getOnChainWalletTransaction();
323+
const transaction = await getOnChainWalletTransactionAsync();
336324
return transaction.resetSendTransaction();
337325
};
338326

@@ -341,7 +329,7 @@ export const updateSelectedAddressType = async ({
341329
}: {
342330
addressType: EAddressType;
343331
}): Promise<void> => {
344-
const wallet = getOnChainWallet();
332+
const wallet = await getOnChainWalletAsync();
345333
const addressTypesToMonitor = wallet.addressTypesToMonitor;
346334
if (!addressTypesToMonitor.includes(addressType)) {
347335
// Append the new address type so we monitor it in subsequent sessions.

src/store/utils/activity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const updateOnChainActivityList = async (): Promise<Result<string>> => {
101101
});
102102
const activityItems = await Promise.all(promises);
103103

104-
const boostFormattedItems = formatBoostedActivityItems({
104+
const boostFormattedItems = await formatBoostedActivityItems({
105105
items: activityItems,
106106
boostedTransactions,
107107
selectedWallet,

src/store/utils/fees.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { dispatch, getFeesStore } from '../helpers';
44
import { updateOnchainFees } from '../slices/fees';
55
import { getFeeEstimates } from '../../utils/wallet/transactions';
66
import { EAvailableNetwork } from '../../utils/networks';
7-
import { getOnChainWallet, getSelectedNetwork } from '../../utils/wallet';
7+
import { getOnChainWalletAsync, getSelectedNetwork } from '../../utils/wallet';
88
import { IOnchainFees } from 'beignet';
99

1010
export const REFRESH_INTERVAL = 60 * 30; // in seconds, 30 minutes
@@ -46,6 +46,6 @@ export const refreshOnchainFeeEstimates = async ({
4646
}: {
4747
forceUpdate?: boolean;
4848
}): Promise<Result<IOnchainFees>> => {
49-
const wallet = getOnChainWallet();
49+
const wallet = await getOnChainWalletAsync();
5050
return await wallet.updateFeeEstimates(forceUpdate);
5151
};

0 commit comments

Comments
 (0)