Skip to content

Commit

Permalink
feat: add new configs folder structure (#54)
Browse files Browse the repository at this point in the history
* add new folder structure

* fix prettier

* apply suggestions

* fix prettier

* fix imports
  • Loading branch information
josemarinas authored Feb 15, 2024
1 parent 1cf46ff commit 3ac6c88
Show file tree
Hide file tree
Showing 21 changed files with 237 additions and 231 deletions.
46 changes: 46 additions & 0 deletions configs/src/deployments/contracts.ts
Original file line number Diff line number Diff line change
@@ -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,
},
};
52 changes: 52 additions & 0 deletions configs/src/deployments/getters.ts
Original file line number Diff line number Diff line change
@@ -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;
}
99 changes: 3 additions & 96 deletions configs/src/deployments/index.ts
Original file line number Diff line number Diff line change
@@ -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';
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
38 changes: 0 additions & 38 deletions configs/src/types.ts → configs/src/deployments/types.ts
Original file line number Diff line number Diff line change
@@ -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<T = NetworkConfig> = {
[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 {
Expand Down
3 changes: 1 addition & 2 deletions configs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './networks';
export * from './types';
export * from './deployments/index';
export * from './deployments';
87 changes: 87 additions & 0 deletions configs/src/networks/getters.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions configs/src/networks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './types';
Loading

0 comments on commit 3ac6c88

Please sign in to comment.