Upgrade your Smart Contract Deployment Script; impress the shi.t out!
Tags: blockchain, web3, codenewbie, tutorial
Published: 2022-05-12 06:21:10

This write-up takes into account our two earlier write-up: [Part 1](https://medium.com/uv-labs/creating-and-deploying-your-first-smart-contract-466b4d4784e2) and [Part 2](https://medium.com/uv-labs/dude-i-just-deployed-my-own-nft-wt-f-9ef91c778af); wherein we have been learning about deployment of smart contracts.

This particular write-up is with the intention of demonstrating a better way for deployment of smart contracts. You can look at this new way of deployment of smart contracts, as an optional strategy; however, if you are shaping your career to become a professional smart contract developer — then I would recommend you to keep this is as a MUST Learn practice.

**RECAP**

Till now, we have been deploying smart contracts, using a simple script in our: `/scripts` folder. Our script to deploy our last NFT Smart Contract was as follows:

```
const { ethers } = require("hardhat");async function main() {
    const NFT = await ethers.getContractFactory("NFT");
    const deployedNFT = await NFT.deploy();
    console.log(`Contract deployed to address:          ${deployedNFT.address}`)
}main()
.then(() => process.exit(0))
.catch((error) => {
    console.error(error);
    process.exit(1);
});
```

**Time to UPGRADE!**

**_First_**: We are going to install hardhat-deploy from here. Depending on what you use, it will be either npm install or yarn add.

**_Second_**: Add require('hardhat-deploy') to our file hardhat-config.js

Our updated config file will look like this:

```

require('dotenv').config();
require('@nomiclabs/hardhat-ethers');
require('hardhat-deploy'); // <--- This is the new additionconst {API_URL, PRIVATE_KEY} = process.env;
....
....
```

**_Third_**: Create a new folder deploy in the root of the project and add the following file to it. Note the name of the file. It is critical.

```
// location of the file: ./deploy/00_deploy_NFT.jsconst func = async function (hre) {
  const { getNamedAccounts, deployments } = hre;
  const { deployer } = await getNamedAccounts();
  const { deploy } = deployments;
  await deploy("NFT", {
    from: deployer,
    log: true,
  });
}func.tags = ["NFT"];module.exports = func;
```

Your folder structure should look like this after the addition of the above file:


![Folder Structure](https://miro.medium.com/max/900/1*DGEtKzyzZrZOROJMvhg51Q.png)

**_Voilà_**! You have upgraded yourself 🥳🥳🥳🥳🥳

Let’s see the magic of what we have done.

1. Open a terminal in the location of the folder and run the command: npx hardhat node —-no-deploy This will spin-off a hardhat node with a number of accounts that are to be used for the purpose of deployments

2. Open a new terminal in the same location of the folder and now run the following command: `npx hardhat deploy —-network localhost`

3. If all is done successfully, you should see the following in your terminal:

```
deploying "NFT" (tx: 0x5c8faff64acb214523566f9fa0d1168159c0c380d71cc5486828e34aff085a19)...: deployed at 0x5FbDB2315678afecb367f032d93F642f64180aa3 with 2525550 gas
```

AND, you will see a new folder in your directory structure with the name deployments. Something like this:

![Folder Structure](https://miro.medium.com/max/912/1*k0tZzdHZEln7sOq2cu8fUg.png)

Dig into the files in the `deployments` folder and you will notice that it has saved the address to which the contract has been deployed, along with the other details. The details saved in the deployments folder are per chain to which the contracts are deployed.

We ran another command in our terminal `npx hardhat deploy --network mumbai-matic` to deploy our contract to the Mumbai Matic test network. Our directory structure afterwards was as follows:

![Folder Structure](https://miro.medium.com/max/912/1*3OlDXE8UgPCz2rJaizfeUA.png)

As you will notice, we now have a new folder with the name mumbai-matic and also have the details saved in that directory.

There you go!!! You are now a better version of a blockchain developer.

We hope you like what we are doing over here. If you do, please do clap 👏 at the end of the article and yes, do share with your friends.

Also note: You can download and run the same code that we will using in our tutorial (aka tuts) from here 👉 [Github repository](https://github.com/UV-Labs/Tutorials). If you like out stuff, give us a star 🌟 on Github too.

Happy Coding!!!

Authors (open to feedback): 👇

[Amateur-Dev](https://medium.com/u/9cd26cbd96b9?source=post_page-----5311a13d173-----------------------------------) and [Pari Tomar](https://medium.com/u/130345445971?source=post_page-----5311a13d173-----------------------------------)