Skip to content

Commit

Permalink
Refactor hardhat-ethers.
Browse files Browse the repository at this point in the history
Add a types module that defines the interface for additional Hardhat
fields.
Separate out compilation projects for the plugin and its tests.
  • Loading branch information
scnale committed Dec 21, 2020
1 parent eab324a commit 29afa95
Show file tree
Hide file tree
Showing 24 changed files with 99 additions and 77 deletions.
2 changes: 2 additions & 0 deletions packages/hardhat-ethers/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Compilation output
/build-test/
/dist
/plugin
/types

# Code coverage artifacts
/coverage
Expand Down
15 changes: 10 additions & 5 deletions packages/hardhat-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repository": "github:nomiclabs/hardhat",
"author": "Nomic Labs LLC",
"license": "MIT",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"main": "plugin/index.js",
"types": "plugin/index.d.ts",
"keywords": [
"ethereum",
"smart-contracts",
Expand All @@ -17,14 +17,19 @@
],
"scripts": {
"lint:fix": "node ../../node_modules/prettier/bin-prettier.js --write \"src/**/*.{js,ts}\" \"test/**/*.{js,ts}\" && yarn lint --fix",
"lint": "node ../../node_modules/tslint/bin/tslint --config tslint.json --project ./tsconfig.json",
"lint": "yarn lint-src && yarn lint-tests",
"lint-tests": "node -r ts-node/register/transpile-only ../../node_modules/tslint/bin/tslint --config tslint.json --project ./tsconfig.json",
"lint-src": "node -r ts-node/register/transpile-only ../../node_modules/tslint/bin/tslint --config tslint.json --project ./src/tsconfig.json",
"test": "node ../../node_modules/mocha/bin/mocha --recursive \"test/**/*.ts\" --exit",
"build": "node ../../node_modules/typescript/bin/tsc --build .",
"clean": "node ../../node_modules/rimraf/bin.js dist"
"build": "node ../../node_modules/typescript/bin/tsc --build src",
"build-test": "node ../../node_modules/typescript/bin/tsc --build .",
"clean": "node ../../node_modules/rimraf/bin.js dist plugin types build-test tsconfig.tsbuildinfo"
},
"files": [
"dist/src/",
"src/",
"plugin/",
"types/",
"LICENSE",
"README.md"
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../../plugin/ethers-provider-wrapper";
1 change: 1 addition & 0 deletions packages/hardhat-ethers/src/dist/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../../plugin/helpers";
1 change: 1 addition & 0 deletions packages/hardhat-ethers/src/dist/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "../../plugin/index";
1 change: 1 addition & 0 deletions packages/hardhat-ethers/src/dist/src/provider-proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../../plugin/provider-proxy";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../../plugin/signer-with-address";
1 change: 1 addition & 0 deletions packages/hardhat-ethers/src/dist/src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "../../plugin/type-extensions";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "../../plugin/updatable-target-proxy";
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
NetworkConfig,
} from "hardhat/types";

import type { FactoryOptions, Libraries } from "../types";

import type { SignerWithAddress } from "./signer-with-address";

interface Link {
Expand All @@ -14,15 +16,6 @@ interface Link {
address: string;
}

export interface Libraries {
[libraryName: string]: string;
}

export interface FactoryOptions {
signer?: ethers.Signer;
libraries?: Libraries;
}

const pluginName = "hardhat-ethers";

export async function getSigners(
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions packages/hardhat-ethers/src/plugin/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as ethers from "ethers";
import "hardhat/types/runtime";

import type {
FactoryOptions as FactoryOptionsT,
getContractFactory as getContractFactoryT,
HardhatEthers,
Libraries as LibrariesT,
} from "../types";

declare module "hardhat/types/runtime" {
// Beware, adding new types to any hardhat type submodule is not a good practice in a Hardhat plugin.
// Doing so increases the risk of a type clash with another plugin.
type Libraries = LibrariesT;
type FactoryOptions = FactoryOptionsT;
// tslint:disable-next-line: naming-convention
type getContractFactory = typeof getContractFactoryT;

interface HardhatRuntimeEnvironment {
// We omit the ethers field because it is redundant.
ethers: Omit<typeof ethers, "ethers"> & HardhatEthers;
}
}
10 changes: 10 additions & 0 deletions packages/hardhat-ethers/src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../config/typescript/tsconfig.json",
"compilerOptions": {
"outDir": "../",
"rootDirs": ["."],
"composite": true
},
"include": ["./**/*.ts"],
"exclude": []
}
57 changes: 0 additions & 57 deletions packages/hardhat-ethers/src/type-extensions.ts

This file was deleted.

34 changes: 34 additions & 0 deletions packages/hardhat-ethers/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type * as ethers from "ethers";

import type { SignerWithAddress } from "../plugin/signer-with-address";

export interface Libraries {
[libraryName: string]: string;
}

export interface FactoryOptions {
signer?: ethers.Signer;
libraries?: Libraries;
}

export declare function getContractFactory(
name: string,
signerOrOptions?: ethers.Signer | FactoryOptions
): Promise<ethers.ContractFactory>;
export declare function getContractFactory(
abi: any[],
bytecode: ethers.utils.BytesLike,
signer?: ethers.Signer
): Promise<ethers.ContractFactory>;

export interface HardhatEthers {
provider: ethers.providers.JsonRpcProvider;

getContractFactory: typeof getContractFactory;
getContractAt: (
nameOrAbi: string | any[],
address: string,
signer?: ethers.Signer
) => Promise<ethers.Contract>;
getSigners: () => Promise<SignerWithAddress[]>;
}
2 changes: 1 addition & 1 deletion packages/hardhat-ethers/test/ethers-provider-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from "chai";
import { ethers } from "ethers";

import { EthersProviderWrapper } from "../src/ethers-provider-wrapper";
import { EthersProviderWrapper } from "../src/plugin/ethers-provider-wrapper";

import { useEnvironment } from "./helpers";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("../../../src/index");
require("../../../src/plugin/index");

module.exports = {
solidity: "0.5.15",
Expand Down
3 changes: 3 additions & 0 deletions packages/hardhat-ethers/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { resetHardhatContext } from "hardhat/plugins-testing";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import path from "path";

// Import this plugin type extensions for the HardhatRuntimeEnvironment
import type {} from "../src/plugin/index";

declare module "mocha" {
interface Context {
env: HardhatRuntimeEnvironment;
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-ethers/test/updatable-target-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "chai";

import { createUpdatableTargetProxy } from "../src/updatable-target-proxy";
import { createUpdatableTargetProxy } from "../src/plugin/updatable-target-proxy";

describe("updatable target proxy", function () {
it("should proxy properties", function () {
Expand Down
8 changes: 5 additions & 3 deletions packages/hardhat-ethers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"extends": "../../config/typescript/tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"outDir": "./build-test",
"rootDirs": ["./test"],
"composite": true
},
"exclude": ["./dist", "./node_modules", "./test/**/hardhat.config.ts"],
"include": ["./test/**/*.ts"],
"exclude": ["./node_modules", "./test/**/hardhat.config.ts"],
"references": [
{
"path": "../hardhat-core/src"
"path": "./src"
}
]
}

0 comments on commit 29afa95

Please sign in to comment.