-
Notifications
You must be signed in to change notification settings - Fork 65
fix: include Across supported tokens in /swap/tokens endpoint #1927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,20 +3,21 @@ import axios from "axios"; | |
| import { constants } from "ethers"; | ||
| import { type, assert, Infer, optional, array, union } from "superstruct"; | ||
|
|
||
| import { CHAIN_IDs, TOKEN_SYMBOLS_MAP } from "../../_constants"; | ||
| import { getRequestId, setRequestSpanAttributes } from "../../_request_utils"; | ||
| import { sendResponse } from "../../_response_utils"; | ||
| import { TypedVercelRequest } from "../../_types"; | ||
| import { | ||
| ENABLED_ROUTES, | ||
| getFallbackTokenLogoURI, | ||
| getLogger, | ||
| handleErrorCondition, | ||
| paramToArray, | ||
| positiveIntStr, | ||
| } from "../../_utils"; | ||
| import { TypedVercelRequest } from "../../_types"; | ||
|
|
||
| import { tracer, processor } from "../../../instrumentation"; | ||
| import mainnetChains from "../../../src/data/chains_1.json"; | ||
| import indirectChains from "../../../src/data/indirect_chains_1.json"; | ||
| import { getRequestId, setRequestSpanAttributes } from "../../_request_utils"; | ||
| import { sendResponse } from "../../_response_utils"; | ||
| import { tracer, processor } from "../../../instrumentation"; | ||
| import { CHAIN_IDs } from "../../_constants"; | ||
|
|
||
| type Token = { | ||
| chainId: number; | ||
|
|
@@ -31,21 +32,6 @@ type Token = { | |
| const chains = mainnetChains; | ||
| const chainIds = [...chains, ...indirectChains].map((chain) => chain.chainId); | ||
|
|
||
| // List of tokens that are statically defined locally. Currently, this list is used for | ||
| // indirect chain tokens, e.g. USDT-SPOT on HyperCore. | ||
| const staticTokens = indirectChains.flatMap((chain) => | ||
| chain.outputTokens.map((token) => ({ | ||
| chainId: chain.chainId, | ||
| address: token.address, | ||
| name: token.name, | ||
| symbol: token.symbol, | ||
| decimals: token.decimals, | ||
| logoUrl: token.logoUrl, | ||
| // TODO: Add more generic price resolution logic for static tokens | ||
| priceUsd: token.symbol === "USDT-SPOT" ? "1" : null, | ||
| })) | ||
| ); | ||
|
|
||
| const SwapTokensQueryParamsSchema = type({ | ||
| chainId: optional(union([positiveIntStr(), array(positiveIntStr())])), | ||
| }); | ||
|
|
@@ -111,18 +97,28 @@ export default async function handler( | |
| ); | ||
| responseJson.push(...jupiterTokens); | ||
|
|
||
| // Add static tokens | ||
| const staticTokens = getStaticTokens(filteredChainIds); | ||
| responseJson.push(...staticTokens); | ||
| // Add tokens from indirect chains (e.g., USDT-SPOT on HyperCore) | ||
| const indirectChainTokens = getIndirectChainTokens(filteredChainIds); | ||
| responseJson.push(...indirectChainTokens); | ||
|
|
||
| // Add tokens from Across' enabled routes (fills gaps from external sources) | ||
| const tokensFromEnabledRoutes = getTokensFromEnabledRoutes( | ||
| filteredChainIds, | ||
| pricesForLifiTokens | ||
| ); | ||
| responseJson.push(...tokensFromEnabledRoutes); | ||
|
|
||
| // Deduplicate tokens (external sources take precedence) | ||
| const deduplicatedTokens = deduplicateTokens(responseJson); | ||
|
|
||
| logger.debug({ | ||
| at: "swap/tokens", | ||
| message: "Response data", | ||
| responseJson, | ||
| responseJson: deduplicatedTokens, | ||
| }); | ||
| sendResponse({ | ||
| response, | ||
| body: responseJson, | ||
| body: deduplicatedTokens, | ||
| statusCode: 200, | ||
| requestId, | ||
| cacheSeconds: 60 * 5, | ||
|
|
@@ -232,6 +228,78 @@ function getJupiterTokens( | |
| }, []); | ||
| } | ||
|
|
||
| function getStaticTokens(chainIds: number[]): Token[] { | ||
| return staticTokens.filter((token) => chainIds.includes(token.chainId)); | ||
| function getIndirectChainTokens(chainIds: number[]): Token[] { | ||
| return indirectChains.flatMap((chain) => { | ||
| if (!chainIds.includes(chain.chainId)) { | ||
| return []; | ||
| } | ||
|
|
||
| return chain.outputTokens.map((token) => ({ | ||
| chainId: chain.chainId, | ||
| address: token.address, | ||
| name: token.name, | ||
| symbol: token.symbol, | ||
| decimals: token.decimals, | ||
| logoUrl: token.logoUrl, | ||
| // TODO: Add more generic price resolution logic for indirect chain tokens | ||
| priceUsd: token.symbol === "USDT-SPOT" ? "1" : null, | ||
| })); | ||
| }); | ||
| } | ||
|
|
||
| function getTokensFromEnabledRoutes( | ||
| chainIds: number[], | ||
| pricesForLifiTokens: Record<number, Record<string, string>> | ||
| ): Token[] { | ||
| const tokens: Token[] = []; | ||
| const seenTokens = new Set<string>(); | ||
|
|
||
| ENABLED_ROUTES.routes.forEach((route) => { | ||
| if (chainIds.includes(route.fromChain)) { | ||
| // Use AddressZero for native tokens, original address otherwise | ||
| const tokenAddress = route.isNative | ||
| ? constants.AddressZero | ||
| : route.fromTokenAddress; | ||
|
|
||
| const tokenKey = `${route.fromChain}-${route.fromTokenSymbol}-${tokenAddress.toLowerCase()}`; | ||
|
||
|
|
||
| // Only add each unique token once | ||
| if (!seenTokens.has(tokenKey)) { | ||
| seenTokens.add(tokenKey); | ||
|
|
||
| const tokenInfo = | ||
| TOKEN_SYMBOLS_MAP[ | ||
| route.fromTokenSymbol as keyof typeof TOKEN_SYMBOLS_MAP | ||
| ]; | ||
|
|
||
| tokens.push({ | ||
| chainId: route.fromChain, | ||
| address: tokenAddress, | ||
| name: tokenInfo.name, | ||
| symbol: route.fromTokenSymbol, | ||
| decimals: tokenInfo.decimals, | ||
| logoUrl: getFallbackTokenLogoURI(route.l1TokenAddress), | ||
| priceUsd: | ||
| pricesForLifiTokens[route.fromChain]?.[tokenAddress] || null, | ||
| }); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return tokens; | ||
| } | ||
|
|
||
| function deduplicateTokens(tokens: Token[]): Token[] { | ||
| const seen = new Map<string, Token>(); | ||
|
|
||
| tokens.forEach((token) => { | ||
| const key = `${token.chainId}-${token.symbol}-${token.address.toLowerCase()}`; | ||
|
|
||
| // Keep first occurrence | ||
| if (!seen.has(key)) { | ||
| seen.set(key, token); | ||
| } | ||
| }); | ||
|
|
||
| return Array.from(seen.values()); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.