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
UBS Tokenize
When your spouse dies do you get their Social Security benefits?
Spectrum Gdvr-2007
Bubble Guppies Who's Gonna Play The Big Bad Wolf Dailymotion
Lexi Vonn
Seething Storm 5E
Plus Portals Stscg
Horned Stone Skull Cozy Grove
Garrick Joker'' Hastings Sentenced
All Obituaries | Ashley's J H Williams & Sons, Inc. | Selma AL funeral home and cremation
Craigslist Free Grand Rapids
Capitulo 2B Answers Page 40
Oxford House Peoria Il
What to do if your rotary tiller won't start – Oleomac
Bernie Platt, former Cherry Hill mayor and funeral home magnate, has died at 90
24 Best Things To Do in Great Yarmouth Norfolk
Nashville Predators Wiki
The Ultimate Style Guide To Casual Dress Code For Women
Florida History: Jacksonville's role in the silent film industry
Schedule An Oil Change At Walmart
Persona 5 Royal Fusion Calculator (Fusion list with guide)
Phoebus uses last-second touchdown to stun Salem for Class 4 football title
Contracts for May 28, 2020
Possum Exam Fallout 76
Mchoul Funeral Home Of Fishkill Inc. Services
Darktide Terrifying Barrage
United E Gift Card
Franklin Villafuerte Osorio
Walter King Tut Johnson Sentenced
Weekly Math Review Q4 3
Diana Lolalytics
Ni Hao Kai Lan Rule 34
Seymour Johnson AFB | MilitaryINSTALLATIONS
Etowah County Sheriff Dept
Bismarck Mandan Mugshots
Stanford Medicine scientists pinpoint COVID-19 virus’s entry and exit ports inside our noses
Craigslist En Brownsville Texas
Busted Newspaper Mcpherson Kansas
Fluffy Jacket Walmart
Zeeks Pizza Calories
25 Hotels TRULY CLOSEST to Woollett Aquatics Center, Irvine, CA
Jackerman Mothers Warmth Part 3
Tanger Outlets Sevierville Directory Map
Diesel Technician/Mechanic III - Entry Level - transportation - job employment - craigslist
O.c Craigslist
The Significance Of The Haitian Revolution Was That It Weegy
Att Corporate Store Location
Bloons Tower Defense 1 Unblocked
Wayward Carbuncle Location
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.