Skip to content

Commit

Permalink
Merge pull request #3730 from NomicFoundation/fix-etherscan-bug
Browse files Browse the repository at this point in the history
Fix hardcoded URL in the etherscan plugin
  • Loading branch information
fvictorio committed Mar 2, 2023
2 parents 18b08c5 + 71c2ff1 commit 6e61bab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
13 changes: 11 additions & 2 deletions packages/hardhat-etherscan/src/etherscan/EtherscanService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,21 @@ export class EtherscanResponse {
}

export async function isAlreadyVerified(
apiURL: string,
apiKey: string,
address: string
): Promise<boolean> {
const url = `https://api.etherscan.io/api?module=contract&action=getsourcecode&address=${address}&apikey=${apiKey}`;
const parameters = new URLSearchParams({
module: "contract",
action: "getsourcecode",
address,
apikey: apiKey,
});

const response = await sendGetRequest(new URL(url));
const url = new URL(apiURL);
url.search = parameters.toString();

const response = await sendGetRequest(url);
const json = await response.body.json();

if (json.message !== "OK") {
Expand Down
8 changes: 7 additions & 1 deletion packages/hardhat-etherscan/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,13 @@ If your constructor has no arguments pass an empty array. E.g:
verificationNetwork
);

if (await isAlreadyVerified(etherscanAPIKey, address)) {
const alreadyVerified = await isAlreadyVerified(
etherscanAPIEndpoints.apiURL,
etherscanAPIKey,
address
);

if (alreadyVerified) {
console.log(`The contract ${address} has already been verified`);
return;
}
Expand Down
12 changes: 1 addition & 11 deletions packages/hardhat-etherscan/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";

import { pluginName } from "../constants";
Expand All @@ -14,13 +11,6 @@ export async function getEtherscanEndpoints(
chainConfig: ChainConfig,
customChains: CustomChain[]
): Promise<EtherscanNetworkEntry> {
if (networkName === HARDHAT_NETWORK_NAME) {
throw new NomicLabsHardhatPluginError(
pluginName,
`The selected network is ${networkName}. Please select a network supported by Etherscan.`
);
}

const chainIdsToNames = new Map(
entries(chainConfig).map(([chainName, config]) => [
config.chainId,
Expand Down
11 changes: 1 addition & 10 deletions packages/hardhat-etherscan/test/integration/PluginTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import {
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
import path from "path";

import {
TASK_VERIFY_GET_ETHERSCAN_ENDPOINT,
TASK_VERIFY_GET_MINIMUM_BUILD,
} from "../../src/constants";
import { TASK_VERIFY_GET_MINIMUM_BUILD } from "../../src/constants";
import { deployContract, getRandomString, useEnvironment } from "../helpers";

// These are skipped because they can't currently be run in CI
Expand Down Expand Up @@ -336,12 +333,6 @@ describe("Plugin integration tests", function () {
// This avoids failure due to compiler downloads.
await this.env.run(TASK_COMPILE, { quiet: true });

const { task }: { task: typeof taskT } = require("hardhat/config");

// We override this task to avoid posting to an actual endpoint and to avoid our own sanity checks.
task(TASK_VERIFY_GET_ETHERSCAN_ENDPOINT).setAction(async () => {
return "http://127.0.0.1:54321";
});
const { ethers } = this.env as any;
const signers = await ethers.getSigners();
signer = signers[0];
Expand Down

0 comments on commit 6e61bab

Please sign in to comment.