Hardhat
/
Deploy Scripts

Deploy Script

Inside the hardhat/deploy folder are where deploy scripts are run from. As you see, there is a script called 00_deploy_contract.js

module.exports = async ({ getNamedAccounts, deployments }) => {
  const { deploy } = deployments;
  const { deployer } = await getNamedAccounts();

  const args = ["Hello!!!!!!!!"];

  await deploy("Greeter", {
    // Learn more about args here: https://www.npmjs.com/package/hardhat-deploy#deploymentsdeploy
    args: args,
    from: deployer,
    log: true,
  });
};
module.exports.tags = ["all", "greeter"];

The const deployer is targeting the hardhat.config.js and looking at the deployer in the namedAccounts

namedAccounts: {
  deployer: {
    default: 0, // here this will by default take the first account as deployer
  },
  tokenOwner: 1,
},

deployer.default is set to 0 meaning the Account #0 in your local hardhat network when you run chain

The args array passes arguments into the the Greeter.sol contract's constructor.

constructor(string memory _greeting) {
    greeting = _greeting;
}

This constructor takes a single string arugment. Passing ["Hello!!!!!!!!"], sets the

string private greeting;

to "Hello!!!!!!!!"

To deploy another contract, create another file incrementing the filename ie 01_deploy_another_contract

For deploying contracts, tags help target contracts you'd like to deploy. So in the 00_deploy_contract.js file:

module.exports.tags = ["all", "greeter"];

The 'all' tag will call this script for when using yarn deploy.

To target just this contract to deploy, use the flag greeter yarn deploy --tags greeter

When you run the deploy script, each deploy script will run async from lowest number to highest. This gives the advantage to deploy multiple contracts and using a previous deployed contact's address in a script to pass into another contract.

So in this example if you wanted to get the contract address from the Greeter contract you could use

const greeterAddress = await deployments.get("Greeter");

To get the contract data.

And then pass greeterAddress.address into the args like so

module.exports = async ({ getNamedAccounts, deployments }) => {
  const { deploy } = deployments;
  const { deployer } = await getNamedAccounts();

  const greeterAddress = await deployments.get("Greeter");

  const args = [greeterAddress.address];

  await deploy("Another", {
    // Learn more about args here: https://www.npmjs.com/package/hardhat-deploy#deploymentsdeploy
    args: args,
    from: deployer,
    log: true,
  });
};
module.exports.tags = ["all", "another"];

You can also create an instance of the previous contract to get the address with ethers.

module.exports = async ({ getNamedAccounts, deployments }) => {
  const { deploy } = deployments;
  const { deployer } = await getNamedAccounts();

  const greeter = await ethers.getContract("Greeter");

  const args = [greeter.address];

  await deploy("Another", {
    // Learn more about args here: https://www.npmjs.com/package/hardhat-deploy#deploymentsdeploy
    args: args,
    from: deployer,
    log: true,
  });
};
module.exports.tags = ["all", "another"];