Sfoglia il codice sorgente

refactor: Use hardhat deploy to deploy the Greeter contract

denhampreen 2 anni fa
parent
commit
77f682038c
10 ha cambiato i file con 447 aggiunte e 205 eliminazioni
  1. 1 0
      .gitignore
  2. 40 1
      README.md
  3. 18 0
      deploy/deploy.ts
  4. 13 1
      hardhat.config.ts
  5. 4 1
      package.json
  6. 346 199
      pnpm-lock.yaml
  7. 0 1
      tasks/deploy/index.ts
  8. 23 0
      tasks/greet.ts
  9. 1 1
      tasks/taskDeploy.ts
  10. 1 1
      tsconfig.json

+ 1 - 0
.gitignore

@@ -9,6 +9,7 @@ coverage
 dist
 node_modules
 types
+deployments
 
 # files
 *.env

+ 40 - 1
README.md

@@ -145,9 +145,28 @@ $ pnpm clean
 Deploy the contracts to Hardhat Network:
 
 ```sh
-$ pnpm deploy:contracts --greeting "Bonjour, le monde!"
+$ pnpm deploy:contracts"
 ```
 
+### Tasks
+
+#### Set Greeting
+
+Call the set greeting task on ganache network
+
+```sh
+$ pnpm task:setGreeting --network ganache --greeting "Bonjour, le monde!" --account 3
+```
+
+#### Deploy a new Greeter contract
+
+Deploy a new instance of the Greeter contract via a task
+
+```sh
+$ pnpm task:deployGreeter --network ganache --greeting "Bonjour, le monde!"
+```
+
+
 ## Tips
 
 ### Syntax Highlighting
@@ -162,6 +181,26 @@ If you use VSCode, you can get Solidity syntax highlighting with the
 To view the coverage report generated by `pnpm coverage`, just click `Go Live` from the status bar to turn the server
 on/off.
 
+## Local development with Ganache
+
+### Install ganache
+
+```sh
+$ npm i -g ganache"
+```
+
+### Run a local blockchain using ganache
+
+```sh
+$ ganache -s test"
+```
+
+> The `-s test` passes a seed to the local chain and makes it deterministic
+
+Make sure to set the mnemonic in your `.env` file to that of the instance running with ganache
+
+
+
 ## License
 
 This project is licensed under MIT.

+ 18 - 0
deploy/deploy.ts

@@ -0,0 +1,18 @@
+import { DeployFunction } from "hardhat-deploy/types";
+import { HardhatRuntimeEnvironment } from "hardhat/types";
+
+const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
+  const { deployer } = await hre.getNamedAccounts();
+  const { deploy } = hre.deployments;
+
+  const greeter = await deploy("Greeter", {
+    from: deployer,
+    args: ["Bonjour, le monde!"],
+    log: true,
+  });
+
+  console.log(`Greeter contract: `, greeter.address);
+};
+export default func;
+func.id = "deploy_greeter"; // id required to prevent reexecution
+func.tags = ["Greeter"];

+ 13 - 1
hardhat.config.ts

@@ -1,11 +1,13 @@
 import "@nomicfoundation/hardhat-toolbox";
 import { config as dotenvConfig } from "dotenv";
+import "hardhat-deploy";
 import type { HardhatUserConfig } from "hardhat/config";
 import type { NetworkUserConfig } from "hardhat/types";
 import { resolve } from "path";
 
 import "./tasks/accounts";
-import "./tasks/deploy";
+import "./tasks/greet";
+import "./tasks/taskDeploy";
 
 const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || "./.env";
 dotenvConfig({ path: resolve(__dirname, dotenvConfigPath) });
@@ -58,6 +60,9 @@ function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig {
 
 const config: HardhatUserConfig = {
   defaultNetwork: "hardhat",
+  namedAccounts: {
+    deployer: 0,
+  },
   etherscan: {
     apiKey: {
       arbitrumOne: process.env.ARBISCAN_API_KEY || "",
@@ -83,6 +88,13 @@ const config: HardhatUserConfig = {
       },
       chainId: chainIds.hardhat,
     },
+    ganache: {
+      accounts: {
+        mnemonic,
+      },
+      chainId: chainIds.ganache,
+      url: "http://localhost:8545",
+    },
     arbitrum: getChainConfig("arbitrum-mainnet"),
     avalanche: getChainConfig("avalanche"),
     bsc: getChainConfig("bsc"),

+ 4 - 1
package.json

@@ -34,6 +34,7 @@
     "ethers": "^5.7.2",
     "fs-extra": "^10.1.0",
     "hardhat": "^2.12.2",
+    "hardhat-deploy": "^0.11.29",
     "hardhat-gas-reporter": "^1.0.9",
     "lodash": "^4.17.21",
     "mocha": "^10.1.0",
@@ -69,7 +70,9 @@
     "clean": "rimraf ./artifacts ./cache ./coverage ./types ./coverage.json && pnpm typechain",
     "compile": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat compile",
     "coverage": "hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --testfiles \"test/**/*.ts\" && pnpm typechain",
-    "deploy:contracts": "hardhat deploy:Greeter",
+    "deploy:contracts": "hardhat deploy",
+    "task:setGreeting": "hardhat task:setGreeting",
+    "task:deployGreeter": "hardhat task:deployGreeter",
     "lint": "pnpm lint:sol && pnpm lint:ts && pnpm prettier:check",
     "lint:sol": "solhint --max-warnings 0 \"contracts/**/*.sol\"",
     "lint:ts": "eslint --ignore-path ./.eslintignore --ext .js,.ts .",

File diff suppressed because it is too large
+ 346 - 199
pnpm-lock.yaml


+ 0 - 1
tasks/deploy/index.ts

@@ -1 +0,0 @@
-import "./greeter";

+ 23 - 0
tasks/greet.ts

@@ -0,0 +1,23 @@
+import type { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
+import { task } from "hardhat/config";
+import type { TaskArguments } from "hardhat/types";
+
+import type { Greeter } from "../../types/Greeter";
+import type { Greeter__factory } from "../../types/factories/Greeter__factory";
+
+task("task:setGreeting")
+  .addParam("greeting", "Say hello, be nice")
+  .addParam("account", "Specify which account [0, 9]")
+  .setAction(async function (taskArguments: TaskArguments, hre) {
+    let { ethers, deployments } = hre;
+
+    let Greeter = await deployments.get("Greeter");
+
+    const signers: SignerWithAddress[] = await ethers.getSigners();
+
+    const greeter = <Greeter>await ethers.getContractAt("Greeter", Greeter.address);
+
+    await greeter.connect(signers[taskArguments.account]).setGreeting(taskArguments.greeting);
+
+    console.log("Greeting set: ", taskArguments.greeting);
+  });

+ 1 - 1
tasks/deploy/greeter.ts → tasks/taskDeploy.ts

@@ -5,7 +5,7 @@ import type { TaskArguments } from "hardhat/types";
 import type { Greeter } from "../../types/Greeter";
 import type { Greeter__factory } from "../../types/factories/Greeter__factory";
 
-task("deploy:Greeter")
+task("task:deployGreeter")
   .addParam("greeting", "Say hello, be nice")
   .setAction(async function (taskArguments: TaskArguments, { ethers }) {
     const signers: SignerWithAddress[] = await ethers.getSigners();

+ 1 - 1
tsconfig.json

@@ -18,5 +18,5 @@
   },
   "exclude": ["node_modules"],
   "files": ["./hardhat.config.ts"],
-  "include": ["src/**/*", "tasks/**/*", "test/**/*"]
+  "include": ["src/**/*", "tasks/**/*", "test/**/*", "deploy/**/*"]
 }

Some files were not shown because too many files changed in this diff