-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(contract): add contract verification task
- [x] Update contract storage with already verified contracts - [x] Fix deployment for poseidon contracts - [x] Add contract verifier
- Loading branch information
Showing
8 changed files
with
180 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { IVerificationSubtaskArgs } from "./types"; | ||
import type { HardhatRuntimeEnvironment, Libraries } from "hardhat/types"; | ||
|
||
export class ContractVerifier { | ||
private hre: HardhatRuntimeEnvironment; | ||
|
||
constructor(hre: HardhatRuntimeEnvironment) { | ||
this.hre = hre; | ||
} | ||
|
||
async verify(address: string, constructorArguments: string, libraries?: string): Promise<[boolean, string]> { | ||
const params: IVerificationSubtaskArgs = { | ||
address, | ||
constructorArguments: JSON.parse(constructorArguments) as unknown[], | ||
}; | ||
|
||
if (libraries) { | ||
params.libraries = JSON.parse(libraries) as Libraries; | ||
} | ||
|
||
// Run etherscan task | ||
const error = await this.hre | ||
.run("verify:verify", params) | ||
.then(() => "") | ||
.catch((err: Error) => { | ||
if (err.message === "Contract source code already verified") { | ||
return ""; | ||
} | ||
|
||
return err.message; | ||
}); | ||
|
||
return [!error, error]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-disable no-console */ | ||
import { task } from "hardhat/config"; | ||
|
||
import type { IStorageInstanceEntry, IVerifyFullArgs } from "../helpers/types"; | ||
|
||
import { ContractStorage } from "../helpers/ContractStorage"; | ||
import { ContractVerifier } from "../helpers/ContractVerifier"; | ||
|
||
task("verify-full", "Verify contracts listed in storage") | ||
.addFlag("force", "Ignore verified status") | ||
.setAction(async ({ force = false }: IVerifyFullArgs, hre) => { | ||
const storage = ContractStorage.getInstance(); | ||
const verifier = new ContractVerifier(hre); | ||
const addressList: string[] = []; | ||
const entryList: IStorageInstanceEntry[] = []; | ||
let index = 0; | ||
|
||
const addEntry = (address: string, entry: IStorageInstanceEntry) => { | ||
if (!entry.verify) { | ||
return; | ||
} | ||
|
||
addressList.push(address); | ||
entryList.push(entry); | ||
index += 1; | ||
}; | ||
|
||
const instances = storage.getInstances(hre.network.name); | ||
|
||
instances.forEach(([key, entry]) => { | ||
if (entry.id.includes("Poseidon")) { | ||
return; | ||
} | ||
|
||
addEntry(key, entry); | ||
}); | ||
|
||
console.log("======================================================================"); | ||
console.log("======================================================================"); | ||
console.log(`Verification batch with ${addressList.length} entries of ${index} total.`); | ||
console.log("======================================================================"); | ||
|
||
const summary: string[] = []; | ||
for (let i = 0; i < addressList.length; i += 1) { | ||
const address = addressList[i]; | ||
const entry = entryList[i]; | ||
|
||
const params = entry.verify; | ||
|
||
console.log("\n======================================================================"); | ||
console.log(`[${i}/${addressList.length}] Verify contract: ${entry.id} ${address}`); | ||
console.log("\tArgs:", params?.args); | ||
|
||
const verifiedEntity = storage.getVerified(address, hre.network.name); | ||
|
||
if (!force && verifiedEntity) { | ||
console.log("Already verified"); | ||
} else { | ||
// eslint-disable-next-line no-await-in-loop | ||
const [ok, err] = await verifier.verify(address, params?.args ?? ""); | ||
|
||
if (ok) { | ||
storage.setVerified(address, hre.network.name, true); | ||
} else { | ||
summary.push(`${address} ${entry.id}: ${err}`); | ||
} | ||
} | ||
} | ||
|
||
console.log("\n======================================================================"); | ||
console.log(`Verification batch has finished with ${summary.length} issue(s).`); | ||
console.log("======================================================================"); | ||
console.log(summary.join("\n")); | ||
console.log("======================================================================"); | ||
}); |