@@ -54,7 +54,7 @@ import {
5454 selectTradeSlippagePercentageDecimal ,
5555} from '@/state/slices/tradeQuoteSlice/selectors'
5656import { tradeQuoteSlice } from '@/state/slices/tradeQuoteSlice/tradeQuoteSlice'
57- import { useAppDispatch , useAppSelector } from '@/state/store'
57+ import { store , useAppDispatch , useAppSelector } from '@/state/store'
5858
5959export 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