Skip to content

Commit

Permalink
fix: use correct account id for matching buy account (#6263)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladinlight authored Feb 20, 2024
1 parent 24a1af2 commit 6b50fb5
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/components/MultiHopTrade/hooks/useReceiveAddress.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { fromAccountId, fromAssetId } from '@shapeshiftoss/caip'
import type { Asset } from '@shapeshiftoss/types'
import { useCallback, useEffect, useState } from 'react'
import { useCallback, useEffect, useMemo, useState } from 'react'
import type { GetReceiveAddressArgs } from 'components/MultiHopTrade/types'
import { getChainAdapterManager } from 'context/PluginProvider/chainAdapterSingleton'
import { useWallet } from 'hooks/useWallet/useWallet'
import { selectPortfolioAccountMetadataByAccountId } from 'state/slices/portfolioSlice/selectors'
import {
selectAccountIdByAccountNumberAndChainId,
selectAccountNumberByAccountId,
selectPortfolioAccountMetadataByAccountId,
} from 'state/slices/portfolioSlice/selectors'
import { isUtxoAccountId } from 'state/slices/portfolioSlice/utils'
import {
selectFirstHopSellAccountId,
Expand Down Expand Up @@ -40,9 +44,26 @@ export const useReceiveAddress = ({
// Selectors
const buyAsset = useAppSelector(selectInputBuyAsset)
const sellAssetAccountId = useAppSelector(selectFirstHopSellAccountId)
const sellAssetAccountNumberFilter = useMemo(
() => ({ accountId: sellAssetAccountId }),
[sellAssetAccountId],
)
const sellAssetAccountNumber = useAppSelector(state =>
selectAccountNumberByAccountId(state, sellAssetAccountNumberFilter),
)

const accountIdsByAccountNumberAndChainId = useAppSelector(
selectAccountIdByAccountNumberAndChainId,
)

const maybeMatchingBuyAccountId = useMemo(() => {
if (!buyAsset) return
if (sellAssetAccountNumber === undefined) return
return accountIdsByAccountNumberAndChainId[sellAssetAccountNumber]?.[buyAsset.chainId]
}, [accountIdsByAccountNumberAndChainId, sellAssetAccountNumber, buyAsset])

const buyAccountId = useAppSelector(state =>
selectLastHopBuyAccountId(state, { accountId: sellAssetAccountId }),
selectLastHopBuyAccountId(state, { accountId: maybeMatchingBuyAccountId }),
)
const buyAccountMetadata = useAppSelector(state =>
selectPortfolioAccountMetadataByAccountId(state, { accountId: buyAccountId }),
Expand Down

0 comments on commit 6b50fb5

Please sign in to comment.