Skip to content

Commit e31dfe9

Browse files
Enable automatic verify for hardhat deploy (#45)
* updated some error logging * implemented ability for hardhat-deploy verify
1 parent 24bc71c commit e31dfe9

9 files changed

+218
-54
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@
5252
},
5353
"dependencies": {
5454
"@nomiclabs/hardhat-ethers": "^2.0.6",
55-
"ethers": "^5.6.8",
5655
"axios": "^0.21.1",
56+
"ethers": "^5.6.8",
5757
"fs-extra": "^9.0.1",
58+
"hardhat-deploy": "^0.11.10",
5859
"js-yaml": "^3.14.0"
5960
}
60-
}
61+
}

src/Tenderly.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
44
import { sep } from "path";
55

66
import { DefaultChainId, NetworkMap, PluginName } from "./index";
7-
import { CONTRACTS_NOT_DETECTED } from "./tenderly/errors";
7+
import {
8+
CONTRACTS_NOT_DETECTED,
9+
NO_COMPILER_FOUND_FOR_CONTRACT
10+
} from "./tenderly/errors";
811
import { TenderlyService } from "./tenderly/TenderlyService";
912
import {
1013
ContractByName,
@@ -286,7 +289,8 @@ export class Tenderly {
286289
);
287290

288291
if (config === undefined) {
289-
console.log(`Error in ${PluginName}: No compiler configuration found`);
292+
console.log(NO_COMPILER_FOUND_FOR_CONTRACT);
293+
console.log(flatContracts);
290294
}
291295

292296
return {

src/TenderlyNetwork.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import os from "os";
66
import path from "path";
77

88
import { PluginName } from "./index";
9+
import { NO_COMPILER_FOUND_FOR_CONTRACT } from "./tenderly/errors";
910
import { TenderlyApiService } from "./tenderly/TenderlyApiService";
1011
import { TenderlyService } from "./tenderly/TenderlyService";
1112
import {
@@ -242,7 +243,8 @@ export class TenderlyNetwork {
242243
);
243244

244245
if (solcConfig === undefined) {
245-
console.log(`Error in ${PluginName}: No compiler configuration found`);
246+
console.log(NO_COMPILER_FOUND_FOR_CONTRACT);
247+
console.log(flatContracts);
246248
}
247249

248250
return {

src/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
import { Tenderly } from "./Tenderly";
1515
import { CONTRACTS_NOT_DETECTED } from "./tenderly/errors";
1616
import { wrapEthers } from "./tenderly/ethers";
17+
import { wrapHHDeployments } from "./tenderly/hardhat-deploy";
1718
import { TENDERLY_RPC_BASE, TenderlyService } from "./tenderly/TenderlyService";
1819
import { Metadata, TenderlyContract } from "./tenderly/types";
1920
import { TenderlyPublicNetwork } from "./tenderly/types/Network";
@@ -32,6 +33,7 @@ extendEnvironment(env => {
3233
extendProvider(env);
3334
populateNetworks(env);
3435
extendEthers(env);
36+
extendHardhatDeploy(env);
3537
});
3638

3739
extendConfig((resolvedConfig, userConfig) => {
@@ -46,6 +48,7 @@ export const setup = (): void => {
4648
extendProvider(env);
4749
populateNetworks(env);
4850
extendEthers(env);
51+
extendHardhatDeploy(env);
4952
});
5053
};
5154

@@ -67,6 +70,21 @@ const extendEthers = (hre: HardhatRuntimeEnvironment): void => {
6770
}
6871
};
6972

73+
const extendHardhatDeploy = (hre: HardhatRuntimeEnvironment): void => {
74+
// ts-ignore is needed here because we want to avoid importing hardhat-deploy in order not to cause duplicated initialization of the .deployments field
75+
if (
76+
"deployments" in hre &&
77+
// @ts-ignore
78+
hre.deployments !== undefined &&
79+
"tenderly" in hre &&
80+
// @ts-ignore
81+
hre.tenderly !== undefined
82+
) {
83+
// @ts-ignore
84+
hre.deployments = wrapHHDeployments(hre.deployments, hre.tenderly);
85+
}
86+
};
87+
7088
const extendProvider = (hre: HardhatRuntimeEnvironment): void => {
7189
if (hre.network.name !== "tenderly") {
7290
return;

src/tenderly/TenderlyService.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ export class TenderlyService {
6464
}
6565

6666
if (!responseData.contracts?.length) {
67-
console.log(NO_NEW_CONTRACTS_VERIFIED_ERROR);
67+
let addresses = "";
68+
for (const cont of request.contracts) {
69+
addresses += cont.contractName + ", ";
70+
}
71+
72+
console.log(NO_NEW_CONTRACTS_VERIFIED_ERROR, addresses);
6873
return;
6974
}
7075

@@ -80,7 +85,9 @@ export class TenderlyService {
8085
}
8186
console.groupEnd();
8287
} catch (error) {
83-
console.log(error);
88+
console.log(error?.response.status);
89+
console.log(error?.response.statusText);
90+
console.log(error?.response?.data?.error?.message);
8491
console.log(API_REQUEST_ERROR);
8592
}
8693
}
@@ -105,7 +112,12 @@ export class TenderlyService {
105112
}
106113

107114
if (!responseData.contracts?.length) {
108-
console.log(NO_NEW_CONTRACTS_VERIFIED_ERROR);
115+
let addresses = "";
116+
for (const cont of request.contracts) {
117+
addresses += cont.contractName + ", ";
118+
}
119+
120+
console.log(NO_NEW_CONTRACTS_VERIFIED_ERROR, addresses);
109121
return;
110122
}
111123

@@ -115,7 +127,9 @@ export class TenderlyService {
115127
`Successfully pushed Smart Contracts for project ${tenderlyProject}. You can view your contracts at ${dashLink}`
116128
);
117129
} catch (error) {
118-
console.log(error);
130+
console.log(error?.response.status);
131+
console.log(error?.response.statusText);
132+
console.log(error?.response?.data?.error?.message);
119133
console.log(API_REQUEST_ERROR);
120134
}
121135
}
@@ -142,13 +156,24 @@ export class TenderlyService {
142156
}
143157

144158
if (!responseData.contracts?.length) {
145-
console.log(NO_NEW_CONTRACTS_VERIFIED_ERROR);
159+
let addresses = "";
160+
for (const cont of request.contracts) {
161+
addresses += cont.contractName + ", ";
162+
}
163+
164+
console.log(NO_NEW_CONTRACTS_VERIFIED_ERROR, addresses);
146165
return;
147166
}
148167

149-
console.log("Smart Contracts successfully verified");
168+
console.group();
169+
for (const contract of responseData.contracts) {
170+
console.log(`Contract at ${contract.address} verified.`);
171+
}
172+
console.groupEnd();
150173
} catch (error) {
151-
console.log(error);
174+
console.log(error?.response.status);
175+
console.log(error?.response.statusText);
176+
console.log(error?.response?.data?.error?.message);
152177
console.log(API_REQUEST_ERROR);
153178
}
154179
}

src/tenderly/errors.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Plugin = "hardhat-tenderly";
22

33
export const BYTECODE_MISMATCH_ERROR = `Error in ${Plugin}: Contract verification failed, bytecode mismatch detected. This can occur if compiler details or source is different compared to the deployed contract.`;
44
export const NO_VERIFIABLE_CONTRACTS_ERROR = `${Plugin}: Contracts are not eligible for verification.`;
5-
export const NO_NEW_CONTRACTS_VERIFIED_ERROR = `${Plugin}: No new contracts have been verified.`;
6-
export const API_REQUEST_ERROR = `Error in ${Plugin}: Verification failed. There was an error during the API request, please contact support with the above error.`;
5+
export const NO_NEW_CONTRACTS_VERIFIED_ERROR = `${Plugin}: Warning: No new contracts have been verified. \n Contract not eligible for verification %s`;
6+
export const NO_COMPILER_FOUND_FOR_CONTRACT = `Error in ${Plugin}: No compiler configuration found for the contracts`;
7+
export const API_REQUEST_ERROR = `Error in ${Plugin}: Verification failed. There was an error during the API request, please contact support with the above error.`;
78
export const CONTRACTS_NOT_DETECTED = `"Could not detect any contracts inside hardhat project. Make sure you have some contracts under ./contracts directory."`;

src/tenderly/hardhat-deploy.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
DeploymentsExtension,
3+
DeployOptions,
4+
DeployResult
5+
} from "hardhat-deploy/types";
6+
7+
import { TenderlyPlugin } from "../type-extensions";
8+
9+
export function wrapHHDeployments(
10+
deployments: DeploymentsExtension,
11+
tenderly: TenderlyPlugin
12+
) {
13+
deployments.deploy = wrapDeploy(deployments.deploy, tenderly);
14+
15+
return deployments;
16+
}
17+
18+
export declare function hhDeploy(
19+
name: string,
20+
options: DeployOptions
21+
): Promise<DeployResult>;
22+
23+
function wrapDeploy(
24+
deployFunc: typeof hhDeploy,
25+
tenderly: TenderlyPlugin
26+
): typeof hhDeploy {
27+
return async function(
28+
name: string,
29+
options: DeployOptions
30+
): Promise<DeployResult> {
31+
const depResult = await deployFunc(name, options);
32+
await tenderly.verify({
33+
name,
34+
address: depResult.address
35+
});
36+
37+
return depResult;
38+
};
39+
}

src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HardhatPluginError } from "hardhat/plugins";
12
import { HardhatConfig } from "hardhat/src/types/config";
23
import { HardhatRuntimeEnvironment } from "hardhat/types";
34

@@ -8,7 +9,6 @@ import {
89
TenderlyContract,
910
TenderlyContractConfig
1011
} from "./tenderly/types";
11-
import {HardhatPluginError} from "hardhat/plugins";
1212

1313
export const getCompilerDataFromContracts = (
1414
contracts: TenderlyContract[],

0 commit comments

Comments
 (0)