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

feat: cleanup useTradeNetworkFeeCryptoBaseUnit #8579

Merged
merged 1 commit into from
Jan 15, 2025
Merged
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
feat: cleanup useTradeNetworkFeeCryptoBaseUnit
gomesalexandre committed Jan 14, 2025
commit 739aa8d9d8e48cd3c25e483e8702b1e7f816c511
Original file line number Diff line number Diff line change
@@ -21,11 +21,9 @@ import {
selectActiveQuote,
selectActiveSwapperName,
selectConfirmedTradeExecution,
selectHopExecutionMetadata,
selectHopSellAccountId,
selectTradeSlippagePercentageDecimal,
} from 'state/slices/tradeQuoteSlice/selectors'
import { HopExecutionState, TransactionExecutionState } from 'state/slices/tradeQuoteSlice/types'
import { useAppSelector } from 'state/store'

export const useTradeNetworkFeeCryptoBaseUnit = ({
@@ -62,24 +60,6 @@ export const useTradeNetworkFeeCryptoBaseUnit = ({
const hop = useMemo(() => getHopByIndex(tradeQuote, hopIndex), [tradeQuote, hopIndex])
const swapper = useMemo(() => (swapperName ? swappers[swapperName] : undefined), [swapperName])

const activeTrade = useAppSelector(selectActiveQuote)
const activeTradeId = activeTrade?.id

const hopExecutionMetadataFilter = useMemo(() => {
if (!activeTradeId) return undefined

return {
tradeId: activeTradeId,
hopIndex,
}
}, [activeTradeId, hopIndex])

const hopExecutionMetadata = useAppSelector(state =>
hopExecutionMetadataFilter
? selectHopExecutionMetadata(state, hopExecutionMetadataFilter)
: undefined,
)

const quoteNetworkFeesCryptoBaseUnitQuery = useQuery({
queryKey: ['quoteNetworkFeesCryptoBaseUnit', tradeQuote],
refetchInterval: 15_000,
@@ -92,10 +72,7 @@ export const useTradeNetworkFeeCryptoBaseUnit = ({
sellAssetAccountId &&
swapper &&
hop &&
isExecutableTradeQuote(tradeQuote) &&
// Stop fetching once the Tx is executed for this step
hopExecutionMetadata?.state === HopExecutionState.AwaitingSwap &&
hopExecutionMetadata?.swap?.state === TransactionExecutionState.AwaitingConfirmation
isExecutableTradeQuote(tradeQuote)
? async () => {
const { accountType, bip44Params } = accountMetadata
const accountNumber = bip44Params.accountNumber
Original file line number Diff line number Diff line change
@@ -10,7 +10,11 @@ import { bnOrZero } from 'lib/bignumber/bignumber'
import { fromBaseUnit } from 'lib/math'
import { selectFeeAssetById } from 'state/slices/assetsSlice/selectors'
import { selectMarketDataByAssetIdUserCurrency } from 'state/slices/marketDataSlice/selectors'
import { selectIsActiveSwapperQuoteLoading } from 'state/slices/tradeQuoteSlice/selectors'
import {
selectHopExecutionMetadata,
selectIsActiveSwapperQuoteLoading,
} from 'state/slices/tradeQuoteSlice/selectors'
import { HopExecutionState, TransactionExecutionState } from 'state/slices/tradeQuoteSlice/types'
import { useAppSelector, useSelectorWithArgs } from 'state/store'

import { isPermit2Hop } from '../MultiHopTradeConfirm/hooks/helpers'
@@ -62,14 +66,33 @@ export const TradeConfirmFooter: FC<TradeConfirmFooterProps> = ({
activeTradeId,
})

const hopExecutionMetadataFilter = useMemo(() => {
if (!activeTradeId) return undefined

return {
tradeId: activeTradeId,
hopIndex: currentHopIndex,
}
}, [activeTradeId, currentHopIndex])

const hopExecutionMetadata = useAppSelector(state =>
hopExecutionMetadataFilter
? selectHopExecutionMetadata(state, hopExecutionMetadataFilter)
: undefined,
)

const {
isLoading: isNetworkFeeCryptoBaseUnitLoading,
isRefetching: isNetworkFeeCryptoBaseUnitRefetching,
data: networkFeeCryptoBaseUnit,
} = useTradeNetworkFeeCryptoBaseUnit({
hopIndex: currentHopIndex,
enabled:
currentTradeStep === StepperStep.FirstHopSwap || currentTradeStep === StepperStep.LastHopSwap,
(currentTradeStep === StepperStep.FirstHopSwap ||
currentTradeStep === StepperStep.LastHopSwap) &&
// Stop fetching once the Tx is executed for this step
hopExecutionMetadata?.state === HopExecutionState.AwaitingSwap &&
hopExecutionMetadata?.swap?.state === TransactionExecutionState.AwaitingConfirmation,
})

const networkFeeCryptoPrecision = useMemo(() => {