2

Just wrote and tested the following on http://remix.ethereum.org

Same happens on Ropsten network.

If I run without sending any value it works, but I can't deploy the Test contract if I put any value > 0 to send because it reverts, but why?

pragma solidity ^0.5.0;

contract Test
{
    uint public m_ReceivedWei;

    function()
        external payable
    {
        m_ReceivedWei += msg.value;
    }

}
2

There is no constructor, so you can't fund the contract in the deploy because by default constructor is not payable. You can add the following line:

constructor() public payable {}

About Constructor:

A constructor is an optional function declared with the constructor keyword which is executed upon contract creation, and where you can run contract initialisation code. [...] If there is no constructor, the contract will assume the default constructor, which is equivalent to constructor() public {}.

Fallback function works fine.

About Fallback function:

A contract can have exactly one unnamed function. This function cannot have arguments, cannot return anything and has to have external visibility. It is executed on a call to the contract if none of the other functions match the given function identifier (or if no data was supplied at all).

2

As you might know the constructor is executed once the smart contract is deployed thus when you deploy with value you need to define a payable constructor as follows:

    constructor() public {        
 m_ReceivedWei += msg.value;
 } .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.