10

Probably this is not possible, but I still have to ask.

Let me follow the example created in truffle init. It contains a contract called MetaCoin.sol. Say,

  1. I have deployed it on the network (the first version of it).
  2. Then I have extended it with a foo() method.
  3. Now I want to deploy a new modified version of the contract with the same name MetaCoin but the new code of it. Is there a way of doing it?

As the hint there is a contract called Migrations.sol which has a method called upgrade():

function upgrade(address new_address) restricted { Migrations upgraded = Migrations(new_address); upgraded.setCompleted(last_completed_migration); }

that sets a new address for the code of the contract. Is there a better way of doing versioning or upgrades (deploy rollouts) of the coins code?

4 Answers4

18

you should use truffle(.cmd) migrate --reset to redeploy it on the network.
everytime you run migrate an new address and new contract is created.

(sorry reputatio too low to comment)

OfficialAllGood
  • 330
  • 2
  • 5
  • 2
    I've upvoted this answer to give you +10 rep. – Badr Bellaj Jul 14 '17 at 15:02
  • 1
    reset sounds like the wrong name :/ but I think it's the way to go. What if I have 10 contracts and I only changed 1, would reset deploy 10 of them or the one changed? – EralpB Sep 01 '17 at 08:29
  • marking as an answer as it seems a correct one after some time of working with truffle framework. I would in general recommend another security framework called https://openzeppelin.org/ – Yauhen Yakimovich Dec 27 '17 at 09:29
3

Since google lists this question pretty far on top by searching for "truffle deploy new contract" and accepted answer does redeploy all(!) contracts I post following solution for redeploying a single contract:

You need to use both flags --f NUMBER and --to NUMBER with the command "truffle migrate" (look here)

Example, for redeploying a contract with the migration file "5_mycontract_migration.js":

truffle migrate --f 5 --to 5
1

To redeploy a single contract MyContract:

  • create a new migration with truffle create migration my_new_migration_name
  • in the new migration file (found in the migrations folder), you can redeploy using deployer.deploy(MyContract)

You can also use the truffle deploy "force" feature: truffle deploy -f migration_name reruns a specific migration

Adil
  • 396
  • 1
  • 8
0

You can not "update" a contract, once you deploy it , it is in the chain block.

But you can redeploy it (with a new contract address).

Use truffle migrate --reset to recreate already migrated contract.
Or just copy the old migration file and rename it to a new migration.

Evol Rof
  • 101
  • 2