DEV Community

Cover image for How to deploy a Smart Contract to the Testnet
Emanuel Ferreira
Emanuel Ferreira

Posted on • Updated on

How to deploy a Smart Contract to the Testnet

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)
    },
  }
};
Enter fullscreen mode Exit fullscreen mode

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)
    },
  }
};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 

Enter fullscreen mode Exit fullscreen mode

Now to deploy your contract just run this commands:

yarn compile 
Enter fullscreen mode Exit fullscreen mode
yarn hardhat run scripts/deploy.js --network rinkeby
Enter fullscreen mode Exit fullscreen mode

result

Deploying contracts with the account: 0xAFEeb469Ce6376979Ea037b4CE6b7172f9018007
FactoryNFT deployed to: 0xF9e6b75cE8da147D4f1d7d2cc597A7A42A645006
Enter fullscreen mode Exit fullscreen mode

Contract deployed!!!

Now you can Follow me on: Twitter
Project Repository: https://github.com/EmanuelCampos/mint-nft/tree/with-deploy-config

Top comments (13)

Collapse
 
rebiacom profile image
Leonardo Mocaiber

Hi, When I try run the compile I'm receiver a error:
"Error HH8: There's one or more errors in your config file:

  • Invalid account: #0 for network: rinkeby - private key too short, expected 32 bytes To learn more about Hardhat's configuration, please go to hardhat.org/config/" Like the url rinkeby dont exist and when I'm try open the rinkeby url return 404. Any tips that can help me out? Thanks, congratulations great article.
Collapse
 
emanuelferreira profile image
Emanuel Ferreira

Hey, are you using the correct private key of your wallet? it's not your address, but your private key

Collapse
 
gitatron profile image
Gitatron Maximus

I had the error like this:

Error HH8: There's one or more errors in your config file:

  • Invalid account: #1 for network: sepolia - private key too long, expected 32 bytes To learn more about Hardhat's configuration, please go to hardhat.org/config/

//Turns out I put a space after the comma when I was listing the accounts. I took the space out and the error resolved.

Collapse
 
mattheweller profile image
Matthew Eller

NGL, I ran into this issue and it took me a few minutes to realize that I was calling the run scripts from the wrong directory...

Collapse
 
theantipioneer profile image
Atang

Save the private key in full with 0x prefix as an environment variable and then pass it in with the method .toString().

process.env.PRIVATE_KEY.toString()

Collapse
 
zlatnaspirala profile image
Nikola

Hi !
I got `The Solidity version pragma statement in these files doesn't match any of the configured compilers in your config. Change the pragma or configure additional compiler versions in your hardhat config.

  • contracts/factoryNFT.sol (^0.8.3)
  • @openzeppelin/contracts/token/ERC721/ERC721.sol (^0.8.0)` etc...

Any suggestion ?

Collapse
 
saurabh619 profile image
Saurabh Bomble

Hey @emanuelferreira , great article.
I am getting an error while deploying to the ropsten network

"Cannot read properties of null (reading 'sendTransaction')"

I have used private key exported from metamask as you asked. I could use some help.

Collapse
 
rackar profile image
yang • Edited

Thanks. What is rinkeby.accouts? "in accounts we add the private address of our wallet." I don't know it is an address or a key? If is private key, how does it transfer to rinkeby? Is there a leak risk?

Collapse
 
emanuelferreira profile image
Emanuel Ferreira

The private key, to deploy the contract using your address/wallet

Collapse
 
cocafe profile image
cocafe.crypto

Hi,

I got the error "Error HH100: Network rinkeby doesn't exist" when running the command to deploy the contract.

What can I do to fix this error?

Collapse
 
cybergypsy14 profile image
Zaigam Akhtar

I got the same error. Somebody please reply?

Collapse
 
keijo_keijonen_94346a53d0 profile image
Keijo Keijonen

what is yarn compile

Collapse
 
emanuelferreira profile image
Emanuel Ferreira

Yarn compile is a script of hardhat to compile the smart contract.
in the package.json file you can add a script called compile with this command:
yarn hardhat compile