[Ethereum] how to know the contract address which truffle is deploying with


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?

Best Answer

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) {

    // 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));
Related Topic