7

I'm trying to deploy the following simple contract to a private Ethereum chain using Geth 1.5.4-stable-b70acf3c:

pragma solidity ^0.4.0;

contract Faucet {
    address owner;
    uint256 sendAmount;

    function Faucet() {
        owner = msg.sender;
        sendAmount = 1000000000000000000;
    }

    function getBalance() returns (uint) {
        return this.balance;
    }

    function getWei() returns (bool) {
        return msg.sender.send(sendAmount);
    }

    function sendWei(address toWhom) returns (bool) {
        return toWhom.send(sendAmount);
    }

    function getSendAmount() returns (uint256) {
        return sendAmount;
    }

    function killMe() returns (bool) {
        if (msg.sender == owner) {
            suicide(owner);
            return true;
        }
    }

    function () payable {}
}

Here are the commands I run in Geth console to deploy the contract:

> var faucetSource = "pragma solidity ^0.4.0; contract Faucet { address owner; uint256 sendAmount; function Faucet() { owner = msg.sender; sendAmount = 1000000000000000000; } function getBalance() returns (uint) { return this.balance; } function getWei() returns (bool) { return msg.sender.send(sendAmount); } function sendWei(address toWhom) returns (bool) { return toWhom.send(sendAmount); } function getSendAmount() returns (uint256) { return sendAmount; } function killMe() returns (bool) { if (msg.sender == owner) { suicide(owner); return true; } } function () payable {} }"

> var faucetCompiled = eth.compile.solidity(faucetSource)

> var faucetContract = eth.contract(faucetCompiled.Faucet.info.abiDefinition);

> var faucetInstanceTransaction = faucetContract.new({from:eth.coinbase, data:faucetCompiled.Faucet.code, value:1000000000000000000, gas:1000000}, function(err, contract){if(err){console.error(err);}else{console.log(contract.address);}});

And here is what I'm getting:

undefined
undefined
> Error: The contract code couldn't be stored, please check your gas amount.

I double checked and eth.coinbase is unlocked, and it has ether:

> web3.fromWei(eth.getBalance(eth.coinbase), 'ether');
8279.53125

Any idea of what's going wrong and how to troubleshoot that kind of issue?

Sebastien
  • 323
  • 2
  • 11

2 Answers2

8

You need to make your constructor payable:

function Faucet() payable {

Edit 2017-06-24:

A constructor is a function. It was supposed to behave as member functions WRT payable from v0.4.0, but was not in practice because of a bug.

From v0.4.0 to v0.4.4 included, "omitted faucet", function Faucet() { and function Faucet() payable { behaved the same and all accepted Ether. This was a bug.

With v0.4.5, this bug was fixed, so "omitted faucet", and function Faucet() { no longer accepted Ether while function Faucet() payable { did accept Ether. https://github.com/ethereum/solidity/releases/tag/v0.4.5

5

try to send more gas

use gas:gasEstimate+40000

or gas: 4000000

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75