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;
c.beneficiary.transfer(amount);
return true;
}
}
"contract" references the deployed contract. I setup my default account and performed the newCampaign function:
> contract.newCampaign(eth.accounts[0], 100)
"0x0122973cbcb7df227e8d625c6ee4a831d716b8b69398e4431ec14f89951fdc25"
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)
true
> contract.checkGoalReached.call(1)
true
Then I tried sending the transaction and logging the result for this function:
> contract.checkGoalReached(0, function(e, result) {console.log(result)})
0xe1bf7a275a62fb3bc2cdd12ff280cde5e9064ddbfeb51ba6713a713cd356c8cc
undefined
> contract.checkGoalReached(1, function(e, result){console.log(result)})
0x613c5973733cde57d4b7325bbab2d841d2fa6c95e371e77ca24a2f5a54768fd3
undefined
"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?