UNPKG

@solarity/hardhat-migrate

Version:
175 lines 9.03 kB
var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) { function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; } var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value"; var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null; var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {}); var _, done = false; for (var i = decorators.length - 1; i >= 0; i--) { var context = {}; for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p]; for (var p in contextIn.access) context.access[p] = contextIn.access[p]; context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); }; var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context); if (kind === "accessor") { if (result === void 0) continue; if (result === null || typeof result !== "object") throw new TypeError("Object expected"); if (_ = accept(result.get)) descriptor.get = _; if (_ = accept(result.set)) descriptor.set = _; if (_ = accept(result.init)) initializers.unshift(_); } else if (_ = accept(result)) { if (kind === "field") initializers.unshift(_); else descriptor[key] = _; } } if (target) Object.defineProperty(target, contextIn.name, descriptor); done = true; }; var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) { var useValue = arguments.length > 2; for (var i = 0; i < initializers.length; i++) { value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg); } return useValue ? value : void 0; }; import { ethers } from "ethers"; import { isFullyQualifiedName } from "hardhat/utils/contract-names"; import { Linker } from "./Linker.js"; import { CatchClassError, fillParameters, getChainId, getInterfaceOnlyWithConstructor } from "../utils/index.js"; import { MigrateError } from "../utils/index.js"; import { Stats } from "../tools/Stats.js"; import { Reporter } from "../tools/reporters/Reporter.js"; import { networkManager } from "../tools/network/NetworkManager.js"; import { TransactionRunner } from "../tools/runners/TransactionRunner.js"; import { ArtifactProcessor } from "../tools/storage/ArtifactProcessor.js"; import { TransactionProcessor } from "../tools/storage/TransactionProcessor.js"; import { VerificationProcessor } from "../tools/storage/VerificationProcessor.js"; import { migratorConfig } from "../tools/network/EthersProvider.js"; let MinimalContract = (() => { let _classDecorators = [CatchClassError]; let _classDescriptor; let _classExtraInitializers = []; let _classThis; var MinimalContract = class { static { _classThis = this; } static { const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0; __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers); MinimalContract = _classThis = _classDescriptor.value; if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata }); __runInitializers(_classThis, _classExtraInitializers); } _bytecode; _interface; _contractName; _rawBytecode; _interfaceOnlyWithConstructor; constructor(_bytecode, _interface, _contractName = "") { this._bytecode = _bytecode; this._interface = _interface; this._contractName = _contractName; this._interfaceOnlyWithConstructor = getInterfaceOnlyWithConstructor(this._interface.fragments); this._rawBytecode = this._bytecode; if (_contractName === "") { try { this._contractName = ArtifactProcessor.tryGetContractName(_bytecode); } catch { throw new MigrateError("Contract name is not provided and cannot be extracted from bytecode."); } } } async deploy(args = [], parameters = {}) { await fillParameters(parameters); await this._tryLinkLibraries(parameters); const tx = await this._createDeployTransaction(args, parameters); if (migratorConfig.execution.continue) { return this._recoverContractAddress(tx, args); } else { return this._processContractDeploymentTransaction(tx, args); } } async _tryLinkLibraries(parameters) { try { if (!Linker.isBytecodeNeedsLinking(this._bytecode)) { return; } this._bytecode = (await Linker.tryLinkBytecode(this._contractName, this._bytecode, parameters.libraries || {})); } catch (e) { throw new MigrateError(`Unable to link libraries for ${this._contractName}! Try manually deploy the libraries and link them.\n Error: ${e.message}`); } } async _createDeployTransaction(args, txOverrides) { const factory = new ethers.ContractFactory(this._interfaceOnlyWithConstructor, this._bytecode); return { contractName: this._contractName, chainId: await getChainId(), from: await (await networkManager.getSigner(txOverrides.from)).getAddress(), ...(await factory.getDeployTransaction(...args, txOverrides)), }; } async _recoverContractAddress(tx, args) { try { const contractAddress = await TransactionProcessor?.tryRestoreContractAddressByKeyFields(tx); Reporter.notifyContractRecovery(tx.contractName, contractAddress); await this._saveContractForVerification(contractAddress, tx, args); return contractAddress; } catch { /* empty */ } Reporter.notifyDeploymentInsteadOfRecovery(tx.contractName); return this._processContractDeploymentTransaction(tx, args); } async _processContractDeploymentTransaction(tx, args) { const signer = await networkManager.getSigner(tx.from); const txResponse = await signer.sendTransaction(tx); await TransactionRunner.reportTransactionResponse(txResponse, tx.contractName); const contractAddress = (await txResponse.wait(0)).contractAddress; if (typeof contractAddress !== "string") { throw new MigrateError("Contract deployment failed. Invalid contract address conversion."); } await this._saveContractForVerification(contractAddress, tx, args); const saveMetadata = { migrationNumber: Stats.currentMigration, contractName: tx.contractName, fullyQualifiedContractName: this._getFullyQualifiedName(tx) || undefined, }; TransactionProcessor?.saveDeploymentTransaction(tx, tx.contractName, contractAddress, saveMetadata); return contractAddress; } async _saveContractForVerification(contractAddress, tx, args) { if (VerificationProcessor.isVerificationDataSaved(contractAddress)) { return; } const contractName = this._getFullyQualifiedName(tx); if (contractName === null) { Reporter.reportVerificationFailedToSave(tx.contractName); return; } VerificationProcessor.saveVerificationFunction({ contractAddress, contractName: contractName, constructorArguments: args, chainId: Number(await getChainId()), }); await ArtifactProcessor.saveArtifactIfNotExist(contractName, this._rawBytecode); } _getFullyQualifiedName(tx) { try { if (!isFullyQualifiedName(tx.contractName)) { return ArtifactProcessor.tryGetContractName(this._rawBytecode); } return tx.contractName; } catch { return null; } } }; return MinimalContract = _classThis; })(); export { MinimalContract }; //# sourceMappingURL=MinimalContract.js.map