Skip to content

Commit 04f5e9d

Browse files
committed
Add CLI support
1 parent 558e35e commit 04f5e9d

File tree

5 files changed

+113
-110
lines changed

5 files changed

+113
-110
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import {SOLIDITY_VERSION, EVM_VERSION} from "@ericxstone/hardhat-blockscout-veri
2727

2828
## Tasks
2929

30-
This plugin adds the `blockscount-verify` task to Hardhat:
30+
This plugin adds the `blockscout-verify` task to Hardhat:
3131
```bash
32-
npx hardhat blockscount-verify <contract file> <contract address>
32+
npx hardhat blockscout-verify <contract file> <contract address>
3333
```
3434

3535
## Configuration
@@ -63,5 +63,5 @@ you need it (tasks, scripts, tests, etc).
6363

6464
You can also use the plugin as CLI
6565
```bash
66-
npx hardhat blockscount-verify <contract file> <contract address>
66+
npx hardhat blockscout-verify <contract file> <contract address>
6767
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ericxstone/hardhat-blockscout-verify",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Hardhat plugin for solidity contract verification on Blockscout block explorer",
55
"repository": "github:ericxstone/hardhat-blockscout-verify",
66
"author": "ericxstone",

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from "path";
55

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

src/tasks/blockscount-verify.ts

Lines changed: 0 additions & 105 deletions
This file was deleted.

src/tasks/blockscout-verify.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import { task } from "hardhat/config";
2+
import { NomicLabsHardhatPluginError } from "hardhat/plugins";
3+
import {
4+
HardhatConfig,
5+
HardhatRuntimeEnvironment,
6+
TaskArguments,
7+
} from "hardhat/types";
8+
import fetch from "node-fetch";
9+
import path from "path";
10+
11+
task("blockscout-verify")
12+
.addParam("filePath", "File path to the contract")
13+
.addParam("address", "Deployed contract address")
14+
.setAction(async function (
15+
args: TaskArguments,
16+
hre: HardhatRuntimeEnvironment
17+
) {
18+
if (!validateArgs(args)) {
19+
throw new NomicLabsHardhatPluginError(
20+
"hardhat-blockscout-verify",
21+
"Missing args for this task"
22+
);
23+
}
24+
const fileName = args.fileName;
25+
const address = args.address;
26+
const contractName = path.basename(fileName, ".sol");
27+
if (!validateContractName(hre.config, contractName)) {
28+
throw new NomicLabsHardhatPluginError(
29+
"hardhat-blockscout-verify",
30+
"Contracts is not defined in Hardhat config"
31+
);
32+
}
33+
if (!validateBlockscoutURL(hre.config)) {
34+
throw new NomicLabsHardhatPluginError(
35+
"hardhat-blockscout-verify",
36+
"Blockscout URL is not defined in Hardhat config"
37+
);
38+
}
39+
console.log(`Task will process ${contractName} in ${fileName}`);
40+
const flattenContent = await hre.run("smart-flatten", {
41+
files: [fileName],
42+
});
43+
console.log("File flatten has completed");
44+
const verifyConfig = hre.config.blockscoutVerify!.contracts[contractName];
45+
const params: any = {
46+
addressHash: address,
47+
name: contractName,
48+
compilerVersion: verifyConfig!.compilerVersion,
49+
optimization: verifyConfig!.optimization,
50+
contractSourceCode: flattenContent,
51+
autodetectConstructorArguments: "true",
52+
evmVersion: verifyConfig!.evmVersion,
53+
optimizationRuns: verifyConfig!.optimizationRuns,
54+
};
55+
const blockscoutURL = hre.config.blockscoutVerify.blockscoutURL;
56+
try {
57+
console.log(`Sending file for verification to ${blockscoutURL}`);
58+
console.log(`Contract address is ${address}`);
59+
const verifyRes = await fetch(
60+
`${blockscoutURL}/api?module=contract&action=verify`,
61+
{
62+
method: "POST",
63+
body: JSON.stringify(params),
64+
headers: {
65+
"Content-Type": "application/json",
66+
},
67+
}
68+
);
69+
if (verifyRes.status === 200) {
70+
console.log(`${contractName} is verified`);
71+
} else {
72+
throw new NomicLabsHardhatPluginError(
73+
"hardhat-blockscout-verify",
74+
"Fail to verify contract"
75+
);
76+
}
77+
} catch (e) {
78+
throw new NomicLabsHardhatPluginError(
79+
"hardhat-blockscout-verify",
80+
"Fail to verify contract"
81+
);
82+
}
83+
});
84+
85+
function validateArgs(args: TaskArguments): boolean {
86+
return args.fileName !== null && args.address !== null;
87+
}
88+
89+
function validateBlockscoutURL(hreConfig: HardhatConfig) {
90+
if (hreConfig.blockscoutVerify.blockscoutURL === null) {
91+
return false;
92+
}
93+
let url;
94+
try {
95+
url = new URL(hreConfig.blockscoutVerify.blockscoutURL);
96+
} catch (_) {
97+
return false;
98+
}
99+
return true;
100+
}
101+
102+
function validateContractName(hreConfig: HardhatConfig, contractName: string) {
103+
return (
104+
hreConfig.blockscoutVerify !== undefined &&
105+
hreConfig.blockscoutVerify.contracts !== undefined &&
106+
hreConfig.blockscoutVerify.contracts[contractName] !== undefined
107+
);
108+
}

0 commit comments

Comments
 (0)