0

I am practicing with the code examples from Andreas' Mastering Ethereum book Chapter 7. I don't understand why this does not compile when I made the Faucet constructor Payable. See images below.

Thank you.

enter image description here

enter image description here

8
  • When I deploy the Faucet contract with a Value it deploys successfully so I am not sure why this error is coming up. This example is on page # 153 of the Mastering Ethereum book.
    – Picard78
    Oct 3 '20 at 9:38
  • Can you add the contracts code as text to the question? Assume we don't have the book. From the screenshot you made Owned's constructor payable, but no Faucet's constructor. Inheritance will not make constructors payable.
    – Ismael
    Oct 3 '20 at 15:04
  • I added constructor() payable public {} to the Faucet contract and it compiled and deployed. I am confused, however, because when I deployed the Faucet contract directly (without this additional constructor) I was able to still add Ether to the Faucet contract which would suggest payable is inherited.
    – Picard78
    Oct 4 '20 at 5:46
  • I just tested it again. I removed constructor() payable public {} from the Faucet contract and was able to deploy it and give it 10 ether which suggests its inherited constructor is payable. However, the Token contract once again does not compile.
    – Picard78
    Oct 4 '20 at 5:51
  • I checked with other version and it is a bug that was fixed with solc 0.6.8, see release notes.
    – Ismael
    Oct 4 '20 at 6:21
1

Syntactically if you didn't write a constructor for Faucet it has the default non-payable constructor. So for the compiler you can't create and send ether to the contract.

constructor() {
    faucet = (new Faucet).value(0.5 ether)();
}

Before solc v0.6.8 there was a bug and the code generated didn't have the check in the constructor so you were able to create the contract and send ether.

The compiler doesn't read the bytecode it only uses the source code and it assumes the constructor is non-payable.

3
  • "default non-payable constructor." What do you mean by default Ismael? Faucet has an inherited constructor from contract Owned which is Payable.
    – Picard78
    Oct 7 '20 at 8:04
  • @Picard78 The constructor from Owned can only construct Owned because it doesn't know anything about Faucet. Faucet has to have its own constructor. If you don't write one the compiler will create a default one that is not payable.
    – Ismael
    Oct 7 '20 at 16:01
  • 1
    Ahhh I see. I thought constructors were inherited as well. I now see the bug before version solc 0.6.8 as well. It all makes sense to me now. Thank you for your patience Ismael and answering my question.
    – Picard78
    Oct 8 '20 at 10:24

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.