1

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) public 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 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 c = campaigns[campaignID];
    if (c.amount < c.fundingGoal)
        return false;
    uint amount = c.amount;
    c.amount = 0;
    c.beneficiary.transfer(amount);
    return true;
}

}

This is a code form solidity documentation that I tried deploying. I made the mapping campigns public so I could see the variables contained within the struct Campaign. But I cannot see the 'mapping funders' variable and I tried making it public "mapping (uint =>Funder) public funders" but it throws an error. How do I see the values contained within the struct Funder?

  • it's not clear what are you asking! please elaborate more on your question. – Badr Bellaj Jun 10 '17 at 17:18
  • 1
    Please add the complete code and elaborate the question a bit more – Aniket Jun 10 '17 at 18:08
  • @BadrBellaj I edited the post. – Mokoye Jun 12 '17 at 3:26
  • @A.K. I just edited the code. I hope this explains better my question – Mokoye Jun 12 '17 at 3:28
0

At least as of the time of writing, v0.4.24,

you have to write an explicit getter function to be able to access that variable.

Marking a mapping as public creates a getter function automatically, but the example of a getter function for a struct in the official documentation notes:

The mapping in the struct is omitted because there is no good way to provide the key for the mapping.

If you only have the one nested mapping in the struct and want to satisfy this specific case, you can write a function like:

function viewFunder(uint campaignID, uint funderID) returns (address addr, uint amount) {
  addr   = campaigns[campaignID].funders[funderID].addr;
  amount = campaigns[campaignID].funders[funderID].amount;
}

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.