Explorar o código

feat: new clean and typechain buidler tasks

Paul Razvan Berg %!s(int64=5) %!d(string=hai) anos
pai
achega
de44b881d8
Modificáronse 11 ficheiros con 138 adicións e 27 borrados
  1. 0 1
      .eslintignore
  2. 0 21
      .eslintrc.js
  3. 21 0
      .eslintrc.yaml
  4. 16 0
      @types/augmentations.d.ts
  5. 8 0
      buidler.config.ts
  6. 6 3
      package.json
  7. 3 1
      tasks/accounts.ts
  8. 12 0
      tasks/clean.ts
  9. 2 0
      tasks/task-names.ts
  10. 33 0
      tasks/typechain.ts
  11. 37 1
      yarn.lock

+ 0 - 1
.eslintignore

@@ -9,6 +9,5 @@ node_modules/
 typechain/
 
 # files
-.eslintrc.js
 .solcover.js
 coverage.json

+ 0 - 21
.eslintrc.js

@@ -1,21 +0,0 @@
-module.exports = {
-  extends: [
-    "eslint:recommended",
-    "plugin:@typescript-eslint/eslint-recommended",
-    "plugin:@typescript-eslint/recommended",
-    "prettier/@typescript-eslint",
-  ],
-  parser: "@typescript-eslint/parser",
-  plugins: ["@typescript-eslint"],
-  root: true,
-  rules: {
-    "@typescript-eslint/no-inferrable-types": "off",
-    "@typescript-eslint/no-unused-vars": [
-      "error",
-      {
-        argsIgnorePattern: "_",
-        varsIgnorePattern: "_",
-      },
-    ],
-  },
-};

+ 21 - 0
.eslintrc.yaml

@@ -0,0 +1,21 @@
+extends:
+  - eslint:recommended
+  - plugin:@typescript-eslint/eslint-recommended
+  - plugin:@typescript-eslint/recommended
+  - prettier/@typescript-eslint
+parser: "@typescript-eslint/parser"
+parserOptions:
+  project: tsconfig.json
+plugins:
+  - "@typescript-eslint"
+root: true
+rules:
+  "@typescript-eslint/no-floating-promises":
+    - error
+    - ignoreIIFE: true
+      ignoreVoid: true
+  "@typescript-eslint/no-inferrable-types": 'off'
+  "@typescript-eslint/no-unused-vars":
+    - error
+    - argsIgnorePattern: _
+      varsIgnorePattern: _

+ 16 - 0
@types/augmentations.d.ts

@@ -1,5 +1,21 @@
 import { Greeter } from "../typechain/Greeter";
 
+export interface TypechainConfig {
+  outDir?: string;
+  target?: "ethers-v4" | "ethers-v5" | "truffle-v4" | "truffle-v5" | "web3-v1";
+}
+
+declare module "@nomiclabs/buidler/types" {
+  interface BuidlerConfig {
+    typechain?: TypechainConfig;
+  }
+
+  interface ProjectPaths {
+    coverage: string;
+    coverageJson: string;
+    typechain: string;
+  }
+}
 declare module "mocha" {
   export interface Context {
     greeter: Greeter;

+ 8 - 0
buidler.config.ts

@@ -5,6 +5,8 @@ dotenvConfig({ path: resolve(__dirname, "./.env") });
 import { BuidlerConfig, usePlugin } from "@nomiclabs/buidler/config";
 import { HDAccountsConfig } from "@nomiclabs/buidler/types";
 import "./tasks/accounts";
+import "./tasks/clean";
+import "./tasks/typechain";
 
 usePlugin("@nomiclabs/buidler-waffle");
 usePlugin("solidity-coverage");
@@ -68,6 +70,8 @@ const config: BuidlerConfig = {
   paths: {
     artifacts: "./artifacts",
     cache: "./cache",
+    coverage: "./coverage",
+    coverageJson: "./coverage.json",
     root: "./",
     sources: "./contracts",
     tests: "./test",
@@ -80,6 +84,10 @@ const config: BuidlerConfig = {
     },
     version: "0.6.10",
   },
+  typechain: {
+    outDir: "typechain",
+    target: "ethers-v5"
+  }
 };
 
 export default config;

+ 6 - 3
package.json

@@ -21,6 +21,7 @@
     "@nomiclabs/buidler-waffle": "^2.0.0",
     "@typechain/ethers-v5": "^0.0.2",
     "@types/chai": "^4.2.11",
+    "@types/fs-extra": "^9.0.1",
     "@types/mocha": "^7.0.2",
     "@types/node": "^14.0.14",
     "@typescript-eslint/eslint-plugin": "^3.6.0",
@@ -33,6 +34,7 @@
     "eslint-config-prettier": "^6.11.0",
     "ethereum-waffle": "^3.0.1",
     "ethers": "^5.0.4",
+    "fs-extra": "^9.0.1",
     "husky": "^4.2.5",
     "mocha": "^8.0.1",
     "prettier": "^2.0.5",
@@ -42,6 +44,7 @@
     "solhint": "^3.0.0",
     "solhint-plugin-prettier": "^0.0.4",
     "solidity-coverage": "^0.7.9",
+    "ts-generator": "^0.0.8",
     "ts-node": "^8.10.2",
     "typechain": "^2.0.0",
     "typescript": "^3.9.6"
@@ -66,15 +69,15 @@
   },
   "scripts": {
     "build": "yarn run compile && yarn run typechain",
-    "clean": "shx rm -rf ./artifacts ./cache ./typechain ./tsconfig.build.tsbuildinfo",
+    "clean": "buidler clean",
     "commit": "git-cz",
     "compile": "buidler compile",
     "coverage": "buidler coverage --solcoverjs ./.solcover.js --network coverage --temp artifacts --testfiles './test/**/*.ts'",
     "lint:sol": "solhint --config ./.solhint.json --max-warnings 0 'contracts/**/*.sol'",
-    "lint:ts": "eslint --config ./.eslintrc.js --ignore-path ./.eslintignore --ext .js,.ts .",
+    "lint:ts": "eslint --config ./.eslintrc.yaml --ignore-path ./.eslintignore --ext .js,.ts .",
     "prettier": "prettier --config .prettierrc --write '**/*.{js,json,md,sol,ts}'",
     "prettier:list-different": "prettier --config .prettierrc --list-different '**/*.{js,json,md,sol,ts}'",
     "test": "buidler test",
-    "typechain": "typechain --outDir typechain --target ethers-v5 'artifacts/*.json'"
+    "typechain": "buidler typechain"
   }
 }

+ 3 - 1
tasks/accounts.ts

@@ -1,7 +1,9 @@
 import { Signer } from "@ethersproject/abstract-signer";
 import { task } from "@nomiclabs/buidler/config";
 
-task("accounts", "Prints the list of accounts", async (_taskArgs, bre) => {
+import { TASK_ACCOUNTS } from "./task-names";
+
+task(TASK_ACCOUNTS, "Prints the list of accounts", async (_taskArgs, bre) => {
   const accounts: Signer[] = await bre.ethers.getSigners();
 
   for (const account of accounts) {

+ 12 - 0
tasks/clean.ts

@@ -0,0 +1,12 @@
+import fsExtra from "fs-extra";
+import { TASK_CLEAN } from "@nomiclabs/buidler/builtin-tasks/task-names";
+import { task } from "@nomiclabs/buidler/config";
+
+task(TASK_CLEAN, "Overrides the standard clean task", async function (_taskArgs, { config }, runSuper) {
+  await fsExtra.remove(config.paths.coverage);
+  await fsExtra.remove(config.paths.coverageJson);
+  if (config.typechain?.outDir) {
+    await fsExtra.remove(config.typechain.outDir);
+  }
+  await runSuper();
+});

+ 2 - 0
tasks/task-names.ts

@@ -0,0 +1,2 @@
+export const TASK_ACCOUNTS: string = "accounts";
+export const TASK_TYPECHAIN: string = "typechain";

+ 33 - 0
tasks/typechain.ts

@@ -0,0 +1,33 @@
+import { TASK_COMPILE } from "@nomiclabs/buidler/builtin-tasks/task-names";
+import { TypeChain } from "typechain/dist/TypeChain";
+import { task } from "@nomiclabs/buidler/config";
+import { tsGenerator } from "ts-generator";
+
+import { TASK_TYPECHAIN } from "./task-names";
+
+task(TASK_TYPECHAIN, "Generate Typechain typings for compiled contracts", async function (_taskArgs, { config, run }) {
+  if (!config.typechain || !config.typechain?.outDir || !config.typechain?.target) {
+    throw new Error("Invalid TypeChain configuration. Please provide it via buidler.config.ts");
+  }
+
+  await run(TASK_COMPILE);
+
+  console.log(
+    `Creating TypeChain artifacts in directory ${config.typechain.outDir} for target ${config.typechain.target}`,
+  );
+
+  const cwd: string = process.cwd();
+  await tsGenerator(
+    { cwd },
+    new TypeChain({
+      cwd,
+      rawConfig: {
+        files: config.paths.artifacts + "/*.json",
+        outDir: config.typechain.outDir,
+        target: config.typechain.target,
+      },
+    }),
+  );
+
+  console.log(`Successfully generated TypeChain artifacts!`);
+});

+ 37 - 1
yarn.lock

@@ -786,6 +786,13 @@
   resolved "https://registry.npmjs.org/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"
   integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag==
 
+"@types/fs-extra@^9.0.1":
+  version "9.0.1"
+  resolved "https://registry.npmjs.org/@types/fs-extra/-/fs-extra-9.0.1.tgz#91c8fc4c51f6d5dbe44c2ca9ab09310bd00c7918"
+  integrity sha512-B42Sxuaz09MhC3DDeW5kubRcQ5by4iuVQ0cRRWM2lggLzAa/KVom0Aft/208NgMvNQQZ86s5rVcqDdn/SH0/mg==
+  dependencies:
+    "@types/node" "*"
+
 "@types/glob@^7.1.1":
   version "7.1.2"
   resolved "https://registry.npmjs.org/@types/glob/-/glob-7.1.2.tgz#06ca26521353a545d94a0adc74f38a59d232c987"
@@ -1462,6 +1469,11 @@ asynckit@^0.4.0:
   resolved "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
   integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=
 
+at-least-node@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
+  integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==
+
 atob@^2.1.2:
   version "2.1.2"
   resolved "https://registry.npmjs.org/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
@@ -4974,6 +4986,16 @@ fs-extra@^7.0.0, fs-extra@^7.0.1:
     jsonfile "^4.0.0"
     universalify "^0.1.0"
 
+fs-extra@^9.0.1:
+  version "9.0.1"
+  resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc"
+  integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==
+  dependencies:
+    at-least-node "^1.0.0"
+    graceful-fs "^4.2.0"
+    jsonfile "^6.0.1"
+    universalify "^1.0.0"
+
 fs-minipass@^1.2.5:
   version "1.2.7"
   resolved "https://registry.npmjs.org/fs-minipass/-/fs-minipass-1.2.7.tgz#ccff8570841e7fe4265693da88936c55aed7f7c7"
@@ -6381,6 +6403,15 @@ jsonfile@^4.0.0:
   optionalDependencies:
     graceful-fs "^4.1.6"
 
+jsonfile@^6.0.1:
+  version "6.0.1"
+  resolved "https://registry.npmjs.org/jsonfile/-/jsonfile-6.0.1.tgz#98966cba214378c8c84b82e085907b40bf614179"
+  integrity sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==
+  dependencies:
+    universalify "^1.0.0"
+  optionalDependencies:
+    graceful-fs "^4.1.6"
+
 jsonify@~0.0.0:
   version "0.0.0"
   resolved "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
@@ -9126,7 +9157,7 @@ shebang-regex@^3.0.0:
   resolved "https://registry.npmjs.org/shebang-regex/-/shebang-regex-3.0.0.tgz#ae16f1644d873ecad843b0307b143362d4c42172"
   integrity sha512-7++dFhtcx3353uBaq8DDR4NuxBetBzC7ZQOhmTQInHEd6bSrXdiEyzCvG07Z44UYdLShWUyXt5M/yhz8ekcb1A==
 
-shelljs@^0.8.1, shelljs@^0.8.3, shelljs@^0.8.4:
+shelljs@^0.8.1, shelljs@^0.8.3:
   version "0.8.4"
   resolved "https://registry.npmjs.org/shelljs/-/shelljs-0.8.4.tgz#de7684feeb767f8716b326078a8a00875890e3c2"
   integrity sha512-7gk3UZ9kOfPLIAbslLzyWeGiEqx9e3rxwZM0KE6EL8GlGwjym9Mrlx5/p33bWTu9YG6vcS4MBxYZDHYr5lr8BQ==
@@ -10218,6 +10249,11 @@ universalify@^0.1.0:
   resolved "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
   integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
 
+universalify@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz#b61a1da173e8435b2fe3c67d29b9adf8594bd16d"
+  integrity sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug==
+
 unorm@^1.3.3:
   version "1.6.0"
   resolved "https://registry.npmjs.org/unorm/-/unorm-1.6.0.tgz#029b289661fba714f1a9af439eb51d9b16c205af"