How to Get the Deployed Contract Address in Truffle?
To get the deployed smart contract address in the truffle follow the below steps:
- Initialize the truffle project in Vs-code.
- Deploy the smart contract.
- If the smart contract is successfully compiled and deployed, one can find the deployed smart contract address in the build .json files.
- The deployed smart contract address can also be located in the console log.
- 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.
Step 6: Create a .env file to store the private and API keys.
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:
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
Step 11: Open the EmpDetails.json file and find the deployed contract address.
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();