Skip to content

Commit

Permalink
refactor(utils): re-organize 'networks' module structure and add tests
Browse files Browse the repository at this point in the history
re #715


Former-commit-id: ff3263d
  • Loading branch information
cedoor committed Mar 25, 2024
1 parent 8abfb93 commit 8d8fb82
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 97 deletions.
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const projects: any = fs
displayName: name,
setupFiles: ["dotenv/config"],
moduleNameMapper: {
"@semaphore-protocol/(.*)/(.*)": "<rootDir>/../$1/src/$2.ts",
"@semaphore-protocol/(.*)": "<rootDir>/../$1/src/index.ts"
"@semaphore-protocol/(.*)/(.*)": "<rootDir>/../$1/src/$2",
"@semaphore-protocol/(.*)": "<rootDir>/../$1/src"
}
}))

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default {
"fs",
"path",
"child_process",
"@semaphore-protocol/utils/supported-networks"
"@semaphore-protocol/utils/networks"
],
plugins: [
typescript({
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/scripts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type DeployedContracts = {
contracts: NetworkDeployedContracts
}[]

const deployedContractsPath = "../utils/src/deployed-contracts.json"
const deployedContractsPath = "../utils/src/networks/deployed-contracts.json"

export function getDeployedContracts(): DeployedContracts {
return JSON.parse(readFileSync(deployedContractsPath, "utf8"))
Expand Down
8 changes: 7 additions & 1 deletion packages/data/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ export default {
{ file: pkg.exports.require, format: "cjs", banner, exports: "auto" },
{ file: pkg.exports.default, format: "es", banner }
],
external: [...Object.keys(pkg.dependencies), "ethers/contract", "ethers/constants", "ethers/providers"],
external: [
...Object.keys(pkg.dependencies),
"ethers/contract",
"ethers/constants",
"ethers/providers",
"@semaphore-protocol/utils/networks"
],
plugins: [json(), typescript({ tsconfig: "./build.tsconfig.json" }), cleanup({ comments: "jsdoc" })]
}
8 changes: 6 additions & 2 deletions packages/data/src/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { SupportedNetwork, getDeployedContract, isSupportedNetwork } from "@semaphore-protocol/utils"
import { defaultNetwork } from "@semaphore-protocol/utils/networks"
import {
SupportedNetwork,
defaultNetwork,
getDeployedContract,
isSupportedNetwork
} from "@semaphore-protocol/utils/networks"
import { ZeroAddress } from "ethers/constants"
import { Contract } from "ethers/contract"
import {
Expand Down
3 changes: 1 addition & 2 deletions packages/data/src/subgraph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { SupportedNetwork } from "@semaphore-protocol/utils"
import { defaultNetwork } from "@semaphore-protocol/utils/networks"
import { defaultNetwork, SupportedNetwork } from "@semaphore-protocol/utils/networks"
import { AxiosRequestConfig } from "axios"
import checkParameter from "./checkParameter"
import getURL from "./getURL"
Expand Down
3 changes: 2 additions & 1 deletion packages/proof/rollup.browser.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default {
"ethers/utils",
"ethers/abi",
"@zk-kit/utils/error-handlers",
"@zk-kit/utils/proof-packing"
"@zk-kit/utils/proof-packing",
"@semaphore-protocol/utils/constants"
],
plugins: [
alias({
Expand Down
3 changes: 2 additions & 1 deletion packages/proof/rollup.node.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default {
"ethers/utils",
"ethers/abi",
"@zk-kit/utils/error-handlers",
"@zk-kit/utils/proof-packing"
"@zk-kit/utils/proof-packing",
"@semaphore-protocol/utils/constants"
],
plugins: [
typescript({
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"default": "./dist/index.js"
},
"./networks": {
"types": "./dist/types/networks.d.ts",
"require": "./dist/lib.commonjs/networks.cjs",
"default": "./dist/lib.esm/networks.js"
"types": "./dist/types/networks/index.d.ts",
"require": "./dist/lib.commonjs/networks/index.cjs",
"default": "./dist/lib.esm/networks/index.js"
},
"./constants": {
"types": "./dist/types/constants.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions packages/utils/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default [
{ file: pkg.exports["."].require, format: "cjs", banner, exports: "auto" },
{ file: pkg.exports["."].default, format: "es", banner }
],
external: [...Object.keys(pkg.dependencies), "ethers/abi", "ethers/utils"],
plugins: [
typescript({
tsconfig: "./build.tsconfig.json"
Expand All @@ -40,6 +41,7 @@ export default [
},
{ dir: "./dist/lib.esm", format: "es", banner, preserveModules: true }
],
external: [...Object.keys(pkg.dependencies), "ethers/abi", "ethers/utils"],
plugins: [
typescript({
tsconfig: "./build.tsconfig.json",
Expand Down
71 changes: 0 additions & 71 deletions packages/utils/src/networks.ts

This file was deleted.

File renamed without changes.
66 changes: 66 additions & 0 deletions packages/utils/src/networks/index.ts
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 }
26 changes: 26 additions & 0 deletions packages/utils/src/networks/supported-networks.ts
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"
}
}
48 changes: 36 additions & 12 deletions packages/utils/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
import { encodeBytes32String } from "ethers/abi"
import { toBigInt } from "ethers/utils"
import decodeMessage from "../src/decode-message"
import { getDeployedContract, isSupportedNetwork } from "../src/networks"
import { getDeployedContract, getHardhatNetworks, isSupportedNetwork, supportedNetworks } from "../src/networks"

describe("Utils", () => {
describe("# isSupportedNetwork", () => {
it("Should return true if the network is supported", () => {
expect(isSupportedNetwork("sepolia")).toBeTruthy()
describe("# networks", () => {
describe("# isSupportedNetwork", () => {
it("Should return true if the network is supported", () => {
expect(isSupportedNetwork("sepolia")).toBeTruthy()
})

it("Should return false if the network is not supported", () => {
expect(isSupportedNetwork("hello")).toBeFalsy()
})
})

it("Should return false if the network is not supported", () => {
expect(isSupportedNetwork("hello")).toBeFalsy()
describe("# getDeployedContract", () => {
it("Should return Semaphore deployment data for Sepolia", () => {
const { address, startBlock } = getDeployedContract("sepolia")

expect(address).toHaveLength(42)
expect(typeof startBlock).toBe("number")
})

it("Should throw an error if the network is not supported", () => {
const fun = () => getDeployedContract("hello" as any)

expect(fun).toThrow("Semaphore has not been deployed on 'hello' yet")
})
})
})

describe("# getDeployedContract", () => {
it("Should return Semaphore deployment data for Sepolia", () => {
const { address, startBlock } = getDeployedContract("sepolia")
describe("# getHardhatNetworks", () => {
it("Should return an empty object if the private key is not defined", () => {
const networks = getHardhatNetworks()

expect(networks).toEqual({})
})

it("Should return a list of networks compatible with the Hardhat 'networks' object", () => {
const networks = getHardhatNetworks("ec12f72ab17a2f14cf538a1a2455d6cd94ec99a90e8d8be591f987744b7b440f")

expect(address).toHaveLength(42)
expect(typeof startBlock).toBe("number")
expect(Object.keys(networks)).toEqual(Object.keys(supportedNetworks))
expect(Object.keys(networks)).toHaveLength(Object.keys(supportedNetworks).length)
expect(networks.sepolia.accounts).toHaveLength(1)
})
})
})

Expand Down

0 comments on commit 8d8fb82

Please sign in to comment.