0
どのようにしてmapping
が動作し、配列のように、アイテムのコレクションです。私は強固な経験はありません、私はちょうど始まっています。私はこのコードを堅牢な公式ドキュメントページで見つけました。マッピングがどのようにしっかりと機能し、他の一般的な言語の別のコンセプトに類似してマッピングされているか
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;
}
}
良い説明。また、ここではマッピングについて説明します。https://ethereum.stackexchange.com/questions/9893/how-does-mapping-in-solidity-work Solidityのマッピング内にマッピングを持つことが可能です;) –