1+ import { constants } from 'ethers' ;
12import { redirect } from 'next/navigation' ;
23
4+ import { ChainId } from '@/bridge/types/ChainId' ;
35import { sanitizeExperimentalFeaturesQueryParam } from '@/bridge/util' ;
46import { isE2eTestingEnvironment , isProductionEnvironment } from '@/bridge/util/CommonUtils' ;
57import { registerLocalNetwork } from '@/bridge/util/networks' ;
@@ -98,6 +100,19 @@ export async function sanitizeAndRedirect(
98100 [ key : string ] : string | string [ ] | undefined ;
99101 } ,
100102 baseUrl : string ,
103+ ) {
104+ const redirectPath = await getSanitizedRedirectPath ( searchParams , baseUrl ) ;
105+
106+ if ( redirectPath ) {
107+ redirect ( redirectPath ) ;
108+ }
109+ }
110+
111+ export async function getSanitizedRedirectPath (
112+ searchParams : {
113+ [ key : string ] : string | string [ ] | undefined ;
114+ } ,
115+ baseUrl : string ,
101116) {
102117 const sourceChainId = decodeChainQueryParam ( searchParams . sourceChain ) ;
103118 const destinationChainId = decodeChainQueryParam ( searchParams . destinationChain ) ;
@@ -114,7 +129,7 @@ export async function sanitizeAndRedirect(
114129
115130 // If both sourceChain and destinationChain are not present, let the client sync with Metamask
116131 if ( ! sourceChainId && ! destinationChainId ) {
117- return ;
132+ return null ;
118133 }
119134
120135 if ( ! isProductionEnvironment || isE2eTestingEnvironment ) {
@@ -125,19 +140,32 @@ export async function sanitizeAndRedirect(
125140 sourceChainId,
126141 destinationChainId,
127142 } ) ;
143+ const sanitizedToken = sanitizeTokenQueryParam ( {
144+ token,
145+ sourceChainId : sanitizedChainIds . sourceChainId ,
146+ destinationChainId : sanitizedChainIds . destinationChainId ,
147+ } ) ;
148+ let sanitizedDestinationToken = sanitizeTokenQueryParam ( {
149+ token : destinationToken ,
150+ sourceChainId : sanitizedChainIds . sourceChainId ,
151+ destinationChainId : sanitizedChainIds . destinationChainId ,
152+ } ) ;
153+
154+ // Reuse the same default selection behavior as setSelectedToken(null) for ApeChain -> Superposition.
155+ if (
156+ sanitizedChainIds . sourceChainId === ChainId . ApeChain &&
157+ sanitizedChainIds . destinationChainId === ChainId . Superposition &&
158+ typeof token === 'undefined' &&
159+ typeof destinationToken === 'undefined'
160+ ) {
161+ sanitizedDestinationToken = constants . AddressZero ;
162+ }
163+
128164 const sanitized = {
129165 ...sanitizedChainIds ,
130166 experiments : sanitizeExperimentalFeaturesQueryParam ( experiments ) ,
131- token : sanitizeTokenQueryParam ( {
132- token,
133- sourceChainId : sanitizedChainIds . sourceChainId ,
134- destinationChainId : sanitizedChainIds . destinationChainId ,
135- } ) ,
136- destinationToken : sanitizeTokenQueryParam ( {
137- token : destinationToken ,
138- sourceChainId : sanitizedChainIds . sourceChainId ,
139- destinationChainId : sanitizedChainIds . destinationChainId ,
140- } ) ,
167+ token : sanitizedToken ,
168+ destinationToken : sanitizedDestinationToken ,
141169 tab : sanitizeTabQueryParam ( tab ) ,
142170 disabledFeatures : DisabledFeaturesParam . decode ( disabledFeatures ) ,
143171 } ;
@@ -160,6 +188,8 @@ export async function sanitizeAndRedirect(
160188 `[sanitizeAndRedirect] sourceChain=${ sanitized . sourceChainId } &destinationChain=${ sanitized . destinationChainId } &experiments=${ sanitized . experiments } &token=${ sanitized . token } &destinationToken=${ sanitized . destinationToken } &tab=${ sanitized . tab } &disabledFeatures=${ sanitized . disabledFeatures } &sanitized=true (after)` ,
161189 ) ;
162190
163- redirect ( getDestinationWithSanitizedQueryParams ( sanitized , searchParams , baseUrl ) ) ;
191+ return getDestinationWithSanitizedQueryParams ( sanitized , searchParams , baseUrl ) ;
164192 }
193+
194+ return null ;
165195}
0 commit comments