Open In App

How to Get the Deployed Contract Address in Truffle?

Improve
Improve
Like Article
Like
Save
Share
Report

To get the deployed smart contract address in the truffle follow the below steps:

  1. Initialize the truffle project in Vs-code.
  2. Deploy the smart contract. 
  3. If the smart contract is successfully compiled and deployed, one can find the deployed smart contract address in the build .json files. 
  4. The deployed smart contract address can also be located in the console log. 
  5. The truffle console can also be used if deployed smart contract address is lost. With the help of the truffle console, one can easily interact with the smart contract and get the deployed smart contract address.

Implementation:  This example includes using the truffle console to get the deployed smart contract address. 

Step 1: Open Vs-code.

Step 2:  Create a folder.

$ mkdir GFG

Step 3: Install the truffle.

$ npm install -g truffle

Step 4: Initialize the truffle project.

$ truffle init

Step 5: To create a smart contract file with a .sol extension in the contracts folder, and create a 1_deploy_contracts.js file in the migrations folder. Here we are using the Goerli test network to deploy the smart contract, So we need to add the Goerli test network details in the truffle-config.js file. 

EmpDetails.sol: Below is the smart contract to store the employee details.

Solidity




// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
/// @title A contract for storing the record of employee
/// @author Jitendra Kumar
/// @notice For now, this contract just add 
/// the basic details of employee
contract EmpDetails{
    // Create a structure for employee
    struct Employee{
        string Name;
        uint EmpId;
        uint Age;
        string addrs;
    }
    // Employee --> DataType and EmployeeDetails --> Variable
    Employee public EmployeeDetails;
    // Create a Function for adding the Employee details
    function addEmployeeDetails() public{
        
        // Adding Emplyee Name
        EmployeeDetails.Name="Jitendra";
        
        // Adding Employee EmpId
        EmployeeDetails.EmpId=1231;
        
        // Adding Employee Age
        EmployeeDetails.Age=24;
          
        // Adding Employee Address
        EmployeeDetails.addrs="Bareilly";
    }
}


1_deploy_contracts.js:

Javascript




const  empDetails= artifacts.require("EmpDetails");
module.exports = function (deployer) {
  deployer.deploy(empDetails);
};


truffle-config.js file:

Javascript




require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
  
const privateKey = process.env.PRIVATE_KEY;
const alchemy_api_key = process.env.ALCHEMY_API_KEY;
  
module.exports = {
   plugins: [
    'truffle-plugin-verify'
  ],
  networks: {
      goerli: {
       provider: () => new HDWalletProvider(privateKey, `https://eth-goerli.g.alchemy.com/v2/${alchemy_api_key}`),
       network_id: 5,
       confirmations: 1,    
       timeoutBlocks: 200,  
       skipDryRun: true     
      }
  },
  mocha: {
      
  },
  
  compilers: {
    solc: {
      version: "^0.8.16",   
      settings: {          
        optimizer: {
          enabled: true,
          runs: 200 
        },
        evmVersion: "byzantium"
      }
    }
  },
  db: {
    enabled: false
  }
};


Step 6: Create a .env file to store the private and API keys. 

.env file: 

ALCHEMY_API_KEY=”Insert the API key from your created Alchemy Application”

PRIVATE_KEY=”Insert the private key from your Metamask Wallet Address”

The project file structure looks like this:

.env file

Project File Structure

Step 7: Put all the files in the given locations or folders and execute the below commands to deploy the smart contract

Step 8: Install the dotenv package.

$ npm i dotenv

Step 9: To compile the smart contract.

$ truffle compile 

Step 10: To deploy the smart contract and find the deployed contract address in the console log.

$ truffle deploy –reset –network goerli

EmpDetails.sol

Deployed Contract Address

Step 11: Open the EmpDetails.json file and find the deployed contract address.

Contract Address in EmpDetails.sol

EmpDetails.json file 

Step 12: Open the truffle console to interact with the smart contract and find the lost deployed smart contract address.

$ truffle console –network goerli

truffle(goerli)>let myContract=await EmpDetails.deployed();

truffle(goerli)>myContract.address

Contract Address

Truffle Console 



Last Updated : 02 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads