0

The following code always reverts to "Contract must be empty".
But shouldn't address(this).balance == 0 if the contract hasn't even been instantiated yet?

Maybe since the contract doesn't even exist yet, neither does it's address/balance?
Even then, I thought all solidity values are set to 0 by default...

// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.10 <0.7.0;

contract test {
    
    constructor() public payable {
        require(msg.value > 0, "User must submit a valid money amount");
        require(address(this).balance == 0, "Contract must be empty");
    }
    
}
  • address(this).balance >= msg.value by definition. – goodvibration Jul 18 at 7:53
0
require(msg.value > 0, "User must submit a valid money amount");

This line ensures users are sending ETH along with their transaction. Any ETH being sent is automatically added to the address(this).balance and so the deployment always fails.

Maybe since the contract doesn't even exist yet, neither does it's address/balance?

This is not actually correct. It's just a public key address even before creating the contract. In fact, you could precompute a smart contract address and send money to it before it was created: https://ethereum.stackexchange.com/a/761/33305. But as mentioned above this wasn't the problem in your code.

| improve this answer | |

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.