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 { logger } from '@/bridge/util/logger' ;
@@ -99,6 +101,19 @@ export async function sanitizeAndRedirect(
99101 [ key : string ] : string | string [ ] | undefined ;
100102 } ,
101103 baseUrl : string ,
104+ ) {
105+ const redirectPath = await getSanitizedRedirectPath ( searchParams , baseUrl ) ;
106+
107+ if ( redirectPath ) {
108+ redirect ( redirectPath ) ;
109+ }
110+ }
111+
112+ export async function getSanitizedRedirectPath (
113+ searchParams : {
114+ [ key : string ] : string | string [ ] | undefined ;
115+ } ,
116+ baseUrl : string ,
102117) {
103118 const sourceChainId = decodeChainQueryParam ( searchParams . sourceChain ) ;
104119 const destinationChainId = decodeChainQueryParam ( searchParams . destinationChain ) ;
@@ -115,7 +130,7 @@ export async function sanitizeAndRedirect(
115130
116131 // If both sourceChain and destinationChain are not present, let the client sync with Metamask
117132 if ( ! sourceChainId && ! destinationChainId ) {
118- return ;
133+ return null ;
119134 }
120135
121136 if ( ! isProductionEnvironment || isE2eTestingEnvironment ) {
@@ -126,19 +141,32 @@ export async function sanitizeAndRedirect(
126141 sourceChainId,
127142 destinationChainId,
128143 } ) ;
144+ const sanitizedToken = sanitizeTokenQueryParam ( {
145+ token,
146+ sourceChainId : sanitizedChainIds . sourceChainId ,
147+ destinationChainId : sanitizedChainIds . destinationChainId ,
148+ } ) ;
149+ let sanitizedDestinationToken = sanitizeTokenQueryParam ( {
150+ token : destinationToken ,
151+ sourceChainId : sanitizedChainIds . sourceChainId ,
152+ destinationChainId : sanitizedChainIds . destinationChainId ,
153+ } ) ;
154+
155+ // Reuse the same default selection behavior as setSelectedToken(null) for ApeChain -> Superposition.
156+ if (
157+ sanitizedChainIds . sourceChainId === ChainId . ApeChain &&
158+ sanitizedChainIds . destinationChainId === ChainId . Superposition &&
159+ typeof token === 'undefined' &&
160+ typeof destinationToken === 'undefined'
161+ ) {
162+ sanitizedDestinationToken = constants . AddressZero ;
163+ }
164+
129165 const sanitized = {
130166 ...sanitizedChainIds ,
131167 experiments : sanitizeExperimentalFeaturesQueryParam ( experiments ) ,
132- token : sanitizeTokenQueryParam ( {
133- token,
134- sourceChainId : sanitizedChainIds . sourceChainId ,
135- destinationChainId : sanitizedChainIds . destinationChainId ,
136- } ) ,
137- destinationToken : sanitizeTokenQueryParam ( {
138- token : destinationToken ,
139- sourceChainId : sanitizedChainIds . sourceChainId ,
140- destinationChainId : sanitizedChainIds . destinationChainId ,
141- } ) ,
168+ token : sanitizedToken ,
169+ destinationToken : sanitizedDestinationToken ,
142170 tab : sanitizeTabQueryParam ( tab ) ,
143171 disabledFeatures : DisabledFeaturesParam . decode ( disabledFeatures ) ,
144172 } ;
@@ -161,6 +189,8 @@ export async function sanitizeAndRedirect(
161189 `[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)` ,
162190 ) ;
163191
164- redirect ( getDestinationWithSanitizedQueryParams ( sanitized , searchParams , baseUrl ) ) ;
192+ return getDestinationWithSanitizedQueryParams ( sanitized , searchParams , baseUrl ) ;
165193 }
194+
195+ return null ;
166196}
0 commit comments