UNPKG

viem

Version:

TypeScript Interface for Ethereum

40 lines 1.26 kB
/** * Filters a chain registry or array by structured criteria. * * @experimental * * @param parameters - Chain registry or array and criteria to filter by. * @returns Matching chains. */ export function filterChains(parameters) { const { chains, sort, testnet, token } = parameters; const values = Array.isArray(chains) ? chains : Object.values(chains); const filtered = []; for (const chain of values) { if (!isChain(chain)) continue; if (token && !(chain.id in token.addresses)) continue; if (testnet === true && chain.testnet !== true) continue; if (testnet === false && chain.testnet === true) continue; filtered.push(chain); } if (sort === 'id') filtered.sort((a, b) => a.id - b.id); if (sort === 'name') filtered.sort((a, b) => a.name.localeCompare(b.name)); return filtered; } function isChain(chain) { return (typeof chain === 'object' && chain !== null && 'id' in chain && typeof chain.id === 'number' && 'name' in chain && typeof chain.name === 'string' && 'nativeCurrency' in chain && 'rpcUrls' in chain); } //# sourceMappingURL=filterChains.js.map