I have setup testrpc with two accounts using this syntax basically:
testrpc -h 0.0.0.0 --account=0x93e707aa1e3ada62,10000000000000000000 --account=0x47e7625a68200ea9,1000000000000000000
I can transfer eth between the two accounts using MetaMask.
I then used the Crowdfuding example as described here:
https://dappsforbeginners.wordpress.com/tutorials/contracts-that-send-transactions/
This defines two functions, newCampaign
:
function newCampaign(address beneficiary, uint goal, uint deadline) returns (uint campaignID) {
campaignID = numCampaigns++; // campaignID is return variable
Campaign c = campaigns[campaignID]; // assigns reference
c.beneficiary = beneficiary;
c.fundingGoal = goal;
c.deadline = block.number + deadline;
}
And, contribute
:
//function to contributes to the campaign
function contribute(uint campaignID) {
Campaign c = campaigns[campaignID];
Funder f = c.funders[c.numFunders++];
f.addr = msg.sender;
f.amount = msg.value;
c.amount += f.amount;
}
I can compile the contract successfully, and deploy seems to work.
I then jump into truffle console. I'm trying to create a new campaign with the first user, and then contribute
to it with the second user.
From within truffle, I run this code:
CrowdFunding.deployed()
.then( function(inst) {
return inst.newCampaign( "0x30924cD9D8e3644B1c186e3381694403A77f87b6", 50, 50 ); } )
.then( function(id) {
console.log( "ID:", id ); });
This does not print out the expected campaign ID, as I expected. Instead it prints out:
ID: { tx: '0x3afb822beb6dae9d73db0505ba0ee1056902e5c968bd8a6888fe0024f633f8e4',
receipt:
{ transactionHash: '0x3afb822beb6dae9d73db0505ba0ee1056902e5c968bd8a6888fe0024f633f8e4',
transactionIndex: 0,
blockHash: '0xccd15e9cb18c9b0d8d9c4298bb886c427f155318755c241515796cb0483ff66d',
blockNumber: 9,
gasUsed: 88745,
cumulativeGasUsed: 88745,
contractAddress: null,
logs: [] },
logs: [] }
undefined
Q1: So, that's my first problem, how do I get the campaignId supposedly returned from the newCampaign call?
Q2 Obviously I will then want to call the contribute function. But, I'm confused as to how I masequerade as the second user? I can get their address from MetaMask, but do I do something like this:
CrowdFunding.deployed()
.doTheThingsToGetCampaignId(). // fix the above code...
.then( function(inst) {
tx = { from: 0x132123 # The second account's address?
return inst.contribute( campaignId, 50 ).call( tx ); // What should be here?
} );
Q3: I see various examples like inst.contribute.call(...)
. What's the right way to call contribute from truffle?