UNPKG

hardhat-deployed

Version:

Hardhat plugin for saving deployed contract address.

54 lines (53 loc) 2.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const plugins_1 = require("hardhat/plugins"); const fs_1 = __importDefault(require("fs")); const constants_1 = require("../constants"); const explorers_1 = require("../misc/explorers"); const helpers_1 = require("../misc/helpers"); const deployed_1 = require("./deployed"); deployed_1.deployedTaskScope .task(constants_1.TASK_DEPLOYED_LIST, "List deployed of one network") .addOptionalParam("explorer", "blockchain's block explorer,such as 'https://etherscan.io/tx/'") .setAction(async (taskArgs, hre) => { // read configs from `hardhat.config.ts` const configs = hre.config.deployed; // get network name const network = hre.network.name; // default value is `hardhat` // `31337` is the chainId of default `localhost` network. Reference: https://hardhat.org/hardhat-network/docs/reference#chainid // `localhost` network's chainId is not explicitly configured in `hardhat.config.ts` // so we use `31337` as default value const chainId = hre.network.config.chainId ?? 31337; // get block explorer const explorer = taskArgs.explorer != null ? taskArgs.explorer : (0, explorers_1.blockchainExplorer)(chainId); // if (explorer == "") { // throw new HardhatPluginError( // PLUGIN_NAME, // "chain not support, please specify block explorer manually by using '--explorer https://etherscan.io/tx/'", // ); // } // if manifest file not exists, throw an error const manifest = `${configs.deployedDir}/${network}/contracts.json`; if (!fs_1.default.existsSync(manifest)) { throw new plugins_1.HardhatPluginError(constants_1.PLUGIN_NAME, `'${manifest}' file not found`); } const content = fs_1.default.readFileSync(manifest, "utf-8"); let json = JSON.parse(content); const list = (0, helpers_1.flatJson)(json).map((element) => { return { Contract: element[0], Address: element[1], Explorer: element[1] == constants_1.DEFAULT_DEPLOYED_ADDRESS ? "" : explorer == "" ? "--" : explorer + element[1], }; }); console.table(list); });