I am trying to demo a function on my own geth testnet. I deployed a sample contract I found at http://solidity.readthedocs.io/en/develop/types.html?highlight=crowdfunding.

pragma solidity ^0.4.11;

contract CrowdFunding {
    // Defines a new type with two fields.
    struct Funder {
        address addr;
        uint amount;

    struct Campaign {
        address beneficiary;
        uint fundingGoal;
        uint numFunders;
        uint amount;
        mapping (uint => Funder) funders;

    uint numCampaigns;
    mapping (uint => Campaign) campaigns;

    function newCampaign(address beneficiary, uint goal) returns (uint campaignID) {
        campaignID = numCampaigns++; // campaignID is return variable
        // Creates new struct and saves in storage. We leave out the mapping type.
        campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);

    function contribute(uint campaignID) payable {
        Campaign storage c = campaigns[campaignID];
        // Creates a new temporary memory struct, initialised with the given values
        // and copies it over to storage.
        // Note that you can also use Funder(msg.sender, msg.value) to initialise.
        c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});
        c.amount += msg.value;

    function checkGoalReached(uint campaignID) returns (bool reached) {
        Campaign storage c = campaigns[campaignID];
        if (c.amount < c.fundingGoal)
            return false;
        uint amount = c.amount;
        c.amount = 0;
        return true;

"contract" references the deployed contract. I setup my default account and performed the newCampaign function:

> contract.newCampaign(eth.accounts[0], 100)

So I have the transactionHash. And a new campaign should have been created for the address associated with my account. But when I try to call the "checkGoalReached" function I always get "true", although for my campaign it should return false given that no one has contributed.

(I tried both 0 and 1 because it is based off the id)

> contract.checkGoalReached.call(0)
> contract.checkGoalReached.call(1)

Then I tried sending the transaction and logging the result for this function:

> contract.checkGoalReached(0, function(e, result) {console.log(result)})
> contract.checkGoalReached(1, function(e, result){console.log(result)})

"false" should be returned, so I must be doing something wrong in the web3 console. What is the correct way to test these functions in the console?

1 Answer 1


I played around with your contract in Remix to confirm it does what it seems to do. It has some unexpected behavior due to the data structure. For example, for any undefined contract it returns true because 0 is not less than 0.

By "tight", I mean an inquiry into a non-existent campaign should probably throw as opposed to returning a misleading result. You might consider tightening that up, using a mapped struct with index. (Are there well-solved and simple storage patterns for Solidity?).

In any case, I confirmed that it returns false as expected with a brand new campaign with a goal of 100. This differs from the results you get calling it, and I think that has to do with trying to use it synchronously.

Basically, you have to wait for results or it will mislead. For example:

var contract;
MyContract.deployed().then(function(instance) { contract = instance; });

Now you can carry on with contract representing your deployed contract. You need to wait for promises to return, so, try it this way:

contract.checkGoalReached().call(0).then(function(response) { console.log(response); });

I assumed you're using Truffle. :-)

Hope it helps.

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.