-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
41 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 |
---|---|---|
@@ -1,54 +1,73 @@ | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
|
||
const { chains } = require("chain-registry"); | ||
const fs = require("fs/promises"); | ||
var { chains, assets } = require("chain-registry"); | ||
var fs = require("fs/promises"); | ||
|
||
async function generate() { | ||
/** @type {string[]} */ | ||
const chainIds = []; | ||
/** @type {string[]} */ var chainIds = []; | ||
/** @type {string[]} */ var chainNames = []; | ||
|
||
/** @type {Record<string, any>} */ var chainIdToName = {}; | ||
/** @type {Record<string, any>} */ var chainNameToId = {}; | ||
/** @type {Record<string, any>} */ var chainRecord = {}; | ||
/** @type {Record<string, any>} */ var assetsRecord = {}; | ||
|
||
/** @type {Record<string, any>} */ | ||
const chainRecord = {}; | ||
async function generate() { | ||
for (var chain of chains) { | ||
delete chain.$schema; | ||
|
||
for (const chain of chains) { | ||
if (!chain.chain_id) continue; | ||
if (!chain.chain_name) continue; | ||
|
||
chainIds.push(chain.chain_id); | ||
chainNames.push(chain.chain_name); | ||
|
||
chainIdToName[chain.chain_id] = chain.chain_name; | ||
chainIdToName[chain.chain_name] = chain.chain_name; | ||
|
||
chainNameToId[chain.chain_id] = chain.chain_id; | ||
chainNameToId[chain.chain_name] = chain.chain_id; | ||
|
||
chainRecord[chain.chain_id] = chain; | ||
} | ||
|
||
for (var asset of assets) { | ||
if (!asset.chain_name) continue; | ||
delete asset.$schema; | ||
assetsRecord[chainNameToId[asset.chain_name]] = asset.assets; | ||
} | ||
|
||
await fs.mkdir("src/chains/", { recursive: true }).catch(() => {}); | ||
|
||
await fs.writeFile( | ||
"src/chains/chainIds.js", | ||
"module.exports=" + JSON.stringify(chainIds), | ||
"utf-8", | ||
); | ||
|
||
const chainIdLiteralType = chainIds | ||
.map((id) => `"${id}"`) | ||
.concat("(string & {})") | ||
.join("|"); | ||
|
||
const chainIdsDts = `/* eslint-disable */ | ||
export type ChainId = ${chainIdLiteralType}; | ||
declare const chainIds: ChainId[]; | ||
export default chainIds; | ||
`; | ||
await fs.writeFile("src/chains/chainIds.d.ts", chainIdsDts, "utf-8"); | ||
|
||
await fs.writeFile( | ||
"src/chains/chainRecord.js", | ||
"module.exports=" + JSON.stringify(chainRecord), | ||
"utf-8", | ||
); | ||
|
||
const chainRecordDts = `/* eslint-disable */ | ||
import { Chain } from "@chain-registry/types"; | ||
import { ChainId } from "./chainIds"; | ||
declare const chainRecord: Record<ChainId, Chain>; | ||
export default chainRecord; | ||
var generatedTs = `/* eslint-disable */ | ||
// @ts-nocheck | ||
import { Asset, Chain } from "@chain-registry/types"; | ||
export const chainIds = ${JSON.stringify(chainIds)} as const; | ||
export type ChainId = (typeof chainIds)[number] | (string & {}); | ||
export const chainNames = ${JSON.stringify(chainNames)} as const; | ||
export type ChainName = (typeof chainNames)[number] | (string & {}); | ||
export type ChainIdOrName = ChainId | ChainName; | ||
export const chainIdToName: Record<ChainIdOrName, ChainName> = ${JSON.stringify( | ||
chainIdToName, | ||
)}; | ||
export const chainNameToId: Record<ChainIdOrName, ChainId> = ${JSON.stringify( | ||
chainNameToId, | ||
)}; | ||
export const chainRecord: Record<ChainId, Chain> = ${JSON.stringify( | ||
chainRecord, | ||
)}; | ||
export const assetsRecord: Record<ChainId, Asset[]> = ${JSON.stringify( | ||
assetsRecord, | ||
)}; | ||
`; | ||
await fs.writeFile("src/chains/chainRecord.d.ts", chainRecordDts, "utf-8"); | ||
|
||
await fs.writeFile("src/chains/generated.ts", generatedTs, { | ||
encoding: "utf-8", | ||
}); | ||
} | ||
|
||
void generate(); |
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,24 @@ | ||
/* eslint-disable */ | ||
// @ts-nocheck | ||
import type { Asset, Chain } from "@chain-registry/types"; | ||
|
||
import { raise } from "@/utils/assert"; | ||
|
||
import type { ChainIdOrName } from "./generated"; | ||
import { assetsRecord, chainNameToId, chainRecord } from "./generated"; | ||
|
||
export const getChain = (idOrName: ChainIdOrName): Chain => { | ||
return ( | ||
chainRecord[chainNameToId[idOrName]] || | ||
raise(`chain '${idOrName}' does not exist in chainRecord`) | ||
); | ||
}; | ||
|
||
export const getAssets = (idOrName: ChainIdOrName): Asset[] => { | ||
return ( | ||
assetsRecord[chainNameToId[idOrName]] || | ||
raise(`chain '${idOrName}' does not exist in assetsRecord`) | ||
); | ||
}; | ||
|
||
export * from "./generated"; |
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