forked from chainapsis/keplr-chain-registry
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add api for getting native mainnet, testnet chain infos
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
), | ||
}); | ||
} |