I'm trying to make a factory contract that accomplishes two things:

  1. Create contracts and endow them at creation with specific parameters
  2. Maintain a mapping of structs with info of all created contracts

Here is my code, then I will explain my question.

pragma solidity ^0.4.18;
import "SimplePurchase.sol"; //this is the child contract being created

contract PurchaseCreator {
    struct PurchaseData {
        address seller;
        uint txnValue;

    //Creates new child contract & stores info in mapping
    function newSimplePurchaseContract(address seller, uint txnValue) isActive public payable returns (SimplePurchase newPurch, uint PurchaseId) {
        PurchaseId = nextPurchaseId++;
        seller = msg.sender;
        txnValue = msg.value;
        purchases[PurchaseId] = PurchaseData(seller, txnValue);

        newPurch = (new SimplePurchase).value(txnValue)();

        return(newPurch, PurchaseId);

Here are my two questions:

  1. When I create the child contract, is it possible to pass through the seller, txnValue, and PurchaseId from the function newSimplePurchaseContract? I'm currently passing the txnValue, but I'm just not sure if it's possible to pass through anything besides value based on reading the solidity docs.

  2. Can the child contract pass information back to this contract to be added to the mapping? I'd like to hold a "buyer" address in the mapping struct as well, but with the way my user flow is set up, I won't know the buyer until the child contract is created.

Let me know if I'm unclear with my question. I've scoured stack exchange and the solidity docs to try to figure this out and haven't found answers, so I apologize if this is a duplicate.

Thanks for your help!

1 Answer 1


You can pass constructor parameters like this:

child = (new Child).value(msg.value)(foo);

Constructors can't return extra values, but they can store them to be retrieved later. See the following working code:

pragma solidity ^0.4.19;

contract Child {
    uint256 public bar;

    function Child(uint256 foo) public payable {
        bar = foo + msg.value;

contract Parent {
    uint256 public bar;
    Child public child;

    function newChild(uint256 foo) public payable {
        child = (new Child).value(msg.value)(foo);
        bar = child.bar();
  • So if I want to pass three pieces of information to the child contract: seller, txnValue, PurchaseId, would I write it like this? child = (new Child).value(txnValue)(seller)(PurchaseId) This is very helpful, thank you.
    – Nick
    Jan 14, 2018 at 0:21
  • No, (new Child).value(txnValue)(seller, PurchaseId). The txnValue isn't something you're passing to the constructor; it's ether you're attaching. The result of .value(txnValue) is a function, which you can call with any number of parameters. (In your original example, you just called it with no parameters: (). You can instead call it with two parameters: (seller, PurchaseId)).
    – user19510
    Jan 14, 2018 at 0:39

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.