Solidity: Smart Contracts Language Tutorial Series 6 – Structures and Maps The Solidity has been updated to read for real

Solidity Tutorial Series part 6 – Solidity structures and Maps. For a complete list of articles in the Solidity series, see the category -Solidity.

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

This series of articles is based on translations of the official Solidity document (current version: 0.4.20) and in-depth Solidity analysis. Please subscribe to blockchain Technology for this part.

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.411.;

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

Refer to the video

We also have the most comprehensive video course on the market: In-depth Solidity, the Ethereum Smart Contract language. We are currently looking for course experiential experts, check out the link.

Reference documentation

Official Solidity documentation

Dynamic And Simple Blockchain – Learn blockchain systematically and build the best blockchain technology blog.

Dynamic Planet Of My Knowledge answers blockchain technology questions. Welcome to join the discussion.

Aid To The Public account “Deep And Simple Blockchain Technology” To obtain blockchain technology information for the first time.