-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(utils): re-organize 'networks' module structure and add tests
- Loading branch information
Showing
15 changed files
with
155 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* @module Networks | ||
* This module provides a collection of utility functions to provide the other internal | ||
* packages and developers with information on deployed contracts and networks supported | ||
* by Semaphore. | ||
*/ | ||
|
||
import deployedContracts from "./deployed-contracts.json" | ||
import supportedNetworks from "./supported-networks" | ||
|
||
export type SupportedNetwork = keyof typeof supportedNetworks | ||
|
||
// Default Semaphore network. | ||
export const defaultNetwork: SupportedNetwork = "sepolia" | ||
|
||
/** | ||
* Returns true if a network is supported by Semaphore, false otherwise. | ||
* @param supportedNetwork The network to be checked. | ||
*/ | ||
export function isSupportedNetwork(supportedNetwork: string): boolean { | ||
return Object.keys(supportedNetworks).includes(supportedNetwork) | ||
} | ||
|
||
/** | ||
* Utility function to get an object compatible with the Hardhat 'networks' option. | ||
* If the private key is not defined it returns an empty object. | ||
* @param privateKey Private key to be used with networks. | ||
* @returns An object compatible with the Hardhat 'networks' option. | ||
*/ | ||
export function getHardhatNetworks(privateKey?: string) { | ||
if (!privateKey) { | ||
return {} | ||
} | ||
|
||
const supportedNetworksCopy = JSON.parse(JSON.stringify(supportedNetworks)) | ||
|
||
for (const key in supportedNetworksCopy) { | ||
if (Object.prototype.hasOwnProperty.call(supportedNetworksCopy, key)) { | ||
supportedNetworksCopy[key].accounts = [`0x${privateKey}`] | ||
} | ||
} | ||
|
||
return supportedNetworksCopy | ||
} | ||
|
||
/** | ||
* Returns name, address and start block of a Semaphore contract deployed | ||
* on a specific supported network. | ||
* @param The network supported by Semaphore. | ||
* @returns An object with name, address and start block of the deployed contract. | ||
*/ | ||
export function getDeployedContract(supportedNetwork: SupportedNetwork) { | ||
if (!isSupportedNetwork(supportedNetwork)) { | ||
throw new Error(`Semaphore has not been deployed on '${supportedNetwork}' yet`) | ||
} | ||
|
||
const deployedContract = deployedContracts.find(({ network }) => network === supportedNetwork) | ||
|
||
return deployedContract!.contracts.find(({ name }) => name === "Semaphore") as { | ||
name: string | ||
address: string | ||
startBlock: number | ||
} | ||
} | ||
|
||
export { deployedContracts, supportedNetworks } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export default { | ||
sepolia: { | ||
name: "Sepolia", | ||
url: "https://rpc2.sepolia.org", | ||
chainId: 11155111, | ||
explorer: "https://sepolia.etherscan.io" | ||
}, | ||
"arbitrum-sepolia": { | ||
name: "Arbitrum Sepolia", | ||
url: "https://sepolia-rollup.arbitrum.io/rpc", | ||
chainId: 421614, | ||
explorer: "https://sepolia.arbiscan.io" | ||
}, | ||
"optimism-sepolia": { | ||
name: "Optimism Sepolia", | ||
url: "https://sepolia.optimism.io", | ||
chainId: 11155420, | ||
explorer: "https://sepolia-optimism.etherscan.io" | ||
}, | ||
"matic-mumbai": { | ||
name: "Matic Mumbai", | ||
url: "https://rpc-mumbai.polygon.technology", | ||
chainId: 80001, | ||
explorer: "https://mumbai.polygonscan.com" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters