UNPKG

@nomicfoundation/hardhat-ignition-viem

Version:

The Viem extension to Hardhat Ignition. Hardhat Ignition is a declarative system for deploying smart contracts on Ethereum. It enables you to define smart contract instances you want to deploy, and any operation you want to run on them. By taking over the

172 lines 7.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (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.ViemIgnitionHelper = void 0; const helpers_1 = require("@nomicfoundation/hardhat-ignition/helpers"); const ignition_core_1 = require("@nomicfoundation/ignition-core"); const plugins_1 = require("hardhat/plugins"); const path_1 = __importDefault(require("path")); class ViemIgnitionHelper { _hre; _config; type = "viem"; _provider; constructor(_hre, _config, provider) { this._hre = _hre; this._config = _config; this._provider = provider ?? this._hre.network.provider; } /** * Deploys the given Ignition module and returns the results of the module * as Viem contract instances. * * @param ignitionModule - The Ignition module to deploy. * @param options - The options to use for the deployment. * @returns Viem contract instances for each contract returned by the module. */ async deploy(ignitionModule, { parameters = {}, config: perDeployConfig = {}, defaultSender = undefined, strategy, strategyConfig, deploymentId: givenDeploymentId = undefined, displayUi = false, } = { parameters: {}, config: {}, defaultSender: undefined, strategy: undefined, strategyConfig: undefined, deploymentId: undefined, displayUi: undefined, }) { const accounts = (await this._hre.network.provider.request({ method: "eth_accounts", })); const artifactResolver = new helpers_1.HardhatArtifactResolver(this._hre); const resolvedConfig = { ...this._config, ...perDeployConfig, }; const resolvedStrategyConfig = ViemIgnitionHelper._resolveStrategyConfig(this._hre, strategy, strategyConfig); const chainId = Number(await this._hre.network.provider.request({ method: "eth_chainId", })); const deploymentId = (0, helpers_1.resolveDeploymentId)(givenDeploymentId, chainId); const deploymentDir = this._hre.network.name === "hardhat" ? undefined : path_1.default.join(this._hre.config.paths.ignition, "deployments", deploymentId); const executionEventListener = displayUi ? new helpers_1.PrettyEventHandler() : undefined; let deploymentParameters; if (typeof parameters === "string") { deploymentParameters = await (0, helpers_1.readDeploymentParameters)(parameters); } else { deploymentParameters = parameters; } const result = await (0, ignition_core_1.deploy)({ config: resolvedConfig, provider: this._provider, deploymentDir, executionEventListener, artifactResolver, ignitionModule, deploymentParameters, accounts, defaultSender, strategy, strategyConfig: resolvedStrategyConfig, maxFeePerGasLimit: this._hre.config.networks[this._hre.network.name]?.ignition .maxFeePerGasLimit, maxPriorityFeePerGas: this._hre.config.networks[this._hre.network.name]?.ignition .maxPriorityFeePerGas, }); if (result.type !== ignition_core_1.DeploymentResultType.SUCCESSFUL_DEPLOYMENT) { const message = (0, helpers_1.errorDeploymentResultToExceptionMessage)(result); throw new plugins_1.HardhatPluginError("hardhat-ignition-viem", message); } return ViemIgnitionHelper._toViemContracts(this._hre, ignitionModule, result); } static async _toViemContracts(hre, ignitionModule, result) { return Object.fromEntries(await Promise.all(Object.entries(ignitionModule.results).map(async ([name, contractFuture]) => [ name, await ViemIgnitionHelper._getContract(hre, contractFuture, result.contracts[contractFuture.id]), ]))); } static async _getContract(hre, future, deployedContract) { if (!(0, ignition_core_1.isContractFuture)(future)) { throw new plugins_1.HardhatPluginError("hardhat-ignition-viem", `Expected contract future but got ${future.id} with type ${future.type} instead`); } return ViemIgnitionHelper._convertContractFutureToViemContract(hre, future, deployedContract); } static async _convertContractFutureToViemContract(hre, future, deployedContract) { switch (future.type) { case ignition_core_1.FutureType.NAMED_ARTIFACT_CONTRACT_DEPLOYMENT: case ignition_core_1.FutureType.NAMED_ARTIFACT_LIBRARY_DEPLOYMENT: case ignition_core_1.FutureType.NAMED_ARTIFACT_CONTRACT_AT: return ViemIgnitionHelper._convertHardhatContractToViemContract(hre, future, deployedContract); case ignition_core_1.FutureType.CONTRACT_DEPLOYMENT: case ignition_core_1.FutureType.LIBRARY_DEPLOYMENT: case ignition_core_1.FutureType.CONTRACT_AT: return ViemIgnitionHelper._convertArtifactToViemContract(hre, future, deployedContract); } } static _convertHardhatContractToViemContract(hre, future, deployedContract) { return hre.viem.getContractAt(future.contractName, ViemIgnitionHelper._ensureAddressFormat(deployedContract.address)); } static async _convertArtifactToViemContract(hre, future, deployedContract) { const publicClient = await hre.viem.getPublicClient(); const [walletClient] = await hre.viem.getWalletClients(); if (walletClient === undefined) { throw new plugins_1.HardhatPluginError("hardhat-ignition-viem", "No default wallet client found"); } const viem = await Promise.resolve().then(() => __importStar(require("viem"))); const contract = viem.getContract({ address: ViemIgnitionHelper._ensureAddressFormat(deployedContract.address), abi: future.artifact.abi, client: { public: publicClient, wallet: walletClient, }, }); return contract; } static _ensureAddressFormat(address) { if (!address.startsWith("0x")) { return `0x${address}`; } return `0x${address.slice(2)}`; } static _resolveStrategyConfig(hre, strategyName, strategyConfig) { if (strategyName === undefined) { return undefined; } if (strategyConfig === undefined) { const fromHardhatConfig = hre.config.ignition?.strategyConfig?.[strategyName]; return fromHardhatConfig; } return strategyConfig; } } exports.ViemIgnitionHelper = ViemIgnitionHelper; //# sourceMappingURL=viem-ignition-helper.js.map