Is it possible to send eth to a contract through its constructor from another contract?

0 votes
contract FirstContract {

    function createOtherContract() payable returns(address) {
        // this function is payable. I want to take this 
        // value and use it when creating an instance of 
        // SecondContract
    }
}

contract SecondContract {
    function SecondContract() payable { 
        // SecondContract's constructor which is also payable
    }

    function acceptEther() payable {
        // Some function which accepts ether
    }
}

FirstContract will be created from the js app when the user clicks a button on the website. then I want to create an instance of the second contract and pass the ether along to the new contract. I cant figure out how to call SecondContract's constructor from the first contract while sending ether.

Sep 26, 2018 in Blockchain by digger
• 27,620 points
57 views

1 answer to this question.

0 votes

I found the solution for this:

pragma solidity ^0.4.0;

contract B {
    function B() payable {}
}

contract A {
    address child;

    function test() {
        child = (new B).value(10)(); //construct a new B with 10 wei
    }
}

Source: http://solidity.readthedocs.io/en/develop/frequently-asked-questions.html#how-do-i-initialize-a-contract-with-only-a-specific-amount-of-wei

Using your code it would look something like the following:

pragma solidity ^0.4.0;

contract FirstContract {

    function createOtherContract() payable returns(address) {
        return (new SecondContract).value(msg.value)();
    }
}

contract SecondContract {
    function SecondContract() payable { 
    }

    function acceptEther() payable {
        // Some function which accepts ether
    }
}
answered Sep 26, 2018 by slayer
• 29,050 points

Related Questions In Blockchain

+1 vote
1 answer

I would like to create a blockchain network which will stay at the same level of complexity, is it possible?

You can very easily create a cryptocurrency having a ...READ MORE

answered Apr 4, 2018 in Blockchain by Christine
• 15,790 points
33 views
+1 vote
1 answer

Is it possible to store blockchain in a sql or no-sql database?

Currently, following are the options to store ...READ MORE

answered Apr 20, 2018 in Blockchain by Perry
• 17,010 points

edited Aug 9, 2018 by Omkar 52 views
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,010 points
40 views
0 votes
1 answer

Is it possible to claim Bitcoin Cash from an exchange that does not support it?

You can't claim BCH without knowing private ...READ MORE

answered Jul 17, 2018 in Blockchain by ariaholic
• 7,320 points
28 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
105 views
0 votes
1 answer
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,620 points
119 views
0 votes
2 answers
0 votes
1 answer