1
// deploy a new purchase contract
function newPurchase()
    public
    payable
    returns(address newContract)
{
    Purchase c = (new Purchase).value(msg.value)(address(msg.sender));
    contracts.push(c);
    lastContractAddress = address(c);
    return c;
}

I was going through this code: https://github.com/jacksonng77/StartEscrow/blob/master/StartEscrow.sol

and I got to this part of the code (see code above) where I am having difficulty trying to understand it. (The full code is on the link above) The difficulty is particularlly on this line

Purchase c = (new Purchase).value(msg.value)(address(msg.sender));

How does this return the address of the contract? Why are we doing a

.value(msg.value)

and what is the work of

(address(msg.sender));

Also this line...

lastContractAddress = address(c)

why can I just say

lastContractAddress = c;

what is the purpose of address()?

1
  • To answer your last question - the code you've posted doesn't include the declaration of lastContractAddress, but its type is most likely address, so address(c) is required in order to cast c to the same type. Commented Aug 25, 2018 at 10:50

1 Answer 1

1

Your function is not a view function because it modifies and thus will be a transaction so it will not return a value , simply the tx receipt.

The value can be returned to another function however, just not to the user. You'll have to use log for that.

  • (new Purchase) - creates new contract , returns an instance so we need to typecast to address (this is since 0.4.15 ish)
  • .value(msg.value) - send some ether to the new contract we're creating
  • address(msg.sender) - should be just msg.sender and is a parameter for the constructor of the new contract

When not sending value upon creation to the new contract we could just say

new Purchase(msg.sender)

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.