TIP
Try Hardhat Ignition for your deployments! Our new declarative system for deploying smart contracts without getting caught up in execution details.
When it comes to deploying, you can use a deployment system like Hardhat Ignition or you can deploy your smart contracts using scripts. You can deploy the Lock
contract from the sample project with a deployment script like this:
TypeScript
JavaScript
import { ethers } from "hardhat";async function main() { const currentTimestampInSeconds = Math.round(Date.now() / 1000); const unlockTime = currentTimestampInSeconds + 60; const lockedAmount = ethers.parseEther("0.001"); const lock = await ethers.deployContract("Lock", [unlockTime], { value: lockedAmount, }); await lock.waitForDeployment(); console.log( `Lock with ${ethers.formatEther( lockedAmount )}ETH and unlock timestamp ${unlockTime} deployed to ${lock.target}` );}// 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;});
// We require the Hardhat Runtime Environment explicitly here. This is optional// but useful for running the script in a standalone fashion through `node <script>`.//// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat// will compile your contracts, add the Hardhat Runtime Environment's members to the// global scope, and execute the script.const hre = require("hardhat");async function main() { const currentTimestampInSeconds = Math.round(Date.now() / 1000); const unlockTime = currentTimestampInSeconds + 60; const lockedAmount = hre.ethers.parseEther("0.001"); const lock = await hre.ethers.deployContract("Lock", [unlockTime], { value: lockedAmount, }); await lock.waitForDeployment(); console.log( `Lock with ${ethers.formatEther( lockedAmount )}ETH and unlock timestamp ${unlockTime} deployed to ${lock.target}` );}// 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;});
You can deploy in the localhost
network following these steps:
Start a local node
npx hardhat node
Open a new terminal and deploy the smart contract in the
localhost
networknpx hardhat run --network localhost scripts/deploy.ts
npx hardhat run --network localhost scripts/deploy.js
As general rule, you can target any network from your Hardhat config using:
npx hardhat run --network <your-network> scripts/deploy.js
I have a deep understanding of smart contract deployment and development workflows, especially in the context of tools like Hardhat Ignition. The article you provided details the utilization of Hardhat Ignition for deploying smart contracts, offering both declarative deployment methods and script-based approaches. It focuses on deploying a "Lock" contract from a sample project using TypeScript and JavaScript.
The code snippets illustrate the deployment process, demonstrating the use of Hardhat's capabilities. They showcase the initialization of a contract deployment, specifying parameters like the unlock time, locked amount in Ether, and deploying the contract to a specified network. Furthermore, it emphasizes best practices such as error handling and asynchronous operations using async/await
.
The utilization of Hardhat commands like npx hardhat node
to start a local node for deployment in a localhost network and subsequent deployment commands (npx hardhat run --network localhost scripts/deploy.ts
and npx hardhat run --network localhost scripts/deploy.js
) further showcases the versatility of this deployment tool across different networks defined in the Hardhat configuration.
Now, breaking down the concepts mentioned in the article:
-
Smart Contract Deployment: The process of deploying immutable code to a blockchain network, enabling the execution of predefined operations as per the contract's logic.
-
Hardhat Ignition: A deployment system/tool that facilitates the deployment of smart contracts. It offers a declarative system to deploy contracts without getting mired in execution details, streamlining the deployment process.
-
Script-based Deployment: Utilizing scripts (in TypeScript or JavaScript) to automate the deployment process, defining parameters, interacting with the Hardhat framework, and executing deployment commands.
-
Lock Contract: Refers to a smart contract named "Lock" in the provided example, demonstrating the deployment process for this contract. It presumably has functionality related to locking funds for a specified duration.
-
TypeScript and JavaScript: Programming languages used to write scripts for smart contract deployment. TypeScript offers static typing, while JavaScript provides dynamic typing.
-
Localhost Network: A local blockchain network used for testing and development purposes. The article describes steps to deploy contracts to this network using Hardhat commands.
-
Hardhat Commands: Utilizing commands like
npx hardhat node
to start a local node andnpx hardhat run --network <network> <script>
to deploy contracts to specified networks configured within Hardhat.
Understanding these concepts enables developers to effectively utilize Hardhat Ignition for deploying smart contracts, whether on a local network for testing or on various other blockchain networks.