Skip to content

Commit

Permalink
feat(utils): add deployed contracts to utils package
Browse files Browse the repository at this point in the history
re #715
  • Loading branch information
cedoor committed Mar 22, 2024
1 parent 00f0551 commit 1647eca
Show file tree
Hide file tree
Showing 21 changed files with 93 additions and 76 deletions.
1 change: 1 addition & 0 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@docusaurus/core": "3.1.1",
"@docusaurus/preset-classic": "3.1.1",
"@mdx-js/react": "^3.0.0",
"@semaphore-protocol/utils": "4.0.0-beta.4",
"@svgr/webpack": "^5.5.0",
"clsx": "^1.2.1",
"docusaurus-plugin-sass": "^0.2.5",
Expand Down
13 changes: 1 addition & 12 deletions apps/docs/src/components/DeployedContracts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deployedContracts } from "@semaphore-protocol/utils"
import Heading from "@theme/Heading"
import { useEffect, useState } from "react"

function capitalizeFirstLetter(s: string): string {
return s.charAt(0).toUpperCase() + s.slice(1)
Expand All @@ -23,17 +23,6 @@ function getEtherscanLink(network: string): string {
}

export default function DeployedContracts() {
const [deployedContracts, setDeployedContracts] = useState<any[]>([])

useEffect(() => {
fetch(
"https://raw.githubusercontent.com/semaphore-protocol/semaphore/main/packages/contracts/deployed-contracts.json"
)
.then((response) => response.json())
.catch(() => [])
.then(setDeployedContracts)
}, [])

return (
<div>
{deployedContracts.map(({ network, contracts }) => (
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GroupResponse, SemaphoreEthers, SemaphoreSubgraph } from "@semaphore-protocol/data"
import supportedNetworks from "@semaphore-protocol/utils/supported-networks"
import { supportedNetworks } from "@semaphore-protocol/utils/networks"
import chalk from "chalk"
import { program } from "commander"
import decompress from "decompress"
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts/deployed-contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
},
{
"name": "Semaphore",
"address": "0x5B8e7cC7bAC61A4b952d472b67056B2f260ba6dc"
"address": "0x5B8e7cC7bAC61A4b952d472b67056B2f260ba6dc",
"startBlock": 5150903
}
]
}
Expand Down
5 changes: 4 additions & 1 deletion packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "@nomicfoundation/hardhat-chai-matchers"
import "@nomicfoundation/hardhat-ethers"
import "@nomicfoundation/hardhat-verify"
import "@openzeppelin/hardhat-upgrades"
import { SupportedNetwork } from "@semaphore-protocol/utils"
import "@typechain/hardhat"
import { config as dotenvConfig } from "dotenv"
import "hardhat-gas-reporter"
Expand All @@ -22,7 +23,7 @@ function getNetworks(): NetworksUserConfig {
const infuraApiKey = process.env.INFURA_API_KEY
const accounts = [`0x${process.env.BACKEND_PRIVATE_KEY}`]

return {
const networks: Record<SupportedNetwork, any> = {
sepolia: {
url: `https://sepolia.infura.io/v3/${infuraApiKey}`,
chainId: 11155111,
Expand All @@ -49,6 +50,8 @@ function getNetworks(): NetworksUserConfig {
accounts
}
}

return networks
}

const hardhatConfig: HardhatUserConfig = {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@nomicfoundation/hardhat-verify": "^2.0.0",
"@openzeppelin/hardhat-upgrades": "^3.0.4",
"@semaphore-protocol/core": "workspace:packages/core",
"@semaphore-protocol/utils": "workspace:packages/utils",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.2.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/scripts/utils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { SupportedNetwork, supportedNetworks } from "@semaphore-protocol/utils"
import { readFileSync, writeFileSync } from "fs"

export type NetworkDeployedContracts = {
name: "Semaphore" | "SemaphoreVerifier" | "PoseidonT3"
address: string
startBlock?: number
}[]

export type DeployedContracts = {
network: string
contracts: NetworkDeployedContracts
}[]

const supportedNetworks = ["sepolia", "arbitrum", "mumbai", "optimism-sepolia", "arbitrum-sepolia"]

export function getDeployedContracts(): DeployedContracts {
return JSON.parse(readFileSync(`./deployed-contracts.json`, "utf8"))
}
Expand All @@ -38,7 +38,7 @@ export function getDeployedContractAddress(network: string, contractName: string
return semaphoreAddress.address
}

export function saveDeployedContracts(contracts: NetworkDeployedContracts, network?: string) {
export function saveDeployedContracts(contracts: NetworkDeployedContracts, network?: SupportedNetwork) {
if (network && supportedNetworks.includes(network)) {
const deployedContracts = getDeployedContracts() as DeployedContracts

Expand Down
11 changes: 9 additions & 2 deletions packages/contracts/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SupportedNetwork } from "@semaphore-protocol/utils"
import { task, types } from "hardhat/config"
import { saveDeployedContracts } from "../scripts/utils"
import { deployContract } from "./utils"
Expand Down Expand Up @@ -51,6 +52,8 @@ task("deploy", "Deploy a Semaphore contract")
console.info(`Semaphore contract has been deployed to: ${semaphoreAddress}`)
}

const deploymentTransaction = semaphore.deploymentTransaction()

saveDeployedContracts(
[
{
Expand All @@ -63,10 +66,14 @@ task("deploy", "Deploy a Semaphore contract")
},
{
name: "Semaphore",
address: semaphoreAddress
address: semaphoreAddress,
startBlock:
deploymentTransaction && deploymentTransaction.blockNumber
? deploymentTransaction.blockNumber
: undefined
}
],
hardhatArguments.network
hardhatArguments.network as SupportedNetwork
)

return {
Expand Down
4 changes: 1 addition & 3 deletions packages/contracts/tasks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ export async function deployContract(

if (network !== undefined && network !== "hardhat" && network !== "localhost") {
contract = await defender.deployContract(contractFactory, args, { salt: process.env.CREATE2_SALT })

await contract.waitForDeployment()
} else {
contract = await contractFactory.deploy(...args)
}

return contract
return contract.waitForDeployment()
}
1 change: 1 addition & 0 deletions packages/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"rollup-plugin-cleanup": "^3.2.1"
},
"dependencies": {
"@semaphore-protocol/utils": "4.0.0-beta.4",
"axios": "1.6.6",
"ethers": "6.11.0"
}
Expand Down
40 changes: 13 additions & 27 deletions packages/data/src/ethers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SupportedNetwork, getDeployedContract, supportedNetworks } from "@semaphore-protocol/utils"
import { defaultNetwork } from "@semaphore-protocol/utils/networks"
import { ZeroAddress } from "ethers/constants"
import { Contract } from "ethers/contract"
import {
Expand Down Expand Up @@ -25,7 +27,7 @@ export default class SemaphoreEthers {
* @param networkOrEthereumURL Ethereum network or custom URL.
* @param options Ethers options.
*/
constructor(networkOrEthereumURL: EthersNetwork | string = "sepolia", options: EthersOptions = {}) {
constructor(networkOrEthereumURL: EthersNetwork | string = defaultNetwork, options: EthersOptions = {}) {
checkParameter(networkOrEthereumURL, "networkOrSubgraphURL", "string")

if (options.provider) {
Expand All @@ -42,33 +44,17 @@ export default class SemaphoreEthers {
networkOrEthereumURL = "maticmum"
}

switch (networkOrEthereumURL) {
case "arbitrum":
options.address ??= "0xc60E0Ee1a2770d5F619858C641f14FC4a6401520"
options.startBlock ??= 77278430
break
case "arbitrum-sepolia":
options.address ??= "0x3889927F0B5Eb1a02C6E2C20b39a1Bd4EAd76131"
options.startBlock ??= 15174410
break
case "maticmum":
options.address ??= "0x3889927F0B5Eb1a02C6E2C20b39a1Bd4EAd76131"
options.startBlock ??= 33995010
break
case "sepolia":
options.address ??= "0x5B8e7cC7bAC61A4b952d472b67056B2f260ba6dc"
options.startBlock ??= 5150903
break
case "optimism-sepolia":
options.address ??= "0x3889927F0B5Eb1a02C6E2C20b39a1Bd4EAd76131"
options.startBlock ??= 7632846
break
default:
if (options.address === undefined) {
throw new Error(`You should provide a Semaphore contract address for this network`)
}
if (supportedNetworks.includes(networkOrEthereumURL as SupportedNetwork)) {
const { address, startBlock } = getDeployedContract(networkOrEthereumURL as SupportedNetwork)

options.address ??= address
options.startBlock ??= startBlock
} else {
if (options.address === undefined) {
throw new Error(`You should provide a Semaphore contract address for this network`)
}

options.startBlock ??= 0
options.startBlock ??= 0
}

let provider: Provider
Expand Down
2 changes: 1 addition & 1 deletion packages/data/src/getURL.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SupportedNetwork } from "./types"
import type { SupportedNetwork } from "@semaphore-protocol/utils"

/**
* Returns the subgraph URL related to the network passed as a parameter.
Expand Down
6 changes: 4 additions & 2 deletions packages/data/src/subgraph.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { SupportedNetwork } from "@semaphore-protocol/utils"
import { defaultNetwork } from "@semaphore-protocol/utils/networks"
import { AxiosRequestConfig } from "axios"
import checkParameter from "./checkParameter"
import getURL from "./getURL"
import request from "./request"
import { GroupOptions, GroupResponse, SupportedNetwork } from "./types"
import { GroupOptions, GroupResponse } from "./types"
import { jsDateToGraphqlDate } from "./utils"

export default class SemaphoreSubgraph {
Expand All @@ -12,7 +14,7 @@ export default class SemaphoreSubgraph {
* Initializes the subgraph object with one of the supported networks or a custom URL.
* @param networkOrSubgraphURL Supported Semaphore network or custom Subgraph URL.
*/
constructor(networkOrSubgraphURL: SupportedNetwork | string = SupportedNetwork.SEPOLIA) {
constructor(networkOrSubgraphURL: SupportedNetwork | string = defaultNetwork) {
checkParameter(networkOrSubgraphURL, "networkOrSubgraphURL", "string")

if (typeof networkOrSubgraphURL === "string" && networkOrSubgraphURL.startsWith("http")) {
Expand Down
8 changes: 0 additions & 8 deletions packages/data/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
export enum SupportedNetwork {
SEPOLIA = "sepolia",
MUMBAI = "mumbai",
OPTIMISM_SEPOLIA = "optimism-sepolia",
ARBITRUM_SEPOLIA = "arbitrum-sepolia",
ARBITRUM = "arbitrum"
}

export type EthersNetwork =
| "homestead"
| "matic"
Expand Down
9 changes: 5 additions & 4 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./supported-networks": {
"types": "./dist/types/supported-networks.d.ts",
"require": "./dist/lib.commonjs/supported-networks.cjs",
"default": "./dist/lib.esm/supported-networks.js"
"./networks": {
"types": "./dist/types/networks.d.ts",
"require": "./dist/lib.commonjs/networks.cjs",
"default": "./dist/lib.esm/networks.js"
},
"./constants": {
"types": "./dist/types/constants.d.ts",
Expand Down Expand Up @@ -47,6 +47,7 @@
"access": "public"
},
"devDependencies": {
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-typescript": "^11.1.6",
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/utils/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typescript from "@rollup/plugin-typescript"
import json from "@rollup/plugin-json"
import * as fs from "fs"
import cleanup from "rollup-plugin-cleanup"

Expand All @@ -23,7 +24,8 @@ export default [
typescript({
tsconfig: "./build.tsconfig.json"
}),
cleanup({ comments: "jsdoc" })
cleanup({ comments: "jsdoc" }),
json()
]
},
{
Expand All @@ -44,7 +46,8 @@ export default [
declaration: false,
declarationDir: undefined
}),
cleanup({ comments: "jsdoc" })
cleanup({ comments: "jsdoc" }),
json()
]
}
]
4 changes: 2 additions & 2 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as constants from "./constants"
import decodeMessage from "./decode-message"
import supportedNetworks from "./supported-networks"

export * from "./networks"
export * from "./types"
export { constants, decodeMessage, supportedNetworks }
export { constants, decodeMessage }
25 changes: 25 additions & 0 deletions packages/utils/src/networks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// eslint-disable-next-line import/no-relative-packages
import deployedContracts from "../../contracts/deployed-contracts.json"
import { SupportedNetwork } from "./types"

// List of Semaphore supported networks.
const supportedNetworks: SupportedNetwork[] = ["sepolia", "mumbai", "optimism-sepolia", "arbitrum-sepolia", "arbitrum"]

// Default Semaphore network.
const defaultNetwork: SupportedNetwork = "sepolia"

export function getDeployedContract(supportedNetwork: SupportedNetwork) {
const deployedContract = deployedContracts.find(({ network }) => network === supportedNetwork)

if (!deployedContract) {
throw new Error(`Semaphore has not been deployed on '${supportedNetwork}' yet`)
}

return deployedContract.contracts.find(({ name }) => name === "Semaphore") as {
name: string
address: string
startBlock: number
}
}

export { defaultNetwork, deployedContracts, supportedNetworks }
6 changes: 0 additions & 6 deletions packages/utils/src/supported-networks.ts

This file was deleted.

11 changes: 10 additions & 1 deletion packages/utils/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { encodeBytes32String } from "ethers/abi"
import { toBigInt } from "ethers/utils"
import { supportedNetworks } from "../src"
import decodeMessage from "../src/decode-message"
import { getDeployedContract, supportedNetworks } from "../src/networks"

describe("Utils", () => {
describe("# supportedNetworks", () => {
Expand All @@ -11,6 +11,15 @@ describe("Utils", () => {
})
})

describe("# getDeployedContract", () => {
it("Should return Semaphore deployment data for Sepolia", () => {
const { address, startBlock } = getDeployedContract("sepolia")

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

describe("# decodeMessage", () => {
it("Should decode a text message previously encoded to 32-byte bigint", () => {
const message = "Hello World"
Expand Down

0 comments on commit 1647eca

Please sign in to comment.