Solidity language

Chain-off, designed for developers and students, Insider!

This article from the block chain technology community , refused reproduced without permission.

Here Insert Picture Description
Solidity 11 languages

Solidity is an Ethernet Square smart contract programming language, I am also studying for a long time, the feeling is a bit difficult, so it is necessary to seriously get to know and understand today talking about there is foundation also has in-depth analysis, before reading, the need for Ethernet Square intelligent preliminary contract or in-depth understanding.

Structure

Solidity provide struct to define custom types, custom types are reference types. We take a look at the following example:

pragma solidity ^ 0.4.11;

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) campaigns;

function newCampaign(address beneficiary, uint goal) public returns (uint campaignID) {

    campaignID = numCampaigns++; // campaignID 作为一个变量返回

    // 创建一个结构体实例,存储在storage ,放入mapping里

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

}



function contribute(uint campaignID) public payable {

    Campaign storage c = campaigns[campaignID];

    // 用mapping对应项创建一个结构体引用

    // 也可以用 Funder(msg.sender, msg.value) 来初始化.

    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;

}

}

The above is a simple version of the crowdfunding contract, but it does allow us to understand the basic concepts structs, and maps that can be used as elements in the array. Which itself comprises an array type mapping and the like.

You can not declare a struct while the self as a member, this limit is based on the size of the structure must be limited. But struct can be used as a member value type of mapping.

Note that in a function, a local variable assigned to a struct, a copy of the actual reference value so modified while local variables will affect the original variables.

Of course, a member may be directly accessed by a modified value, without necessarily assigned to a local variable, such as campaigns [campaignID] .amount = 0

Mapping

Stored mapping relationship type texture mapping a key-value pairs. Defined ways of mapping (_KeyType => _KeyValue). Type allows inter key mapping, variable length arrays, contracts, enumerate, almost all types of structures in vitro. Value types without any limitation, any type may include a mapping type.

Mapping can be regarded as a hash table, all possible key will be virtualized to create mapped to the default value of a type. In the mapping table, do not store key data, storing only its keccak256 hash value, the hash values ​​need to use when looking for value. The length of the mapping is not, there is no concept of key set or set of values.

Mapping type can only be used as a state variable, or as a reference in the internal storage type function.

It can be marked as public by mapping to make Solidity create an access device. A key-value as a parameter to access it provides, corresponding to the return value. Value type mapping may be mapped, using the Accessor access, to provide the mapping value corresponding to the key, the process repeats. Look at 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);

}

}

NOTE: output mapping does not provide an iterative method may be implemented such a data structure itself.

Well, today on the first write so, in his own writing when they are out of the small problems, after all, not particularly skilled, can be learned, not long Cou Cou written out alive, there's hope that we do not pointed message , more exchanges.

Guess you like

Origin blog.csdn.net/weixin_44172023/article/details/93718142