Skip to content

Commit a41d43e

Browse files
gomesalexandreNeOMakinG
authored andcommitted
fix: read fresh swap state in trade execution event handlers (#11536)
1 parent 6f385cd commit a41d43e

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/components/MultiHopTrade/components/TradeConfirm/hooks/useTradeExecution.tsx

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import {
5454
selectTradeSlippagePercentageDecimal,
5555
} from '@/state/slices/tradeQuoteSlice/selectors'
5656
import { tradeQuoteSlice } from '@/state/slices/tradeQuoteSlice/tradeQuoteSlice'
57-
import { useAppDispatch, useAppSelector } from '@/state/store'
57+
import { store, useAppDispatch, useAppSelector } from '@/state/store'
5858

5959
export const useTradeExecution = (
6060
hopIndex: SupportedTradeQuoteStepIndex,
@@ -249,15 +249,21 @@ export const useTradeExecution = (
249249
}
250250

251251
// Update the swap with the actual buy amount if available
252-
if (actualBuyAmountCryptoBaseUnit && activeSwapId) {
253-
const currentSwap = swapsById[activeSwapId]
254-
if (currentSwap) {
255-
dispatch(
256-
swapSlice.actions.upsertSwap({
257-
...currentSwap,
258-
actualBuyAmountCryptoBaseUnit,
259-
}),
260-
)
252+
// Read fresh state to avoid stale closure - swapsById captured at render time may have outdated status
253+
if (actualBuyAmountCryptoBaseUnit) {
254+
const freshActiveSwapId = swapSlice.selectors.selectActiveSwapId(store.getState())
255+
if (freshActiveSwapId) {
256+
const currentSwap = swapSlice.selectors.selectSwapsById(store.getState())[
257+
freshActiveSwapId
258+
]
259+
if (currentSwap) {
260+
dispatch(
261+
swapSlice.actions.upsertSwap({
262+
...currentSwap,
263+
actualBuyAmountCryptoBaseUnit,
264+
}),
265+
)
266+
}
261267
}
262268
}
263269
},
@@ -271,15 +277,21 @@ export const useTradeExecution = (
271277
}
272278

273279
// Update the swap with the actual buy amount if available
274-
if (actualBuyAmountCryptoBaseUnit && activeSwapId) {
275-
const currentSwap = swapsById[activeSwapId]
276-
if (currentSwap) {
277-
dispatch(
278-
swapSlice.actions.upsertSwap({
279-
...currentSwap,
280-
actualBuyAmountCryptoBaseUnit,
281-
}),
282-
)
280+
// Read fresh state to avoid stale closure - swapsById captured at render time may have outdated status
281+
if (actualBuyAmountCryptoBaseUnit) {
282+
const freshActiveSwapId = swapSlice.selectors.selectActiveSwapId(store.getState())
283+
if (freshActiveSwapId) {
284+
const currentSwap = swapSlice.selectors.selectSwapsById(store.getState())[
285+
freshActiveSwapId
286+
]
287+
if (currentSwap) {
288+
dispatch(
289+
swapSlice.actions.upsertSwap({
290+
...currentSwap,
291+
actualBuyAmountCryptoBaseUnit,
292+
}),
293+
)
294+
}
283295
}
284296
}
285297

0 commit comments

Comments
 (0)