0

In the Solidity documentation it is stated that:

A require-style exception is generated in the following situations:

...

  1. If your contract receives Ether via a public getter function.

...

What is an example of that and why does it raise the exception?

0

Let's consider the following contract:

contract pubGetterException  {
  uint public value;

  function pubGetterException(uint _value) public {
    value = _value;
 }
}

The unsigned integer variable value has a public getter which is automatically generated (you don't have to write it explicitly in your contract), since it is declared as public.

If you have a look at the ABI, it looks like this:

[
    {
        "constant": true,
        "inputs": [],
        "name": "value",
        "outputs": [
            {
                "name": "",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "view",
        "type": "function"
    },
    {
        "inputs": [
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "constructor"
    }
]

Here, note that the automatically generated getter function is NOT a payable function ie. it is not able to receive ether, therefore it will throw a require-style exception as stated in the Solidity documentation.

  • So ... the documentation is too generic? I.e. if you would create a public getter function yourself and make it explicitly payable, then the require-style exception would not be raised? – Ytsen de Boer Mar 12 '18 at 12:44
  • I have not tested this, but I think yes. – István András Seres Mar 12 '18 at 12:46

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.