|
| 1 | +import { getNetworkName } from "./coingecko"; |
| 2 | + |
| 3 | +type RequestObject = { |
| 4 | + method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD"; |
| 5 | + headers?: { |
| 6 | + [key: string]: string; |
| 7 | + } | null; |
| 8 | + path: string; |
| 9 | + query?: { |
| 10 | + [key: string]: string | string[]; |
| 11 | + } | null; |
| 12 | + body?: string | null; |
| 13 | +}; |
| 14 | + |
| 15 | +type ResponseObject = |
| 16 | + | { |
| 17 | + status: number; |
| 18 | + headers?: { |
| 19 | + [key: string]: string; |
| 20 | + } | null; |
| 21 | + body?: string; |
| 22 | + } |
| 23 | + | string |
| 24 | + | ArrayBuffer; |
| 25 | + |
| 26 | +export async function main(params: RequestObject): Promise<ResponseObject> { |
| 27 | + try { |
| 28 | + const { chainId } = processParams(params); |
| 29 | + const { name: networkName, id: networkId } = await getNetworkName(chainId); |
| 30 | + const coingeckoTokenList = `https://tokens.coingecko.com/${networkId}/all.json`; |
| 31 | + const superfluidTokenList = |
| 32 | + "https://raw.githubusercontent.com/superfluid-finance/tokenlist/main/superfluid.extended.tokenlist.json"; |
| 33 | + const tokenLists = await Promise.all([ |
| 34 | + fetch(coingeckoTokenList).then((res) => res.json()), |
| 35 | + fetch(superfluidTokenList).then((res) => res.json()), |
| 36 | + ]); |
| 37 | + const lastTimestamp = Math.max( |
| 38 | + ...tokenLists.map((tokenList) => new Date(tokenList.timestamp).getTime()), |
| 39 | + ); |
| 40 | + const tokenList = { |
| 41 | + name: `EVMcrispr Token List (${networkName})`, |
| 42 | + logoURI: "https://evmcrispr.com/favicon.ico", |
| 43 | + timestamp: new Date(lastTimestamp).toISOString(), |
| 44 | + tokens: tokenLists[0].tokens |
| 45 | + .concat( |
| 46 | + tokenLists[1].tokens.filter((token) => token.chainId === chainId), |
| 47 | + ) |
| 48 | + .reduce((acc, token) => { |
| 49 | + if (acc.find((t) => t.address === token.address)) { |
| 50 | + return acc; |
| 51 | + } |
| 52 | + acc.push(token); |
| 53 | + return acc; |
| 54 | + }, []), |
| 55 | + version: { |
| 56 | + patch: 1, |
| 57 | + }, |
| 58 | + }; |
| 59 | + return { |
| 60 | + status: 200, |
| 61 | + headers: { |
| 62 | + "Content-Type": "application/json", |
| 63 | + "Access-Control-Allow-Origin": "*", |
| 64 | + }, |
| 65 | + body: JSON.stringify(tokenList), |
| 66 | + }; |
| 67 | + } catch (error) { |
| 68 | + if (isErrorWithStatusAndBody(error)) { |
| 69 | + return error; |
| 70 | + } |
| 71 | + return { |
| 72 | + status: 500, |
| 73 | + body: "Internal Server Error", |
| 74 | + }; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// main({ method: "GET", path: "/v0/1" }).then((res) => { |
| 79 | +// console.log(res); |
| 80 | +// }); |
| 81 | + |
| 82 | +function isErrorWithStatusAndBody( |
| 83 | + error: unknown, |
| 84 | +): error is { status: number; body: string } { |
| 85 | + const err = error as { [key: string]: unknown }; |
| 86 | + return ( |
| 87 | + err && |
| 88 | + typeof err === "object" && |
| 89 | + typeof err.status === "number" && |
| 90 | + typeof err.body === "string" |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +function processParams(params: RequestObject): { chainId: number } { |
| 95 | + const { method, path } = params; |
| 96 | + |
| 97 | + if (method !== "GET") { |
| 98 | + throw { |
| 99 | + status: 405, |
| 100 | + body: "Method Not Allowed", |
| 101 | + }; |
| 102 | + } |
| 103 | + |
| 104 | + if (!path.startsWith("/v0/")) { |
| 105 | + throw { |
| 106 | + status: 400, |
| 107 | + body: "Invalid URL", |
| 108 | + }; |
| 109 | + } |
| 110 | + const [, , chainId] = path.split("/"); |
| 111 | + |
| 112 | + if (!Number(chainId)) { |
| 113 | + throw { |
| 114 | + status: 400, |
| 115 | + body: "Invalid chainId", |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + return { chainId: Number(chainId) }; |
| 120 | +} |
0 commit comments