From b613a67bab90227c6862859e56d3ad8eac5a6639 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Manuel=20Mari=C3=B1as=20Bascoy?= Date: Mon, 29 Jan 2024 17:47:22 +0100 Subject: [PATCH] add missing methods and local network support --- configs/CHANGELOG.md | 12 ++++++ configs/package.json | 2 +- configs/src/deployments/index.ts | 4 ++ configs/src/networks.ts | 58 +++++++++++++++++++------- configs/src/test/unit/networks.test.ts | 47 ++++++++++++--------- configs/src/types.ts | 2 + 6 files changed, 90 insertions(+), 35 deletions(-) diff --git a/configs/CHANGELOG.md b/configs/CHANGELOG.md index b03df4fe..53d66bb1 100644 --- a/configs/CHANGELOG.md +++ b/configs/CHANGELOG.md @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## v0.2.0 + +### Added + +- `getNetworkByChainId` function +- Support for local networks + +### Changed + +- `getNetworkByNameOrAlias` to `getNetworkByNameOrAliasOrChainId` +- All undefined return values to `null` instead of `undefined` + ## v0.1.0 ### Added diff --git a/configs/package.json b/configs/package.json index 871822ce..4ebf8e8a 100644 --- a/configs/package.json +++ b/configs/package.json @@ -1,7 +1,7 @@ { "name": "@aragon/osx-commons-configs", "author": "Aragon Association", - "version": "0.1.0", + "version": "0.2.0", "license": "AGPL-3.0-or-later", "typings": "dist/index.d.ts", "main": "dist/index.js", diff --git a/configs/src/deployments/index.ts b/configs/src/deployments/index.ts index 8e4e7e46..2d97db42 100644 --- a/configs/src/deployments/index.ts +++ b/configs/src/deployments/index.ts @@ -30,6 +30,10 @@ const contracts: { baseSepolia, arbitrum, arbitrumSepolia, + local: { + [SupportedVersions.V1_0_0]: {} as NetworkDeployment, + [SupportedVersions.V1_3_0]: {} as NetworkDeployment, + }, }; /** diff --git a/configs/src/networks.ts b/configs/src/networks.ts index ff8ee4b3..3ad1a275 100644 --- a/configs/src/networks.ts +++ b/configs/src/networks.ts @@ -19,17 +19,28 @@ export function getNetwork(network: SupportedNetworks): NetworkConfig | null { 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. + * @param {string | SupportedNetworks | number} 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 +export function getNetworkByNameAliasOrChainId( + network: string | SupportedNetworks | number ): NetworkConfig | null { - const networkConfig = - getNetworkByAlias(network) || getNetwork(network as SupportedNetworks); + let networkConfig: NetworkConfig | null = null; + if (typeof network === 'number') { + networkConfig = getNetworkByChainId(network); + } else { + networkConfig = + getNetworkByAlias(network) || getNetwork(network as SupportedNetworks); + } if (networkConfig) { return networkConfig; } @@ -85,89 +96,106 @@ export function getNetworkAlias( } export const networks: NetworkConfigs = { - mainnet: { + [SupportedNetworks.MAINNET]: { url: 'https://rpc.tenderly.co/fork/d3168a50-0941-42e2-8b9b-bf544c60c356', isTestnet: false, chainId: 1, + name: SupportedNetworks.MAINNET, aliases: { ethers5: 'homestead', }, }, - goerli: { + [SupportedNetworks.GOERLI]: { url: 'https://goerli.infura.io/v3/481a4cdc7c774286b8627f21c6827f48', isTestnet: true, chainId: 5, + name: SupportedNetworks.GOERLI, aliases: {}, }, - sepolia: { + [SupportedNetworks.SEPOLIA]: { url: 'https://sepolia.infura.io/v3/481a4cdc7c774286b8627f21c6827f48', isTestnet: true, chainId: 11155111, + name: SupportedNetworks.SEPOLIA, aliases: {}, }, - polygon: { + [SupportedNetworks.POLYGON]: { url: 'https://polygon-mainnet.infura.io/v3/481a4cdc7c774286b8627f21c6827f48', isTestnet: false, chainId: 137, feesUrl: 'https://gasstation-mainnet.matic.network/v2', + name: SupportedNetworks.POLYGON, aliases: { ethers5: 'matic', ethers6: 'matic', alchemySubgraphs: 'matic', }, }, - mumbai: { + [SupportedNetworks.MUMBAI]: { url: 'https://polygon-mumbai.infura.io/v3/481a4cdc7c774286b8627f21c6827f48', isTestnet: true, chainId: 80001, feesUrl: 'https://gasstation-mumbai.matic.today/v2', + name: SupportedNetworks.MUMBAI, aliases: { ethers5: 'maticmum', ethers6: 'matic-mumbai', alchemySubgraphs: 'mumbai', }, }, - baseMainnet: { + [SupportedNetworks.BASE]: { url: 'https://developer-access-mainnet.base.org', isTestnet: false, chainId: 8453, gasPrice: 1000, + name: SupportedNetworks.BASE, aliases: { alchemySubgraphs: 'base', }, }, - baseGoerli: { + [SupportedNetworks.BASE_GOERLI]: { url: 'https://goerli.base.org', isTestnet: true, chainId: 84531, gasPrice: 1000000, + name: SupportedNetworks.BASE_GOERLI, aliases: { alchemySubgraphs: 'base-testnet', }, }, - baseSepolia: { + [SupportedNetworks.BASE_SEPOLIA]: { url: 'https://sepolia.base.org', isTestnet: true, chainId: 84532, gasPrice: 1000000, + name: SupportedNetworks.BASE_SEPOLIA, aliases: { alchemySubgraphs: 'base-sepolia', }, }, - arbitrum: { + [SupportedNetworks.ARBITRUM]: { url: 'https://arbitrum-mainnet.infura.io/v3/481a4cdc7c774286b8627f21c6827f48', isTestnet: false, chainId: 42161, + name: SupportedNetworks.ARBITRUM, aliases: { alchemySubgraphs: 'arbitrum-one', }, }, - arbitrumSepolia: { + [SupportedNetworks.ARBITRUM_SEPOLIA]: { url: 'https://arbitrum-sepolia.infura.io/v3/481a4cdc7c774286b8627f21c6827f48', isTestnet: true, chainId: 421614, + name: SupportedNetworks.ARBITRUM_SEPOLIA, aliases: { alchemySubgraphs: 'arbitrum-sepolia', }, }, + [SupportedNetworks.LOCAL]: { + url: 'http://localhost:8545', + isTestnet: true, + chainId: 31337, + name: SupportedNetworks.LOCAL, + aliases: {}, + }, }; diff --git a/configs/src/test/unit/networks.test.ts b/configs/src/test/unit/networks.test.ts index 5a736202..cf4fc30d 100644 --- a/configs/src/test/unit/networks.test.ts +++ b/configs/src/test/unit/networks.test.ts @@ -2,7 +2,7 @@ import { getNetwork, getNetworkAlias, getNetworkByAlias, - getNetworkByNameOrAlias, + getNetworkByNameAliasOrChainId, getNetworkNameByAlias, networks, } from '../../networks'; @@ -36,8 +36,19 @@ describe('Deployments', () => { }); }); describe('getNetworkByNameOrAlias', () => { - it('should return the correct value', () => { - let inputs = Object.values(SupportedNetworks) + it('should return a network given a name', () => { + const inputs = Object.values(SupportedNetworks).map(network => { + return { + network, + expected: networks[network], + }; + }); + inputs.map(({network, expected}) => { + expect(getNetworkByNameAliasOrChainId(network)).toMatchObject(expected); + }); + }); + it('should return a network given an alias', () => { + const inputs = Object.values(SupportedNetworks) .flatMap(nw => { return Object.values(SupportedAliases).map(alias => { return { @@ -47,23 +58,21 @@ describe('Deployments', () => { }); }) .filter(({network}) => network !== undefined); - - inputs = inputs.concat( - Object.values(SupportedNetworks).map(network => { - return { - network, - expected: networks[network], - }; - }) - ); - inputs.map(({network, expected}) => { - if (!expected) { - expect(getNetworkByNameOrAlias(network as string)).toBeNull(); - return; - } - const res = getNetworkByNameOrAlias(network as string); - expect(res).toMatchObject(expected); + expect(getNetworkByNameAliasOrChainId(network as string)).toMatchObject( + expected + ); + }); + }); + it('should return a network given a chainId', () => { + const inputs = Object.values(SupportedNetworks).map(network => { + return { + network: networks[network].chainId, + expected: networks[network], + }; + }); + inputs.map(({network, expected}) => { + expect(getNetworkByNameAliasOrChainId(network)).toMatchObject(expected); }); }); }); diff --git a/configs/src/types.ts b/configs/src/types.ts index 279cc73a..4bd47279 100644 --- a/configs/src/types.ts +++ b/configs/src/types.ts @@ -2,6 +2,7 @@ export type NetworkConfig = { url: string; isTestnet: boolean; chainId: number; + name: SupportedNetworks; feesUrl?: string; gasPrice?: number; aliases: NetworkAliases; @@ -32,6 +33,7 @@ export enum SupportedNetworks { BASE_SEPOLIA = 'baseSepolia', ARBITRUM = 'arbitrum', ARBITRUM_SEPOLIA = 'arbitrumSepolia', + LOCAL = 'local', } // the entries in this enum has to be in order from