Create a child contract from a parent contract and forward the sender amount to it?

by user2087292   Last Updated November 10, 2018 01:28 AM

I want to spawn a child contract from a parent/owner contract. I would like the caller of the creating function on the parent contract to pay for gas and fee and also pass on the sent value (payable amount) to the child contract on creation.

pragma solidity ^0.4.25;

contract Child {
   string public a;
   constructor (string arg) public payable { 
       a = arg;
   }
}

contract Factory {
    constructor () public {}
    function createChild(string arg) public payable {
        address issueContract = (new Child).value(msg.value)(arg);
    }
}

Any idea why the transaction to Factory.createchild keeps failing when the value/payable amount passed is greater than 0 WEI? (it does only work when not value is passed at all ... )

Thanks a lot!



Answers 1


Hmmm ... something else going on here. When I run it in Remix, all is well.

Here, I've added events to report what's happening.

pragma solidity ^0.4.25;

contract Child {

   string public a;

   event LogCreatedBy(address creator, string arg);

   constructor (string arg) public payable { 
       a = arg;
       emit LogCreatedBy(msg.sender, a);
   }
}

contract Factory {

    event LogCreatedChild(address sender, string arg, address created);

    function createChild(string arg) public payable {
        address issueContract = (new Child).value(msg.value)(arg);
        emit LogCreatedChild(msg.sender, arg, issueContract);
    }
}

And here it is in Remix to show it working. I sent 100 Wei. No issue.

enter image description here

Is it possible the issue is client-side?

Hope it helps.

Rob Hitchens B9lab
Rob Hitchens B9lab
November 10, 2018 00:57 AM

Related Questions


Hidden information for game contract

Updated September 29, 2017 04:28 AM



Issue in insertion to an array inside a struct

Updated September 28, 2017 06:28 AM