Skip to content

Commit 0087e51

Browse files
committed
fix: make whole boost logic async
1 parent 53cbb44 commit 0087e51

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

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/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/utils/boost.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { EBoostType, IBoostedTransactions } from 'beignet';
1+
import { EBoostType, IBoostedTransactions, Wallet as TWallet } from 'beignet';
22

33
import { getActivityStore, getWalletStore } from '../store/helpers';
44
import { IActivityItem, TOnchainActivityItem } from '../store/types/activity';
55
import { TWalletName } from '../store/types/wallet';
66
import { EAvailableNetwork } from './networks';
77
import {
8-
getOnChainWallet,
8+
getOnChainWalletAsync,
9+
// getOnChainWallet,
910
getSelectedNetwork,
1011
getSelectedWallet,
1112
} from './wallet';
@@ -30,18 +31,21 @@ export const getBoostedTransactions = ({
3031

3132
/**
3233
* Returns an array of parents for a boosted transaction id.
34+
* @param {TWallet} wallet
3335
* @param {string} txId
3436
* @param {IBoostedTransactions} [boostedTransactions]
3537
* @returns {string[]}
3638
*/
3739
export const getBoostedTransactionParents = ({
40+
wallet,
3841
txId,
3942
boostedTransactions,
4043
}: {
44+
wallet: TWallet;
4145
txId: string;
4246
boostedTransactions?: IBoostedTransactions;
4347
}): string[] => {
44-
return getOnChainWallet().getBoostedTransactionParents({
48+
return wallet.getBoostedTransactionParents({
4549
txid: txId,
4650
boostedTransactions,
4751
});
@@ -57,11 +61,13 @@ export const getBoostedTransactionParents = ({
5761
* @returns {boolean}
5862
*/
5963
export const hasBoostedParents = ({
64+
wallet,
6065
txId,
6166
boostedTransactions,
6267
selectedWallet = getSelectedWallet(),
6368
selectedNetwork = getSelectedNetwork(),
6469
}: {
70+
wallet: TWallet;
6571
txId: string;
6672
boostedTransactions?: IBoostedTransactions;
6773
selectedWallet?: TWalletName;
@@ -74,6 +80,7 @@ export const hasBoostedParents = ({
7480
});
7581
}
7682
const boostedParents = getBoostedTransactionParents({
83+
wallet,
7784
txId,
7885
boostedTransactions,
7986
});
@@ -89,7 +96,7 @@ export const hasBoostedParents = ({
8996
* @param {EAvailableNetwork} [selectedNetwork]
9097
* @returns {TOnchainActivityItem|undefined}
9198
*/
92-
export const getRootParentActivity = ({
99+
const getRootParentActivity = async ({
93100
txId,
94101
items,
95102
boostedTransactions,
@@ -101,14 +108,16 @@ export const getRootParentActivity = ({
101108
boostedTransactions?: IBoostedTransactions;
102109
selectedWallet?: TWalletName;
103110
selectedNetwork?: EAvailableNetwork;
104-
}): TOnchainActivityItem | undefined => {
111+
}): Promise<TOnchainActivityItem | undefined> => {
112+
const wallet = await getOnChainWalletAsync();
105113
if (!boostedTransactions) {
106114
boostedTransactions = getBoostedTransactions({
107115
selectedWallet,
108116
selectedNetwork,
109117
});
110118
}
111119
const boostedParents = getBoostedTransactionParents({
120+
wallet,
112121
txId,
113122
boostedTransactions,
114123
});
@@ -147,7 +156,7 @@ export const getParentsActivity = ({
147156
* @param {EAvailableNetwork} [selectedNetwork]
148157
* @returns {TOnchainActivityItem[]}
149158
*/
150-
export const formatBoostedActivityItems = ({
159+
export const formatBoostedActivityItems = async ({
151160
items,
152161
boostedTransactions,
153162
selectedWallet,
@@ -157,18 +166,18 @@ export const formatBoostedActivityItems = ({
157166
boostedTransactions: IBoostedTransactions;
158167
selectedWallet: TWalletName;
159168
selectedNetwork: EAvailableNetwork;
160-
}): TOnchainActivityItem[] => {
169+
}): Promise<TOnchainActivityItem[]> => {
161170
const formattedItems: TOnchainActivityItem[] = [];
162171

163-
items.forEach((item) => {
172+
for (const item of items) {
164173
const { txId } = item;
165174

166175
// if boosted tx don't add for now
167176
if (txId in boostedTransactions) {
168-
return;
177+
continue;
169178
}
170179

171-
const rootParent = getRootParentActivity({
180+
const rootParent = await getRootParentActivity({
172181
txId,
173182
items,
174183
boostedTransactions,
@@ -179,18 +188,18 @@ export const formatBoostedActivityItems = ({
179188
// if we can't find a parent tx leave as is
180189
if (!rootParent) {
181190
formattedItems.push(item);
182-
return;
191+
continue;
183192
}
184193

185194
const parentBoostType = boostedTransactions[rootParent.txId].type;
186195

187196
// if it's an RBF tx leave as is, only mark as boosted
188197
if (parentBoostType === EBoostType.rbf) {
189198
formattedItems.push({ ...item, isBoosted: true });
190-
return;
199+
continue;
191200
}
192201

193-
const value = calculateBoostTransactionValue({
202+
const value = await calculateBoostTransactionValue({
194203
currentActivityItem: item,
195204
items,
196205
boostedTransactions,
@@ -207,7 +216,7 @@ export const formatBoostedActivityItems = ({
207216
address: rootParent.address,
208217
isBoosted: true,
209218
});
210-
});
219+
}
211220

212221
return formattedItems;
213222
};
@@ -220,7 +229,7 @@ export const formatBoostedActivityItems = ({
220229
* @param {IBoostedTransactions} boostedTransactions
221230
* @returns {number}
222231
*/
223-
export const calculateBoostTransactionValue = ({
232+
export const calculateBoostTransactionValue = async ({
224233
currentActivityItem,
225234
items,
226235
boostedTransactions,
@@ -230,14 +239,14 @@ export const calculateBoostTransactionValue = ({
230239
items: TOnchainActivityItem[];
231240
boostedTransactions: IBoostedTransactions;
232241
includeFee?: boolean;
233-
}): number => {
242+
}): Promise<number> => {
234243
const boostedTransaction = Object.values(boostedTransactions).find(
235244
(tx) => tx.childTransaction === currentActivityItem.txId,
236245
);
237246
if (!boostedTransaction) {
238247
return currentActivityItem.value;
239248
}
240-
const rootParent = getRootParentActivity({
249+
const rootParent = await getRootParentActivity({
241250
txId: currentActivityItem.txId,
242251
items,
243252
boostedTransactions,

src/utils/wallet/transfer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import {
77
import { err, ok } from '@synonymdev/result';
88
import lm, { ldk } from '@synonymdev/react-native-ldk';
99

10-
import { getCurrentWallet, getSelectedNetwork, refreshWallet } from '.';
10+
import {
11+
getCurrentWallet,
12+
getOnChainWalletAsync,
13+
getSelectedNetwork,
14+
refreshWallet,
15+
} from '.';
1116
import { btcToSats } from '../conversion';
12-
import { getBoostedTransactionParents } from '../boost';
1317
import { getTransactions } from './electrum';
1418
import {
1519
ETransferStatus,
@@ -33,6 +37,7 @@ export const getTransferForTx = async (
3337
): Promise<TTransfer | undefined> => {
3438
const { currentWallet, selectedNetwork } = getCurrentWallet();
3539
const transfers = currentWallet.transfers[selectedNetwork];
40+
const wallet = await getOnChainWalletAsync();
3641

3742
if (tx.type === EPaymentType.sent) {
3843
const transfersToSpending = transfers.filter((t) => {
@@ -46,7 +51,9 @@ export const getTransferForTx = async (
4651

4752
// check if the tx is a transfer that was boosted
4853
if (!transferToSpending) {
49-
const boostedParents = getBoostedTransactionParents({ txId: tx.txid });
54+
const boostedParents = wallet.getBoostedTransactionParents({
55+
txid: tx.txid,
56+
});
5057
const isBoosted = boostedParents.length > 0;
5158
if (isBoosted) {
5259
transferToSpending = transfersToSpending.find((t) => {

0 commit comments

Comments
 (0)