Rahul Rathod
Raaulc's Blog

Raaulc's Blog

4. Running the smart contract

4. Running the smart contract

Rahul Rathod's photo
Rahul Rathod
·Oct 3, 2021·

2 min read

So, We have created our smart contract! Now, We need to run it, that too we will do in three steps: Compile. Deploy. Run. Go into the scripts directory and make a file named run.js. This is what run.js is going to have inside it:

const main = async () => {
  const nftContractFactory = await hre.ethers.getContractFactory('HelloNFT');
  const nftContract = await nftContractFactory.deploy();
  await nftContract.deployed();
  console.log("Contract deployed to:", nftContract.address);
};

const runMain = async () => {
  try {
    await main();
    process.exit(0);
  } catch (error) {
    console.log(error);
    process.exit(1);
  }
};

runMain();

Let's go line by line here.

const nftContractFactory = await hre.ethers.getContractFactory("HelloNFT");

This will compile our contract and generate the necessary files we need to work with our contract.

const nftContract = await nftContractFactory.deploy();

Hardhat will create a local Ethereum network for us, but just for this contract. Then, after the script completes the local network will go to garbage collection. So, every time you run the contract, it'll be a new blockchain.

await nftContract.deployed();

just the process of actually deploying contracts with fake miners created by hardhat.

console.log("Contract deployed to:", nftContract.address);

Now it is deployed nftContract.address which will give us the address of the deployed contract. This address is the address of our smart contract.

Now let's run it:-

npx hardhat run scripts/run.js

Screenshot 2021-10-04 at 4.14.31 AM.png

and you will get the address! Tada !!!!

Next in line - We're going to mint some NFTs :)

 
Share this