Skip to content

Commit

Permalink
Split ChainIcon type for reusability, added missing utility functions…
Browse files Browse the repository at this point in the history
… from website.
  • Loading branch information
DominicF96 committed Aug 7, 2024
1 parent 3b632a1 commit 952e73f
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 29 deletions.
8 changes: 1 addition & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pinax/chains",
"version": "2.0.9",
"version": "2.0.10",
"description": "Single-source-of-truth for the metadata of chains supported by Pinax.",
"keywords": [
"chains",
Expand Down Expand Up @@ -33,12 +33,6 @@
"generate:type_graphid": "bun ./scripts/generate/V2/type_graphid.ts",
"generate:type_pinaxid": "bun ./scripts/generate/V2/type_pinaxid.ts",
"generate:copy_token_icons": "bun ./scripts/generate/V2/copy_token_icons.ts",
"verify:match_legacy_check": "bun ./scripts/verify/V2/match_legacy_data.ts",
"generate_v1": "npm run generate_v1:data",
"generate_v1:data": "node ./scripts/generate/V1/data_json.js",
"generate_v1:types": "npm run generate_v1:type_graphid && npm run generate_v1:type_pinaxid && npm run format",
"generate_v1:type_graphid": "node ./scripts/generate/V1/type_graphid.js",
"generate_v1:type_pinaxid": "node ./scripts/generate/V1/type_pinaxid.js",
"test": "bun test",
"build": "tsc && bun build ./index.ts --outdir=build",
"prepublishOnly": "npm run build",
Expand Down
5 changes: 0 additions & 5 deletions test.ts

This file was deleted.

4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "ESNext", /* Specify what module code is generated. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"declarationMap": true, /* Create sourcemaps for d.ts files. */
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
Expand All @@ -11,7 +11,7 @@
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
"emitDeclarationOnly": true
"emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
},
"include": ["index.ts", "types/*.d.ts", "types/block.types.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
Expand Down
33 changes: 18 additions & 15 deletions types/chain.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,31 @@ export type ___InternalEVM = ChainBase & {
supported_services: ___InternalSupportedServices;
};

export type ChainIcon = {
// Token Icon ID
// See if icon is available on https://tokenicons.io/, if not create PR to add it.
// Then go on the Github Repo and find the icon ID under /packages/core/src/metadata/
// https://github.com/0xa3k5/token-icons
id: string;

// Theme of the Icon brand
// To avoid placing a dark icon on a dark background.
// If the brand is in midtones or some vibrant color, use 'both'.
brand_theme: 'light' | 'dark' | 'both';

// Generated by ./scripts/generate/data_json.js
variants?: Array<'branded' | 'mono'>;
};

/**
* Describes the Data that needs to be provided for a Chain.
*
* The Chain type theb extends the Chain type to include
* the generated fields.
*/
export interface ___InternalChain extends ChainBase {
icon: {
// Token Icon ID
// See if icon is available on https://tokenicons.io/, if not create PR to add it.
// Then go on the Github Repo and find the icon ID under /packages/core/src/metadata/
// https://github.com/0xa3k5/token-icons
id: string;

// Theme of the Icon brand
// To avoid placing a dark icon on a dark background.
// If the brand is in midtones or some vibrant color, use 'both'.
brand_theme: 'light' | 'dark' | 'both';

// Generated by ./scripts/generate/data_json.js
variants?: Array<'branded' | 'mono'>;
};
// Token Icon
icon: ChainIcon;

// Whether or not the chain supports our existing services
supported_services: ___InternalSupportedServices;
Expand Down
127 changes: 127 additions & 0 deletions utils/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ const findChainById = (
return undefined;
};

/**
* Finds the mainnet chain or its subnet by ID from the given database.
*
* @param {Array<Chain>} db - The database of chains to search through.
* @param {string} id - The ID of the chain or subnet to find.
* @returns {Chain | undefined} - The chain containing the subnet with the given ID, or undefined if not found.
*/
const findSubnetMainnet = (db: Array<Chain>, id: string) => {
for (const chain of db) {
if (chain.id === id) {
return chain;
}
const consensus = chain.consensus?.find((consensus) => consensus.id === id);
if (consensus) {
return chain;
}
const testnet = chain.testnets?.find((testnet) => testnet.id === id);
if (testnet) {
return chain;
}
const evm = chain.evms?.find((evm) => evm.id === id);
if (evm) {
return chain;
}
}
return undefined;
};

/**
* Checks whether a chain has full block support. Chains that use RPC poller only
* support partial blocks.
Expand All @@ -185,6 +213,100 @@ const hasChainFullBlockSupport = (chain: Chain | Testnet | ConsensusLayer) => {
return chain.is_detailed_blocks;
};

/**
* Calculates the number of supported chains from a given array of chains.
*
* @param {Array<Chain | Testnet | ConsensusLayer | EVM>} chains - The array of chains to check for support.
* @returns {number} The number of supported chains.
*/
const getNumberOfSupportedChains = (
chains: Array<Chain | Testnet | ConsensusLayer | EVM>,
) => {
let supportedChains = 0;
chains
.filter((c: any) => isChainSupported(c))
.forEach((c: any) => {
supportedChains++;
c.consensus?.forEach((consensus: any) => {
if (isChainSupported(consensus)) {
supportedChains++;
}
});
c.evms?.forEach((evm: any) => {
if (isChainSupported(evm)) {
supportedChains++;
}
});
c.testnets?.forEach((tn: any) => {
if (isChainSupported(tn)) {
supportedChains++;
}
});
});
return supportedChains;
};

/**
* Determines the status of a given chain.
*
* @param {Chain | ConsensusLayer | EVM | Testnet} chain - The chain to check the status of.
* @returns {string} The status of the chain, which can be "supported", "beta", "deprecated", or "unsupported".
*/
const getChainStatus = (chain: Chain | ConsensusLayer | EVM | Testnet) => {
if (isChainSupported(chain)) {
return 'supported';
}
if (isChainBeta(chain)) {
return 'beta';
}
if (isChainDeprecated(chain)) {
return 'deprecated';
}
return 'unsupported';
};

/**
* Retrieves the supported services for a given chain.
*
* @param {Chain | Testnet | ConsensusLayer | EVM} chain - The chain object to check for supported services.
* @returns {Array<[ServiceID, string | null]>} An array of tuples where each tuple contains a service ID and the release date (beta or full) of the service.
*/
const getSupportedServices = (
chain: Chain | Testnet | ConsensusLayer | EVM,
) => {
let supServices = [] as Array<[ServiceID, string | null]>;
(['rpc', 'firehose', 'substreams'] as Array<ServiceID>).forEach((service) => {
if (isServiceBeta(chain as any, service as any)) {
supServices.push([
service,
chain.supported_services[
service as keyof typeof chain.supported_services
]?.beta_released_at,
]);
} else if (isServiceSupported(chain as any, service as any)) {
supServices.push([
service,
chain.supported_services[
service as keyof typeof chain.supported_services
]?.full_released_at,
]);
}
});
return supServices;
};

/**
* Retrieves the subnets of a given chain by concatenating its testnets, evms, and consensus arrays.
*
* @param {Chain} chain - The chain object containing testnets, evms, and consensus arrays.
* @returns {Array<any>} - An array containing all subnets from the testnets, evms, and consensus arrays.
*/
const getChainSubnets = (chain: Chain) => {
return (chain.testnets as Array<any>)
?.concat((chain.evms as Array<any>) || [])
.concat((chain.consensus as Array<any>) || []);
};

export {
isServiceSupported,
isServiceBeta,
Expand All @@ -194,5 +316,10 @@ export {
isChainDeprecated,
isChainConsensusLayer,
findChainById,
findSubnetMainnet,
hasChainFullBlockSupport,
getNumberOfSupportedChains,
getChainStatus,
getSupportedServices,
getChainSubnets,
};

0 comments on commit 952e73f

Please sign in to comment.