Home > Blockchain >  Extinguished solidity learning notes (1)
Extinguished solidity learning notes (1)

Time:09-22

In this paper, the main content comes from: https://solidity-cn.readthedocs.io/zh/latest/

Intelligent contract can be simply interpreted as an executable program fragment, after Solidity write specific code, published to block chain, then by miners packaging records in one block, when you need to call the method of intelligent contract only need to send this smart contract address a deal,

Because smart contract trigger condition and operation logic has been written in the code, is stored in the block on the chain, so can eliminate the interference of man-made factors, to a great extent

Virtual machine running environment of EVM entry is intelligent contracts, it is not only the sand box packaging, and it is completely isolated, that is to run the code in the EVM entry is unable to access the network, file system and other processes, even smart contracts between the access is limited,

Solidity is a contract oriented, create a high-level programming language for the realization of intelligent contracts,

Solidity is statically typed language, support inheritance, the library and the user-defined types of complex features,



A, Solidity source file structure

In the source file can contain any number of contract definition, import instructions and miscellaneous note,

Version 1.1 of mixed note

In order to avoid future be incompatible changes may be compiled by the compiler, the source file can (and should) version of the so-called hybrid pragma annotations, therefore need to be a when change semantics must synchronize to modify the way syntax introduced changes, it is sometimes hard to do, of course, as a result, at least on version contains significant changes, read through the change log is a good way to forever, these versions of the version number is always 0. X. 0 or x., in the form of 0.0

Pragma solidity ^ 0.4.0;
In this way, the source file will not allow below 0.4.0 version of the compiler, does not allow above (contain) 0.5.0 version of the compiler (the second condition by using ^ be added), the practice of consideration is that the compiler version 0.5.0 before there would be no significant changes, so can make sure the source is always expected to be compiled, in the example above is not fixed to the compiler version number, the compiler of the patch version can also be used,

Can use more complex rules to specify the version of the compiler, expression follow NPM version semantics,

1.2 import other source files

At the global level, import statements can use the following format:

The import "filename";

This statement will import from "filename" all global symbol to the current global scope (unlike ES6, Solidity is backwards compatible),

The import * as symbolName from "filename";
. To create a new global symbol symbolName, its members are from "filename" in the global symbol,

The import {symbol1 as alias, symbol2} from "filename";
. Alias and create a new global symbols symbol2, respectively from "filename" reference symbol1 and symbol2,
` ` `
The import "filename" as symbolName;

//this statement is equivalent to
The import * as symbolName from "filename";
` ` `
1.3 annotations
` ` `
You can use the single-line comments (//) and multiline comment (/*... */)

//this is a single-line comments,

/*
This is a
Multiline comment,
*/
` ` `
In addition, there is another kind of annotation called natspec annotation, the document also has not yet been written, they are in three the backslash (///) or a binary number at the beginning of blocks (/* *... */), they should be directly used on function declaration or statement,

In the example below, we recorded the title of the contract, two into the parameter and return value:
` ` `
Pragma solidity ^ 0.4.0;

/* * @ title shape calculators, */
Contract shapeCalculator {
/* * @ dev for rectangular area and perimeter,
* @ param w width rectangle,
* @ param rectangular height h,
* @ return for surface area s,
* @ return p obtained girth,
*/
The function a rectangle (uint uint w, h) returns (uint s, uint p) {
S=w * h;
P=2 * (w + h);
}
}

` ` `
Second, contract structure

In Solidity, similar to the object-oriented programming language classes in the contract, each contract can be included in the state variable, function, function of modifier, events, structure types, and enumerated type declaration, and contracts can inherit from other contracts,


2.1 state variables
State variables are permanently stored in the store the values in the contract,
` ` `
Pragma solidity ^ 0.4.0;

Contract SimpleStorage {
Uint storedData;//state variable
//...
}
` ` `
2.2 function

Function is the executable unit code in the contract,
` ` `
Pragma solidity ^ 0.4.0;

Contract SimpleAuction {
Function the bid () public payable {//function
//...
}
}
` ` `
Function calls can occur in the internal or external contract, and the function of other contracts with varying degrees of visibility,

2.3 function decorator

Decorator () function can be used to declaratively improved function semantics,
` ` `
Pragma solidity ^ 0.4.22;

Contract Purchase {
Address public seller;

The modifier onlySeller () {//modifier
Require (
MSG. Sender==seller,
"Only the seller can call this."
);
_;
}

The function abort () public onlySeller {//Modifier usage
//...
}
}
` ` `
2.4 event

Event is to be able to easily log function calls the etheric fang virtual machine interface,
` ` `
Pragma solidity ^ 0.4.21;
Contract SimpleAuction {
The event HighestBidIncreased (address bidder, uint amount);//event

The function the bid () public payable {
//...
Emit HighestBidIncreased (MSG) sender, MSG. Value);//triggers
}
}
` ` `
2.5 structure type

Structure is the several variables can be grouped, custom types of
` ` `
Pragma solidity ^ 0.4.0;

Contract Ballot {
Struct Voter {//structure
Uint weight;
Bool voted;
Address the delegate;
Uint vote;
}
}
` ` `
2.6 enumeration type

Enumeration can be used to create composed of a certain number of "constant values" custom types,
` ` `
Pragma solidity ^ 0.4.0;

Contract Purchase {
Enum State {Created, Locked, Inactive}//enumeration
}
` ` `
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Copyright statement: in this paper, the author (Li_Yu_Qing)

CodePudding user response:

  • Related