Skip to content

Commit 53cbb44

Browse files
committed
fix: WIP
1 parent 136a3d4 commit 53cbb44

File tree

9 files changed

+136
-89
lines changed

9 files changed

+136
-89
lines changed

src/AppOnboarded.tsx

Lines changed: 5 additions & 6 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,22 +57,23 @@ const AppOnboarded = (): ReactElement => {
5957
// on AppState change
6058
const appStateSubscription = AppState.addEventListener(
6159
'change',
62-
(nextAppState) => {
60+
async (nextAppState) => {
61+
const electrum = await getOnChainWalletElectrumAsync();
6362
// on App to foreground
6463
if (
6564
appState.current.match(/inactive|background/) &&
6665
nextAppState === 'active'
6766
) {
6867
// resubscribe to electrum connection changes
69-
electrum?.startConnectionPolling();
68+
electrum.startConnectionPolling();
7069
}
7170

7271
// on App to background
7372
if (
7473
appState.current.match(/active|inactive/) &&
7574
nextAppState === 'background'
7675
) {
77-
electrum?.stopConnectionPolling();
76+
electrum.stopConnectionPolling();
7877
}
7978

8079
appState.current = nextAppState;

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: 23 additions & 23 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,
@@ -125,7 +126,7 @@ export const generateNewReceiveAddress = async ({
125126
keyDerivationPath?: IKeyDerivationPath;
126127
}): Promise<Result<IAddress>> => {
127128
try {
128-
const wallet = getOnChainWallet();
129+
const wallet = await getOnChainWalletAsync();
129130
return wallet.generateNewReceiveAddress({ addressType, keyDerivationPath });
130131
} catch (e) {
131132
console.log(e);
@@ -138,22 +139,22 @@ export const generateNewReceiveAddress = async ({
138139
* @returns {Promise<string>}
139140
*/
140141
export const clearUtxos = async (): Promise<string> => {
141-
const wallet = getOnChainWallet();
142+
const wallet = await getOnChainWalletAsync();
142143
return await wallet.clearUtxos();
143144
};
144145

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-
};
146+
// export const updateWalletBalance = ({
147+
// balance,
148+
// }: {
149+
// balance: number;
150+
// }): Result<string> => {
151+
// try {
152+
// const wallet = getOnChainWalletAsync();
153+
// return wallet.updateWalletBalance({ balance });
154+
// } catch (e) {
155+
// return err(e);
156+
// }
157+
// };
157158

158159
/**
159160
* Parses and adds unconfirmed transactions to the store.
@@ -216,7 +217,7 @@ export const injectFakeTransaction = (
216217
// scanAllAddresses?: boolean;
217218
// replaceStoredTransactions?: boolean;
218219
// }): Promise<Result<string | undefined>> => {
219-
// const wallet = getOnChainWallet();
220+
// const wallet = async getOnChainWalletAsync();
220221
// return await wallet.updateTransactions({
221222
// scanAllAddresses,
222223
// replaceStoredTransactions,
@@ -233,7 +234,7 @@ export const deleteOnChainTransactionById = async ({
233234
}: {
234235
txid: string;
235236
}): Promise<void> => {
236-
const wallet = getOnChainWallet();
237+
const wallet = await getOnChainWalletAsync();
237238
return await wallet.deleteOnChainTransactionById({ txid });
238239
};
239240

@@ -255,7 +256,7 @@ export const addBoostedTransaction = async ({
255256
type?: EBoostType;
256257
fee: number;
257258
}): Promise<Result<IBoostedTransaction>> => {
258-
const wallet = getOnChainWallet();
259+
const wallet = await getOnChainWalletAsync();
259260
return await wallet.addBoostedTransaction({
260261
newTxId,
261262
oldTxId,
@@ -290,7 +291,7 @@ export const setupOnChainTransaction = async ({
290291
outputs?: IOutput[]; // Used to pre-specify outputs to use.
291292
} = {}): Promise<TSetupTransactionResponse> => {
292293
rbf = rbf ?? getSettingsStore().rbf;
293-
const transaction = getOnChainWalletTransaction();
294+
const transaction = await getOnChainWalletTransactionAsync();
294295
return await transaction.setupTransaction({
295296
inputTxHashes,
296297
utxos,
@@ -310,7 +311,7 @@ export const getChangeAddress = async ({
310311
}: {
311312
addressType?: EAddressType;
312313
}): Promise<Result<IAddress>> => {
313-
const wallet = getOnChainWallet();
314+
const wallet = await getOnChainWalletAsync();
314315
return await wallet.getChangeAddress(addressType);
315316
};
316317

@@ -331,8 +332,7 @@ export const updateSendTransaction = (
331332
* @returns {Result<string>}
332333
*/
333334
export const resetSendTransaction = async (): Promise<Result<string>> => {
334-
await waitForWallet();
335-
const transaction = getOnChainWalletTransaction();
335+
const transaction = await getOnChainWalletTransactionAsync();
336336
return transaction.resetSendTransaction();
337337
};
338338

@@ -341,7 +341,7 @@ export const updateSelectedAddressType = async ({
341341
}: {
342342
addressType: EAddressType;
343343
}): Promise<void> => {
344-
const wallet = getOnChainWallet();
344+
const wallet = await getOnChainWalletAsync();
345345
const addressTypesToMonitor = wallet.addressTypesToMonitor;
346346
if (!addressTypesToMonitor.includes(addressType)) {
347347
// Append the new address type so we monitor it in subsequent sessions.

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
};

src/utils/ledger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from './lightning';
1717
import { EAvailableNetwork } from './networks';
1818
import {
19-
getOnChainWalletElectrum,
19+
getOnChainWalletElectrumAsync,
2020
getScriptHash,
2121
getSelectedNetwork,
2222
getSelectedWallet,
@@ -59,7 +59,7 @@ export const getChannelCloseTime = async (
5959

6060
// we have to construct IAddress to get the history
6161
const scriptHash = await getScriptHash(address, selectedNetwork);
62-
const el = getOnChainWalletElectrum();
62+
const el = await getOnChainWalletElectrumAsync();
6363
const scriptHashes: IAddress[] = [
6464
{
6565
index: 0,

src/utils/lightning/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ import {
4242
getBip39Passphrase,
4343
getCurrentAddressIndex,
4444
getMnemonicPhrase,
45-
getOnChainWalletData,
46-
getOnChainWalletElectrum,
45+
getOnChainWalletDataAsync,
46+
getOnChainWalletElectrumAsync,
4747
getSelectedNetwork,
4848
getSelectedWallet,
4949
ldkSeed,
@@ -214,7 +214,7 @@ export const setLdkStoragePath = (): Promise<Result<string>> =>
214214
const broadcastTransaction: TBroadcastTransaction = async (
215215
rawTx: string,
216216
): Promise<Result<string>> => {
217-
const electrum = getOnChainWalletElectrum();
217+
const electrum = await getOnChainWalletElectrumAsync();
218218
const res = await electrum.broadcastTransaction({
219219
rawTx,
220220
subscribeToOutputAddress: false,
@@ -229,7 +229,7 @@ const broadcastTransaction: TBroadcastTransaction = async (
229229
const getScriptPubKeyHistory = async (
230230
scriptPubKey: string,
231231
): Promise<TGetAddressHistory[]> => {
232-
const electrum = getOnChainWalletElectrum();
232+
const electrum = await getOnChainWalletElectrumAsync();
233233
return await electrum.getScriptPubKeyHistory(scriptPubKey);
234234
};
235235

@@ -913,7 +913,7 @@ export const getBestBlock = async (
913913
selectedNetwork: EAvailableNetwork = getSelectedNetwork(),
914914
): Promise<THeader> => {
915915
try {
916-
const beignetHeader = getOnChainWalletData().header;
916+
const beignetHeader = (await getOnChainWalletDataAsync()).header;
917917
const storageHeader = getWalletStore().header[selectedNetwork];
918918
const header =
919919
beignetHeader.height > storageHeader.height
@@ -937,7 +937,7 @@ export const getTransactionData = async (
937937
let transactionData = DefaultTransactionDataShape;
938938
try {
939939
const data = [{ tx_hash: txId }];
940-
const electrum = getOnChainWalletElectrum();
940+
const electrum = await getOnChainWalletElectrumAsync();
941941
const response = await electrum.getTransactions({
942942
txHashes: data,
943943
});

src/utils/wallet/electrum.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { err, ok, Result } from '@synonymdev/result';
22

33
import { EAvailableNetwork } from '../networks';
44
import {
5+
ITransaction,
56
getCustomElectrumPeers,
6-
getOnChainWallet,
7+
getOnChainWalletAsync,
78
getOnChainWalletElectrum,
9+
getOnChainWalletElectrumAsync,
810
getSelectedNetwork,
9-
ITransaction,
1011
refreshWallet,
1112
} from './index';
1213
import {
@@ -35,7 +36,7 @@ export type TUnspentAddressScriptHashData = {
3536
* @returns {Promise<boolean>}
3637
*/
3738
export const isConnectedElectrum = async (): Promise<boolean> => {
38-
const electrum = getOnChainWalletElectrum();
39+
const electrum = await getOnChainWalletElectrumAsync();
3940
return electrum.isConnected();
4041
};
4142

@@ -65,7 +66,7 @@ export const listUnspentAddressScriptHashes = async ({
6566
}: {
6667
addresses: TUnspentAddressScriptHashData;
6768
}): Promise<Result<IGetUtxosResponse>> => {
68-
const electrum = getOnChainWalletElectrum();
69+
const electrum = await getOnChainWalletElectrumAsync();
6970
const unspentAddressResult = await electrum.listUnspentAddressScriptHashes({
7071
addresses,
7172
});
@@ -89,7 +90,7 @@ export const subscribeToAddresses = async ({
8990
scriptHashes?: string[];
9091
onReceive?: () => void;
9192
} = {}): Promise<Result<string>> => {
92-
const electrum = getOnChainWalletElectrum();
93+
const electrum = await getOnChainWalletElectrumAsync();
9394
return electrum.subscribeToAddresses({ scriptHashes, onReceive });
9495
};
9596

@@ -132,7 +133,7 @@ export const getTransactions = async ({
132133
}: {
133134
txHashes: ITxHash[];
134135
}): Promise<Result<IGetTransactions>> => {
135-
const electrum = getOnChainWalletElectrum();
136+
const electrum = await getOnChainWalletElectrumAsync();
136137
return await electrum.getTransactions({ txHashes });
137138
};
138139

@@ -147,7 +148,7 @@ export interface IPeerData {
147148
* @return {Promise<Result<IPeerData>>}
148149
*/
149150
export const getConnectedPeer = async (): Promise<Result<IPeerData>> => {
150-
const electrum = getOnChainWalletElectrum();
151+
const electrum = await getOnChainWalletElectrumAsync();
151152
const peerData = await electrum.getConnectedPeer();
152153
return peerData as Result<IPeerData>;
153154
};
@@ -173,7 +174,7 @@ export const getTransactionsFromInputs = async ({
173174
}: {
174175
txHashes: ITxHash[];
175176
}): Promise<Result<IGetTransactionsFromInputs>> => {
176-
const electrum = getOnChainWalletElectrum();
177+
const electrum = await getOnChainWalletElectrumAsync();
177178
return await electrum.getTransactionsFromInputs({ txHashes });
178179
};
179180

@@ -197,7 +198,7 @@ export const getAddressHistory = async ({
197198
scriptHashes?: IAddress[];
198199
scanAllAddresses?: boolean;
199200
}): Promise<Result<IGetAddressHistoryResponse[]>> => {
200-
const electrum = getOnChainWalletElectrum();
201+
const electrum = await getOnChainWalletElectrumAsync();
201202
return await electrum.getAddressHistory({ scriptHashes, scanAllAddresses });
202203
};
203204

@@ -217,7 +218,7 @@ export const connectToElectrum = async ({
217218
showNotification?: boolean;
218219
selectedNetwork?: EAvailableNetwork;
219220
} = {}): Promise<Result<string>> => {
220-
const electrum = getOnChainWalletElectrum();
221+
const electrum = await getOnChainWalletElectrumAsync();
221222

222223
// Attempt to disconnect from any old/lingering connections
223224
await electrum?.disconnect();
@@ -254,7 +255,7 @@ export const getAddressBalance = async ({
254255
}: {
255256
addresses: string[];
256257
}): Promise<Result<number>> => {
257-
const wallet = getOnChainWallet();
258+
const wallet = await getOnChainWalletAsync();
258259
return await wallet.getAddressesBalance(addresses);
259260
};
260261

@@ -269,7 +270,7 @@ export const getBlockHex = async ({
269270
}: {
270271
height?: number;
271272
}): Promise<Result<string>> => {
272-
const electrum = getOnChainWalletElectrum();
273+
const electrum = await getOnChainWalletElectrumAsync();
273274
return await electrum.getBlockHex({ height });
274275
};
275276

@@ -280,14 +281,14 @@ export const getBlockHex = async ({
280281
* @param {EAvailableNetwork} [selectedNetwork]
281282
* @returns {string}
282283
*/
283-
export const getBlockHashFromHex = ({
284-
blockHex,
285-
}: {
286-
blockHex?: string;
287-
}): string => {
288-
const electrum = getOnChainWalletElectrum();
289-
return electrum.getBlockHashFromHex({ blockHex });
290-
};
284+
// export const getBlockHashFromHex = ({
285+
// blockHex,
286+
// }: {
287+
// blockHex?: string;
288+
// }): string => {
289+
// const electrum = getOnChainWalletElectrum();
290+
// return electrum.getBlockHashFromHex({ blockHex });
291+
// };
291292

292293
/**
293294
* Returns last known block height, and it's corresponding hex from local storage.

0 commit comments

Comments
 (0)