-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Advanced Sample Project with TypeScript
- Loading branch information
Showing
10 changed files
with
281 additions
and
5 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/e2e/test/fixture-projects/advanced-ts-sample-project/package.json.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "advanced-typescript-project", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/hardhat-core/sample-projects/advanced-ts/.eslintrc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] }, | ||
], | ||
}, | ||
}; |
42 changes: 42 additions & 0 deletions
42
packages/hardhat-core/sample-projects/advanced-ts/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!" | ||
``` |
42 changes: 42 additions & 0 deletions
42
packages/hardhat-core/sample-projects/advanced-ts/hardhat.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
32 changes: 32 additions & 0 deletions
32
packages/hardhat-core/sample-projects/advanced-ts/scripts/deploy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <script>`. | ||
// | ||
// When running the script with `npx hardhat run <script>` you'll find the Hardhat | ||
// Runtime Environment's members available in the global scope. | ||
const hre = require("hardhat"); // TODO: change this to an import | ||
|
||
async function main() { | ||
// Hardhat always runs the compile task when running scripts with its command | ||
// line interface. | ||
// | ||
// If this script is run directly using `node` you may want to call compile | ||
// manually to make sure everything is compiled | ||
// await hre.run('compile'); | ||
|
||
// We get the contract to deploy | ||
const Greeter = await hre.ethers.getContractFactory("Greeter"); | ||
const greeter = await Greeter.deploy("Hello, Hardhat!"); | ||
|
||
await greeter.deployed(); | ||
|
||
console.log("Greeter deployed to:", greeter.address); | ||
} | ||
|
||
// We recommend this pattern to be able to use async/await everywhere | ||
// and properly handle errors. | ||
main() | ||
.then(() => process.exit(0)) | ||
.catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/hardhat-core/sample-projects/advanced-ts/test/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { expect } from "chai"; | ||
import { ethers } from "hardhat"; | ||
|
||
describe("Greeter", function () { | ||
it("Should return the new greeting once it's changed", async function () { | ||
const Greeter = await ethers.getContractFactory("Greeter"); | ||
const greeter = await Greeter.deploy("Hello, world!"); | ||
await greeter.deployed(); | ||
|
||
expect(await greeter.greet()).to.equal("Hello, world!"); | ||
|
||
const setGreetingTx = await greeter.setGreeting("Hola, mundo!"); | ||
|
||
// wait until the transaction is mined | ||
await setGreetingTx.wait(); | ||
|
||
expect(await greeter.greet()).to.equal("Hola, mundo!"); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/hardhat-core/sample-projects/advanced-ts/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2018", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"outDir": "dist", | ||
"declaration": true | ||
}, | ||
"include": ["./scripts", "./test", "./typechain"], | ||
"files": ["./hardhat.config.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters