@@ -19,6 +19,7 @@ import { ISelectedAccountController } from '../../interfaces/selectedAccount'
1919/* eslint-disable no-await-in-loop */
2020import { ISignAccountOpController , SignAccountOpError } from '../../interfaces/signAccountOp'
2121import { IStorageController } from '../../interfaces/storage'
22+ import { IUiController , View } from '../../interfaces/ui'
2223import {
2324 CachedSupportedChains ,
2425 CachedTokenListKey ,
@@ -85,6 +86,8 @@ type SwapAndBridgeErrorType = {
8586
8687const HARD_CODED_CURRENCY = 'usd'
8788
89+ const isSwapAndBridge = ( route : string | undefined ) => route === 'swap-and-bridge'
90+
8891const CONVERSION_PRECISION = 16
8992const CONVERSION_PRECISION_POW = BigInt ( 10 ** CONVERSION_PRECISION )
9093
@@ -270,6 +273,10 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
270273
271274 #onBroadcastFailed: OnBroadcastFailed
272275
276+ #ui: IUiController
277+
278+ #isOnSwapAndBridgeRoute: boolean = false
279+
273280 constructor ( {
274281 eventEmitterRegistry,
275282 callRelayer,
@@ -290,7 +297,8 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
290297 getVisibleUserRequests,
291298 swapProvider,
292299 onBroadcastSuccess,
293- onBroadcastFailed
300+ onBroadcastFailed,
301+ ui
294302 } : {
295303 eventEmitterRegistry ?: IEventEmitterRegistryController
296304 callRelayer : Function
@@ -312,6 +320,7 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
312320 swapProvider : SwapProvider
313321 onBroadcastSuccess : OnBroadcastSuccess
314322 onBroadcastFailed : OnBroadcastFailed
323+ ui : IUiController
315324 } ) {
316325 super ( eventEmitterRegistry )
317326
@@ -335,6 +344,7 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
335344 this . #getVisibleUserRequests = getVisibleUserRequests
336345 this . #onBroadcastSuccess = onBroadcastSuccess
337346 this . #onBroadcastFailed = onBroadcastFailed
347+ this . #ui = ui
338348
339349 // eslint-disable-next-line @typescript-eslint/no-floating-promises
340350 this . #initialLoadPromise = this . #load( ) . finally ( ( ) => {
@@ -352,6 +362,29 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
352362 BRIDGE_STATUS_INTERVAL ,
353363 this . emitError . bind ( this )
354364 )
365+
366+ this . #ui. uiEvent . on ( 'updateView' , ( view : View ) => {
367+ if ( isSwapAndBridge ( view . currentRoute ) ) {
368+ this . #isOnSwapAndBridgeRoute = true
369+ // Fetch a fresh quote immediately if the form is ready and the last
370+ // quote is older than 20 seconds (e.g. user was away on another screen).
371+ // If the user just briefly switched screens, skip the immediate fetch
372+ // and let the normal interval handle the next update.
373+ const isQuoteStale = Date . now ( ) - this . updateQuoteInterval . startedRunningAt > 20_000
374+ this . updateQuoteInterval . restart ( {
375+ runImmediately : ! ! this . #shouldAutoUpdateQuote && isQuoteStale
376+ } )
377+ } else if ( isSwapAndBridge ( view . previousRoute ) ) {
378+ this . #isOnSwapAndBridgeRoute = false
379+ this . updateQuoteInterval . stop ( )
380+ }
381+ } )
382+
383+ this . #ui. uiEvent . on ( 'removeView' , ( view : View ) => {
384+ if ( ! isSwapAndBridge ( view . currentRoute ) ) return
385+ this . #isOnSwapAndBridgeRoute = false
386+ this . updateQuoteInterval . stop ( )
387+ } )
355388 }
356389
357390 #emitUpdateIfNeeded( forceUpdate : boolean = false ) {
@@ -2606,6 +2639,7 @@ export class SwapAndBridgeController extends EventEmitter implements ISwapAndBri
26062639
26072640 get #shouldAutoUpdateQuote( ) {
26082641 return (
2642+ this . #isOnSwapAndBridgeRoute &&
26092643 this . formStatus === SwapAndBridgeFormStatus . ReadyToSubmit &&
26102644 ! this . hasProceeded &&
26112645 this . quote &&
0 commit comments