@nomicfoundation/hardhat-viem
Version:
Hardhat plugin for viem
164 lines • 7.54 kB
JavaScript
;
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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContractAt = exports.sendDeploymentTransaction = exports.innerDeployContract = exports.deployContract = void 0;
const bytecode_1 = require("./bytecode");
const clients_1 = require("./clients");
const errors_1 = require("./errors");
async function getContractAbiAndBytecode(artifacts, contractName, libraries) {
const artifact = await artifacts.readArtifact(contractName);
const bytecode = await (0, bytecode_1.resolveBytecodeWithLinkedLibraries)(artifact, libraries);
return {
abi: artifact.abi,
bytecode,
};
}
async function deployContract({ artifacts, network }, contractName, constructorArgs = [], config = {}) {
const { client, confirmations, libraries = {}, ...deployContractParameters } = config;
const [publicClient, walletClient, { abi, bytecode }] = await Promise.all([
client?.public ?? (0, clients_1.getPublicClient)(network.provider),
client?.wallet ?? getDefaultWalletClient(network.provider, network.name),
getContractAbiAndBytecode(artifacts, contractName, libraries),
]);
return innerDeployContract(publicClient, walletClient, abi, bytecode, constructorArgs, deployContractParameters, confirmations);
}
exports.deployContract = deployContract;
async function innerDeployContract(publicClient, walletClient, contractAbi, contractBytecode, constructorArgs, deployContractParameters = {}, confirmations = 1) {
let deploymentTxHash;
// If gasPrice is defined, then maxFeePerGas and maxPriorityFeePerGas
// must be undefined because it's a legaxy tx.
if (deployContractParameters.gasPrice !== undefined) {
deploymentTxHash = await walletClient.deployContract({
abi: contractAbi,
bytecode: contractBytecode,
args: constructorArgs,
...deployContractParameters,
maxFeePerGas: undefined,
maxPriorityFeePerGas: undefined,
});
}
else {
deploymentTxHash = await walletClient.deployContract({
abi: contractAbi,
bytecode: contractBytecode,
args: constructorArgs,
...deployContractParameters,
gasPrice: undefined,
});
}
if (confirmations < 0) {
throw new errors_1.HardhatViemError("Confirmations must be greater than 0.");
}
if (confirmations === 0) {
throw new errors_1.InvalidConfirmationsError();
}
const { contractAddress } = await publicClient.waitForTransactionReceipt({
hash: deploymentTxHash,
confirmations,
});
if (contractAddress === null || contractAddress === undefined) {
const transaction = await publicClient.getTransaction({
hash: deploymentTxHash,
});
throw new errors_1.DeployContractError(deploymentTxHash, transaction.blockNumber);
}
const contract = await innerGetContractAt(publicClient, walletClient, contractAbi, contractAddress);
return contract;
}
exports.innerDeployContract = innerDeployContract;
async function sendDeploymentTransaction({ artifacts, network }, contractName, constructorArgs = [], config = {}) {
const { client, libraries = {}, ...deployContractParameters } = config;
const [publicClient, walletClient, { abi, bytecode }] = await Promise.all([
client?.public ?? (0, clients_1.getPublicClient)(network.provider),
client?.wallet ?? getDefaultWalletClient(network.provider, network.name),
getContractAbiAndBytecode(artifacts, contractName, libraries),
]);
return innerSendDeploymentTransaction(publicClient, walletClient, abi, bytecode, constructorArgs, deployContractParameters);
}
exports.sendDeploymentTransaction = sendDeploymentTransaction;
async function innerSendDeploymentTransaction(publicClient, walletClient, contractAbi, contractBytecode, constructorArgs, deployContractParameters = {}) {
let deploymentTxHash;
// If gasPrice is defined, then maxFeePerGas and maxPriorityFeePerGas
// must be undefined because it's a legaxy tx.
if (deployContractParameters.gasPrice !== undefined) {
deploymentTxHash = await walletClient.deployContract({
abi: contractAbi,
bytecode: contractBytecode,
args: constructorArgs,
...deployContractParameters,
maxFeePerGas: undefined,
maxPriorityFeePerGas: undefined,
});
}
else {
deploymentTxHash = await walletClient.deployContract({
abi: contractAbi,
bytecode: contractBytecode,
args: constructorArgs,
...deployContractParameters,
gasPrice: undefined,
});
}
const deploymentTx = await publicClient.getTransaction({
hash: deploymentTxHash,
});
const { getContractAddress } = await Promise.resolve().then(() => __importStar(require("viem")));
const contractAddress = getContractAddress({
from: walletClient.account.address,
nonce: BigInt(deploymentTx.nonce),
});
const contract = await innerGetContractAt(publicClient, walletClient, contractAbi, contractAddress);
return { contract, deploymentTransaction: deploymentTx };
}
async function getContractAt({ artifacts, network }, contractName, address, config = {}) {
const [publicClient, walletClient, contractArtifact] = await Promise.all([
config.client?.public ?? (0, clients_1.getPublicClient)(network.provider),
config.client?.wallet ??
getDefaultWalletClient(network.provider, network.name),
artifacts.readArtifact(contractName),
]);
return innerGetContractAt(publicClient, walletClient, contractArtifact.abi, address);
}
exports.getContractAt = getContractAt;
async function innerGetContractAt(publicClient, walletClient, contractAbi, address) {
const viem = await Promise.resolve().then(() => __importStar(require("viem")));
const contract = viem.getContract({
address,
client: {
public: publicClient,
wallet: walletClient,
},
abi: contractAbi,
});
return contract;
}
async function getDefaultWalletClient(provider, networkName) {
const [defaultWalletClient] = await (0, clients_1.getWalletClients)(provider);
if (defaultWalletClient === undefined) {
throw new errors_1.DefaultWalletClientNotFoundError(networkName);
}
return defaultWalletClient;
}
//# sourceMappingURL=contracts.js.map