0

what does "Replacing" mean in migration?

a simple test contract C.sol:

pragma solidity ^0.4.17;

contract C{
    uint public ConstructorUint;

    function C(uint SetConstructorUint)public{
        ConstructorUint = SetConstructorUint;
    }
} 

the Migration file 99_migration.js:

var Cnumber0 = artifacts.require("C"); 
var Cnumber1 = artifacts.require("C");
var Cnumber2 = artifacts.require("C"); 
var Cnumber3 = artifacts.require("C"); 
var Cnumber4 = artifacts.require("C"); 
var Cnumber5 = artifacts.require("C"); 

module.exports = function(deployer) {
    deployer.deploy(Cnumber0, 0);
    deployer.deploy(Cnumber1, 1);
    deployer.deploy(Cnumber2, 2);
    deployer.deploy(Cnumber3, 3);
    deployer.deploy(Cnumber4, 4);
    deployer.deploy(Cnumber5, 5);    
}

the truffle.js:

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    }
  }
};

upon starting up Ganache and running in the terminal.

$ truffle migrate --network development

I find that create contracts happened, as expected.

in the terminal running.

$ truffle networks

I find only One C contract with the last at address of the C contracts. starting over by closing Ganache and running

$ truffle networks --clean

this time I try

$ truffle develop

this opens the truffle console where from the truffle console

truffle(develop)> migrate

I see this:

Saving artifacts... Running migration: 99_migration.js Deploying C... ... 0xf56571c947f71fa8d3220ce2707accdf97a00a3ac2042d18dcff0410e26bf476
C: 0x345ca3e014aaf5dca488057592ee47305d9b3e10 Replacing C... ... 0x17acd2015ea93445bca90c4f207073a7f86eb3556de00936e78f6fd4fe67c7a3
C: 0xf25186b5081ff5ce73482ad761db0eb0d25abfbf Replacing C... ... 0xcc2a645db6d4c32405ab3c771069262370512689de895a6374b78b72d73b12c8
C: 0x8f0483125fcb9aaaefa9209d8e9d7b9c8b9fb90f Replacing C... ... 0xac0d4f51c458e28fc3ecc871994e03e3aed86032dd881cf7dd57f2dffb58d378
C: 0x9fbda871d559710256a2502a2517b794b482db40 Replacing C... ... 0xf6cded7c4228a00cc2f12287417b0c697dec09635c013e28440d10dea7fc0565
C: 0x2c2b9c9a4a25e24b174f26114e8926a9f2128fe4 Replacing C... ... 0x539e27630c8881957a07ded94f9f2fb60963e505991eec1a588893e96d80b654
C: 0x30753e4a8aad7f8597332e813735def5dd395028 Saving successful migration to network... ... 0x68e7f2440f71ec0386d4214eb0c3e2346112dd1073495fc2ef1c8244691e5684 Saving artifacts...

again that Replacing word so I use the functions of the contracts by "ContractName.at("ContractAddress").FunctionName(FunctionArguments)

truffle(develop)> C.at("0x30753e4a8aad7f8597332e813735def5dd395028").ConstructorUint()

{ [String: '5'] s: 1, e: 0, c: [ 5 ] }

truffle(develop)> C.at("0xf25186b5081ff5ce73482ad761db0eb0d25abfbf").ConstructorUint()

{ [String: '1'] s: 1, e: 0, c: [ 1 ] }

truffle(develop)> C.at("0x2c2b9c9a4a25e24b174f26114e8926a9f2128fe4").ConstructorUint()

{ [String: '4'] s: 1, e: 0, c: [ 4 ] }

as you can see the numbers 5, 1, and 4 do exist. so the contracts do too? however from a separate terminal I again check the network by

$ truffle networks

and I get this:

Network: UNKNOWN (id: 4447) C: 0x30753e4a8aad7f8597332e813735def5dd395028 Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0

only one contract at the last address? so what happens in a non test net does the contract deploy with migrations many times with different constructor values as expected or does it do something else unexpected. if it does the unexpected what is a work around to accomplish multi deployment of the same contract, such as this.

1 Answer 1

1

Nice observation, deployer.deploy(Cnumber0, 0); will create smartcontract and deployed in the network. It doesn't matter how many times you called.

And every contract will have a unique id and unique datastore. So as per your deployment code

var Cnumber0 = artifacts.require("C"); 
var Cnumber1 = artifacts.require("C");
var Cnumber2 = artifacts.require("C"); 
var Cnumber3 = artifacts.require("C"); 
var Cnumber4 = artifacts.require("C"); 
var Cnumber5 = artifacts.require("C");



module.exports = function(deployer) {
    deployer.deploy(Cnumber0, 0);
    deployer.deploy(Cnumber1, 1);
    deployer.deploy(Cnumber2, 2);
    deployer.deploy(Cnumber3, 3);
    deployer.deploy(Cnumber4, 4);
    deployer.deploy(Cnumber5, 5);    
}

you want to deploy 5 smart contract's with different args. So as per my above explanation. Eth will maintain separate state for every smartcontract. So that a reason when you invoke you will get different values.

Once contract is deployed you can't modify the contract logic. So while writing contract we need to be little careful.

Your Answer

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

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