Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improve tradeQuoteSlice selectors #7560

Merged
merged 3 commits into from
Aug 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,15 @@ const ApprovalStepPending = ({
const [isExactAllowance, toggleIsExactAllowance] = useToggle(isLifiStep ? true : false)

const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp()
const hopExecutionMetadataFilter = useMemo(() => {
return {
tradeId: activeTradeId,
hopIndex,
}
}, [activeTradeId, hopIndex])

const { state, allowanceReset, approval } = useAppSelector(state =>
selectHopExecutionMetadata(state, activeTradeId, hopIndex),
selectHopExecutionMetadata(state, hopExecutionMetadataFilter),
)

const isAwaitingReset = useMemo(() => {
Expand Down Expand Up @@ -374,8 +380,14 @@ const ApprovalStepComplete = ({
activeTradeId,
}: ApprovalStepProps) => {
const translate = useTranslate()
const hopExecutionMetadataFilter = useMemo(() => {
return {
tradeId: activeTradeId,
hopIndex,
}
}, [activeTradeId, hopIndex])
const { state, allowanceReset, approval } = useAppSelector(state =>
selectHopExecutionMetadata(state, activeTradeId, hopIndex),
selectHopExecutionMetadata(state, hopExecutionMetadataFilter),
)

const stepIndicator = useMemo(() => {
Expand Down Expand Up @@ -453,8 +465,14 @@ export const ApprovalStep = ({
isAllowanceResetStep,
activeTradeId,
}: ApprovalStepProps) => {
const hopExecutionMetadataFilter = useMemo(() => {
return {
tradeId: activeTradeId,
hopIndex,
}
}, [activeTradeId, hopIndex])
const { state } = useAppSelector(state =>
selectHopExecutionMetadata(state, activeTradeId, hopIndex),
selectHopExecutionMetadata(state, hopExecutionMetadataFilter),
)

const isComplete = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,32 @@ export const Hop = ({
number: { toCrypto },
} = useLocaleFormatter()
const translate = useTranslate()
const hopTotalProtocolFeesFiatPrecisionFilter = useMemo(() => {
return {
hopIndex,
}
}, [hopIndex])
const networkFeeFiatPrecision = useAppSelector(state =>
selectHopNetworkFeeUserCurrencyPrecision(state, hopIndex),
selectHopNetworkFeeUserCurrencyPrecision(state, hopTotalProtocolFeesFiatPrecisionFilter),
)
const protocolFeeFiatPrecision = useAppSelector(state =>
selectHopTotalProtocolFeesFiatPrecision(state, hopIndex),
)
const isMultiHopTrade = useAppSelector(selectIsActiveQuoteMultiHop)

const hopExecutionMetadataFilter = useMemo(() => {
return {
tradeId: activeTradeId,
hopIndex,
}
}, [activeTradeId, hopIndex])

const {
state: hopExecutionState,
approval,
swap,
allowanceReset,
} = useAppSelector(state => selectHopExecutionMetadata(state, activeTradeId, hopIndex))
} = useAppSelector(state => selectHopExecutionMetadata(state, hopExecutionMetadataFilter))

const isError = useMemo(
() => [approval.state, swap.state].includes(TransactionExecutionState.Failed),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ export const HopTransactionStep = ({

const checkLedgerAppOpenIfLedgerConnected = useLedgerOpenApp()

const hopExecutionMetadataFilter = useMemo(() => {
return {
tradeId: activeTradeId,
hopIndex,
}
}, [activeTradeId, hopIndex])

const {
swap: { state: swapTxState, sellTxHash, buyTxHash, message },
} = useAppSelector(state => selectHopExecutionMetadata(state, activeTradeId, hopIndex))
} = useAppSelector(state => selectHopExecutionMetadata(state, hopExecutionMetadataFilter))

const isError = useMemo(() => swapTxState === TransactionExecutionState.Failed, [swapTxState])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ export const useAllowanceApproval = (
const dispatch = useAppDispatch()
const { showErrorToast } = useErrorHandler()
const wallet = useWallet().state.wallet ?? undefined
const sellAssetAccountId = useAppSelector(state => selectHopSellAccountId(state, hopIndex))

const hopSellAccountIdFilter = useMemo(() => {
return {
hopIndex,
}
}, [hopIndex])

const sellAssetAccountId = useAppSelector(state =>
selectHopSellAccountId(state, hopSellAccountIdFilter),
)

const isReset = useMemo(() => allowanceType === AllowanceType.Reset, [allowanceType])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,16 @@ export const useThorStreamingProgress = (
const streamingSwapDataRef = useRef<ThornodeStreamingSwapResponseSuccess>()
const { poll, cancelPolling } = usePoll<ThornodeStreamingSwapResponseSuccess | undefined>()
const dispatch = useAppDispatch()
const hopExecutionMetadataFilter = useMemo(() => {
return {
tradeId: confirmedTradeId,
hopIndex,
}
}, [confirmedTradeId, hopIndex])

const {
swap: { sellTxHash, streamingSwap: streamingSwapMeta },
} = useAppSelector(state => selectHopExecutionMetadata(state, confirmedTradeId, hopIndex))
} = useAppSelector(state => selectHopExecutionMetadata(state, hopExecutionMetadataFilter))

useEffect(() => {
// don't start polling until we have a tx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ export const useTradeExecution = (
const trackMixpanelEvent = useMixpanel()
const hasMixpanelSuccessOrFailFiredRef = useRef(false)

const sellAssetAccountId = useAppSelector(state => selectHopSellAccountId(state, hopIndex))
const hopSellAccountIdFilter = useMemo(() => {
return {
hopIndex,
}
}, [hopIndex])

const sellAssetAccountId = useAppSelector(state =>
selectHopSellAccountId(state, hopSellAccountIdFilter),
)

const accountMetadataFilter = useMemo(
() => ({ accountId: sellAssetAccountId }),
Expand Down
13 changes: 13 additions & 0 deletions src/state/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import type { QueryStatus } from '@reduxjs/toolkit/dist/query'
import type { AccountId, AssetId, ChainId } from '@shapeshiftoss/caip'
import type { TradeQuote } from '@shapeshiftoss/swapper'
import type { HistoryTimeframe } from '@shapeshiftoss/types'
import type { TxStatus } from '@shapeshiftoss/unchained-client'
import createCachedSelector from 're-reselect'
Expand Down Expand Up @@ -52,6 +53,8 @@ type ParamFilter = Partial<{
feeModel: ParameterModel
timeframe: HistoryTimeframe
onlyConnectedChains: boolean
hopIndex: number
tradeId: TradeQuote['id']
}>

type ParamFilterKey = keyof ParamFilter
Expand All @@ -66,6 +69,13 @@ export const selectParamFromFilter = <T extends ParamFilterKey>(param: T) =>
`${param}-${filter?.[param] ?? param}`,
)

export const selectRequiredParamFromFilter = <T extends ParamFilterKey>(param: T) =>
createCachedSelector(
(_state: ReduxState, filter: Required<Pick<ParamFilter, T>>): NonNullable<ParamFilter[T]> =>
filter[param] as NonNullable<ParamFilter[T]>,
(paramValue: NonNullable<ParamFilter[T]>): NonNullable<ParamFilter[T]> => paramValue,
)((_state: ReduxState, filter: Required<Pick<ParamFilter, T>>) => `${param}-${filter[param]}`)

export const selectAccountIdParamFromFilter = selectParamFromFilter('accountId')
export const selectAccountIdsParamFromFilter = selectParamFromFilter('accountIds')
export const selectFromParamFromFilter = selectParamFromFilter('from')
Expand All @@ -88,3 +98,6 @@ export const selectTxStatusParamFromFilter = selectParamFromFilter('txStatus')
export const selectFeeModelParamFromFilter = selectParamFromFilter('feeModel')
export const selectTimeframeParamFromFilter = selectParamFromFilter('timeframe')
export const selectOnlyConnectedChainsParamFromFilter = selectParamFromFilter('onlyConnectedChains')

export const selectHopIndexParamFromRequiredFilter = selectRequiredParamFromFilter('hopIndex')
export const selectTradeIdParamFromRequiredFilter = selectRequiredParamFromFilter('tradeId')
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 8 additions & 4 deletions src/state/slices/tradeQuoteSlice/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import { TradeQuoteRequestError, TradeQuoteWarning } from 'state/apis/swapper/ty
import { getEnabledSwappers } from 'state/helpers'
import type { ReduxState } from 'state/reducer'
import { createDeepEqualOutputSelector } from 'state/selector-utils'
import {
selectHopIndexParamFromRequiredFilter,
selectTradeIdParamFromRequiredFilter,
} from 'state/selectors'
import { selectFeeAssetById } from 'state/slices/assetsSlice/selectors'
import {
selectFirstHopSellAccountId,
Expand Down Expand Up @@ -484,7 +488,7 @@ export const selectSecondHopNetworkFeeUserCurrencyPrecision: Selector<
export const selectHopNetworkFeeUserCurrencyPrecision = createDeepEqualOutputSelector(
selectFirstHopNetworkFeeUserCurrencyPrecision,
selectSecondHopNetworkFeeUserCurrencyPrecision,
(_state: ReduxState, hopIndex: number) => hopIndex,
selectHopIndexParamFromRequiredFilter,
(firstHopNetworkFeeUserCurrencyPrecision, secondHopNetworkFeeUserCurrencyPrecision, hopIndex) => {
return hopIndex === 0
? firstHopNetworkFeeUserCurrencyPrecision
Expand Down Expand Up @@ -646,16 +650,16 @@ export const selectConfirmedTradeExecutionState = createSelector(
export const selectHopSellAccountId = createSelector(
selectFirstHopSellAccountId,
selectSecondHopSellAccountId,
(_state: ReduxState, hopIndex: number) => hopIndex,
selectHopIndexParamFromRequiredFilter,
(firstHopSellAccountId, secondHopSellAccountId, hopIndex) => {
return hopIndex === 0 ? firstHopSellAccountId : secondHopSellAccountId
},
)

export const selectHopExecutionMetadata = createDeepEqualOutputSelector(
selectTradeQuoteSlice,
(_state: ReduxState, tradeId: TradeQuote['id']) => tradeId,
(_state: ReduxState, _tradeId: TradeQuote['id'], hopIndex: number) => hopIndex,
selectTradeIdParamFromRequiredFilter,
selectHopIndexParamFromRequiredFilter,
(swappers, tradeId, hopIndex) => {
return hopIndex === 0
? swappers.tradeExecution[tradeId].firstHop
Expand Down
Loading