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?
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}();
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
}
}
TLDR: encapsulate in parentheses like this (new SomeTest).value(1 ether) .
newContract = new SomeTest{value:1 ether}();
here is the link updated docs.soliditylang.org/en/develop/…
Commented
Aug 27, 2022 at 15:08