Skip to content

Commit

Permalink
feat: hardhat circom template
Browse files Browse the repository at this point in the history
  • Loading branch information
kidneyweakx committed Aug 22, 2023
1 parent 5eec13e commit 61057ac
Show file tree
Hide file tree
Showing 8 changed files with 4,903 additions and 244 deletions.
153 changes: 153 additions & 0 deletions packages/zk-circuits/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { HardhatUserConfig } from 'hardhat/config'
import { NetworkUserConfig } from 'hardhat/types'
// hardhat plugin
import '@nomiclabs/hardhat-ethers'
import '@nomiclabs/hardhat-etherscan'
import '@nomicfoundation/hardhat-toolbox'
import 'hardhat-circom'

import { config as dotenvConfig } from 'dotenv'
import { resolve } from 'path'
import { loadTasks } from './scripts/helpers/hardhatConfigHelpers'

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

const taskFolder = ['tasks']
loadTasks(taskFolder)

const chainIds = {
ganache: 1337,
goerli: 5,
sepolia: 11155111,
hardhat: 31337,
quorum: 81712,
mainnet: 1,
avalanche: 43114,
bsc: 56,
'arbitrum-mainnet': 42161,
'polygon-mainnet': 137,
'optimism-goerli': 420,
'optimism-mainnet': 10,
'polygon-mumbai': 80001,
}

// Ensure that we have all the environment variables we need.
const pk: string | undefined = process.env.PRIVATE_KEY
if (!pk) {
throw new Error('Please set your pk in a .env file')
}

const infuraApiKey: string | undefined = process.env.INFURA_API_KEY
if (!infuraApiKey) {
throw new Error('Please set your INFURA_API_KEY in a .env file')
}

function getChainConfig (chain: keyof typeof chainIds): NetworkUserConfig {
let jsonRpcUrl: string
switch (chain) {
case 'avalanche':
jsonRpcUrl = 'https://api.avax.network/ext/bc/C/rpc'
break
case 'optimism-goerli':
jsonRpcUrl = 'https://goerli.optimism.io'
break
case 'quorum':
jsonRpcUrl = process.env.QUORUM_URL || ''
break
default:
jsonRpcUrl = `https://${chain}.infura.io/v3/${infuraApiKey}`
}
return {
accounts: [`0x${pk}`],
chainId: chainIds[chain],
url: jsonRpcUrl,
}
}

const config: HardhatUserConfig = {
defaultNetwork: 'hardhat',
networks: {
hardhat: {
chainId: chainIds.hardhat,
},
local: {
url: 'http://127.0.0.1:8545',
},
arbitrum: getChainConfig('arbitrum-mainnet'),
avalanche: getChainConfig('avalanche'),
bsc: getChainConfig('bsc'),
goerli: getChainConfig('goerli'),
sepolia: getChainConfig('sepolia'),
mainnet: getChainConfig('mainnet'),
optimism: getChainConfig('optimism-mainnet'),
'optimism-goerli': getChainConfig('optimism-goerli'),
'polygon-mainnet': getChainConfig('polygon-mainnet'),
'polygon-mumbai': getChainConfig('polygon-mumbai'),
},
paths: {
artifacts: './artifacts',
cache: './cache',
sources: './contracts',
tests: './test',
},
solidity: {
compilers: [
{
version: '0.8.18',
},
{
version: '0.6.11',
},
],
settings: {
metadata: {
// Not including the metadata hash
// https://github.com/paulrberg/hardhat-template/issues/31
bytecodeHash: 'none',
},
// Disable the optimizer when debugging
// https://hardhat.org/hardhat-network/#solidity-optimizer-support
optimizer: {
enabled: true,
runs: 800,
},
},
},
circom: {
inputBasePath: './circuits',
ptau: 'https://hermez.s3-eu-west-1.amazonaws.com/powersOfTau28_hez_final_15.ptau',
circuits: [
{
name: 'himitsu',
},
],
},
etherscan: {
apiKey: {
arbitrumOne: process.env.ARBISCAN_API_KEY || '',
avalanche: process.env.SNOWTRACE_API_KEY || '',
bsc: process.env.BSCSCAN_API_KEY || '',
goerli: process.env.ETHERSCAN_API_KEY || '',
sepolia: process.env.ETHERSCAN_API_KEY || '',
mainnet: process.env.ETHERSCAN_API_KEY || '',
optimisticEthereum: process.env.OPTIMISM_API_KEY || '',
polygon: process.env.POLYGONSCAN_API_KEY || '',
optimisticGoerli: process.env.OPTIMISM_API_KEY || '',
polygonMumbai: process.env.POLYGONSCAN_API_KEY || '',
},
},

gasReporter: {
currency: 'USD',
gasPrice: 100,
enabled: process.env.REPORT_GAS as string === 'true',
excludeContracts: [],
src: './contracts',
},
typechain: {
outDir: 'typechain',
target: 'ethers-v5',
},
}

export default config
24 changes: 24 additions & 0 deletions packages/zk-circuits/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "zk-circuits",
"version": "0.1.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "kidneyweakx",
"license": "MIT",
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^2.0.2",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@nomiclabs/hardhat-etherscan": "^3.1.7",
"@openzeppelin/contracts": "^4.9.3",
"circomlib": "^2.0.5",
"circomlibjs": "^0.1.7",
"dotenv": "^16.3.1",
"ethers": "^5.7.0",
"hardhat": "^2.17.1",
"hardhat-circom": "^3.3.2",
"snarkjs": "^0.7.0",
"typescript": "^4.9.4"
}
}
12 changes: 12 additions & 0 deletions packages/zk-circuits/scripts/helpers/hardhatConfigHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs'
import path from 'path'

export const loadTasks = (taskFolders: string[]): void =>
taskFolders.forEach((folder) => {
const tasksPath = path.join(__dirname, '../', folder)
fs.readdirSync(tasksPath)
.filter((pth) => pth.includes('.ts') || pth.includes('.js'))
.forEach((task) => {
require(`${tasksPath}/${task}`)
})
})
11 changes: 11 additions & 0 deletions packages/zk-circuits/scripts/helpers/pathHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import fs from 'fs'
import path from 'path'

export const writeFileSync = (
dirPath: string,
fileName: string,
data: any,
) => {
fs.mkdirSync(dirPath, { recursive: true })
fs.writeFileSync(path.join(dirPath, fileName), data, 'utf8')
}
38 changes: 38 additions & 0 deletions packages/zk-circuits/scripts/tasks/deployProcess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { task } from 'hardhat/config'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import { writeFileSync } from '../helpers/pathHelper'

task('deploy:contract', 'Deploy contract')
.addParam('contract')
.addFlag('verify', 'Verify contract after deploy')
.setAction(async ({ contract, verify }, hre) => {
await hre.run('compile')
const [signer] = await hre.ethers.getSigners()
const contractFactory = await hre.ethers.getContractFactory(contract)
// if you mint in constructor, you need to add value in deploy function
const deployContract = await contractFactory.connect(signer).deploy()
console.log(`TestToken.sol deployed to ${deployContract.address}`)

const address = {
main: deployContract.address,
}
const addressData = JSON.stringify(address)
writeFileSync(`scripts/address/${hre.network.name}/`, 'mainContract.json', addressData)

await deployContract.deployed()

if (verify) {
console.log('verifying contract...')
await deployContract.deployTransaction.wait(5)
try {
await hre.run('verify:verify', {
address: deployContract.address,
constructorArguments: [address.main],
contract: contract,
})
} catch (e) {
console.log(e)
}
}
},
)
20 changes: 20 additions & 0 deletions packages/zk-circuits/scripts/tasks/verifyProcess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from 'fs'
import { task } from 'hardhat/config'

task('verify:contract', 'Verify deployed contract')
.addParam('file')
.addParam('contract')
.setAction(async ({ file, contract }, hre) => {
try {
const contractAddress = fs.readFileSync(`scripts/address/${hre.network.name}/mainContract.json`)
const addressData = JSON.parse(contractAddress.toString())
await hre.run('verify:verify', {
address: addressData.main,
constructorArguments: [],
contract: `contracts/${file}.sol:${contract}`,
})
} catch (e) {
console.log(e)
}
},
)
5 changes: 5 additions & 0 deletions packages/zk-circuits/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "tsconfig/base.json",
"include": ["hardhhat.config.ts"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 61057ac

Please sign in to comment.