Solidity_study
2024-03-12 18:09:33 0 举报
AI智能生成
随着学习深入将不定期持续更新 在完善所有知识点将允许克隆
作者其他创作
大纲/内容
Variable
Data Types
Value Types
Booleans
The possible values are constants true and false.
Operators
! (logical negation)
&& (logical conjunction, “and”)
|| (logical disjunction, “or”)
== (equality)
!= (inequality)
Integers
int / uint: Signed and unsigned integers of various sizes.
Keywords uint8 to uint256 in steps of 8 (unsigned of 8 up to 256 bits) and int8 to int256.
uint and int are aliases for uint256 and int256, respectively.
Operators
Comparisons: <=, <, ==, !=, >=, > (evaluate to bool)
The value of a comparison is the one obtained by comparing the integer value.
Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation)
Bit operations are performed on the two’s complement representation of the number. This means that, for example ~int256(0) == int256(-1).
Shift operators: << (left shift), >> (right shift)
x << y is equivalent to the mathematical expression x * 2**y.
x >> y is equivalent to the mathematical expression x / 2**y, rounded towards negative infinity.
Arithmetic operators: +, -, unary - (only for signed integers), *, /, % (modulo), ** (exponentiation)
Addition, Subtraction and Multiplication
Division
Modulo
Exponentiation
For an integer type X, you can use type(X).min and type(X).max to access the minimum and maximum value representable by the type.
Fixed Point Numbers
Fixed point numbers are not fully supported by Solidity yet. They can be declared, but cannot be assigned to or from.
Rational and Integer Literals
Address
The two flavous
address
Holds a 20 byte value (size of an Ethereum address).
address payable
Same as address, but with the additional members transfer and send.
Type conversions
Implicit conversions from address payable to address are allowed, whereas conversions from address to address payable must be explicit via payable(<address>).
Explicit conversions to and from address are allowed for uint160, integer literals, bytes20 and contract types.
Only expressions of type address and contract-type can be converted to the type address payable via the explicit conversion payable(...). For contract-type, this conversion is only allowed if the contract can receive Ether, i.e., the contract either has a receive or a payable fallback function. Note that payable(0) is valid and is an exception to this rule.
Operators
<=, <, ==, !=, >= and >
Members of Addresses
Address Literals
Contract Types
Every contract defines its own type. You can implicitly convert contracts to contracts they inherit from. Contracts can be explicitly converted to and from the address type.
Enums
Unicode Literals
string memory a = unicode"Hello 😃";
Hexadecimal Literals
Hexadecimal literals are prefixed with the keyword hex and are enclosed in double or single-quotes (hex"001122FF", hex'0011_22_FF').
String Literals and Types
the following escape characters:
\<newline> (escapes an actual newline)
\\ (backslash)
\' (single quote)
\" (double quote)
\n (newline)
\r (carriage return)
\t (tab)
\xNN (hex escape, see below)
\uNNNN (unicode escape, see below)
Fixed-size byte arrays
Operators
Comparisons: <=, <, ==, !=, >=, > (evaluate to bool)
Bit operators: &, |, ^ (bitwise exclusive or), ~ (bitwise negation)
Shift operators: << (left shift), >> (right shift)
Index access: If x is of type bytesI, then x[k] for 0 <= k < I returns the k th byte (read-only).
Members
.length yields the fixed length of the byte array (read-only).
Dynamical-sized byte array
bytes
Dynamically-sized byte array, see Arrays. Not a value-type!
string
Dynamically-sized UTF-8-encoded string, see Arrays. Not a value-type!
User Defined Value Types
Definition: type C is V
Function Types
two flavours
Internal functions(By default)
External functions
Function types are notated as follows
function (<parameter types>) {internal|external} [pure|view|payable] [returns (<return types>)]
Conversions
pure functions can be converted to view and non-payable functions
view functions can be converted to non-payable functions
payable functions can be converted to non-payable functions
Members
External (or public) functions have the following members
.address returns the address of the contract of the function.
.selector returns the ABI function selector
Reference Types
Data location
three data locations
memory
storage
calldata
Arrays
Definition
fixed sized arrays
T[k]
dynamic size arrays
Structs
Mapping Types
Iterable Mappings
Operator
Order of Precedence of Operators
Expressions and Control Structures
Control Structures
Function Calls
Creating contracts via new
Order of Evaluation of Expressions
Assignment
Scoping and Declarations
Error handling: Assert, Require, Revert and Exceptions
Units and Globally Available Variables
Units
Ether Units
分支主题
Time Units
分支主题
Contracts
收藏
收藏
0 条评论
下一页