CrowdFunding example from official docs does not work (state is not changed)

rroschinrroschin Member Posts: 5
Hi,

I am trying to develop something useful with Solidity, but there are too much 'restrictions' and 'still not implemented' in Ethereum/Solidity, so I don't know what should work.
Here is an example from http://solidity.readthedocs.org/en/latest/types.html?#structs . I changed a little bit (replaced msg.sender/value with function params) and added some functions, so I can see the state (variables in storage):

contract CrowdFunding {
  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++;
    campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
  }
  
  function contribute(uint campaignID, address who, uint value) {
    Campaign c = campaigns[campaignID];
    c.funders[c.numFunders++] = Funder({addr: who, amount: value});
    c.amount += value;
  }
  
  function check1() constant returns (uint check_1) { return numCampaigns; }
  function check2() constant returns (address check_2) { return campaigns[1].beneficiary; }
  function check3() constant returns (uint check_3) { return campaigns[1].fundingGoal; }
  function check4() constant returns (uint check_4) { return campaigns[1].numFunders; }
  function check5() constant returns (uint check_5) { return campaigns[1].amount; }
  function check6() constant returns (address check_6) { return campaigns[1].funders[1].addr; }
  function check7() constant returns (uint check_7) { return campaigns[1].funders[1].amount; }
}
I do have:
  • solc version: Version: 0.2.1-0/Release-Linux/g++/int linked to libethereum-1.1.2-0/Release-Linux/g++/int
  • geth up and running my private network version: 1.4.0-unstable
  • custom genesis.json file, so my primary account has a lot of ether
  • nodejs app with web3 to deploy and run contracts
I am able to deploy contract, get its address and invoke its methods.
Here is what I do from js side:
var _LOG = '[ethereum.js]'; console.log(_LOG + 'Test CrowdFunding.'); var cont; //contract - this is ok var deasync = require('deasync'); var r1 = cont.newCampaign('0x208b30d135dc9db86eff421645b04c56cd3a11bd', 100); console.log(_LOG + 'R1: ' + r1); deasync.sleep(60000); //sleep 1 minute to get this actually do what it should var r2 = cont.contribute(1, '0x208b30d135dc9db86eff421645b04c56cd3a11bd', 55); console.log(_LOG + 'R2: ' + r2); deasync.sleep(60000); //sleep 1 minute to get this actually do what it should console.log(_LOG + 'R3: ' + cont.check1()); console.log(_LOG + 'R4: ' + cont.check2()); console.log(_LOG + 'R5: ' + cont.check3()); console.log(_LOG + 'R6: ' + cont.check4()); console.log(_LOG + 'R7: ' + cont.check5()); console.log(_LOG + 'R8: ' + cont.check6()); console.log(_LOG + 'R9: ' + cont.check7());

And the result will be:

[ethereum.js] Test CrowdFunding.
[ethereum.js] R1: 0xec7d714c2014448af7ff35cc30a5ed54745ed43aa67d5a96ed235f48244f26e5
[ethereum.js] R2: 0x39e32ab3f4455db6ac13de9fe6e1469b9391ab9dc5ed30ef4680e03047623059
[ethereum.js] R3: 0
[ethereum.js] R4: 0x0000000000000000000000000000000000000000
[ethereum.js] R5: 0
[ethereum.js] R6: 0
[ethereum.js] R7: 0
[ethereum.js] R8: 0x0000000000000000000000000000000000000000
[ethereum.js] R9: 0
And this is just default values. No actual actions were done here. But I can see that transactions from R1 and R2 were mined and are in blocks in Ethereum (check in geth console). I also tried to wait 5 minutes (not 1 minute), but result was the same.

So please tell me what is going on here and how can I understand that my code was executed, and if not - why not?

Thanks.
Sign In or Register to comment.