@solarity/hardhat-migrate
Version:
The simplest way to deploy smart contracts
139 lines • 6.71 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MinimalContract = void 0;
const ethers_1 = require("ethers");
const contract_names_1 = require("hardhat/utils/contract-names");
const Linker_1 = require("./Linker");
const utils_1 = require("../utils");
const errors_1 = require("../errors");
const Stats_1 = require("../tools/Stats");
const Reporter_1 = require("../tools/reporters/Reporter");
const NetworkManager_1 = require("../tools/network/NetworkManager");
const TransactionRunner_1 = require("../tools/runners/TransactionRunner");
const ArtifactProcessor_1 = require("../tools/storage/ArtifactProcessor");
const TransactionProcessor_1 = require("../tools/storage/TransactionProcessor");
const VerificationProcessor_1 = require("../tools/storage/VerificationProcessor");
let MinimalContract = class MinimalContract {
_hre;
_bytecode;
_interface;
_contractName;
_rawBytecode;
_interfaceOnlyWithConstructor;
constructor(_hre, _bytecode, _interface, _contractName = "") {
this._hre = _hre;
this._bytecode = _bytecode;
this._interface = _interface;
this._contractName = _contractName;
this._interfaceOnlyWithConstructor = (0, utils_1.getInterfaceOnlyWithConstructor)(this._interface.fragments);
this._rawBytecode = this._bytecode;
if (_contractName === "") {
try {
this._contractName = ArtifactProcessor_1.ArtifactProcessor.tryGetContractName(_bytecode);
}
catch {
throw new errors_1.MigrateError("Contract name is not provided and cannot be extracted from bytecode.");
}
}
}
async deploy(args = [], parameters = {}) {
await (0, utils_1.fillParameters)(parameters);
await this._tryLinkLibraries(parameters);
const tx = await this._createDeployTransaction(args, parameters);
if (this._hre.config.migrate.execution.continue) {
return this._recoverContractAddress(tx, args);
}
else {
return this._processContractDeploymentTransaction(tx, args);
}
}
async _tryLinkLibraries(parameters) {
try {
if (Linker_1.Linker?.isBytecodeNeedsLinking(this._bytecode)) {
return;
}
this._bytecode = (await Linker_1.Linker?.tryLinkBytecode(this._contractName, this._bytecode, parameters.libraries || {}));
}
catch (e) {
throw new errors_1.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_1.ethers.ContractFactory(this._interfaceOnlyWithConstructor, this._bytecode);
return {
contractName: this._contractName,
chainId: await (0, utils_1.getChainId)(),
from: await (await NetworkManager_1.networkManager.getSigner(txOverrides.from)).getAddress(),
...(await factory.getDeployTransaction(...args, txOverrides)),
};
}
async _recoverContractAddress(tx, args) {
try {
const contractAddress = await TransactionProcessor_1.TransactionProcessor?.tryRestoreContractAddressByKeyFields(tx);
Reporter_1.Reporter.notifyContractRecovery(tx.contractName, contractAddress);
await this._saveContractForVerification(contractAddress, tx, args);
return contractAddress;
}
catch {
/* empty */
}
Reporter_1.Reporter.notifyDeploymentInsteadOfRecovery(tx.contractName);
return this._processContractDeploymentTransaction(tx, args);
}
async _processContractDeploymentTransaction(tx, args) {
const signer = await NetworkManager_1.networkManager.getSigner(tx.from);
const txResponse = await signer.sendTransaction(tx);
await TransactionRunner_1.TransactionRunner.reportTransactionResponse(txResponse, tx.contractName);
const contractAddress = (await txResponse.wait(0)).contractAddress;
if (typeof contractAddress !== "string") {
throw new errors_1.MigrateError("Contract deployment failed. Invalid contract address conversion.");
}
await this._saveContractForVerification(contractAddress, tx, args);
const saveMetadata = {
migrationNumber: Stats_1.Stats.currentMigration,
contractName: tx.contractName,
fullyQualifiedContractName: this._getFullyQualifiedName(tx) || undefined,
};
TransactionProcessor_1.TransactionProcessor?.saveDeploymentTransaction(tx, tx.contractName, contractAddress, saveMetadata);
return contractAddress;
}
async _saveContractForVerification(contractAddress, tx, args) {
if (VerificationProcessor_1.VerificationProcessor.isVerificationDataSaved(contractAddress)) {
return;
}
const contractName = this._getFullyQualifiedName(tx);
if (contractName === null) {
Reporter_1.Reporter.reportVerificationFailedToSave(tx.contractName);
return;
}
VerificationProcessor_1.VerificationProcessor.saveVerificationFunction({
contractAddress,
contractName: contractName,
constructorArguments: args,
chainId: Number(await (0, utils_1.getChainId)()),
});
await ArtifactProcessor_1.ArtifactProcessor.saveArtifactIfNotExist(this._hre, contractName, this._rawBytecode);
}
_getFullyQualifiedName(tx) {
try {
if (!(0, contract_names_1.isFullyQualifiedName)(tx.contractName)) {
return ArtifactProcessor_1.ArtifactProcessor.tryGetContractName(this._rawBytecode);
}
return tx.contractName;
}
catch {
return null;
}
}
};
exports.MinimalContract = MinimalContract;
exports.MinimalContract = MinimalContract = __decorate([
utils_1.catchError
], MinimalContract);
//# sourceMappingURL=MinimalContract.js.map