9

I am using

truffle migrate --reset --compile-all

to deploy the contract to local network

but in order to use new web3.eth.Contract(jsonInterface[, address][, options]), i should be able to know the contract address,

so how could know the address which truffle is deploying?

3 Answers 3

25

There are a few ways to do this:

1) In your blockchain node (testrpc|ganache or your test/live network with geth/parity), the contract deployments will be logged and you can see the address created then

Transaction: 0xc2471aa6d1e020921d41247ac2a86eb5ad2447e93450347365a25f8d632e34bd
Contract created: 0x98445ab3eaafdd2293981525631730c64adec41a // <--- contract address
Gas usage: 245439
Block Number: 20
Block Time: Thu Dec 28 2017 19:43:44 GMT+0800 (+08)

2 & 3) You can get the address via the artifact after the contract is deployed, either from the artifact directly or by using the artifact and getting the contract instance.

Example: log from deployment file: (migrations/2_deploy_contracts.js)

(Note: I've used the deployment file as the example, but you can retrieve the address from any javascript file where you've imported and made available the artifact)

var SimpleStorage = artifacts.require("./SimpleStorage.sol");

module.exports = function(deployer) {
  deployer.deploy(SimpleStorage)

    // Option 2) Console log the address:
    .then(() => console.log(SimpleStorage.address))

    // Option 3) Retrieve the contract instance and get the address from that:
    .then(() => SimpleStorage.deployed())
    .then(_instance => console.log(_instance.address));
};
0
2

If the contract is being deployed you can obviously read it from the log. What if the contract has deployed but you did not take a record of it? There are a few ways to do it:

1. Using etherscan (or Rinkeby scan, Ropsten scan etc..) and search your wallet address used for deployment of the contract. But what if I don't recall the wallet address used? You can get it from truffle.js (or truffle-config.js) in your project folder. Or 
2. You can reconnect to the deployed contract using truffle and retrieve the address:  
$truffle console --network rinkeby
truffle(rinkeby)>myToken = await MyToken.deployed()
truffle(rinkeby)>await myToken.address

This will do it! Remember to replace "MyToken" with your token's contract name, and replace "rinkeby" with your network name such as: ropsten etc..

0

You can find the contract address in your build .json files. For example build/contracts/deployedContract.json Just search the file for address.

If you deployed the contract and lost the address/build.json you can simply retrieve it by truffle commands. You just need the name of the deployed contract.

For example, I deployed NewContract but lost his address.

truffle console --network private

truffle(private)> 

truffle(private)> let contract = await NewContract.deployed()

truffle(private)> contract.address
'0xb0B8956f1B95a562bC7CF3e77Ec8BeA712321315'

And that is it, you have your address

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.