From 8d77d569ac47160ae95fea40206602787edd9377 Mon Sep 17 00:00:00 2001 From: woodenfurniture <125113430+woodenfurniture@users.noreply.github.com> Date: Thu, 7 Nov 2024 14:52:48 +1100 Subject: [PATCH] fix: corrupted accountId in redux causing receive address dramas --- .../MultiHopTrade/hooks/useReceiveAddress.tsx | 32 ++++++++++--------- .../slices/tradeInputSlice/tradeInputSlice.ts | 25 +++++++++++++++ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/components/MultiHopTrade/hooks/useReceiveAddress.tsx b/src/components/MultiHopTrade/hooks/useReceiveAddress.tsx index a50c34b977e..f8825d8d403 100644 --- a/src/components/MultiHopTrade/hooks/useReceiveAddress.tsx +++ b/src/components/MultiHopTrade/hooks/useReceiveAddress.tsx @@ -45,23 +45,12 @@ export const useReceiveAddress = ({ // flashing during state changes. Any of the conditions below returning true should be treated as // "we're not yet ready to determine if a wallet receive address is available" const isInitializing = useMemo(() => { - if (!buyAsset || !wallet || !buyAccountId || !buyAccountMetadata) { - return true - } - - const buyAssetChainId = buyAsset.chainId - const buyAssetAccountChainId = fromAccountId(buyAccountId).chainId - - /** - * do NOT remove - * super dangerous - don't use the wrong bip44 params to generate receive addresses - */ - if (buyAssetChainId !== buyAssetAccountChainId) { + if (!buyAsset || !wallet) { return true } return false - }, [buyAccountId, buyAccountMetadata, buyAsset, wallet]) + }, [buyAsset, wallet]) const { data: walletReceiveAddress, isLoading } = useQuery({ queryKey: [ @@ -76,9 +65,22 @@ export const useReceiveAddress = ({ queryFn: isInitializing ? skipToken : async () => { - // Already covered in shouldSkip, but TypeScript lyfe mang. + // Already partially covered in isInitializing, but TypeScript lyfe mang. if (!buyAsset || !wallet || !buyAccountId || !buyAccountMetadata) { - return + return undefined + } + + const buyAssetChainId = buyAsset.chainId + const buyAssetAccountChainId = buyAccountId + ? fromAccountId(buyAccountId).chainId + : undefined + + /** + * do NOT remove + * super dangerous - don't use the wrong bip44 params to generate receive addresses + */ + if (buyAssetChainId !== buyAssetAccountChainId) { + return undefined } if (isUtxoAccountId(buyAccountId) && !buyAccountMetadata?.accountType) diff --git a/src/state/slices/tradeInputSlice/tradeInputSlice.ts b/src/state/slices/tradeInputSlice/tradeInputSlice.ts index b2e75338605..ec961819ffb 100644 --- a/src/state/slices/tradeInputSlice/tradeInputSlice.ts +++ b/src/state/slices/tradeInputSlice/tradeInputSlice.ts @@ -59,6 +59,14 @@ export const tradeInput = createSlice({ state.sellAmountCryptoPrecision = '0' } + // Reset the buyAssetAccountId if the chain has changed + if (asset.chainId !== state.buyAsset.chainId) { + state.buyAssetAccountId = undefined + } + + // Reset the manual receive address + state.manualReceiveAddress = undefined + state.buyAsset = asset }, setSellAsset: (state, action: PayloadAction) => { @@ -76,6 +84,14 @@ export const tradeInput = createSlice({ // clear the sell amount state.sellAmountCryptoPrecision = '0' + // Reset the sellAssetAccountId if the chain has changed + if (asset.chainId !== state.sellAsset.chainId) { + state.sellAssetAccountId = undefined + } + + // Reset the manual receive address + state.manualReceiveAddress = undefined + state.sellAsset = action.payload }, setSellAssetAccountId: (state, action: PayloadAction) => { @@ -89,10 +105,19 @@ export const tradeInput = createSlice({ state.sellAmountCryptoPrecision = bnOrZero(action.payload).toString() }, switchAssets: state => { + // Switch the assets const buyAsset = state.sellAsset state.sellAsset = state.buyAsset state.buyAsset = buyAsset state.sellAmountCryptoPrecision = '0' + + // Switch the account IDs + const sellAssetAccountId = state.sellAssetAccountId + state.sellAssetAccountId = state.buyAssetAccountId + state.buyAssetAccountId = sellAssetAccountId + + // Reset the manual receive address + state.manualReceiveAddress = undefined }, setManualReceiveAddress: (state, action: PayloadAction) => { state.manualReceiveAddress = action.payload