3

I'm reading the Truffle Migrations contract and looking at the code for upgrade():

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

I can't work out what effect the new_address given to the constructor on the line I've highlighted has. Migrations's constructor doesn't take any explicit arguments:

function Migrations() public {
  owner = msg.sender;
}

Can anybody clarify what's going on in Solidity here? I can't find a doc that explains what this mysterious extra argument does.

3

It's not an argument passed into the constructor. It's the location of a Migrations contract.

Passing arguments would look more like this:

MyContract c = new MyContract(argumentsForConstructor);

upgrade() is instantiating a Migrations at an address. Roughly, "There is a contract at new_address and it's ABI is Migrations."

Migrations upgraded is sort of like uint x. The left side casts upgraded as type Migrations. The right side instantiates a Migrations at address new_address. You can get carried away with the comparison and try uint x = uint(2); You might see this line is roughly the same idea:

Migrations upgraded = Migrations(new_address);

upgraded then goes on to invoke setCompleted() in the new_address instance and passes it last_completed_migration. That passes whatever is stored in last_completed_migration to the contract at new_address.

Hope it helps.

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.