@@ -222,20 +222,21 @@ export async function isSwappable(token1: string, token2: string, chainId: numbe
222
222
}
223
223
224
224
export async function getSwappableTokenOptions ( inputTokenId : string , chainId : ChainId ) {
225
- const allTokenOptions = getTokenOptionsByChainId ( chainId )
226
-
227
- // Filter out the input token first
228
- const tokenOptions = allTokenOptions . filter ( ( tokenId ) => tokenId !== inputTokenId )
225
+ // Get all available tokens for the chain except the input token
226
+ const tokenOptions = getTokenOptionsByChainId ( chainId ) . filter (
227
+ ( tokenId ) => tokenId !== inputTokenId
228
+ )
229
229
230
- // Check which tokens are swappable with the input token
231
- const swappableChecks = await Promise . all (
232
- tokenOptions . map ( async ( tokenId ) => ( {
233
- tokenId,
234
- swappable : await isSwappable ( tokenId , inputTokenId , chainId ) ,
235
- } ) )
230
+ // Check swappability in parallel and maintain order
231
+ const swappableTokens = await Promise . all (
232
+ tokenOptions . map ( async ( tokenId ) => {
233
+ const swappable = await isSwappable ( tokenId , inputTokenId , chainId )
234
+ return swappable ? tokenId : null
235
+ } )
236
236
)
237
237
238
- return swappableChecks . filter ( ( { swappable } ) => swappable ) . map ( ( { tokenId } ) => tokenId )
238
+ // Filter out non-swappable tokens (null values)
239
+ return swappableTokens . filter ( ( tokenId ) : tokenId is TokenId => tokenId !== null )
239
240
}
240
241
241
242
export function getTokenOptionsByChainId ( chainId : ChainId ) : TokenId [ ] {
0 commit comments