Skip to content

Commit

Permalink
Improve documentation for @semaphore/hardhat package (#772)
Browse files Browse the repository at this point in the history
* docs(hardhat): improve code comments and readme for @semaphore/hardhat package

* docs(hardhat): update package.json description
  • Loading branch information
0xjei authored May 7, 2024
1 parent 74df3c2 commit 1b34ad3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
20 changes: 15 additions & 5 deletions packages/hardhat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1 align="center">
Semaphore Hardhat plugin
</h1>
<p align="center">A Semaphore Hardhat plugin to deploy Semaphore contracts.</p>
<p align="center">A Hardhat plugin to deploy Semaphore contracts.</p>
</p>

<p align="center">
Expand Down Expand Up @@ -46,14 +46,14 @@
</h4>
</div>

| This Hardhat plugin provides two simple tasks that can be used to deploy Semaphore contracts without any additional configuration. |
| ---------------------------------------------------------------------------------------------------------------------------------- |
| The Semaphore Hardhat plugin simplifies the deployment of Semaphore contracts, reducing setup time and complexity. |
| ------------------------------------------------------------------------------------------------------------------ |

## 🛠 Install

### npm or yarn

Install the `@semaphore-protocol/hardhat` package with npm:
To install the Semaphore Hardhat plugin, use npm or yarn:

```bash
npm i @semaphore-protocol/hardhat
Expand All @@ -67,7 +67,7 @@ yarn add @semaphore-protocol/hardhat

## 📜 Usage

Import the plugin in your `hardhat.config.ts` file:
To use the plugin, import it in your Hardhat configuration file (`hardhat.config.ts`):

```typescript
import "@semaphore-protocol/hardhat"
Expand Down Expand Up @@ -110,3 +110,13 @@ task("deploy", "Deploy a Greeter contract")
return greeter
})
```

### Deploying Contracts

Use the provided tasks to deploy your Semaphore contracts:

```bash
npx hardhat deploy
```

This command will deploy a Semaphore contract using the addresses provided or deploy necessary dependencies like Semaphore Verifier and Poseidon library.
2 changes: 1 addition & 1 deletion packages/hardhat/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@semaphore-protocol/hardhat",
"version": "4.0.0-beta.9",
"description": "A Semaphore Hardhat plugin to deploy verifiers and Semaphore contract.",
"description": "A Hardhat plugin to deploy Semaphore contracts.",
"type": "module",
"license": "MIT",
"main": "dist/index.js",
Expand Down
8 changes: 8 additions & 0 deletions packages/hardhat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import "@nomicfoundation/hardhat-ethers"
import "./tasks/deploy-semaphore"
import "./tasks/deploy-semaphore-verifier"

/**
* Extends the Hardhat configuration to include paths for dependency compilation.
* This setup ensures that specific contracts are pre-compiled, optimizing the deployment process.
* @param config The Hardhat configuration object that is being extended.
* @param userConfig The user-provided configuration, if any, which may override or extend the default settings.
*/
extendConfig((config: HardhatConfig, userConfig: Readonly<HardhatUserConfig>) => {
// Default paths for contracts that need to be compiled before deployment.
config.dependencyCompiler.paths = [
"@semaphore-protocol/contracts/base/SemaphoreVerifier.sol",
"@semaphore-protocol/contracts/Semaphore.sol",
"poseidon-solidity/PoseidonT3.sol"
]

// Merge user-specified paths with the default paths.
if (userConfig.dependencyCompiler?.paths) {
config.dependencyCompiler.paths = [...config.dependencyCompiler.paths, ...userConfig.dependencyCompiler.paths]
}
Expand Down
4 changes: 4 additions & 0 deletions packages/hardhat/src/tasks/deploy-semaphore-verifier.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { task, types } from "hardhat/config"

/**
* Defines a Hardhat task to deploy the SemaphoreVerifier contract.
* This task can optionally log the deployment address.
*/
task("deploy:semaphore-verifier", "Deploy a SemaphoreVerifier contract")
.addOptionalParam<boolean>("logs", "Print the logs", true, types.boolean)
.setAction(async ({ logs }, { ethers }): Promise<any> => {
Expand Down
7 changes: 7 additions & 0 deletions packages/hardhat/src/tasks/deploy-semaphore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { task, types } from "hardhat/config"

/**
* Defines a Hardhat task to deploy the Semaphore contract.
* This task handles the deployment of dependent contracts like SemaphoreVerifier and PoseidonT3 if not provided.
*/
task("deploy:semaphore", "Deploy a Semaphore contract")
.addOptionalParam<boolean>("semaphoreVerifier", "SemaphoreVerifier contract address", undefined, types.string)
.addOptionalParam<boolean>("poseidon", "Poseidon library address", undefined, types.string)
Expand All @@ -9,6 +13,7 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
{ logs, semaphoreVerifier: semaphoreVerifierAddress, poseidon: poseidonAddress },
{ ethers }
): Promise<any> => {
// Deploy SemaphoreVerifier if not provided.
if (!semaphoreVerifierAddress) {
const SemaphoreVerifierFactory = await ethers.getContractFactory("SemaphoreVerifier")

Expand All @@ -21,6 +26,7 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
}
}

// Deploy PoseidonT3 if not provided.
if (!poseidonAddress) {
const PoseidonT3Factory = await ethers.getContractFactory("PoseidonT3")
const poseidonT3 = await PoseidonT3Factory.deploy()
Expand All @@ -32,6 +38,7 @@ task("deploy:semaphore", "Deploy a Semaphore contract")
}
}

// Deploy the Semaphore contract with the necessary library.
const SemaphoreFactory = await ethers.getContractFactory("Semaphore", {
libraries: {
PoseidonT3: poseidonAddress
Expand Down

0 comments on commit 1b34ad3

Please sign in to comment.