How to deploy smart contract on Goerli testnet with help of truffle and infura in detail

Before deploying smart contract on mainnet we have to deploy in testnet so we can test that our smart contract is working properly or not. we are going to deploy our smart contract on Goerli testnet. We are going to discuss below topics in this blog.

  • how to install truffle

  • create a project using truffle

  • create smart contract

  • compile smart contract

  • deploy smart contract on goerli testnet

I assume that you already created account on Metamask and Infura.

Step 1: Install truffle

Follow the below instruction to Install the truffle.

  • Open the command prompt.

  • Run the npm install -g truffle command.

image.png

Step 2: Initiating a project in Truffle

Follow below instruction for initiating a project in truffle.

  • Choose you project directory and create empty folder.

  • Now open that folder in vscode.

  • Open terminal and run truffle init command.

image.png

This command will create some files and folder in your working directory.

image.png

Step 4: Create and migrate smart contract in truffle

Follow below instruction for create smart contract.

  • Create Counter.sol file in contract directory.

  • Copy and paste below code in Counter.sol file

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.3;

contract Counter {
    uint public counter = 0;

    function Increment () public {
        counter++;
    }

    function Decrement () public {
        counter--;
    }

    function showCounter() public view returns(uint) {
        return counter;
    }
}
  • Create 1_counter_migration.js file inside migrations folder and copy paste below code.
const Coumter = artifacts.require('Coumter');

module.exports = function (deployer) {
    deployer.deploy(Coumter);
}
  • Now we are going to compile our smart contract for that run truffle compile command

image.png

Now we are going to deploy smart contract on Goerli testnet

  • Run npm install @truffle/hdwallet-provider command

image.png

this will install require packages and create node module folder on your root directory.

  • Now open truffle-config.js file. This file contain configurations of truffle and testnet.

  • Create .env file inside root directory and put MNEMONIC of your metamask wallet.

image.png

  • Now you have to uncomment below code inside truffle-config.js file.

image.png

  • Now run truffle migrate --network goerli command

image.png

Note: If you are getting error something like below while running truffle migrate --network goerli command

image.png

Then you have to replace code from this

provider: () => new HDWalletProvider(MNEMONIC,`https://goerli.infura.io/v3/${PROJECT_ID}`),

to this

provider: () => new HDWalletProvider(MNEMONIC,`wss://goerli.infura.io/ws/v3/${PROJECT_ID}`),
  • Now copy transaction hash.

image.png

image.png

  • You will get Detail of your transaction.

image.png

Now you successfully deploy your smart contract to goerli testnet.

0
Subscribe to my newsletter

Read articles from Jasmeen Maradeeya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jasmeen Maradeeya
Jasmeen Maradeeya

I am developer with over 5+ years of experience.