Skip to content

Commit

Permalink
fix: corrupted accountId in redux causing receive address dramas
Browse files Browse the repository at this point in the history
  • Loading branch information
woodenfurniture committed Nov 7, 2024
1 parent 984ba21 commit 8d77d56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/components/MultiHopTrade/hooks/useReceiveAddress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions src/state/slices/tradeInputSlice/tradeInputSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Asset>) => {
Expand All @@ -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<AccountId | undefined>) => {
Expand All @@ -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<string | undefined>) => {
state.manualReceiveAddress = action.payload
Expand Down

0 comments on commit 8d77d56

Please sign in to comment.