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?

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

  • Where's the docs on this? I haven't seen this anywhere. – Travis Reeder Jun 23 '17 at 17:06
  • I updated my answer with some insights. – Xavier Leprêtre B9lab Jun 24 '17 at 15:34
4

try to send more gas

use gas:gasEstimate+40000

or gas: 4000000

-6

I am having the same error for the following Solidity code:

pragma solidity ^0.4.9;

/* Contract accepting ethers during 10 minutes */
contract Trading {

    address receivingAccount;
    uint deadline;

    struct Asset {
        string name;
        uint price;
        uint barcode;
    }

    struct Stock {
        Asset asset;
        uint count;
    }

    struct Vendor {
        string name;
        address account;
        Stock[] stocks;
    }

    Asset[] assets;
    Vendor[] vendors;

    /*  at initialization, setup the owner */
    function Trading(address _vendor0, address _vendor1) {
        // create 2 assets
        assets[0] = Asset("Asset0", 12, 1234);
        Asset memory asset1 = Asset("Asset1", 34, 5678);
        assets[1] = asset1;

        // create 2 vendors
        Vendor memory vendor0 = Vendor("Vendor0", _vendor0, new Stock[](5));
        Vendor memory vendor1 = Vendor("Vendor1", _vendor1, new Stock[](5));

        // add assets to the stock of vendor0
        vendor0.stocks[0] = Stock(assets[0], 2);
        vendor0.stocks[1] = Stock(asset1, 2);

        // add assets to the stock of vendor1
        vendor1.stocks[0] = Stock(assets[0], 1);
        vendor1.stocks[1] = Stock(asset1, 1);

        // set the deadline as 10 minutes
        deadline = now + 10 * 1 minutes;
    }   

    function listAssets() public returns (uint[]) {
        uint[] memory barcodes = new uint[](assets.length);
        for (uint i=0; i<assets.length; i++) {
            barcodes[i] = assets[i].barcode;
        }
        return barcodes;
    }

    modifier afterDeadline() { if (now >= deadline) _; }

    /* checks if the time limit has been reached and ends the contract */
    function dispose() afterDeadline {
        suicide(receivingAccount);
    }
}

I think I have quite enough ethers:

> eth.getBalance(eth.coinbase)
2.26000330774e+21
> 

By the way, I am using a local private network with the following CustomGenesis.json file:

{
   "nonce": "0x0000000000000042",
   "timestamp": "0x0",
   "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
   "extraData": "0x0",
   "gasLimit": "0x8000000000",
   "difficulty": "0x400",
   "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
   "coinbase": "0x3333333333333333333333333333333333333333",
   "alloc": {
      "0x7fde9d137b8f26e09ab3719b9585f7bef638e96c" : {
       "balance" : "10000000000000000000"
      },
      "0x65bc8f15748b55bf993a2fd28dbdd21b8a620d6c" : {
       "balance" : "20000000000000000000"
      },
      "0xc306c64d00f58e58526d4ac820d13d6d61747f7b" : {
       "balance" : "30000000000000000000"
      }
   }
}

Any ideas?

  • If you have another question, please ask it by clicking the Ask Question button. – Waqar Lim Mar 18 '17 at 8:39

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.