|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs-extra'); |
| 4 | +const path = require('path'); |
| 5 | +const { spawnSync, execSync } = require('child_process'); |
| 6 | + |
| 7 | +const projectName = process.argv[2]; |
| 8 | + |
| 9 | +if (!projectName) { |
| 10 | + console.error('No project name specified'); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +const root = path.resolve(projectName); |
| 15 | + |
| 16 | +fs.ensureDirSync(root); |
| 17 | +process.chdir(root); |
| 18 | + |
| 19 | +console.log(`Creating a new Hardhat project in ${root}.`); |
| 20 | + |
| 21 | +// Initialize a new npm project |
| 22 | +console.log('Initializing a new npm project...'); |
| 23 | +spawnSync('npm', ['init', '-y'], { stdio: 'inherit' }); |
| 24 | + |
| 25 | +// Install Hardhat and initialize a new TypeScript project |
| 26 | +console.log('Installing Hardhat...'); |
| 27 | +spawnSync('npm', ['install', 'hardhat'], { stdio: 'inherit' }); |
| 28 | + |
| 29 | +console.log('Initializing a new Hardhat TypeScript project...'); |
| 30 | +const result = execSync( |
| 31 | + 'HARDHAT_CREATE_TYPESCRIPT_PROJECT_WITH_DEFAULTS=true npx hardhat', |
| 32 | + { |
| 33 | + stdio: 'inherit', |
| 34 | + } |
| 35 | +); |
| 36 | + |
| 37 | +console.log('Installing Hardhat Plugins...'); |
| 38 | +spawnSync( |
| 39 | + 'npm', |
| 40 | + [ |
| 41 | + 'install', |
| 42 | + '--save-dev', |
| 43 | + '-f', |
| 44 | + 'solhint', |
| 45 | + 'prettier', |
| 46 | + 'prettier-plugin-solidity', |
| 47 | + 'hardhat-deploy', |
| 48 | + '@types/chai', |
| 49 | + '@types/mocha', |
| 50 | + '@types/node', |
| 51 | + 'dotenv', |
| 52 | + '@nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers', |
| 53 | + '@openzeppelin/contracts', |
| 54 | + 'hardhat-contract-sizer', |
| 55 | + 'hardhat-gas-reporter', |
| 56 | + ], |
| 57 | + { stdio: 'inherit' } |
| 58 | +); |
| 59 | + |
| 60 | +// Add the "compile" script to package.json |
| 61 | +console.log("Adding the 'compile' script to package.json..."); |
| 62 | +const packageJsonPath = path.join(root, 'package.json'); |
| 63 | +const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')); |
| 64 | +packageJson.scripts.compile = 'hardhat compile'; |
| 65 | +fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 66 | + |
| 67 | +// Edit the hardhat.config.ts |
| 68 | +console.log('Editing the hardhat.config.ts...'); |
| 69 | +const configPath = path.join(root, 'hardhat.config.ts'); |
| 70 | +const configContent = fs.readFileSync(configPath, 'utf-8'); |
| 71 | +let modifiedContent = configContent.replace( |
| 72 | + '};', |
| 73 | + ` |
| 74 | + networks: { |
| 75 | + hardhat: { |
| 76 | + forking: { |
| 77 | + url: nodeUrl('matic') ?? '', |
| 78 | + blockNumber: 37377259, // Latest as of 28.12.2022 |
| 79 | + }, |
| 80 | + }, |
| 81 | + matic: { |
| 82 | + url: nodeUrl('matic'), |
| 83 | + accounts: accounts('matic'), |
| 84 | + }, |
| 85 | + mumbai: { |
| 86 | + url: nodeUrl('mumbai'), |
| 87 | + accounts: accounts('mumbai'), |
| 88 | + }, |
| 89 | + rinkeby: { |
| 90 | + url: nodeUrl('rinkeby'), |
| 91 | + accounts: accounts('rinkeby'), |
| 92 | + }, |
| 93 | + }, |
| 94 | + gasReporter: { |
| 95 | + enabled: process.env.REPORT_GAS !== undefined, |
| 96 | + currency: 'USD', |
| 97 | + token: 'MATIC', |
| 98 | + gasPriceApi: |
| 99 | + 'https://api.polygonscan.com/api?module=proxy&action=eth_gasPrice', |
| 100 | + coinmarketcap: process.env.COINMARKETCAP_API_KEY, |
| 101 | + }, |
| 102 | + etherscan: { |
| 103 | + apiKey: { |
| 104 | + mainnet: process.env.ETHERSCAN_API_KEY || '', |
| 105 | + rinkeby: process.env.ETHERSCAN_API_KEY || '', |
| 106 | + polygon: process.env.POLYGONSCAN_API_KEY || '', |
| 107 | + polygonMumbai: process.env.POLYGONSCAN_API_KEY || '', |
| 108 | + }, |
| 109 | + }, |
| 110 | + mocha: { |
| 111 | + timeout: 0, |
| 112 | + }, |
| 113 | + namedAccounts: { |
| 114 | + deployer: { |
| 115 | + default: 0, |
| 116 | + }, |
| 117 | + proxyAdminOwner: { |
| 118 | + default: 1, |
| 119 | + }, |
| 120 | + bob: { |
| 121 | + default: 5, |
| 122 | + }, |
| 123 | + alice: { |
| 124 | + default: 6, |
| 125 | + }, |
| 126 | + mat: { |
| 127 | + default: 7, |
| 128 | + }, |
| 129 | + }, |
| 130 | +}; |
| 131 | + ` |
| 132 | +); |
| 133 | + |
| 134 | +modifiedContent = modifiedContent.replace( |
| 135 | + `import "@nomicfoundation/hardhat-toolbox";`, |
| 136 | + `import '@nomicfoundation/hardhat-toolbox'; |
| 137 | +import 'hardhat-deploy'; |
| 138 | +import 'hardhat-gas-reporter'; |
| 139 | +import 'hardhat-contract-sizer'; |
| 140 | +import { nodeUrl, accounts } from './utils/network'; |
| 141 | +import * as dotenv from 'dotenv'; |
| 142 | +import './tasks/acl'; |
| 143 | +
|
| 144 | +dotenv.config(); |
| 145 | + ` |
| 146 | +); |
| 147 | +fs.writeFileSync(configPath, modifiedContent); |
| 148 | + |
| 149 | +// Copy files to the project directory |
| 150 | +console.log('Copying files to the project directory...'); |
| 151 | +fs.copySync(path.resolve(__dirname, '../files'), `${root}/`); |
| 152 | + |
| 153 | +console.log(` |
| 154 | +Success! Created ${projectName} at ${root}. |
| 155 | +
|
| 156 | +Inside that directory, you can run several commands: |
| 157 | +
|
| 158 | + npm run compile |
| 159 | + Compiles the contracts. |
| 160 | +
|
| 161 | + npm run test |
| 162 | + Runs the tests. |
| 163 | +
|
| 164 | + npm run test:watch |
| 165 | + Runs the tests in watch mode. |
| 166 | +
|
| 167 | +We suggest that you begin by typing: |
| 168 | +
|
| 169 | + cd ${projectName} |
| 170 | + npm run test |
| 171 | +
|
| 172 | +Happy hacking! |
| 173 | +`); |
0 commit comments