From 1c332be4365af4a7c7777774726ad4559d6ff0a7 Mon Sep 17 00:00:00 2001 From: "F. Eugene Aumson" Date: Fri, 6 Aug 2021 16:26:17 -0400 Subject: [PATCH] Add Advanced Sample Project with TypeScript --- .../package.json.template | 6 ++ packages/e2e/test/index.ts | 34 ++++++++++++ .../sample-projects/advanced-ts/.eslintrc.js | 41 ++++++++++++++ .../sample-projects/advanced-ts/README.md | 42 ++++++++++++++ .../advanced-ts/hardhat.config.ts | 42 ++++++++++++++ .../advanced-ts/scripts/deploy.ts | 32 +++++++++++ .../sample-projects/advanced-ts/test/index.ts | 19 +++++++ .../sample-projects/advanced-ts/tsconfig.json | 12 ++++ packages/hardhat-core/src/internal/cli/cli.ts | 3 + .../src/internal/cli/project-creation.ts | 55 +++++++++++++++++-- 10 files changed, 281 insertions(+), 5 deletions(-) create mode 100644 packages/e2e/test/fixture-projects/advanced-ts-sample-project/package.json.template create mode 100644 packages/hardhat-core/sample-projects/advanced-ts/.eslintrc.js create mode 100644 packages/hardhat-core/sample-projects/advanced-ts/README.md create mode 100644 packages/hardhat-core/sample-projects/advanced-ts/hardhat.config.ts create mode 100644 packages/hardhat-core/sample-projects/advanced-ts/scripts/deploy.ts create mode 100644 packages/hardhat-core/sample-projects/advanced-ts/test/index.ts create mode 100644 packages/hardhat-core/sample-projects/advanced-ts/tsconfig.json diff --git a/packages/e2e/test/fixture-projects/advanced-ts-sample-project/package.json.template b/packages/e2e/test/fixture-projects/advanced-ts-sample-project/package.json.template new file mode 100644 index 0000000000..c16090a0a0 --- /dev/null +++ b/packages/e2e/test/fixture-projects/advanced-ts-sample-project/package.json.template @@ -0,0 +1,6 @@ +{ + "name": "advanced-typescript-project", + "version": "1.0.0", + "license": "MIT", + "dependencies": {} +} diff --git a/packages/e2e/test/index.ts b/packages/e2e/test/index.ts index 807f7f340e..982dccb444 100644 --- a/packages/e2e/test/index.ts +++ b/packages/e2e/test/index.ts @@ -114,5 +114,39 @@ describe("e2e tests", function () { }); } }); + + describe("advanced TypeScript sample project", function () { + useFixture("advanced-ts-sample-project"); + + before(function () { + shell.exec(`${hardhatBinary}`, { + env: { + ...process.env, + HARDHAT_CREATE_ADVANCED_TYPESCRIPT_SAMPLE_PROJECT_WITH_DEFAULTS: + "true", + }, + }); + }); + + for (const suggestedCommand of [ + // This list should be kept reasonably in sync with + // hardhat-core/sample-projects/advanced-ts/README.txt + `${hardhatBinary} compile`, + `${hardhatBinary} test`, + `${hardhatBinary} run scripts/deploy.ts`, + "ts-node scripts/deploy.ts", + "REPORT_GAS=true npx hardhat test", + `${hardhatBinary} coverage`, + "npx eslint '**/*.{ts,js}'", + "npx eslint '**/*.{ts,js}' --fix", + "npx prettier '**/*.{json,sol,md}' --check", + "npx solhint 'contracts/**/*.sol'", + "npx solhint 'contracts/**/*.sol' --fix", + ]) { + it(`should permit successful execution of the suggested command "${suggestedCommand}"`, async function () { + shell.exec(suggestedCommand); + }); + } + }); }); }); diff --git a/packages/hardhat-core/sample-projects/advanced-ts/.eslintrc.js b/packages/hardhat-core/sample-projects/advanced-ts/.eslintrc.js new file mode 100644 index 0000000000..c651ed9efc --- /dev/null +++ b/packages/hardhat-core/sample-projects/advanced-ts/.eslintrc.js @@ -0,0 +1,41 @@ +module.exports = { + env: { + browser: false, + es2021: true, + mocha: true, + node: true, + }, + plugins: ["@typescript-eslint"], + extends: [ + "standard", + "plugin:prettier/recommended", + "plugin:node/recommended", + ], + parser: "@typescript-eslint/parser", + parserOptions: { + ecmaVersion: 12, + }, + overrides: [ + { + files: ["scripts/**"], + rules: { "no-process-exit": "off" }, + }, + { + files: ["hardhat.config.ts"], + globals: { hre: true }, + }, + { + files: ["hardhat.config.ts", "scripts/**", "test/**"], + rules: { + "node/no-unpublished-import": "off", + "node/no-unpublished-require": "off", + }, + }, + ], + rules: { + "node/no-unsupported-features/es-syntax": [ + "error", + { ignores: ["modules"] }, + ], + }, +}; diff --git a/packages/hardhat-core/sample-projects/advanced-ts/README.md b/packages/hardhat-core/sample-projects/advanced-ts/README.md new file mode 100644 index 0000000000..89e5961b33 --- /dev/null +++ b/packages/hardhat-core/sample-projects/advanced-ts/README.md @@ -0,0 +1,42 @@ +# Advanced Sample Hardhat Project + +This project demonstrates an advanced Hardhat use case, integrating other tools commonly used alongside Hardhat in the ecosystem. + +The project comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts. It also comes with a variety of other tools, preconfigured to work with the project code. + +Try running some of the following tasks: + +```shell +npx hardhat accounts +npx hardhat compile +npx hardhat clean +npx hardhat test +npx hardhat node +npx hardhat help +REPORT_GAS=true npx hardhat test +npx hardhat coverage +npx hardhat run scripts/deploy.js +npx ts-node scripts/deploy.js +npx eslint '**/*.{js,ts}' +npx eslint '**/*.{js,ts}' --fix +npx prettier '**/*.{json,sol,md}' --check +npx prettier '**/*.{json,sol,md}' --write +npx solhint 'contracts/**/*.sol' +npx solhint 'contracts/**/*.sol' --fix +``` + +# Etherscan verification + +To try out Etherscan verification, you first need to deploy a contract to an Ethereum network that's supported by Etherscan, such as Ropsten. + +In this project, copy the .env.template file to a file named .env, and then edit it to fill in the details. Enter your Etherscan API key, your Ropsten node URL (eg from Alchemy), and the private key of the account which will send the deployment transaction. With a valid .env file in place, first deploy your contract: + +```shell +hardhat run --network ropsten scripts/sample-script.js +``` + +Then, copy the deployment address and paste it in to replace `DEPLOYED_CONTRACT_ADDRESS` in this command: + +```shell +npx hardhat verify --network ropsten DEPLOYED_CONTRACT_ADDRESS "Hello, Hardhat!" +``` diff --git a/packages/hardhat-core/sample-projects/advanced-ts/hardhat.config.ts b/packages/hardhat-core/sample-projects/advanced-ts/hardhat.config.ts new file mode 100644 index 0000000000..2752d7fc1e --- /dev/null +++ b/packages/hardhat-core/sample-projects/advanced-ts/hardhat.config.ts @@ -0,0 +1,42 @@ +import * as dotenv from "dotenv"; + +import { HardhatUserConfig, task } from "hardhat/config"; +import "@nomiclabs/hardhat-etherscan"; +import "@nomiclabs/hardhat-waffle"; +import "@typechain/hardhat"; +import "hardhat-gas-reporter"; +import "solidity-coverage"; + +dotenv.config(); + +// This is a sample Hardhat task. To learn how to create your own go to +// https://hardhat.org/guides/create-task.html +task("accounts", "Prints the list of accounts", async (taskArgs, hre) => { + const accounts = await hre.ethers.getSigners(); + + for (const account of accounts) { + console.log(account.address); + } +}); + +// You need to export an object to set up your config +// Go to https://hardhat.org/config/ to learn more + +const config: HardhatUserConfig = { + solidity: "0.8.4", + networks: { + ropsten: { + url: process.env.ROPSTEN_URL || "", + accounts: [process.env.PRIVATE_KEY || `0x${"0".repeat(40)}`], + }, + }, + gasReporter: { + enabled: process.env.REPORT_GAS !== undefined, + currency: "USD", + }, + etherscan: { + apiKey: process.env.ETHERSCAN_API_KEY, + }, +}; + +export default config; diff --git a/packages/hardhat-core/sample-projects/advanced-ts/scripts/deploy.ts b/packages/hardhat-core/sample-projects/advanced-ts/scripts/deploy.ts new file mode 100644 index 0000000000..7c02c6e030 --- /dev/null +++ b/packages/hardhat-core/sample-projects/advanced-ts/scripts/deploy.ts @@ -0,0 +1,32 @@ +// We require the Hardhat Runtime Environment explicitly here. This is optional +// but useful for running the script in a standalone fashion through `node