UNPKG

@matterlabs/hardhat-zksync-verify

Version:
148 lines 6.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ZkSyncEtherscanVerificationIdResponse = exports.ZkSyncEtherscanExplorerService = void 0; const axios_1 = __importDefault(require("axios")); const querystring_1 = __importDefault(require("querystring")); const service_1 = require("../service"); const errors_1 = require("../zksync-block-explorer/errors"); const utils_1 = require("../../utils"); const errors_2 = require("../../errors"); const constants_1 = require("../../constants"); const errors_3 = require("../errors"); const verification_status_response_1 = require("./verification-status-response"); const constants_2 = require("./constants"); class ZkSyncEtherscanExplorerService extends service_1.VerificationService { constructor(hre, apikey, verifyUrl, browserUrl) { super(hre, verifyUrl, browserUrl); this.apikey = apikey; } static async fromChainConfig(hre, apiKey, chainConfig) { const resolvedApiKey = resolveApiKey(apiKey, chainConfig.network); const apiUrl = chainConfig.urls.apiURL; const browserUrl = chainConfig.urls.browserURL ? chainConfig.urls.browserURL.trim().replace(/\/$/, '') : undefined; return new ZkSyncEtherscanExplorerService(hre, resolvedApiKey, apiUrl, browserUrl); } async getVerificationStatus(verificationId, contractInformation) { try { const [verifyUrl, params] = (0, utils_1.extractQueryParams)(this.verifyUrl); const response = await axios_1.default.get(verifyUrl, { params: { apikey: this.apikey, module: 'contract', action: 'checkverifystatus', guid: verificationId, ...params, }, }); if (response.status !== 200) { throw new errors_3.ZksyncContractVerificationInvalidStatusCodeError(this.verifyUrl, response.status, response.data); } const zkSyncBlockExplorerResponse = new verification_status_response_1.ZksyncEtherscanResponse(response.data); if (zkSyncBlockExplorerResponse.isPending()) { return zkSyncBlockExplorerResponse; } if (zkSyncBlockExplorerResponse.isFailure() || zkSyncBlockExplorerResponse.isAlreadyVerified()) { return zkSyncBlockExplorerResponse; } if (!zkSyncBlockExplorerResponse.isOk()) { throw new errors_2.ZkSyncVerifyPluginError(zkSyncBlockExplorerResponse.message); } if (zkSyncBlockExplorerResponse.isSuccess()) { console.log(`Successfully verified contract ${contractInformation.contractName} on the block explorer. ${this.getContractBorwserUrl(contractInformation.contractAddress)} `); } return zkSyncBlockExplorerResponse; } catch (error) { (0, utils_1.handleAxiosError)(error); } } generateRequest(initialRequest) { return { compilermode: 'zksync', module: 'contract', action: 'verifysourcecode', apikey: this.apikey, zksolcVersion: initialRequest.compilerZksolcVersion, contractaddress: initialRequest.contractAddress, contractname: initialRequest.contractName, sourceCode: initialRequest.sourceCode, codeformat: 'solidity-standard-json-input', compilerversion: initialRequest.compilerSolcVersion, constructorArguements: initialRequest.constructorArguments.slice(2), }; } async getVerificationId(req) { try { const [verifyUrl, params] = (0, utils_1.extractQueryParams)(this.verifyUrl); const request = this.generateRequest(req); const response = await axios_1.default.post(verifyUrl, querystring_1.default.stringify({ ...request, ...params, sourceCode: JSON.stringify(request.sourceCode), })); const zkSyncEtherscanResponse = new ZkSyncEtherscanVerificationIdResponse(response.data); if (!zkSyncEtherscanResponse.isOk()) { throw new errors_2.ZkSyncVerifyPluginError(zkSyncEtherscanResponse.result); } return zkSyncEtherscanResponse.result; } catch (error) { (0, utils_1.handleAxiosError)(error); } } getContractBorwserUrl(address) { return this.browserUrl || this.browserUrl === '' ? `${this.browserUrl}/address/${address}#code` : ''; } async getSupportedCompilerVersions() { return await this.hre.run(constants_1.TASK_VERIFY_GET_COMPILER_VERSIONS); } async getSolcVersion(contractInformation) { const solcVersion = contractInformation.solcLongVersion; if (!solcVersion.startsWith('zkVM')) { return `v${solcVersion}`; } const response = await axios_1.default.get(constants_2.SOLC_COMPILERS_LIST); if (response.status !== 200) { throw new errors_2.ZkSyncVerifyPluginError(constants_2.SOLC_COMPILERS_LIST_ERROR); } const normalSolcVersion = solcVersion.split('-')[1]; const solcBuild = response.data.builds.find((b) => b.version === normalSolcVersion && !b.prerelease); if (!solcBuild) { throw new errors_2.ZkSyncVerifyPluginError((0, constants_2.SOLC_COMPILER_VERSION_NOTFOUND)(normalSolcVersion)); } return `v${solcBuild.longVersion}`; } } exports.ZkSyncEtherscanExplorerService = ZkSyncEtherscanExplorerService; function resolveApiKey(apiKey, network) { if (apiKey === undefined || apiKey === '') { throw new errors_1.ZksyncMissingApiKeyError(network); } if (typeof apiKey === 'string') { return apiKey; } const key = apiKey[network]; if (key === undefined || key === '') { throw new errors_1.ZksyncMissingApiKeyError(network); } return key; } class ZkSyncEtherscanVerificationIdResponse { constructor(response) { this.status = parseInt(response.status, 10); this.message = response.message; this.result = response.result; } isOk() { return this.status === 1 && this.message === 'OK'; } } exports.ZkSyncEtherscanVerificationIdResponse = ZkSyncEtherscanVerificationIdResponse; //# sourceMappingURL=service.js.map