Skip to content

Commit

Permalink
feat(api): helper endpoint to retrieve supported chains
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki committed Sep 11, 2024
1 parent 8026ca9 commit f924102
Show file tree
Hide file tree
Showing 3 changed files with 1,497 additions and 0 deletions.
73 changes: 73 additions & 0 deletions api/chains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { VercelResponse } from "@vercel/node";
import { Infer, optional, string, type } from "superstruct";
import {
getLogger,
handleErrorCondition,
positiveIntStr,
boolStr,
} from "./_utils";
import { TypedVercelRequest } from "./_types";

import mainnetChains from "../src/data/chains_1.json";

const ChainsQueryParams = type({
inputTokenSymbol: optional(string()),
outputTokenSymbol: optional(string()),
chainId: optional(positiveIntStr()),
omitTokens: optional(boolStr()),
});

type ChainsQueryParams = Infer<typeof ChainsQueryParams>;

const handler = async (
{ query }: TypedVercelRequest<ChainsQueryParams>,
response: VercelResponse
) => {
const logger = getLogger();
logger.debug({
at: "TokenList",
message: "Query data",
query,
});

try {
const { inputTokenSymbol, outputTokenSymbol, chainId, omitTokens } = query;

const filteredChains = mainnetChains.filter((chain) => {
return (
(inputTokenSymbol
? !!chain.inputTokens.find(
(token) =>
token.symbol.toUpperCase() === inputTokenSymbol.toUpperCase()
)
: true) &&
(outputTokenSymbol
? !!chain.outputTokens.find(
(token) =>
token.symbol.toUpperCase() === outputTokenSymbol.toUpperCase()
)
: true) &&
(chainId ? chain.chainId === Number(chainId) : true)
);
});

if (omitTokens === "true") {
filteredChains.forEach((chain) => {
chain.inputTokens = [];
chain.outputTokens = [];
});
}

logger.debug({
at: "Chains",
message: "Response data",
responseJson: filteredChains,
});
response.setHeader("Cache-Control", "s-maxage=3600");
response.status(200).json(filteredChains);
} catch (error: unknown) {
return handleErrorCondition("token-list", response, logger, error);
}
};

export default handler;
69 changes: 69 additions & 0 deletions scripts/generate-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,75 @@ async function generateRoutes(hubPoolChainId = 1) {
parser: "json",
})
);

// helper file with chains
const chainsFileContent = (
hubPoolChainId === CHAIN_IDs.MAINNET
? enabledMainnetChainConfigs
: enabledSepoliaChainConfigs
).map((chainConfig) => {
const [chainKey] =
Object.entries(chainConfigs).find(
([, config]) => config.chainId === chainConfig.chainId
) || [];
if (!chainKey) {
throw new Error(
`Could not find chain key for chain ${chainConfig.chainId}`
);
}
const assetsBaseUrl = `https://raw.githubusercontent.com/across-protocol/frontend/master`;
const getTokenInfo = (tokenSymbol: string) => {
const tokenInfo =
TOKEN_SYMBOLS_MAP[tokenSymbol as keyof typeof TOKEN_SYMBOLS_MAP];
return {
address: utils.getAddress(
tokenInfo.addresses[chainConfig.chainId] as string
),
symbol: tokenSymbol,
name: tokenInfo.name,
decimals: tokenInfo.decimals,
logoUrl: `${assetsBaseUrl}/src/assets/token-logos/${tokenSymbol.toLowerCase()}.svg`,
};
};
return {
chainId: chainConfig.chainId,
name: chainConfig.name,
publicRpcUrl: chainConfig.publicRpcUrl,
explorerUrl: chainConfig.blockExplorer,
logoUrl: `${assetsBaseUrl}/scripts/chain-configs/${chainKey.toLowerCase()}/assets/logo.svg`,
spokePool: chainConfig.spokePool,
inputTokens: routeFileContent.routes
.filter((route) => route.fromChain === chainConfig.chainId)
.map((route) => getTokenInfo(route.fromTokenSymbol))
.reduce(
(acc, token) => {
if (!acc.find((t) => t.symbol === token.symbol)) {
return [...acc, token];
}
return acc;
},
[] as ReturnType<typeof getTokenInfo>[]
),
outputTokens: routeFileContent.routes
.filter((route) => route.toChain === chainConfig.chainId)
.map((route) => getTokenInfo(route.toTokenSymbol))
.reduce(
(acc, token) => {
if (!acc.find((t) => t.symbol === token.symbol)) {
return [...acc, token];
}
return acc;
},
[] as ReturnType<typeof getTokenInfo>[]
),
};
});
writeFileSync(
`./src/data/chains_${hubPoolChainId}.json`,
await prettier.format(JSON.stringify(chainsFileContent, null, 2), {
parser: "json",
})
);
}

function transformBridgeRoute(route: Route, hubPoolChainId: number) {
Expand Down
Loading

0 comments on commit f924102

Please sign in to comment.