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
• 26,740 points
1,733 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,370 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
875 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 21, 2018 in Blockchain by Perry
• 17,100 points

edited Aug 9, 2018 by Omkar 1,172 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,100 points
730 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 aryya
• 7,460 points
755 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
1,942 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,468 views
+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,610 points
1,460 views
0 votes
2 answers

Is it possible to modify a variable value from another contract?

No, you can't directly edit a variable ...READ MORE

answered Sep 24, 2018 in Blockchain by Sai
4,379 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP