How to Get the Deployed Contract Address in Truffle? - GeeksforGeeks (2024)

Last Updated : 02 Dec, 2022

Summarize

Comments

Improve

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:

How to Get the Deployed Contract Address in Truffle? - GeeksforGeeks (1)

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

How to Get the Deployed Contract Address in Truffle? - GeeksforGeeks (2)

Deployed Contract Address

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

How to Get the Deployed Contract Address in Truffle? - GeeksforGeeks (3)

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

How to Get the Deployed Contract Address in Truffle? - GeeksforGeeks (4)

Truffle Console



Please Login to comment...

How to Get the Deployed Contract Address in Truffle? - GeeksforGeeks (2024)
Top Articles
WEBTOON Originals contract draws criticism from creators
3 Artificial Intelligence (AI) Stocks to Buy Now and Hold for Decades | The Motley Fool
Radikale Landküche am Landgut Schönwalde
Kathleen Hixson Leaked
Room Background For Zepeto
La connexion à Mon Compte
Es.cvs.com/Otchs/Devoted
Sam's Club Gas Price Hilliard
How To Get Free Credits On Smartjailmail
Which aspects are important in sales |#1 Prospection
8 Ways to Make a Friend Feel Special on Valentine's Day
United Dual Complete Providers
Top tips for getting around Buenos Aires
Fool’s Paradise movie review (2023) | Roger Ebert
Q Management Inc
Spider-Man: Across The Spider-Verse Showtimes Near Marcus Bay Park Cinema
How to Watch the Fifty Shades Trilogy and Rom-Coms
Why Does Lawrence Jones Have Ptsd
Epguides Strange New Worlds
Program Logistics and Property Manager - Baghdad, Iraq
Ac-15 Gungeon
Form F-1 - Registration statement for certain foreign private issuers
Shadbase Get Out Of Jail
F45 Training O'fallon Il Photos
Foodsmart Jonesboro Ar Weekly Ad
Darrell Waltrip Off Road Center
How do you get noble pursuit?
Angel Haynes Dropbox
Weather October 15
Hwy 57 Nursery Michie Tn
Kleinerer: in Sinntal | markt.de
Sinai Sdn 2023
Tire Pro Candler
The Menu Showtimes Near Amc Classic Pekin 14
JD Power's top airlines in 2024, ranked - The Points Guy
Old Peterbilt For Sale Craigslist
Hair Love Salon Bradley Beach
AP Microeconomics Score Calculator for 2023
Ket2 Schedule
The 38 Best Restaurants in Montreal
Go Smiles Herndon Reviews
Bitchinbubba Face
18006548818
Pathfinder Wrath Of The Righteous Tiefling Traitor
Southwest Airlines Departures Atlanta
Costner-Maloy Funeral Home Obituaries
Lightfoot 247
Phunextra
Cars & Trucks near Old Forge, PA - craigslist
Ark Silica Pearls Gfi
Escape From Tarkov Supply Plans Therapist Quest Guide
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 5715

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.