2

solc 0.8.4

I'm trying to save the value of msg.value in my constructor. If I assign it to one of the state variables, it gets reverted by the EVM when I try to deploy the contract. However, if I assign that state variable a non-zero value (either before or after setting it to msg.value), then it works.

State variables:

address payable contract_owner;
uint256 wei_received;

Constructor version 1
The wei_received state variable is set to 1 before it is set to msg.value.
Success: The contract deploys and the value of wei_received is set correctly.

constructor () payable
{
    contract_owner = payable (msg.sender);
    wei_received = 1;
    wei_received = msg.value;
}

Constructor version 2
The wei_received state variable is not being set to 1.
Failure: Reverted by the EVM at time of deployment.

constructor () payable
{
    contract_owner = payable (msg.sender);
    wei_received = msg.value;
}

Constructor version 3
The wei_received state variable is set to 1 after it is set to msg.value.
Failure: The contract deploys but the value of wei_received is set incorrectly.

constructor () payable
{
    contract_owner = payable (msg.sender);
    wei_received = msg.value;
    wei_received = 1;
}

Is that by design for some reason? Is it a bug in this version of solidity? The workaround is simple, but I'm wondering if anyone else has noticed this and if there is a reason for it.

1 Answer 1

1

I tried the different scenarios on Remix and had no issues.

For example for the constructor version 2 scenario I successfully deployed this contract :

pragma solidity ^0.8.4;

contract B {
    address payable contract_owner;
    uint256 wei_received;
    
   constructor () payable {
    contract_owner = payable (msg.sender);
    wei_received = msg.value;
    } 

    function getReceipt() public view returns (uint256) {
        return wei_received;
    }
}

Furthermore I get the correct value for wei_received when calling the getReceipt function.

With constructor version 3 I get 1 for wei_received which is the expected result.

1
  • I was able to solve this problem by increasing my gas limit. I've never seen such bizarre behavior caused by insufficient gas.
    – Zephyrus
    Commented May 20, 2021 at 16:08

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.