Solidity Tutorial Series part 6 – Solidity structures and Maps.

Writing in the front

Solidity is the Ethereum Smart Contract programming language. Before reading this, you should know something about Ethereum, smart contracts, and if you don’t already, what is Ethereum

The first half of this article is for translations of the official Solidity document (current version: 0.4.20) and the second half is for code explanations for things that are not in the official Solidity document (subscribe to the Solidity column).

Structure (user-defined Structs)

Solidity provides structs to define custom types, which are reference types. Let’s look at the following example:

Pragma solidity ^ 0.4.11;



contract CrowdFunding {

// Define a new type with two members

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) public returns (uint campaignID) {

campaignID = numCampaigns++; CampaignID is returned as a variable

// Create a struct instance, store it in storage, and place it in the mapping

campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);

}



function contribute(uint campaignID) public payable {

Campaign storage c = campaigns[campaignID];

// Create a structure reference with the mapping entry

// Funder(msg.sender, msg.value) can also be initialized.

c.funders[c.numFunders++] = Funder({addr: msg.sender, amount: msg.value});

c.amount += msg.value;

}



function checkGoalReached(uint campaignID) public 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;

}

}

Copy the code

The above is a simplified version of the crowdfunding contract, but it gives us an idea of the basic concepts of structs, which can be used as elements in maps and arrays. It can also contain types such as maps and arrays.

You cannot declare a struct and also have its own struct as a member. This limitation is based on the fact that the size of the structure must be finite. But struct can be a value type member of mapping.

Note that in a function, assigning a struct to a local variable (of type storage by default) is actually a copy reference, so modifying the local variable value will affect the original variable.

Of course, it is also possible to modify the value directly by accessing the member without having to assign to a local variable such as campaigns[campaignID].amount = 0

Mapping (the Mappings)

Mapping type, a mapping structure for key – value pairs. The mapping mode is _KeyType => _KeyValue. Key types allow for almost any type outside the structure except mappings, variable-length arrays, contracts, enumerations, and structures (). There are no restrictions on value types and mapping types can be included for any type.

The map can be viewed as a hash table, where all possible keys are created virtually, mapped to a default value for a type (binary all-zero representation). In the mapping table, the key’s data is not stored, only its KECCAK256 hash value, which is needed when looking up values. Because of this, maps have no length, and there is no concept of a set of keys or a set of values.

Map type, which can only be used as a state variable or as a reference to a storage type in an internal function.

You can have Solidity create an accessor by marking the map as public. Accessing it by providing a key value as an argument returns the corresponding value. The value type of the map can also be a map, and when accessed using an accessor, the key corresponding to the map value is supplied, and the process is repeated. Here’s an example:

Pragma solidity ^ 0.4.0;



contract MappingExample {

mapping(address => uint) public balances;



function update(uint newBalance) public {

balances[msg.sender] = newBalance;

}

}



contract MappingUser {

function f() public returns (uint) {

MappingExample m = new MappingExample();

m.update(100);

return m.balances(this);

}

}

Copy the code

Note: The map does not provide a means of iterating output; you can implement such a data structure yourself. Reference iterable mapping

Reference documentation

Official Solidity documentation

Blockchain in Simple terms – systematic learning of blockchain to create the best blockchain technology blog


WeChat pay

  • By Tiny Bear
  • Links to this article: Learnblockchain. Cn / 2017/12/27 /…
  • Copyright notice: This article is published under a CC BY-NC-SA 3.0 license. Reprint please indicate the source!