From 1cf46ff15dbda8481f9ee37558e7ea8b257d51f2 Mon Sep 17 00:00:00 2001 From: josemarinas <36479864+josemarinas@users.noreply.github.com> Date: Tue, 6 Feb 2024 10:38:01 +0100 Subject: [PATCH] feat: Add missing methods and local network support (#51) * add missing methods and local network support * revert getNetworkByNameAliasOrChainId * fix prettier * ci: another fix for the labeler --------- Co-authored-by: Mathias Scherer --- .github/labeler.yml | 24 ++++++------- configs/CHANGELOG.md | 11 ++++++ configs/package.json | 2 +- configs/src/deployments/index.ts | 4 +++ configs/src/networks.ts | 43 +++++++++++++++++------ configs/src/test/unit/networks.test.ts | 48 ++++++++++++++++---------- configs/src/types.ts | 2 ++ 7 files changed, 93 insertions(+), 41 deletions(-) diff --git a/.github/labeler.yml b/.github/labeler.yml index 8b790737..e4c0d711 100644 --- a/.github/labeler.yml +++ b/.github/labeler.yml @@ -1,27 +1,27 @@ contracts: - changed-files: - any-glob-to-any-file: - - ./contracts/**/* - - ./contracts/* - - ./.github/workflows/contracts-*.yml + - 'contracts/**/*' + - 'contracts/*' + - '.github/workflows/contracts-*.yml' sdk: - changed-files: - any-glob-to-any-file: - - ./sdk/**/* - - ./sdk/* - - ./.github/workflows/sdk-*.yml + - 'sdk/**/*' + - 'sdk/*' + - '.github/workflows/sdk-*.yml' subgraph: - changed-files: - any-glob-to-any-file: - - ./subgraph/**/* - - ./subgraph/* - - ./.github/workflows/subgraph-*.yml + - 'subgraph/**/*' + - 'subgraph/*' + - '.github/workflows/subgraph-*.yml' configs: - changed-files: - any-glob-to-any-file: - - ./configs/**/* - - ./configs/* - - ./.github/workflows/configs-*.yml + - 'configs/**/*' + - 'configs/*' + - '.github/workflows/configs-*.yml' diff --git a/configs/CHANGELOG.md b/configs/CHANGELOG.md index b03df4fe..7110ab29 100644 --- a/configs/CHANGELOG.md +++ b/configs/CHANGELOG.md @@ -5,6 +5,17 @@ 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 + +- 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..65e4bb93 100644 --- a/configs/src/networks.ts +++ b/configs/src/networks.ts @@ -19,6 +19,12 @@ 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. * @@ -85,89 +91,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..7eca8927 100644 --- a/configs/src/test/unit/networks.test.ts +++ b/configs/src/test/unit/networks.test.ts @@ -2,6 +2,7 @@ import { getNetwork, getNetworkAlias, getNetworkByAlias, + getNetworkByChainId, getNetworkByNameOrAlias, getNetworkNameByAlias, networks, @@ -36,8 +37,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(getNetworkByNameOrAlias(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 +59,10 @@ 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(getNetworkByNameOrAlias(network as string)).toMatchObject( + expected + ); }); }); }); @@ -123,4 +122,17 @@ describe('Deployments', () => { }); }); }); + describe('getNetworkByChainId', () => { + it('should get the network given the chainId', () => { + const inputs = Object.values(SupportedNetworks).map(network => { + return { + network: networks[network].chainId, + expected: networks[network], + }; + }); + inputs.map(({network, expected}) => { + expect(getNetworkByChainId(network)).toBe(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