1

I tried doing

address NewContract = new SomeTest.value(1 ether)();

But this yields a syntax error.

I try to call the payable constructor of a new contract with ether. What is the syntax for this?

1 Answer 1

2

UPDATE: if using solidity version 0.7.x and later, you will need to use a slightly different syntax when passing value to a constructor.

Assuming that the name of the contract is SomeTest

old method - v0.6.x and earlier -

newContract = (new SomeTest).value(1 ether)();

new method - v0.7.x onwards -

newContract = (new SomeTest){value:1 ether}();

UPDATE END

Try using the below syntax (ensure that the SomeTest constructor is marked payable):

pragma solidity ^0.4.24;

contract SomeTest 
{
    constructor() public payable {}
}

contract A 
{
    address newContract;

    function test() public 
    {
      //construct a new SomeTest with 1 ether
      newContract = (new SomeTest).value(1 ether)();//works upto 0.6.x 
    }
}

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

TLDR: encapsulate in parentheses like this (new SomeTest).value(1 ether) .

1

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.