Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
poma committed Feb 13, 2021
1 parent 984996d commit c7e205d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

## What

This plugin adds extra features on top of `@nomiclabs/hardhat-ethers`. It supports mainnet, bsc, and most common testnets.
This plugin adds extra features on top of `@nomiclabs/hardhat-ethers` and allows creating contract instances without
manually downloading ABI: `ethers.getVerifiedContractAt('<address>')`. It supports Mainnet, BSC, and most testnets.

## Installation

Expand Down Expand Up @@ -46,7 +47,7 @@ export async function getVerifiedContractAt(

## Usage

You need to add the following Etherscan config to your `hardhat.config.js` file:
You need to add the following Etherscan config to your `hardhat.config.js` file. Etherscan API key is optional but without it Etherscan allows only 1 request per 5 seconds.

```js
module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hardhat-etherscan-abi",
"version": "0.0.1",
"version": "0.0.2",
"description": "Hardhat plugin fetching contract ABI from etherscan",
"repository": "github:poma/hardhat-etherscan-abi",
"homepage": "https://github.com/poma/hardhat-etherscan-abi",
Expand Down
22 changes: 11 additions & 11 deletions src/network/prober.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
HARDHAT_NETWORK_NAME,
NomicLabsHardhatPluginError,
} from "hardhat/plugins";
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
import { EthereumProvider } from "hardhat/types";

const pluginName = "hardhat-etherscan-abi";
Expand Down Expand Up @@ -62,12 +59,13 @@ export async function getEtherscanEndpoints(
provider: EthereumProvider,
networkName: string
): Promise<EtherscanURLs> {
if (networkName === HARDHAT_NETWORK_NAME) {
throw new NomicLabsHardhatPluginError(
pluginName,
`The selected network is ${networkName}. Please select a network supported by Etherscan.`
);
}
// Disable this check because ABI download can be useful in fork mode
// if (networkName === HARDHAT_NETWORK_NAME) {
// throw new NomicLabsHardhatPluginError(
// pluginName,
// `The selected network is ${networkName}. Please select a network supported by Etherscan.`
// );
// }

const chainID = parseInt(await provider.send("eth_chainId"), 16) as NetworkID;

Expand All @@ -80,7 +78,9 @@ export async function getEtherscanEndpoints(
Possible causes are:
- The selected network (${networkName}) is wrong.
- Faulty hardhat network config.`
- Faulty hardhat network config.
If you use Mainnet fork mode try setting 'chainId: 1' in hardhat config`
);
}

Expand Down
11 changes: 11 additions & 0 deletions test/fixture-projects/hardhat-project/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import '@nomiclabs/hardhat-ethers';
import '../../../src/index';

export default {
solidity: "0.7.3",
networks: {
hardhat: {
chainId: 1,
}
}
};
22 changes: 22 additions & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { resetHardhatContext } from 'hardhat/plugins-testing';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import path from 'path';

declare module 'mocha' {
interface Context {
env: HardhatRuntimeEnvironment;
}
}

export function useEnvironment(fixtureProjectName: string, networkName = 'hardhat'): void {
beforeEach('Loading hardhat environment', async function () {
process.chdir(path.join(__dirname, 'fixture-projects', fixtureProjectName));
process.env.HARDHAT_NETWORK = networkName;

this.env = require('hardhat');
});

afterEach('Resetting hardhat', function () {
resetHardhatContext();
});
}
4 changes: 4 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--require ts-node/register
--require source-map-support/register
--recursive test/**/*.test.ts
--timeout 10000
22 changes: 18 additions & 4 deletions test/project.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
// import { assert } from 'chai';
import { assert } from 'chai';

describe("Integration tests examples", function () {
describe("Hardhat Runtime Environment extension", function () {
it("should get some tests later");
import { useEnvironment } from './helpers';

describe('Integration tests', function () {
describe('Hardhat Runtime Environment extension', function () {
useEnvironment('hardhat-project');

// EIP-2470 Deployer has pretty short ABI, and is deployed on most networks
const testAddress = '0xce0042B868300000d44A59004Da54A005ffdcf9f';
const testABI = 'function deploy(bytes _initCode, bytes32 _salt) returns (address createdContract) @8500000';

it('should get the Contract ABI from Etherscan', async function () {
// @ts-ignore
const contract = await this.env.ethers.getVerifiedContractAt(testAddress);
const abi = contract.interface.format()
assert.equal(abi.length, 1);
assert.equal(abi[0], testABI);
});
});
});

0 comments on commit c7e205d

Please sign in to comment.