UNPKG

@tenderly/hardhat-tenderly

Version:

Package for overloading some of the HardhatRuntimeEnvironment components

141 lines 8.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setup = setup; require("@nomicfoundation/hardhat-ethers"); require("@openzeppelin/hardhat-upgrades"); const plugins_1 = require("hardhat/plugins"); const config_1 = require("hardhat/config"); const constants_1 = require("@tenderly/api-client/common/constants"); const logger_1 = require("./logger"); const api_client_1 = require("@tenderly/api-client"); const hardhat_integration_1 = require("@tenderly/hardhat-integration"); const extend_ethers_1 = require("./extenders/extend-ethers"); const extend_upgrades_1 = require("./extenders/extend-upgrades"); const extend_hardhat_deploy_1 = require("./extenders/extend-hardhat-deploy"); const tenderly_network_resolver_1 = require("./extenders/tenderly-network-resolver"); const populate_hardhat_verify_config_1 = require("./extenders/populate-hardhat-verify-config"); const tenderlyService = new api_client_1.TenderlyService(constants_1.PLUGIN_NAME); function setup(cfg = { automaticVerifications: true }) { console.log("\x1b[31m%s\x1b[0m", "tdly.setup() function is no longer needed in hardhat.config.ts, you can set automatic verification through TENDERLY_AUTOMATIC_VERIFICATION=true variable"); // print in red color } (0, config_1.extendEnvironment)((hre) => { if (process.env.TENDERLY_AUTOMATIC_VERIFICATION === undefined) { process.env.TENDERLY_AUTOMATIC_VERIFICATION = "true"; } const hardhatTenderlyVersion = require("../package.json").version; process.env.HARDHAT_TENDERLY_VERSION = hardhatTenderlyVersion; hre.tenderly = (0, plugins_1.lazyObject)(() => new hardhat_integration_1.Tenderly(hre)); logger_1.logger.info("@tenderly/hardhat-tenderly version:", hardhatTenderlyVersion); logger_1.logger.info("Tenderly running configuration: ", { username: hre.config.tenderly?.username, project: hre.config.tenderly?.project, automaticVerification: process.env.AUTOMATIC_VERIFICATION_ENABLED === "true" || process.env.TENDERLY_AUTOMATIC_VERIFICATION === "true", privateVerification: hre.config.tenderly?.privateVerification, networkName: hre.network.name, }); if (hre.ethers !== undefined) { printErrorIfEthersAndHardhatTenderlyVersionsArentCompatible(hre, hardhatTenderlyVersion); } const shouldCheckForOutdatedVersion = (process.env.TENDERLY_ENABLE_OUTDATED_VERSION_CHECK === undefined || process.env.TENDERLY_ENABLE_OUTDATED_VERSION_CHECK === "true"); if (shouldCheckForOutdatedVersion) { printWarningIfVersionIsOutdated(hre, hardhatTenderlyVersion).then(); } extendProvider(hre); populateNetworks(); if (process.env.AUTOMATIC_VERIFICATION_ENABLED === "true" || process.env.TENDERLY_AUTOMATIC_VERIFICATION === "true") { logger_1.logger.debug("Automatic verification is enabled, proceeding to extend ethers library."); (0, extend_ethers_1.extendEthers)(hre); (0, extend_upgrades_1.extendUpgrades)(hre); (0, extend_hardhat_deploy_1.extendHardhatDeploy)(hre); logger_1.logger.debug("Wrapping ethers library finished."); } if ((0, populate_hardhat_verify_config_1.shouldPopulateHardhatVerifyConfig)(hre)) { logger_1.logger.info("Automatic population of hardhat-verify `etherscan` configuration is enabled."); // If the config already exists, we should not overwrite it, either remove it or turn off automatic population. const etherscanConfig = (0, populate_hardhat_verify_config_1.findEtherscanConfig)(hre); if (etherscanConfig !== undefined) { throw new Error(`Hardhat-verify's 'etherscan' configuration with network '${hre.network.name}' is already populated. Please remove the following configuration:\n${JSON.stringify(etherscanConfig, null, 2)}\nOr set 'TENDERLY_AUTOMATIC_POPULATE_HARDHAT_VERIFY_CONFIG' environment variable to 'false'`); } (0, populate_hardhat_verify_config_1.populateHardhatVerifyConfig)(hre).then(); } logger_1.logger.debug("Setup finished."); }); (0, config_1.extendConfig)((resolvedConfig) => { resolvedConfig.networks.tenderly = { ...resolvedConfig.networks.tenderly, }; }); const extendProvider = (hre) => { if (!(0, tenderly_network_resolver_1.isTenderlyNetworkConfig)(hre.network.config)) { logger_1.logger.info(`Used network is not 'tenderly' so there is no extending of the provider.`); return; } if ("url" in hre.network.config && hre.network.config.url !== undefined) { if (hre.network.config.url.includes("devnet")) { const devnetID = hre.network.config.url.split("/").pop(); hre.tenderly.network().setDevnetID(devnetID); logger_1.logger.info(`There is a devnet url in the '${hre.network.name}' network`, { devnetID }); return; } const forkID = hre.network.config.url.split("/").pop(); hre.tenderly.network().setFork(forkID); logger_1.logger.info(`There is a fork url in the 'tenderly' network`, { forkID }); return; } const tenderlyNetwork = new hardhat_integration_1.TenderlyNetwork(hre); tenderlyNetwork .initializeFork() .then(async (_) => { hre.tenderly.setNetwork(tenderlyNetwork); const forkID = await hre.tenderly.network().getForkID(); hre.network.config.url = `${constants_1.TENDERLY_JSON_RPC_BASE_URL}/fork/${forkID ?? ""}`; // hre.ethers.provider = new hre.ethers.BrowserProvider(hre.tenderly.network()); }) .catch((_) => { logger_1.logger.error(`Error happened while trying to initialize fork ${constants_1.PLUGIN_NAME}. Check your tenderly configuration`); }); }; const populateNetworks = () => { tenderlyService .getNetworks() .then((networks) => { let network; let slug; for (network of networks) { constants_1.NETWORK_NAME_CHAIN_ID_MAP[network.slug] = network.ethereum_network_id; if (network?.metadata?.slug !== undefined) { constants_1.NETWORK_NAME_CHAIN_ID_MAP[network.metadata.slug] = network.ethereum_network_id; } constants_1.CHAIN_ID_NETWORK_NAME_MAP[network.ethereum_network_id] = network.slug; if (network?.metadata?.secondary_slugs !== undefined) { for (slug of network.metadata.secondary_slugs) { constants_1.NETWORK_NAME_CHAIN_ID_MAP[slug] = network.ethereum_network_id; } } } logger_1.logger.silly("Obtained supported public networks: ", constants_1.NETWORK_NAME_CHAIN_ID_MAP); }) .catch((e) => { logger_1.logger.error("Error encountered while fetching public networks:", e); }); }; function printErrorIfEthersAndHardhatTenderlyVersionsArentCompatible(hre, hardhatTenderlyVersion) { const versionCompatibilityChecker = new hardhat_integration_1.VersionCompatibilityChecker(); const [areCompatible, ethersVersion] = versionCompatibilityChecker.areEthersAndHardhatTenderlyVersionsCompatible(hre, hardhatTenderlyVersion); if (!areCompatible) { const compatibleHardhatTenderlyVersion = versionCompatibilityChecker.compatibleHardhatTenderlyVersionForEthersVersion(ethersVersion); console.log("\x1b[31m%s%s\x1b[0m", // print in red color `The '@tenderly/hardhat-tenderly@${hardhatTenderlyVersion}' doesn't support 'ethers@${ethersVersion}'.\n`, `Please update the plugin to the latest hardhat-tenderly version: 'npm install @tenderly/hardhat-tenderly@${compatibleHardhatTenderlyVersion}'\n`); } } async function printWarningIfVersionIsOutdated(hre, hardhatTenderlyVersion) { const outdatedVersionChecker = new hardhat_integration_1.OutdatedVersionChecker(); const [isVersionOutdated, latestHardhatTenderlyVersion] = await outdatedVersionChecker.isVersionOutdated(hardhatTenderlyVersion); if (isVersionOutdated) { console.log("\x1b[33m%s\x1b[0m%s", // print in yellow color `Please update the plugin to the new version: 'npm install @tenderly/hardhat-tenderly@^${latestHardhatTenderlyVersion}'\n`, "You can disable this message by setting the ‘TENDERLY_ENABLE_OUTDATED_VERSION_CHECK=false’ environment variable."); } } //# sourceMappingURL=setup.js.map