Open In App

Deploying Smart Contract on Test/Main Network Using Truffle

Last Updated : 09 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Smart Contract is a computer program that directly and automatically controls the transfer of digital assets between the parties under certain conditions. A smart contract works in the same way as a traditional contract while also automatically enforcing the contract. Smart contracts are programs that execute exactly as they are set up(coded, programmed) by their creators. Just like a traditional contract is enforceable by law, smart contracts are enforceable by code.

Up to this point, we have our smart contracts written and compiled without any errors. Let’s see how we can deploy our smart contract on Goerli test network. Deploying a contract on the main blockchain costs money (in the form of cryptocurrency), so we have test networks to develop and test our smart contracts. In the test network, fake ethers are added to our wallet to facilitate transactions. One such network provided by Ethereum is Goerli.

Installing MetaMask

MetaMask provides browser support to communicate with blockchain networks. We can add an account, get ethers to our account or send transactions to another account. It is like a wallet from where we can spend our cryptocurrency. To install metamask, 

  1. Go to https://metamask.io/ 
  2. Download it to your browser.

On starting for the first time, to set up your wallet MetaMask gives you a seed phrase of 12 words. This phrase is unique and can be used to restore your account, in case you forgot your password.

Getting Test Ethers

Deploying contracts on the test network requires some test ethers, which, obviously does not have any real value. In the blockchain, every interaction with a contract costs some fees(gas) and the interaction is known as a transaction. Since deploying our contract on Goerli is also a transaction, we need some test ethers to facilitate the transaction. To get test ethers we will be using a Faucet.  Go to https://goerlifaucet.com/ and do the simple task copy the Ethereum Wallet Address from Metamask and paste it into Goerli Faucet,then click the “Send Me ETH” button.

Goerli Faucet :- 

Goerli Faucet

Creating Infura Endpoint 

We need an API through which we can access the Goerli test network. Infura makes it quite easier to access the test/main network and deploy our contract on them. Create an infura endpoint on the Goerli test network. 

  • Go to infura.io and sign up for an account if you don’t have one.
  • Click on Ethereum on the left panel.
  • Write your project name and click to create the project.
  • This should create an endpoint for your project and it should look somewhat similar to below.

Now we need some npm packages like dotenv and HDWalletProvider (this signs our transaction and makes the deployment process a less pain. )

npm install dotenv
npm install @truffle/hdwallet-provider

Configuring truffle-config.js And Deploying To Goerli test network

Now we have, our truffle hdWallet-provider in our node modules, an endpoint from infura account, and the private -key  of the Account Address which we have imported from Metamask Wallet.

Let’s configure our truffle-config.js.  Open truffle-config.js and import truffle hdWallet provider and dotenv package at the top.

require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');

Create a .env file and import the private key of your Account Address and other API keys like Infura, Etherscan etc. For some security reasons we can not import these keys directly into the truffle-config.js file.

.env file :- 

INFURA_API_KEY=”Insert the API key from your created Infura Account”
PRIVATE_KEY=”Insert the private key from your Metamask Wallet Address”
ETHERSCAN_API_KEY="Insert the API key from your created Etherscan Account"

truffle Project Setup

 

To configure the network, we will edit the network section of truffle-config.js. After updating the truffle-config.js file, it should look somewhat like this:-

truffle-config.js file :-

Javascript




require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
 
//to fetch these keys from .env file
const privateKey = process.env.PRIVATE_KEY;
const infura_api_key = process.env.INFURA_API_KEY;
const etherscan_api_key = process.env.ETHERSCAN_API_KEY;
 
module.exports = {
  plugins: [
   'truffle-plugin-verify'
 ],
 api_keys: {
    etherscan: etherscan_api_key
  },
 
  networks: {
    goerli: {
      provider: () => new HDWalletProvider(privateKey, `https://goerli.infura.io/v3/${infura_api_key}`),
      network_id: 5, //Goerli's id
      gas: 5000000, //gas limit
      confirmations: 1,    // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
     },
  },
 
  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },
 
  // Configure your compilers
  compilers: {
    solc: {
      version: "^0.8.12",    // Fetch exact version from solc-bin (default: truffle's version)
      settings: {          // See the solidity docs for advice about optimization and evmVersion
        optimizer: {
          enabled: true,
          runs: 200
        },
        evmVersion: "istanbul"
      }
    }
  },
  db: {
    enabled: false
  }
};



truffle-config.js file

Save the file and then open the truffle console and then migrate on the Goerli test network.

truffle migrate --network goerli

That’s all done. Once your contract is deployed on goerli, you can check the transaction detail on etherscan.  Go to the link and copy and paste your deployed contract address to get the details about your transaction.

Note:

  1. To deploy on the main network, get infura endpoint for the main network, and configure the config file.
  2. Replace goerli with main, and network id with 1.
  3. Then truffle migrate –network main.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads