>
>
Complete the 2023 Solidity Survey here
>
>

#Deploying your contracts

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:

  1. Start a local node

    npx hardhat node
    
  2. Open a new terminal and deploy the smart contract in the localhost network

    TypeScript
    JavaScript
    npx 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