Skip to content

Commit

Permalink
Add CLI support
Browse files Browse the repository at this point in the history
  • Loading branch information
EricxStone committed Dec 30, 2021
1 parent 558e35e commit 04f5e9d
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 110 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import {SOLIDITY_VERSION, EVM_VERSION} from "@ericxstone/hardhat-blockscout-veri

## Tasks

This plugin adds the `blockscount-verify` task to Hardhat:
This plugin adds the `blockscout-verify` task to Hardhat:
```bash
npx hardhat blockscount-verify <contract file> <contract address>
npx hardhat blockscout-verify <contract file> <contract address>
```

## Configuration
Expand Down Expand Up @@ -63,5 +63,5 @@ you need it (tasks, scripts, tests, etc).

You can also use the plugin as CLI
```bash
npx hardhat blockscount-verify <contract file> <contract address>
npx hardhat blockscout-verify <contract file> <contract address>
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ericxstone/hardhat-blockscout-verify",
"version": "0.0.3",
"version": "0.0.4",
"description": "Hardhat plugin for solidity contract verification on Blockscout block explorer",
"repository": "github:ericxstone/hardhat-blockscout-verify",
"author": "ericxstone",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from "path";

// This import is needed to let the TypeScript compiler know that it should include your type
// extensions in your npm package's types file.
import "./tasks/blockscount-verify";
import "./tasks/blockscout-verify";
import "./tasks/smart-flatten";
import "./type-extensions";

Expand Down
105 changes: 0 additions & 105 deletions src/tasks/blockscount-verify.ts

This file was deleted.

108 changes: 108 additions & 0 deletions src/tasks/blockscout-verify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { task } from "hardhat/config";
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
import {
HardhatConfig,
HardhatRuntimeEnvironment,
TaskArguments,
} from "hardhat/types";
import fetch from "node-fetch";
import path from "path";

task("blockscout-verify")
.addParam("filePath", "File path to the contract")
.addParam("address", "Deployed contract address")
.setAction(async function (
args: TaskArguments,
hre: HardhatRuntimeEnvironment
) {
if (!validateArgs(args)) {
throw new NomicLabsHardhatPluginError(
"hardhat-blockscout-verify",
"Missing args for this task"
);
}
const fileName = args.fileName;
const address = args.address;
const contractName = path.basename(fileName, ".sol");
if (!validateContractName(hre.config, contractName)) {
throw new NomicLabsHardhatPluginError(
"hardhat-blockscout-verify",
"Contracts is not defined in Hardhat config"
);
}
if (!validateBlockscoutURL(hre.config)) {
throw new NomicLabsHardhatPluginError(
"hardhat-blockscout-verify",
"Blockscout URL is not defined in Hardhat config"
);
}
console.log(`Task will process ${contractName} in ${fileName}`);
const flattenContent = await hre.run("smart-flatten", {
files: [fileName],
});
console.log("File flatten has completed");
const verifyConfig = hre.config.blockscoutVerify!.contracts[contractName];
const params: any = {
addressHash: address,
name: contractName,
compilerVersion: verifyConfig!.compilerVersion,
optimization: verifyConfig!.optimization,
contractSourceCode: flattenContent,
autodetectConstructorArguments: "true",
evmVersion: verifyConfig!.evmVersion,
optimizationRuns: verifyConfig!.optimizationRuns,
};
const blockscoutURL = hre.config.blockscoutVerify.blockscoutURL;
try {
console.log(`Sending file for verification to ${blockscoutURL}`);
console.log(`Contract address is ${address}`);
const verifyRes = await fetch(
`${blockscoutURL}/api?module=contract&action=verify`,
{
method: "POST",
body: JSON.stringify(params),
headers: {
"Content-Type": "application/json",
},
}
);
if (verifyRes.status === 200) {
console.log(`${contractName} is verified`);
} else {
throw new NomicLabsHardhatPluginError(
"hardhat-blockscout-verify",
"Fail to verify contract"
);
}
} catch (e) {
throw new NomicLabsHardhatPluginError(
"hardhat-blockscout-verify",
"Fail to verify contract"
);
}
});

function validateArgs(args: TaskArguments): boolean {
return args.fileName !== null && args.address !== null;
}

function validateBlockscoutURL(hreConfig: HardhatConfig) {
if (hreConfig.blockscoutVerify.blockscoutURL === null) {
return false;
}
let url;
try {
url = new URL(hreConfig.blockscoutVerify.blockscoutURL);
} catch (_) {
return false;
}
return true;
}

function validateContractName(hreConfig: HardhatConfig, contractName: string) {
return (
hreConfig.blockscoutVerify !== undefined &&
hreConfig.blockscoutVerify.contracts !== undefined &&
hreConfig.blockscoutVerify.contracts[contractName] !== undefined
);
}

0 comments on commit 04f5e9d

Please sign in to comment.