Skip to content

Commit

Permalink
refactor(contracts): improve task to deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Jan 23, 2024
1 parent 7a1ba9e commit d593f48
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { NetworksUserConfig } from "hardhat/types"
import { resolve } from "path"
import "solidity-coverage"
import "./tasks/accounts"
import "./tasks/deploy-semaphore"
import "./tasks/deploy"

dotenvConfig({ path: resolve(__dirname, "../../.env") })

Expand Down
3 changes: 1 addition & 2 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"scripts": {
"start": "hardhat node",
"compile": "hardhat compile",
"deploy:semaphore": "hardhat deploy:semaphore",
"verify:contracts": "hardhat run scripts/verify-contracts.ts",
"deploy": "hardhat deploy",
"test": "hardhat test",
"test:report-gas": "REPORT_GAS=true hardhat test",
"test:coverage": "hardhat coverage",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { writeFileSync } from "fs"
import { task, types } from "hardhat/config"
import { deployContract } from "./utils"

task("deploy:semaphore", "Deploy a Semaphore contract")
task("deploy", "Deploy a Semaphore contract")
.addOptionalParam<boolean>("verifier", "Verifier contract address", undefined, types.string)
.addOptionalParam<boolean>("poseidon", "Poseidon library address", undefined, types.string)
.addOptionalParam<boolean>("logs", "Print the logs", true, types.boolean)
Expand All @@ -13,15 +14,7 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
if (!verifierAddress) {
const VerifierFactory = await ethers.getContractFactory(`SemaphoreVerifier`)

let verifier

if (hardhatArguments.network !== undefined && hardhatArguments.network !== "hardhat") {
verifier = await defender.deployContract(VerifierFactory, { salt: process.env.CREATE2_SALT })

await verifier.waitForDeployment()
} else {
verifier = await VerifierFactory.deploy()
}
const verifier = await deployContract(defender, VerifierFactory, hardhatArguments.network)

verifierAddress = await verifier.getAddress()

Expand All @@ -33,15 +26,7 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
if (!poseidonAddress) {
const PoseidonT3Factory = await ethers.getContractFactory("PoseidonT3")

let poseidonT3

if (hardhatArguments.network !== undefined && hardhatArguments.network !== "hardhat") {
poseidonT3 = await defender.deployContract(PoseidonT3Factory, { salt: process.env.CREATE2_SALT })

await poseidonT3.waitForDeployment()
} else {
poseidonT3 = await PoseidonT3Factory.deploy()
}
const poseidonT3 = await deployContract(defender, PoseidonT3Factory, hardhatArguments.network)

poseidonAddress = await poseidonT3.getAddress()

Expand All @@ -56,19 +41,9 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
}
})

let semaphore

if (hardhatArguments.network !== undefined && hardhatArguments.network !== "hardhat") {
semaphore = await defender.deployContract(SemaphoreFactory, [verifierAddress], {
salt: process.env.CREATE2_SALT,
unsafeAllow: ["external-library-linking", "constructor"],
unsafeAllowDeployContract: true
})

await semaphore.waitForDeployment()
} else {
semaphore = await SemaphoreFactory.deploy(verifierAddress)
}
const semaphore = await deployContract(defender, SemaphoreFactory, hardhatArguments.network, [
verifierAddress
])

const semaphoreAddress = await semaphore.getAddress()

Expand Down
21 changes: 21 additions & 0 deletions packages/contracts/tasks/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DefenderHardhatUpgrades } from "@openzeppelin/hardhat-upgrades"
import { ContractFactory } from "ethers"

export async function deployContract(
defender: DefenderHardhatUpgrades,
contractFactory: ContractFactory,
network?: string,
args: any[] = []
) {
let contract

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
}
2 changes: 1 addition & 1 deletion packages/contracts/test/Semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Semaphore", () => {
const members = Array.from({ length: 3 }, (_, i) => new Identity(i)).map(({ commitment }) => commitment)

before(async () => {
const { semaphore } = await run("deploy:semaphore", {
const { semaphore } = await run("deploy", {
logs: false
})

Expand Down

0 comments on commit d593f48

Please sign in to comment.