|
| 1 | +import { sortChains } from "@src/helpers"; |
| 2 | +import type { Shared } from "@src/shared/types"; |
| 3 | +import _ from "lodash"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Generic factory function to create type-safe chain query objects. |
| 7 | + * |
| 8 | + * @template T - Chain type extending ChainCommon constraint |
| 9 | + * @param chains - Record of chain definitions keyed by chain identifier |
| 10 | + * @returns Query object with type-safe methods for chain operations |
| 11 | + * |
| 12 | + * @example |
| 13 | + * ```typescript |
| 14 | + * const evmQueries = createChainQueries<Sablier.EVM.Chain>(evmChains); |
| 15 | + * const solanaQueries = createChainQueries<Sablier.Solana.Chain>(solanaChains); |
| 16 | + * ``` |
| 17 | + */ |
| 18 | +export function createChainQueries<T extends Shared.Chain>(chains: Record<string, T>) { |
| 19 | + return { |
| 20 | + /** |
| 21 | + * Find a chain by its numeric ID. |
| 22 | + * |
| 23 | + * @param chainId - The numeric chain identifier |
| 24 | + * @returns The chain if found, undefined otherwise |
| 25 | + */ |
| 26 | + get: (chainId: number): T | undefined => { |
| 27 | + return _.find(chains, (c) => c.id === chainId); |
| 28 | + }, |
| 29 | + |
| 30 | + /** |
| 31 | + * Get all chains sorted by name. |
| 32 | + * |
| 33 | + * @returns Array of all chains sorted alphabetically |
| 34 | + */ |
| 35 | + getAll: (): T[] => { |
| 36 | + return sortChains(_.values(chains)); |
| 37 | + }, |
| 38 | + |
| 39 | + /** |
| 40 | + * Find a chain by its slug identifier. |
| 41 | + * |
| 42 | + * @param slug - The chain slug (e.g., "ethereum", "solana-mainnet") |
| 43 | + * @returns The chain if found, undefined otherwise |
| 44 | + */ |
| 45 | + getBySlug: (slug: string): T | undefined => { |
| 46 | + return _.find(chains, (c) => c.slug === slug); |
| 47 | + }, |
| 48 | + |
| 49 | + /** |
| 50 | + * Get all mainnet chains sorted by name. |
| 51 | + * |
| 52 | + * @returns Array of mainnet chains sorted alphabetically |
| 53 | + */ |
| 54 | + getMainnets: (): T[] => { |
| 55 | + return sortChains(_.filter(_.values(chains), (c) => !c.isTestnet)); |
| 56 | + }, |
| 57 | + |
| 58 | + /** |
| 59 | + * Find a chain by its numeric ID, throwing an error if not found. |
| 60 | + * |
| 61 | + * @param chainId - The numeric chain identifier |
| 62 | + * @returns The chain |
| 63 | + * @throws Error if chain with the given ID is not found |
| 64 | + */ |
| 65 | + getOrThrow: (chainId: number): T => { |
| 66 | + const chain = _.find(chains, (c) => c.id === chainId); |
| 67 | + if (!chain) { |
| 68 | + throw new Error(`Sablier SDK: Chain with ID ${chainId} not found`); |
| 69 | + } |
| 70 | + return chain; |
| 71 | + }, |
| 72 | + |
| 73 | + /** |
| 74 | + * Get all testnet chains sorted by name. |
| 75 | + * |
| 76 | + * @returns Array of testnet chains sorted alphabetically |
| 77 | + */ |
| 78 | + getTestnets: (): T[] => { |
| 79 | + return sortChains(_.filter(_.values(chains), (c) => c.isTestnet)); |
| 80 | + }, |
| 81 | + }; |
| 82 | +} |
0 commit comments