ReasonJun

Hardhat : ethers.getContractFactory() / deploy() / deployed() 본문

Blockchain/HardHat

Hardhat : ethers.getContractFactory() / deploy() / deployed()

ReasonJun 2023. 10. 20. 14:46
728x90
import { ethers } from "hardhat";

async function main() {
  console.log('deploying vendingMachine contract')
  const VendingMachine = await ethers.getContractFactory("VendingMachine");
  const vendingMachine = await VendingMachine.deploy();
  await vendingMachine.deployed();

  console.log(`vendingMachine contract is deployed to ${vendingMachine.address}`);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

This is a Hardhat deployment script that deploys a VendingMachine contract.

 

The first line imports the ethers library from the hardhat package.

The main() function first logs a message to the console indicating that the VendingMachine contract is being deployed.

 

The ethers.getContractFactory() method is then used to create a contract factory for the VendingMachine contract. The contract factory is a JavaScript object that can be used to deploy new instances of the VendingMachine contract.

 

The deploy() method is then called on the contract factory to deploy a new instance of the VendingMachine contract. The deploy() method returns a promise that resolves to the deployed contract object.

 

The deployed() method is then called on the deployed contract object to wait for the contract to be fully deployed. The deployed() method returns a promise that resolves when the contract is fully deployed.

 

Once the contract is fully deployed, the script logs the contract address to the console.

 

The main() function is then wrapped in a try/catch block to handle any errors that may occur during deployment.

 

This script can be run using the following command:

npx hardhat run scripts/deployVendingMachine.js

This will deploy a new VendingMachine contract to the network that Hardhat is connected to. The contract address will be logged to the console.

728x90
Comments