Skip to content

Commit

Permalink
Add api for getting native mainnet, testnet chain infos
Browse files Browse the repository at this point in the history
  • Loading branch information
delivan committed Aug 29, 2023
1 parent 0f5c7c7 commit 4a44820
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pages/api/native-chains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from "next";

import path from "path";
import { promises as fs } from "fs";
import { ChainInfo } from "@keplr-wallet/types";
import Cors from "cors";
import {
nativeMainnetChainIdentifiers,
nativeTestnetChainIdentifiers,
} from "../../src/constants";
import { ChainIdHelper } from "@keplr-wallet/cosmos";

type Data = {
mainnetChains: ChainInfo[];
testnetChains: ChainInfo[];
};

const cors = Cors({
methods: ["GET"],
});

export default async function (
req: NextApiRequest,
res: NextApiResponse<Data>,
) {
await new Promise((resolve, reject) => {
cors(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}

return resolve(result);
});
});

const jsonDirectory = path.join(process.cwd(), "cosmos");

const fetchChains = (await fs.readdir(jsonDirectory)).map((fileName) =>
fs.readFile(`${jsonDirectory}/${fileName}`, "utf8"),
);

const chainInfos: ChainInfo[] = (await Promise.all(fetchChains)).map(
(chainInfo) => JSON.parse(chainInfo),
);

//Return the content of the data file in json format
res.status(200).json({
mainnetChains: chainInfos.filter((chainInfo) =>
nativeMainnetChainIdentifiers.includes(
ChainIdHelper.parse(chainInfo.chainId).identifier,
),
),
testnetChains: chainInfos.filter((chainInfo) =>
nativeTestnetChainIdentifiers.includes(
ChainIdHelper.parse(chainInfo.chainId).identifier,
),
),
});
}

0 comments on commit 4a44820

Please sign in to comment.