UNPKG

@foundry-rs/hardhat-anvil

Version:

Hardhat plugin for managing Anvil

85 lines 3.58 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AnvilService = void 0; const debug_1 = __importDefault(require("debug")); const errors_1 = require("hardhat/internal/core/errors"); const url_1 = require("url"); const anvil_server_1 = require("./anvil-server"); const log = (0, debug_1.default)("hardhat:plugin:anvil-service"); const DEFAULT_PORT = 8545; class AnvilService { constructor(Anvil, options) { log("Initializing server"); this._server = Anvil; this._options = options; } static getDefaultAccountConfig() { return Object.assign({ locked: false, hdPath: "m/44'/60'/0'/0/", mnemonic: "test test test test test test test test test test test junk" }, AnvilService.getDefaultOptions()); } static getDefaultOptions() { return { url: `http://127.0.0.1:${DEFAULT_PORT}/`, launch: true, }; } /** * * @param options * @returns type checked options for `anvil` */ static async getCheckedArgs(options) { // Get and initialize option validator const { default: optionsSchema } = await Promise.resolve().then(() => __importStar(require("./anvil-options-ti"))); const { createCheckers } = await Promise.resolve().then(() => __importStar(require("ts-interface-checker"))); const { AnvilOptionsTi } = createCheckers(optionsSchema); // Validate all options against the validator try { AnvilOptionsTi.check(options); } catch (e) { const err = e; throw new errors_1.NomicLabsHardhatPluginError("@foundry-rs/hardhat-anvil", `Anvil network config is invalid: ${err.message}`, err); } // Validate and parse hostname and port from URL (this validation is priority) const url = new url_1.URL(options.url); if (url.hostname !== "localhost" && url.hostname !== "127.0.0.1") { throw new errors_1.NomicLabsHardhatPluginError("@foundry-rs/hardhat-anvil", "Anvil network only works with localhost"); } return options; } static async create(options, inherit = false) { const args = await AnvilService.getCheckedArgs(options); const Anvil = await anvil_server_1.AnvilServer.launch(args, inherit); return new AnvilService(Anvil, args); } stopServer() { this._server.kill(); } async waitUntilClosed() { return this._server.waitUntilClosed(); } } exports.AnvilService = AnvilService; //# sourceMappingURL=anvil-service.js.map