Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/screens/Settings/AddressTypePreference/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const AddressTypeSettings = ({
onPress: async (): Promise<void> => {
navigation.goBack();
await updateSelectedAddressType({ addressType: addressType.type });
await refreshWallet({ lightning: false, onchain: true });
await refreshWallet({ lightning: false });
},
testID: addressType.type,
})),
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Settings/DevSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ const DevSettings = ({
const fakeTx = getFakeTransaction(id);
fakeTx[id].height = 0;
injectFakeTransaction(fakeTx);
refreshWallet({ selectedWallet, selectedNetwork }).then();
refreshWallet().then();
},
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/screens/Settings/ElectrumConfig/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ const ElectrumConfig = ({
autoComplete="off"
keyboardType="default"
autoCorrect={false}
onChangeText={setHost}
returnKeyType="done"
testID="HostInput"
onChangeText={setHost}
/>

<Caption13Up style={styles.label} color="secondary">
Expand All @@ -326,8 +326,9 @@ const ElectrumConfig = ({
autoComplete="off"
keyboardType="number-pad"
autoCorrect={false}
onChangeText={setPort}
returnKeyType="done"
testID="PortInput"
onChangeText={setPort}
/>

<View
Expand Down
6 changes: 1 addition & 5 deletions src/screens/Settings/GapLimit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ const GapLimit = (): ReactElement => {
});
if (res.isOk()) {
dispatch(updateWallet({ gapLimitOptions: res.value }));
await refreshWallet({
lightning: false,
onchain: true,
scanAllAddresses: true,
});
await refreshWallet({ lightning: false, scanAllAddresses: true });
showToast({
type: 'success',
title: t('gap.gap_limit_update_title'),
Expand Down
12 changes: 2 additions & 10 deletions src/screens/Wallets/BoostPrompt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ import { useAppDispatch, useAppSelector } from '../../hooks/redux';
import { rootNavigation } from '../../navigation/root/RootNavigator';
import { resetSendTransaction } from '../../store/actions/wallet';
import { viewControllerSelector } from '../../store/reselect/ui';
import {
selectedNetworkSelector,
selectedWalletSelector,
transactionSelector,
} from '../../store/reselect/wallet';
import { transactionSelector } from '../../store/reselect/wallet';
import { closeSheet } from '../../store/slices/ui';
import { TOnchainActivityItem } from '../../store/types/activity';
import { EUnit } from '../../store/types/wallet';
Expand All @@ -48,8 +44,6 @@ const BoostForm = ({
const { t } = useTranslation('wallet');
const dispatch = useAppDispatch();
const transaction = useAppSelector(transactionSelector);
const selectedNetwork = useAppSelector(selectedNetworkSelector);
const selectedWallet = useAppSelector(selectedWalletSelector);

const [preparing, setPreparing] = useState(true);
const [loading, setLoading] = useState(false);
Expand All @@ -69,8 +63,6 @@ const BoostForm = ({
useEffect(() => {
(async (): Promise<void> => {
const res = await setupBoost({
selectedWallet,
selectedNetwork,
txid: activityItem.txId,
});
setPreparing(false);
Expand All @@ -84,7 +76,7 @@ const BoostForm = ({
return (): void => {
resetSendTransaction();
};
}, [activityItem.txId, selectedNetwork, selectedWallet, dispatch]);
}, [activityItem.txId, dispatch]);

const onSwitchView = (): void => {
if (showCustom) {
Expand Down
30 changes: 30 additions & 0 deletions src/store/mmkv-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,36 @@ export const reduxStorage: Storage = {
},
};

// Used to prevent duplicate notifications for the same txId that seems to occur when:
// - when Bitkit is brought from background to foreground
// - connection to electrum server is lost and then re-established
export const receivedTxIds = {
STORAGE_KEY: 'receivedTxIds',

// Get stored txIds
get: (): string[] => {
return JSON.parse(storage.getString(receivedTxIds.STORAGE_KEY) || '[]');
},

// Save txIds to storage
save: (txIds: string[]): void => {
storage.set(receivedTxIds.STORAGE_KEY, JSON.stringify(txIds));
},

// Add a new txId
add: (txId: string): void => {
const txIds = receivedTxIds.get();
txIds.push(txId);
receivedTxIds.save(txIds);
},

// Check if txId exists
has: (txId: string): boolean => {
const txIds = receivedTxIds.get();
return txIds.includes(txId);
},
};

export class WebRelayCache {
location: string;

Expand Down
12 changes: 2 additions & 10 deletions src/store/utils/blocktank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,10 @@ export const startChannelPurchase = async ({
/**
* Creates, broadcasts and confirms a given Blocktank channel purchase by orderId.
* @param {string} orderId
* @param {EAvailableNetwork} [selectedNetwork]
* @param {TWalletName} [selectedWallet]
* @returns {Promise<Result<string>>}
*/
export const confirmChannelPurchase = async ({
order,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
order: IBtOrder;
selectedNetwork?: EAvailableNetwork;
Expand Down Expand Up @@ -293,12 +289,8 @@ export const confirmChannelPurchase = async ({

watchOrder(order.id).then();
dispatch(setLightningSetupStep(0));
refreshWallet({
onchain: true,
lightning: false, // No need to refresh lightning wallet at this time.
selectedWallet,
selectedNetwork,
}).then();
// No need to refresh lightning wallet at this time.
refreshWallet({ lightning: false }).then();

if (!__E2E__) {
await scheduleNotifications();
Expand Down
11 changes: 0 additions & 11 deletions src/store/utils/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,9 @@ export const showNewOnchainTxPrompt = ({
value,
},
});

dispatch(closeSheet('receiveNavigation'));
};

export const showNewTxPrompt = (txId: string): void => {
const activityItem = getActivityStore().items.find(({ id }) => id === txId);

if (activityItem) {
vibrate({ type: 'default' });
showBottomSheet('newTxPrompt', { activityItem });
dispatch(closeSheet('receiveNavigation'));
}
};

export const checkForAppUpdate = async (): Promise<void> => {
const currentBuild = Number(getBuildNumber());
const response = await fetch(releaseUrl);
Expand Down
1 change: 0 additions & 1 deletion src/utils/startup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ export const startWalletServices = async ({
onchain: restore,
lightning,
scanAllAddresses: restore,
showNotification: !restore,
});
await runChecks({ selectedWallet, selectedNetwork });
}
Expand Down
8 changes: 1 addition & 7 deletions src/utils/wallet/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,7 @@ export const runStorageCheck = async ({
);

await clearUtxos();

await refreshWallet({
onchain: true,
lightning: true,
scanAllAddresses: true,
showNotification: false,
});
await refreshWallet({ scanAllAddresses: true });

return ok('Replaced Impacted Addresses');
};
Expand Down
4 changes: 1 addition & 3 deletions src/utils/wallet/electrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,9 @@ export const getAddressHistory = async ({
*/
export const connectToElectrum = async ({
customPeers,
showNotification = true,
selectedNetwork = getSelectedNetwork(),
}: {
customPeers?: TServer[];
showNotification?: boolean;
selectedNetwork?: EAvailableNetwork;
} = {}): Promise<Result<string>> => {
const electrum = await getOnChainWalletElectrumAsync();
Expand All @@ -240,7 +238,7 @@ export const connectToElectrum = async ({
}

// Check for any new transactions that we might have missed while disconnected.
refreshWallet({ showNotification }).then();
refreshWallet().then();

return ok(connectRes.value);
};
Expand Down
24 changes: 4 additions & 20 deletions src/utils/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { BIP32Factory } from 'bip32';
import * as bip39 from 'bip39';
import { getAddressInfo } from 'bitcoin-address-validation';
import * as bitcoin from 'bitcoinjs-lib';
import { InteractionManager } from 'react-native';

import {
generateNewReceiveAddress,
Expand All @@ -54,6 +53,7 @@ import {
getStore,
getWalletStore,
} from '../../store/helpers';
import { receivedTxIds } from '../../store/mmkv-storage';
import {
getDefaultGapLimitOptions,
getDefaultWalletShape,
Expand All @@ -75,7 +75,7 @@ import {
import { updateActivityList } from '../../store/utils/activity';
import { refreshOrdersList } from '../../store/utils/blocktank';
import { moveMetaIncTxTags } from '../../store/utils/metadata';
import { showNewOnchainTxPrompt, showNewTxPrompt } from '../../store/utils/ui';
import { showNewOnchainTxPrompt } from '../../store/utils/ui';
import BitcoinActions from '../bitcoin-actions';
import { btcToSats } from '../conversion';
import { promiseTimeout } from '../helpers';
Expand Down Expand Up @@ -147,25 +147,16 @@ export const refreshWallet = async ({
onchain = true,
lightning = true,
scanAllAddresses = false, // If set to false, on-chain scanning will adhere to the gap limit (20).
showNotification = true, // Whether to show newTxPrompt on new incoming transactions.
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
onchain?: boolean;
lightning?: boolean;
scanAllAddresses?: boolean;
showNotification?: boolean;
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
} = {}): Promise<Result<string>> => {
try {
// wait for interactions/animations to be completed
await new Promise((resolve) => {
InteractionManager.runAfterInteractions(() => resolve(null));
});

let notificationTxid: string | undefined;

if (onchain) {
await refreshBeignet(scanAllAddresses);
}
Expand All @@ -180,10 +171,6 @@ export const refreshWallet = async ({
moveMetaIncTxTags();
}

if (showNotification && notificationTxid) {
showNewTxPrompt(notificationTxid);
}

return ok('');
} catch (e) {
return err(e);
Expand Down Expand Up @@ -1013,9 +1000,6 @@ const onElectrumConnectionChange = (isConnected: boolean): void => {
}
};

// Used to prevent duplicate notifications for the same txid that seems to occur when Bitkit is brought from background to foreground.
const receivedTxids: string[] = [];

const onMessage: TOnMessage = async (key, data): Promise<void> => {
switch (key) {
case 'transactionReceived': {
Expand All @@ -1026,15 +1010,15 @@ const onMessage: TOnMessage = async (key, data): Promise<void> => {
!wallet?.isSwitchingNetworks
) {
const { transaction } = txMsg;
const isDuplicate = receivedTxids.includes(transaction.txid);
const isDuplicate = receivedTxIds.has(transaction.txid);
const transfer = await getTransferForTx(transaction);

if (!transfer && !isDuplicate) {
showNewOnchainTxPrompt({
id: transaction.txid,
value: btcToSats(transaction.value),
});
receivedTxids.push(transaction.txid);
receivedTxIds.add(transaction.txid);
}
}
refreshWallet({ lightning: false }).then();
Expand Down
13 changes: 1 addition & 12 deletions src/utils/wallet/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -745,26 +745,15 @@ const runCoinSelect = async ({

/**
*
* @param {TWalletName} [selectedWallet]
* @param {EAvailableNetwork} [selectedNetwork]
* @param {string} txid
*/
export const setupBoost = async ({
txid,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
txid: string;
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
}): Promise<Result<Partial<ISendTransaction>>> => {
// Ensure all utxos are up-to-date if attempting to boost immediately after a transaction.
const refreshResponse = await refreshWallet({
onchain: true,
lightning: false,
selectedWallet,
selectedNetwork,
});
const refreshResponse = await refreshWallet({ lightning: false });
if (refreshResponse.isErr()) {
return err(refreshResponse.error.message);
}
Expand Down
Loading