Skip to content

Commit 9a66439

Browse files
committed
chore: small optimization for getSwappableTokenOptions
1 parent 5aaa9a1 commit 9a66439

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/config/tokens.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,21 @@ export async function isSwappable(token1: string, token2: string, chainId: numbe
222222
}
223223

224224
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+
)
229229

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+
})
236236
)
237237

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)
239240
}
240241

241242
export function getTokenOptionsByChainId(chainId: ChainId): TokenId[] {

0 commit comments

Comments
 (0)