How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat

In this article I will teach you how to deploy your contract to the Rinkeby Testnet.

To be faster we will use our project from another article: How to create a smart contract to mint an nft

Creating an account and an Ethereum project in Infu…


This content originally appeared on DEV Community and was authored by Emanuel Ferreira

In this article I will teach you how to deploy your contract to the Rinkeby Testnet.

To be faster we will use our project from another article: How to create a smart contract to mint an nft

Creating an account and an Ethereum project in Infura

Infura provides instant access over HTTPS and WebSockets to the Ethereum network.

Access Infura

  1. Create your account (Common Steps).
  2. Click Ethereum, then create new project.

image

image

  1. Give a name for your project .
  2. Save your project id.

image

Initial settings

Returning to the project, let's make the initial settings for our project.

Let's go to the hardhat.config.js file and add an object called network along with another object called rinkeby containing url and accounts.

require("@nomiclabs/hardhat-waffle");

/**
 * @type import('hardhat/config').HardhatUserConfig
 */

module.exports = {
  solidity: "0.8.3",
  networks: {
   rinkeby: {
     url: "", //Infura url with projectId
     accounts: [""] // add the account that will deploy the contract (private key)
    },
  }
};

Now in url we add the url with the project id provided by Infura and in accounts we add the private address of our wallet.

Note: Be careful with your private key, it gives access to your wallet and will spend its crypto to deploy the contract.

require("@nomiclabs/hardhat-waffle");

/**
 * @type import('hardhat/config').HardhatUserConfig
 */

module.exports = {
  solidity: "0.8.3",
  networks: {
   rinkeby: {
     url: "https://rinkeby.infura.io/v3/ba900937b83f4883b926713999277b1f", //Infura url with projectId
     accounts: ["123456789101112131415"] // add the account that will deploy the contract (private key)
    },
  }
};

Creating the script to deploy

Now to deploy the smart contract to rinkeby testnet, we are going to make a script with the hardhat that will make it easier for us to upload it with a command.

In the root directory, create a folder called scripts and inside a file called deploy.js

/node_modules
/contracts
/test
/scripts
 | - deploy.js
hardhat.config.js
package.json
yarn.lock

Now we're going to modify the file by adding the script below, where I'll explain it line by line.

deploy.js

const hre = require("hardhat"); //import the hardhat

async function main() {
  const [deployer] = await ethers.getSigners(); //get the account to deploy the contract

  console.log("Deploying contracts with the account:", deployer.address); 

  const FactoryNFT = await hre.ethers.getContractFactory("FactoryNFT"); // Getting the Contract
  const factoryNFT = await FactoryNFT.deploy(); //deploying the contract

  await factoryNFT.deployed(); // waiting for the contract to be deployed

  console.log("FactoryNFT deployed to:", factoryNFT.address); // Returning the contract address on the rinkeby
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  }); // Calling the function to deploy the contract 

Now to deploy your contract just run this commands:

yarn compile 
yarn hardhat run scripts/deploy.js --network rinkeby

result

Deploying contracts with the account: 0xAFEeb469Ce6376979Ea037b4CE6b7172f9018007
FactoryNFT deployed to: 0xF9e6b75cE8da147D4f1d7d2cc597A7A42A645006


This content originally appeared on DEV Community and was authored by Emanuel Ferreira


Print Share Comment Cite Upload Translate Updates
APA

Emanuel Ferreira | Sciencx (2021-09-17T11:54:02+00:00) How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat. Retrieved from https://www.scien.cx/2021/09/17/how-to-deploy-a-smart-contract-to-rinkeby-testnet-using-infura-and-hardhat/

MLA
" » How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat." Emanuel Ferreira | Sciencx - Friday September 17, 2021, https://www.scien.cx/2021/09/17/how-to-deploy-a-smart-contract-to-rinkeby-testnet-using-infura-and-hardhat/
HARVARD
Emanuel Ferreira | Sciencx Friday September 17, 2021 » How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat., viewed ,<https://www.scien.cx/2021/09/17/how-to-deploy-a-smart-contract-to-rinkeby-testnet-using-infura-and-hardhat/>
VANCOUVER
Emanuel Ferreira | Sciencx - » How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/17/how-to-deploy-a-smart-contract-to-rinkeby-testnet-using-infura-and-hardhat/
CHICAGO
" » How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat." Emanuel Ferreira | Sciencx - Accessed . https://www.scien.cx/2021/09/17/how-to-deploy-a-smart-contract-to-rinkeby-testnet-using-infura-and-hardhat/
IEEE
" » How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat." Emanuel Ferreira | Sciencx [Online]. Available: https://www.scien.cx/2021/09/17/how-to-deploy-a-smart-contract-to-rinkeby-testnet-using-infura-and-hardhat/. [Accessed: ]
rf:citation
» How to deploy a Smart Contract to Rinkeby Testnet using Infura and Hardhat | Emanuel Ferreira | Sciencx | https://www.scien.cx/2021/09/17/how-to-deploy-a-smart-contract-to-rinkeby-testnet-using-infura-and-hardhat/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.