以下是按評估順序列出的操作符優先順序。
優先順序 | 描述 | 操作符 |
---|---|---|
1 | 後置自增和自減 | ++ , -- |
建立型別範例 | new <typename> | |
陣列元素 | <array>[<index>] | |
存取成員 | <object>.<member> | |
函數呼叫 | <func>(<args...>) | |
小括號 | (<statement>) | |
2 | 前置自增和自減 | ++ , -- |
一元運算的加和減 | + , - | |
一元操作符 | delete | |
邏輯非 | ! | |
按位元非 | ~ | |
3 | 乘方 | ** |
4 | 乘、除和模運算 | * , / , % |
5 | 算術加和減 | + , - |
6 | 移位元運算符 | << , >> |
7 | 按位元與 | & |
8 | 按位元互斥或 | ^ |
9 | 按位元或 | | |
10 | 非等操作符 | < , > , <= , >= |
11 | 等於操作符 | == , != |
12 | 邏輯與 | && |
13 | 邏輯或 | || |
14 | 三元操作符 | <conditional> ? <if-true> : <if-false> |
15 | 賦值操作符 | = , |= , ^= , &= , <<= , >>= , += , -= , *= , /= , %= |
16 | 逗號 | , |
abi.encode(...) returns (bytes)
: ABI - 對給定引數進行編碼abi.encodePacked(...) returns (bytes)
:對給定引數執行 緊打包編碼abi.encodeWithSelector(bytes4 selector, ...) returns (bytes)
: ABI - 對給定引數進行編碼,並以給定的函數選擇器作為起始的 4 位元組資料一起返回abi.encodeWithSignature(string signature, ...) returns (bytes)
:等價於 abi.encodeWithSelector(bytes4(keccak256(signature), ...)
block.blockhash(uint blockNumber) returns (bytes32)
:指定區塊的區塊雜湊——僅可用於最新的 256 個區塊且不包括當前區塊;而 blocks 從 0.4.22 版本開始已經不推薦使用,由 blockhash(uint blockNumber)
代替block.coinbase
(address
):挖出當前區塊的礦工的地址block.difficulty
(uint
):當前區塊的難度值block.gaslimit
(uint
):當前區塊的 gas 上限block.number
(uint
):當前區塊的區塊號block.timestamp
(uint
):當前區塊的時間戳gasleft() returns (uint256)
:剩餘的 gasmsg.data
(bytes
):完整的 calldatamsg.gas
(uint
):剩餘的 gas - 自 0.4.21 版本開始已經不推薦使用,由 gesleft()
代替msg.sender
(address
):訊息傳送方(當前呼叫)msg.value
(uint
):隨訊息傳送的 wei 的數量now
(uint
):當前區塊的時間戳(等價於 block.timestamp
)tx.gasprice
(uint
):交易的 gas pricetx.origin
(address
):交易傳送方(完整呼叫鏈上的原始傳送方)assert(bool condition)
:如果條件值為 false
則中止執行並回退所有狀態變更(用做內部錯誤)require(bool condition)
:如果條件值為 false
則中止執行並回退所有狀態變更(用做異常輸入或外部元件錯誤)require(bool condition, string message)
:如果條件值為 false
則中止執行並回退所有狀態變更(用做異常輸入或外部元件錯誤),可以同時提供錯誤訊息revert()
:中止執行並回復所有狀態變更revert(string message)
:中止執行並回復所有狀態變更,可以同時提供錯誤訊息blockhash(uint blockNumber) returns (bytes32)
:指定區塊的區塊雜湊——僅可用於最新的 256 個區塊keccak256(...) returns (bytes32)
:計算 緊打包編碼 的 Ethereum-SHA-3(Keccak-256)雜湊sha3(...) returns (bytes32)
:等價於 keccak256
sha256(...) returns (bytes32)
:計算 緊打包編碼 的 SHA-256 雜湊ripemd160(...) returns (bytes20)
:計算 緊打包編碼 的 RIPEMD-160 雜湊ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address)
:基於橢圓曲線簽名找回與指定公鑰關聯的地址,發生錯誤的時候返回 0addmod(uint x, uint y, uint k) returns (uint)
:計算 (x + y) % k
的值,其中加法的結果即使超過 2**256
也不會被擷取。從 0.5.0 版本開始會加入對 k != 0
的 assert(即會在此函數開頭執行 assert(k != 0);
作為引數檢查,譯者注)。mulmod(uint x, uint y, uint k) returns (uint)
:計算 (x * y) % k
的值,其中乘法的結果即使超過 2**256
也不會被擷取。從 0.5.0 版本開始會加入對 k != 0
的 assert(即會在此函數開頭執行 assert(k != 0);
作為引數檢查,譯者注)。this
(型別為當前合約的變數):當前合約範例,可以準確地轉換為 address
super
:當前合約的上一級繼承關係的合約selfdestruct(address recipient)
:銷燬當前合約,把餘額傳送到給定地址suicide(address recipient)
:與 selfdestruct
等價,但已不推薦使用<address>.balance
(uint256
): 地址型別 的餘額,以 Wei 為單位<address>.send(uint256 amount) returns (bool)
:向 地址型別 傳送給定數量的 Wei,失敗時返回 false
<address>.transfer(uint256 amount)
:向 地址型別 傳送給定數量的 Wei,失敗時會把錯誤丟擲(throw)註解
不要用 block.timestamp
、now
或者 blockhash
作為隨機種子,除非你明確知道你在做什麼。
時間戳和區塊雜湊都可以在一定程度上被礦工所影響。如果你用雜湊值作為隨機種子,那麼例如挖礦團體中的壞人就可以使用給定的雜湊來執行一個賭場功能,如果他們沒贏錢,他們可以簡單地換一個雜湊再試。
當前區塊的時間戳必須比前一個區塊的時間戳大,但唯一可以確定的就是它會是權威鏈(主鏈或者主分支)上兩個連續區塊時間戳之間的一個數值。
註解
出於擴充套件性的原因,你無法取得所有區塊的雜湊。只有最新的 256 個區塊的雜湊可以拿到,其他的都將為 0。
function myFunction() <visibility specifier> returns (bool) {
return true;
}
public
:內部、外部均可見(參考為儲存/狀態變數建立 getter 函數)private
:僅在當前合約內可見external
:僅在外部可見(僅可修飾函數)——就是說,僅可用於訊息呼叫(即使在合約內呼叫,也只能通過 this.func
的方式)internal
:僅在內部可見(也就是在當前 Solidity 原始碼檔案內均可見,不僅限於當前合約內,譯者注)pure
修飾函數時:不允許修改或存取狀態——但目前並不是強制的。view
修飾函數時:不允許修改狀態——但目前不是強制的。payable
修飾函數時:允許從呼叫中接收 以太幣Ether 。constant
修飾狀態變數時:不允許賦值(除初始化以外),不會佔據 儲存插槽storage slot 。constant
修飾函數時:與 view
等價。anonymous
修飾事件時:不把事件簽名作為 topic 儲存。indexed
修飾事件時:將引數作為 topic 儲存。以下是 Solidity 的保留字,未來可能會變為語法的一部分:
abstract
, after
, alias
, apply
, auto
, case
, catch
, copyof
, default
, define
, final
, immutable
, implements
, in
, inline
, let
, macro
, match
, mutable
, null
, of
, override
, partial
, promise
, reference
, relocatable
, sealed
, sizeof
, static
, supports
, switch
, try
, type
, typedef
, typeof
,unchecked
.
SourceUnit = (PragmaDirective | ImportDirective | ContractDefinition)*
// Pragma actually parses anything up to the trailing ';' to be fully forward-compatible.
PragmaDirective = 'pragma' Identifier ([^;]+) ';'
ImportDirective = 'import' StringLiteral ('as' Identifier)? ';'
| 'import' ('*' | Identifier) ('as' Identifier)? 'from' StringLiteral ';'
| 'import' '{' Identifier ('as' Identifier)? ( ',' Identifier ('as' Identifier)? )* '}' 'from' StringLiteral ';'
ContractDefinition = ( 'contract' | 'library' | 'interface' ) Identifier
( 'is' InheritanceSpecifier (',' InheritanceSpecifier )* )?
'{' ContractPart* '}'
ContractPart = StateVariableDeclaration | UsingForDeclaration
| StructDefinition | ModifierDefinition | FunctionDefinition | EventDefinition | EnumDefinition
InheritanceSpecifier = UserDefinedTypeName ( '(' Expression ( ',' Expression )* ')' )?
StateVariableDeclaration = TypeName ( 'public' | 'internal' | 'private' | 'constant' )* Identifier ('=' Expression)? ';'
UsingForDeclaration = 'using' Identifier 'for' ('*' | TypeName) ';'
StructDefinition = 'struct' Identifier '{'
( VariableDeclaration ';' (VariableDeclaration ';')* ) '}'
ModifierDefinition = 'modifier' Identifier ParameterList? Block
ModifierInvocation = Identifier ( '(' ExpressionList? ')' )?
FunctionDefinition = 'function' Identifier? ParameterList
( ModifierInvocation | StateMutability | 'external' | 'public' | 'internal' | 'private' )*
( 'returns' ParameterList )? ( ';' | Block )
EventDefinition = 'event' Identifier EventParameterList 'anonymous'? ';'
EnumValue = Identifier
EnumDefinition = 'enum' Identifier '{' EnumValue? (',' EnumValue)* '}'
ParameterList = '(' ( Parameter (',' Parameter)* )? ')'
Parameter = TypeName StorageLocation? Identifier?
EventParameterList = '(' ( EventParameter (',' EventParameter )* )? ')'
EventParameter = TypeName 'indexed'? Identifier?
FunctionTypeParameterList = '(' ( FunctionTypeParameter (',' FunctionTypeParameter )* )? ')'
FunctionTypeParameter = TypeName StorageLocation?
// semantic restriction: mappings and structs (recursively) containing mappings
// are not allowed in argument lists
VariableDeclaration = TypeName StorageLocation? Identifier
TypeName = ElementaryTypeName
| UserDefinedTypeName
| Mapping
| ArrayTypeName
| FunctionTypeName
UserDefinedTypeName = Identifier ( '.' Identifier )*
Mapping = 'mapping' '(' ElementaryTypeName '=>' TypeName ')'
ArrayTypeName = TypeName '[' Expression? ']'
FunctionTypeName = 'function' FunctionTypeParameterList ( 'internal' | 'external' | StateMutability )*
( 'returns' FunctionTypeParameterList )?
StorageLocation = 'memory' | 'storage' | 'calldata'
StateMutability = 'pure' | 'constant' | 'view' | 'payable'
Block = '{' Statement* '}'
Statement = IfStatement | WhileStatement | ForStatement | Block | InlineAssemblyStatement |
( DoWhileStatement | PlaceholderStatement | Continue | Break | Return |
Throw | EmitStatement | SimpleStatement ) ';'
ExpressionStatement = Expression
IfStatement = 'if' '(' Expression ')' Statement ( 'else' Statement )?
WhileStatement = 'while' '(' Expression ')' Statement
PlaceholderStatement = '_'
SimpleStatement = VariableDefinition | ExpressionStatement
ForStatement = 'for' '(' (SimpleStatement)? ';' (Expression)? ';' (ExpressionStatement)? ')' Statement
InlineAssemblyStatement = 'assembly' StringLiteral? InlineAssemblyBlock
DoWhileStatement = 'do' Statement 'while' '(' Expression ')'
Continue = 'continue'
Break = 'break'
Return = 'return' Expression?
Throw = 'throw'
EmitStatement = 'emit' FunctionCall
VariableDefinition = ('var' IdentifierList | VariableDeclaration | '(' VariableDeclaration? (',' VariableDeclaration? )* ')' ) ( '=' Expression )?
IdentifierList = '(' ( Identifier? ',' )* Identifier? ')'
// Precedence by order (see github.com/ethereum/solidity/pull/732)
Expression
= Expression ('++' | '--')
| NewExpression
| IndexAccess
| MemberAccess
| FunctionCall
| '(' Expression ')'
| ('!' | '~' | 'delete' | '++' | '--' | '+' | '-') Expression
| Expression '**' Expression
| Expression ('*' | '/' | '%') Expression
| Expression ('+' | '-') Expression
| Expression ('<<' | '>>') Expression
| Expression '&' Expression
| Expression '^' Expression
| Expression '|' Expression
| Expression ('<' | '>' | '<=' | '>=') Expression
| Expression ('==' | '!=') Expression
| Expression '&&' Expression
| Expression '||' Expression
| Expression '?' Expression ':' Expression
| Expression ('=' | '|=' | '^=' | '&=' | '<<=' | '>>=' | '+=' | '-=' | '*=' | '/=' | '%=') Expression
| PrimaryExpression
PrimaryExpression = BooleanLiteral
| NumberLiteral
| HexLiteral
| StringLiteral
| TupleExpression
| Identifier
| ElementaryTypeNameExpression
ExpressionList = Expression ( ',' Expression )*
NameValueList = Identifier ':' Expression ( ',' Identifier ':' Expression )*
FunctionCall = Expression '(' FunctionCallArguments ')'
FunctionCallArguments = '{' NameValueList? '}'
| ExpressionList?
NewExpression = 'new' TypeName
MemberAccess = Expression '.' Identifier
IndexAccess = Expression '[' Expression? ']'
BooleanLiteral = 'true' | 'false'
NumberLiteral = ( HexNumber | DecimalNumber ) (' ' NumberUnit)?
NumberUnit = 'wei' | 'szabo' | 'finney' | 'ether'
| 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'years'
HexLiteral = 'hex' ('"' ([0-9a-fA-F]{2})* '"' | '\'' ([0-9a-fA-F]{2})* '\'')
StringLiteral = '"' ([^"\r\n\\] | '\\' .)* '"'
Identifier = [a-zA-Z_$] [a-zA-Z_$0-9]*
HexNumber = '0x' [0-9a-fA-F]+
DecimalNumber = [0-9]+ ( '.' [0-9]* )? ( [eE] [0-9]+ )?
TupleExpression = '(' ( Expression? ( ',' Expression? )* )? ')'
| '[' ( Expression ( ',' Expression )* )? ']'
ElementaryTypeNameExpression = ElementaryTypeName
ElementaryTypeName = 'address' | 'bool' | 'string' | 'var'
| Int | Uint | Byte | Fixed | Ufixed
Int = 'int' | 'int8' | 'int16' | 'int24' | 'int32' | 'int40' | 'int48' | 'int56' | 'int64' | 'int72' | 'int80' | 'int88' | 'int96' | 'int104' | 'int112' | 'int120' | 'int128' | 'int136' | 'int144' | 'int152' | 'int160' | 'int168' | 'int176' | 'int184' | 'int192' | 'int200' | 'int208' | 'int216' | 'int224' | 'int232' | 'int240' | 'int248' | 'int256'
Uint = 'uint' | 'uint8' | 'uint16' | 'uint24' | 'uint32' | 'uint40' | 'uint48' | 'uint56' | 'uint64' | 'uint72' | 'uint80' | 'uint88' | 'uint96' | 'uint104' | 'uint112' | 'uint120' | 'uint128' | 'uint136' | 'uint144' | 'uint152' | 'uint160' | 'uint168' | 'uint176' | 'uint184' | 'uint192' | 'uint200' | 'uint208' | 'uint216' | 'uint224' | 'uint232' | 'uint240' | 'uint248' | 'uint256'
Byte = 'byte' | 'bytes' | 'bytes1' | 'bytes2' | 'bytes3' | 'bytes4' | 'bytes5' | 'bytes6' | 'bytes7' | 'bytes8' | 'bytes9' | 'bytes10' | 'bytes11' | 'bytes12' | 'bytes13' | 'bytes14' | 'bytes15' | 'bytes16' | 'bytes17' | 'bytes18' | 'bytes19' | 'bytes20' | 'bytes21' | 'bytes22' | 'bytes23' | 'bytes24' | 'bytes25' | 'bytes26' | 'bytes27' | 'bytes28' | 'bytes29' | 'bytes30' | 'bytes31' | 'bytes32'
Fixed = 'fixed' | ( 'fixed' [0-9]+ 'x' [0-9]+ )
Ufixed = 'ufixed' | ( 'ufixed' [0-9]+ 'x' [0-9]+ )
InlineAssemblyBlock = '{' AssemblyItem* '}'
AssemblyItem = Identifier | FunctionalAssemblyExpression | InlineAssemblyBlock | AssemblyLocalBinding | AssemblyAssignment | AssemblyLabel | NumberLiteral | StringLiteral | HexLiteral
AssemblyLocalBinding = 'let' Identifier ':=' FunctionalAssemblyExpression
AssemblyAssignment = ( Identifier ':=' FunctionalAssemblyExpression ) | ( '=:' Identifier )
AssemblyLabel = Identifier ':'
FunctionalAssemblyExpression = Identifier '(' AssemblyItem? ( ',' AssemblyItem )* ')'