0

I'm trying to understand the following code from the Solidity documentation:

struct Funder {
    address addr;
    uint amount;
}

contract Crowdfunding {
    struct Campaign {
        address payable beneficiary;
        uint fundingGoal;
        uint numOfFunders;
        uint amount;
        mapping(uint => Funder) funders;
    }
    
    uint numOfCampaigns;
    mapping(uint => Campaign) campaigns;
    
    function newCampaign(address payable beneficiary, uint goal) public returns (uint campaignID) {
        numOfCampaigns ++;
        Campaign storage c = campaigns[campaignID]; // this line 
        c.beneficiary = beneficiary;
        c.fundingGoal = goal;
    }
}

But, I have three questions:

  1. Why doesn't this line Campaign storage c = campaigns[campaignID]; throw an out-of-range failing assertion? As far as I understand, the instance of Campaign with a matching campaignID doesn't exist in the campaigns array since we're creating a new one. Is this a way of instantiating a new Campaign struct?

  2. The documentation explains that the reason they're not instantiating a new Campaign struct with campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0) in this case is "because the RHS creates a memory-struct "Campaign" that contains a mapping". But, here also says that "Assignments between storage and memory (or from calldata) always create an independent copy", which seem to mean it's permitted. So, I'm not sure why assigning a memory-struct to a storage is a problem.

  3. Finally, the newCampaign function signature says that it's to return campaignID by stating returns (uint campaignID). But, the executable code doesn't actually return anything. Is this simply implied that the variable campaignID will be returned?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.