Skip to content

Commit

Permalink
add missing methods and local network support
Browse files Browse the repository at this point in the history
  • Loading branch information
josemarinas committed Jan 29, 2024
1 parent 67b6ccd commit b613a67
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 35 deletions.
12 changes: 12 additions & 0 deletions configs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion configs/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 4 additions & 0 deletions configs/src/deployments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ const contracts: {
baseSepolia,
arbitrum,
arbitrumSepolia,
local: {
[SupportedVersions.V1_0_0]: {} as NetworkDeployment,
[SupportedVersions.V1_3_0]: {} as NetworkDeployment,
},
};

/**
Expand Down
58 changes: 43 additions & 15 deletions configs/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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: {},
},
};
47 changes: 28 additions & 19 deletions configs/src/test/unit/networks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
getNetwork,
getNetworkAlias,
getNetworkByAlias,
getNetworkByNameOrAlias,
getNetworkByNameAliasOrChainId,
getNetworkNameByAlias,
networks,
} from '../../networks';
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions configs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type NetworkConfig = {
url: string;
isTestnet: boolean;
chainId: number;
name: SupportedNetworks;
feesUrl?: string;
gasPrice?: number;
aliases: NetworkAliases;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b613a67

Please sign in to comment.