diff --git a/configs/src/deployments/contracts.ts b/configs/src/deployments/contracts.ts new file mode 100644 index 00000000..031b02c7 --- /dev/null +++ b/configs/src/deployments/contracts.ts @@ -0,0 +1,46 @@ +import {SupportedNetworks} from '../networks'; +import * as arbitrum from './json/arbitrum.json'; +import * as arbitrumSepolia from './json/arbitrumSepolia.json'; +import * as baseGoerli from './json/baseGoerli.json'; +import * as baseMainnet from './json/baseMainnet.json'; +import * as baseSepolia from './json/baseSepolia.json'; +import * as goerli from './json/goerli.json'; +import * as mainnet from './json/mainnet.json'; +import * as mumbai from './json/mumbai.json'; +import * as polygon from './json/polygon.json'; +import * as sepolia from './json/sepolia.json'; +import {NetworkDeployment, SupportedVersions} from './types'; + +export { + mainnet, + goerli, + sepolia, + polygon, + mumbai, + baseMainnet, + baseGoerli, + baseSepolia, + arbitrum, + arbitrumSepolia, +}; + +export const contracts: { + [network in SupportedNetworks]: { + [version in SupportedVersions]?: NetworkDeployment; + }; +} = { + mainnet, + goerli, + sepolia, + polygon, + mumbai, + baseMainnet, + baseGoerli, + baseSepolia, + arbitrum, + arbitrumSepolia, + local: { + [SupportedVersions.V1_0_0]: {} as NetworkDeployment, + [SupportedVersions.V1_3_0]: {} as NetworkDeployment, + }, +}; diff --git a/configs/src/deployments/getters.ts b/configs/src/deployments/getters.ts new file mode 100644 index 00000000..30432b52 --- /dev/null +++ b/configs/src/deployments/getters.ts @@ -0,0 +1,52 @@ +import {SupportedNetworks} from '../networks'; +import {contracts} from './contracts'; +import { + NetworkDeployment, + NetworkDeployments, + SupportedVersions, +} from './types'; + +/** + * Retrieves the network deployments based on the specified network. + * + * @param {SupportedNetworks} network - The network to retrieve the deployments for. + * @return {NetworkDeployments} The network deployments for the specified network. + */ +export function getNetworkDeployments( + network: SupportedNetworks +): NetworkDeployments { + return contracts[network]; +} + +/** + * Retrieves the network deployment for a specific version. + * + * @param {SupportedNetworks} network - The network to retrieve the deployment for. + * @param {SupportedVersions} version - The version of the deployment. + * @return {NetworkDeployment | null} The network deployment for the specified version, or null if not found. + */ +export function getNetworkDeploymentForVersion( + network: SupportedNetworks, + version: SupportedVersions +): NetworkDeployment | null { + return getNetworkDeployments(network)[version] || null; +} + +/** + * Retrieves the latest network deployment for the specified network. + * + * @param {SupportedNetworks} network - The network to retrieve the deployment for. + * @return {NetworkDeployment | null} The latest network deployment, or null if not found. + */ +export function getLatestNetworkDeployment( + network: SupportedNetworks +): NetworkDeployment | null { + const versions = Object.values(SupportedVersions).reverse(); + for (const version of versions) { + const deployment = getNetworkDeploymentForVersion(network, version); + if (deployment) { + return deployment; + } + } + return null; +} diff --git a/configs/src/deployments/index.ts b/configs/src/deployments/index.ts index 2d97db42..98551508 100644 --- a/configs/src/deployments/index.ts +++ b/configs/src/deployments/index.ts @@ -1,96 +1,3 @@ -import { - NetworkDeployment, - NetworkDeployments, - SupportedNetworks, - SupportedVersions, -} from '../types'; -import * as arbitrum from './arbitrum.json'; -import * as arbitrumSepolia from './arbitrumSepolia.json'; -import * as baseGoerli from './baseGoerli.json'; -import * as baseMainnet from './baseMainnet.json'; -import * as baseSepolia from './baseSepolia.json'; -import * as goerli from './goerli.json'; -import * as mainnet from './mainnet.json'; -import * as mumbai from './mumbai.json'; -import * as polygon from './polygon.json'; -import * as sepolia from './sepolia.json'; - -const contracts: { - [network in SupportedNetworks]: { - [version in SupportedVersions]?: NetworkDeployment; - }; -} = { - mainnet, - goerli, - sepolia, - polygon, - mumbai, - baseMainnet, - baseGoerli, - baseSepolia, - arbitrum, - arbitrumSepolia, - local: { - [SupportedVersions.V1_0_0]: {} as NetworkDeployment, - [SupportedVersions.V1_3_0]: {} as NetworkDeployment, - }, -}; - -/** - * Retrieves the network deployments based on the specified network. - * - * @param {SupportedNetworks} network - The network to retrieve the deployments for. - * @return {NetworkDeployments} The network deployments for the specified network. - */ -export function getNetworkDeployments( - network: SupportedNetworks -): NetworkDeployments { - return contracts[network]; -} - -/** - * Retrieves the network deployment for a specific version. - * - * @param {SupportedNetworks} network - The network to retrieve the deployment for. - * @param {SupportedVersions} version - The version of the deployment. - * @return {NetworkDeployment | null} The network deployment for the specified version, or null if not found. - */ -export function getNetworkDeploymentForVersion( - network: SupportedNetworks, - version: SupportedVersions -): NetworkDeployment | null { - return getNetworkDeployments(network)[version] || null; -} - -/** - * Retrieves the latest network deployment for the specified network. - * - * @param {SupportedNetworks} network - The network to retrieve the deployment for. - * @return {NetworkDeployment | null} The latest network deployment, or null if not found. - */ -export function getLatestNetworkDeployment( - network: SupportedNetworks -): NetworkDeployment | null { - const versions = Object.values(SupportedVersions).reverse(); - for (const version of versions) { - const deployment = getNetworkDeploymentForVersion(network, version); - if (deployment) { - return deployment; - } - } - return null; -} - -export { - contracts, - mainnet, - goerli, - sepolia, - polygon, - mumbai, - baseMainnet, - baseGoerli, - baseSepolia, - arbitrum, - arbitrumSepolia, -}; +export * from './types'; +export * from './getters'; +export * from './contracts'; diff --git a/configs/src/deployments/arbitrum.json b/configs/src/deployments/json/arbitrum.json similarity index 100% rename from configs/src/deployments/arbitrum.json rename to configs/src/deployments/json/arbitrum.json diff --git a/configs/src/deployments/arbitrumSepolia.json b/configs/src/deployments/json/arbitrumSepolia.json similarity index 100% rename from configs/src/deployments/arbitrumSepolia.json rename to configs/src/deployments/json/arbitrumSepolia.json diff --git a/configs/src/deployments/baseGoerli.json b/configs/src/deployments/json/baseGoerli.json similarity index 100% rename from configs/src/deployments/baseGoerli.json rename to configs/src/deployments/json/baseGoerli.json diff --git a/configs/src/deployments/baseMainnet.json b/configs/src/deployments/json/baseMainnet.json similarity index 100% rename from configs/src/deployments/baseMainnet.json rename to configs/src/deployments/json/baseMainnet.json diff --git a/configs/src/deployments/baseSepolia.json b/configs/src/deployments/json/baseSepolia.json similarity index 100% rename from configs/src/deployments/baseSepolia.json rename to configs/src/deployments/json/baseSepolia.json diff --git a/configs/src/deployments/goerli.json b/configs/src/deployments/json/goerli.json similarity index 100% rename from configs/src/deployments/goerli.json rename to configs/src/deployments/json/goerli.json diff --git a/configs/src/deployments/mainnet.json b/configs/src/deployments/json/mainnet.json similarity index 100% rename from configs/src/deployments/mainnet.json rename to configs/src/deployments/json/mainnet.json diff --git a/configs/src/deployments/mumbai.json b/configs/src/deployments/json/mumbai.json similarity index 100% rename from configs/src/deployments/mumbai.json rename to configs/src/deployments/json/mumbai.json diff --git a/configs/src/deployments/polygon.json b/configs/src/deployments/json/polygon.json similarity index 100% rename from configs/src/deployments/polygon.json rename to configs/src/deployments/json/polygon.json diff --git a/configs/src/deployments/sepolia.json b/configs/src/deployments/json/sepolia.json similarity index 100% rename from configs/src/deployments/sepolia.json rename to configs/src/deployments/json/sepolia.json diff --git a/configs/src/types.ts b/configs/src/deployments/types.ts similarity index 77% rename from configs/src/types.ts rename to configs/src/deployments/types.ts index 4bd47279..fae16a3d 100644 --- a/configs/src/types.ts +++ b/configs/src/deployments/types.ts @@ -1,41 +1,3 @@ -export type NetworkConfig = { - url: string; - isTestnet: boolean; - chainId: number; - name: SupportedNetworks; - feesUrl?: string; - gasPrice?: number; - aliases: NetworkAliases; -}; - -export type NetworkAliases = { - [index in SupportedAliases]?: string; -}; - -export type NetworkConfigs = { - [network in SupportedNetworks]: T; -}; - -export enum SupportedAliases { - ETHERS_5 = 'ethers5', - ETHERS_6 = 'ethers6', - ALCHEMY_SUBGRAPHS = 'alchemySubgraphs', -} - -export enum SupportedNetworks { - MAINNET = 'mainnet', - GOERLI = 'goerli', - SEPOLIA = 'sepolia', - POLYGON = 'polygon', - MUMBAI = 'mumbai', - BASE = 'baseMainnet', - BASE_GOERLI = 'baseGoerli', - BASE_SEPOLIA = 'baseSepolia', - ARBITRUM = 'arbitrum', - ARBITRUM_SEPOLIA = 'arbitrumSepolia', - LOCAL = 'local', -} - // the entries in this enum has to be in order from // oldest to newest so that getLatestNetworkVersion() works as expected export enum SupportedVersions { diff --git a/configs/src/index.ts b/configs/src/index.ts index 48e54ee3..188d6a8b 100644 --- a/configs/src/index.ts +++ b/configs/src/index.ts @@ -1,3 +1,2 @@ export * from './networks'; -export * from './types'; -export * from './deployments/index'; +export * from './deployments'; diff --git a/configs/src/networks/getters.ts b/configs/src/networks/getters.ts new file mode 100644 index 00000000..4b2c5a5d --- /dev/null +++ b/configs/src/networks/getters.ts @@ -0,0 +1,87 @@ +import {networks} from './networks'; +import {NetworkConfig, SupportedAliases, SupportedNetworks} from './types'; + +/** + * Retrieves the network configuration for a given supported network. + * + * @param {SupportedNetworks} network - The supported network to retrieve the configuration for. + * @return {NetworkConfig | null} The network configuration if it exists, otherwise null. + */ +export function getNetwork(network: SupportedNetworks): NetworkConfig | null { + if (networks[network]) { + return networks[network]; + } + + return null; +} + +export function getNetworkByChainId(chainId: number): NetworkConfig | null { + return ( + Object.values(networks).find(network => network.chainId === chainId) || null + ); +} + +/** + * Retrieves the network configuration object by name or alias. + * + * @param {string | SupportedNetworks} network - The name or alias of the network. + * @return {NetworkConfig | null} The network configuration object if found, or `null` if not found. + */ +export function getNetworkByNameOrAlias( + network: string | SupportedNetworks +): NetworkConfig | null { + const networkConfig = + getNetworkByAlias(network) || getNetwork(network as SupportedNetworks); + if (networkConfig) { + return networkConfig; + } + return null; +} + +/** + * Retrieves the network configuration object based on the given alias. + * + * @param {string} alias - The alias of the network. + * @return {NetworkConfig | null} The network configuration object corresponding to the alias, or null if not found. + */ +export function getNetworkByAlias(alias: string): NetworkConfig | null { + const networkName = getNetworkNameByAlias(alias); + if (networkName) { + return getNetwork(networkName); + } + return null; +} + +/** + * Retrieves the network name by its alias. If the name is already supported it returns the alias back as name + * + * @param {string} alias - The alias of the network. + * @return {SupportedNetworks | null} The network name corresponding to the alias, or null if no match is found. + */ +export function getNetworkNameByAlias(alias: string): SupportedNetworks | null { + if (Object.values(SupportedNetworks).includes(alias as SupportedNetworks)) { + return alias as SupportedNetworks; + } + + for (const networkName of Object.values(SupportedNetworks)) { + const network = getNetwork(networkName); + if (network) { + const aliases = Object.values(network.aliases); + if (aliases.includes(alias)) { + return networkName; + } + } + } + return null; +} + +export function getNetworkAlias( + aliasName: SupportedAliases, + network: SupportedNetworks +): string | null { + const networkConfig = getNetwork(network); + if (!networkConfig) { + return null; + } + return networkConfig.aliases[aliasName] || network; +} diff --git a/configs/src/networks/index.ts b/configs/src/networks/index.ts new file mode 100644 index 00000000..fcb073fe --- /dev/null +++ b/configs/src/networks/index.ts @@ -0,0 +1 @@ +export * from './types'; diff --git a/configs/src/networks.ts b/configs/src/networks/networks.ts similarity index 51% rename from configs/src/networks.ts rename to configs/src/networks/networks.ts index 65e4bb93..a090aa44 100644 --- a/configs/src/networks.ts +++ b/configs/src/networks/networks.ts @@ -1,94 +1,4 @@ -import { - NetworkConfig, - NetworkConfigs, - SupportedAliases, - SupportedNetworks, -} from './types'; - -/** - * Retrieves the network configuration for a given supported network. - * - * @param {SupportedNetworks} network - The supported network to retrieve the configuration for. - * @return {NetworkConfig | null} The network configuration if it exists, otherwise null. - */ -export function getNetwork(network: SupportedNetworks): NetworkConfig | null { - if (networks[network]) { - return networks[network]; - } - - return null; -} - -export function getNetworkByChainId(chainId: number): NetworkConfig | null { - return ( - Object.values(networks).find(network => network.chainId === chainId) || null - ); -} - -/** - * Retrieves the network configuration object by name or alias. - * - * @param {string | SupportedNetworks} network - The name or alias of the network. - * @return {NetworkConfig | null} The network configuration object if found, or `null` if not found. - */ -export function getNetworkByNameOrAlias( - network: string | SupportedNetworks -): NetworkConfig | null { - const networkConfig = - getNetworkByAlias(network) || getNetwork(network as SupportedNetworks); - if (networkConfig) { - return networkConfig; - } - return null; -} - -/** - * Retrieves the network configuration object based on the given alias. - * - * @param {string} alias - The alias of the network. - * @return {NetworkConfig | null} The network configuration object corresponding to the alias, or null if not found. - */ -export function getNetworkByAlias(alias: string): NetworkConfig | null { - const networkName = getNetworkNameByAlias(alias); - if (networkName) { - return getNetwork(networkName); - } - return null; -} - -/** - * Retrieves the network name by its alias. If the name is already supported it returns the alias back as name - * - * @param {string} alias - The alias of the network. - * @return {SupportedNetworks | null} The network name corresponding to the alias, or null if no match is found. - */ -export function getNetworkNameByAlias(alias: string): SupportedNetworks | null { - if (Object.values(SupportedNetworks).includes(alias as SupportedNetworks)) { - return alias as SupportedNetworks; - } - - for (const networkName of Object.values(SupportedNetworks)) { - const network = getNetwork(networkName); - if (network) { - const aliases = Object.values(network.aliases); - if (aliases.includes(alias)) { - return networkName; - } - } - } - return null; -} - -export function getNetworkAlias( - aliasName: SupportedAliases, - network: SupportedNetworks -): string | null { - const networkConfig = getNetwork(network); - if (!networkConfig) { - return null; - } - return networkConfig.aliases[aliasName] || network; -} +import {NetworkConfigs, SupportedNetworks} from './types'; export const networks: NetworkConfigs = { [SupportedNetworks.MAINNET]: { diff --git a/configs/src/networks/types.ts b/configs/src/networks/types.ts new file mode 100644 index 00000000..d6989923 --- /dev/null +++ b/configs/src/networks/types.ts @@ -0,0 +1,37 @@ +export enum SupportedAliases { + ETHERS_5 = 'ethers5', + ETHERS_6 = 'ethers6', + ALCHEMY_SUBGRAPHS = 'alchemySubgraphs', +} + +export enum SupportedNetworks { + MAINNET = 'mainnet', + GOERLI = 'goerli', + SEPOLIA = 'sepolia', + POLYGON = 'polygon', + MUMBAI = 'mumbai', + BASE = 'baseMainnet', + BASE_GOERLI = 'baseGoerli', + BASE_SEPOLIA = 'baseSepolia', + ARBITRUM = 'arbitrum', + ARBITRUM_SEPOLIA = 'arbitrumSepolia', + LOCAL = 'local', +} + +export type NetworkConfig = { + url: string; + isTestnet: boolean; + chainId: number; + name: SupportedNetworks; + feesUrl?: string; + gasPrice?: number; + aliases: NetworkAliases; +}; + +export type NetworkAliases = { + [index in SupportedAliases]?: string; +}; + +export type NetworkConfigs = { + [network in SupportedNetworks]: NetworkConfig; +}; diff --git a/configs/src/test/unit/deployments.test.ts b/configs/src/test/unit/deployments.test.ts index 33a75a62..1faaa86b 100644 --- a/configs/src/test/unit/deployments.test.ts +++ b/configs/src/test/unit/deployments.test.ts @@ -1,10 +1,11 @@ import { + SupportedVersions, contracts, getLatestNetworkDeployment, getNetworkDeploymentForVersion, getNetworkDeployments, } from '../../deployments'; -import {SupportedNetworks, SupportedVersions} from '../../types'; +import {SupportedNetworks} from '../../networks'; describe('Deployments', () => { describe('getNetworkDeployments', () => { diff --git a/configs/src/test/unit/networks.test.ts b/configs/src/test/unit/networks.test.ts index 7eca8927..0c62653a 100644 --- a/configs/src/test/unit/networks.test.ts +++ b/configs/src/test/unit/networks.test.ts @@ -1,3 +1,8 @@ +import { + NetworkConfig, + SupportedAliases, + SupportedNetworks, +} from '../../networks'; import { getNetwork, getNetworkAlias, @@ -5,9 +10,8 @@ import { getNetworkByChainId, getNetworkByNameOrAlias, getNetworkNameByAlias, - networks, -} from '../../networks'; -import {NetworkConfig, SupportedAliases, SupportedNetworks} from '../../types'; +} from '../../networks/getters'; +import {networks} from '../../networks/networks'; describe('Deployments', () => { describe('getNetwork', () => {