I was going through truffle-contract docs and came across this function

  • MyContract.deployed()

As mentioned in docs this function create an instance of contract abstraction(abi) representing the contract at its deployed address. What is the deployed address here?

Is it the same address at which the contract is deployed on Ethereum or something else set internally in an application?

Second thing: if my contract is already deployed on an Ethereum testnet like Ropsten and it has a contract address, then do I have to run this function MyContract.deployed()?


What is the deployed address here?

If your contract isn't deployed yet, then the deployed address is not yet viable.

So you can do something like:

let myContract;
before(async function() {
    myContract = await MyContract.new(myArgs);

And then the deployed address is:

  • myContract.address if you're using web3 v0.x
  • myContract._address if you're using web3 v1.x

At present, Truffle uses web3 v0.x (v0.18.4 if I remember correctly).


MyContract.deployed() is meant to be used with contracts you have previously deployed to a network, most likely using truffle migrate.

If in your migration scripts you deployed contracts using deployer.deploy(), truffle migrate will modify your build artifacts (by default under the build/ directory) to contain deployment information under the .networks property.

MyContract.deployed() will use this information, assuming the contract has in fact been deployed to the same network as is currently being used by Truffle.

Note that you can interact with multiple networks using the --network flag and switch between artifacts for multiple networks with the --build-directory flag that most Truffle commands take.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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