备忘单
运算符的优先顺序
以下是按计算顺序列出的运算符的优先顺序。
优先 |
描述 |
算符 |
---|---|---|
1 |
后置增减量 |
|
新建表达式 |
|
|
数组订阅 |
|
|
成员访问 |
|
|
函数式调用 |
|
|
圆括号 |
|
|
2 |
前缀递增和递减 |
|
一元减 |
|
|
一元运算 |
|
|
逻辑非 |
|
|
按位取反 |
|
|
3 |
求幂 |
|
4 |
乘法、除法和模 |
|
5 |
加减法 |
|
6 |
位移位运算符 |
|
7 |
按位与 |
|
8 |
按位异或 |
|
9 |
按位或 |
|
10 |
不等式运算符 |
|
11 |
相等运算符 |
|
12 |
逻辑与 |
|
13 |
逻辑或 |
|
14 |
三元运算符 |
|
分配运算符 |
|
|
15 |
逗号运算符 |
|
全局变量
abi.decode(bytes memory encodedData, (...)) returns (...)
: ABI-decodes the provided data. The types are given in parentheses as second argument. Example:(uint a, uint[2] memory b, bytes memory c) = abi.decode(data, (uint, uint[2], bytes))
abi.encode(...) returns (bytes memory)
: ABI -对给定参数进行编码abi.encodePacked(...) returns (bytes memory)
:执行 packed encoding 给出的参数。请注意,此编码可能不明确!abi.encodeWithSelector(bytes4 selector, ...) returns (bytes memory)
: ABI -从第二个开始对给定参数进行编码,并在给定的四字节选择器前面加上前缀abi.encodeWithSignature(string memory signature, ...) returns (bytes memory)
: Equivalent toabi.encodeWithSelector(bytes4(keccak256(bytes(signature)), ...)`
bytes.concat(...) returns (bytes memory)
: Concatenates variable number of arguments to one byte arrayblock.basefee
(uint
): current block's base fee (EIP-3198 and EIP-1559)block.chainid
(uint
): current chain idblock.coinbase
(address payable
): current block miner's addressblock.difficulty
(uint
): current block difficultyblock.gaslimit
(uint
): current block gaslimitblock.number
(uint
): current block numberblock.timestamp
(uint
): current block timestampgasleft() returns (uint256)
:剩余气体msg.data
(bytes
): complete calldatamsg.sender
(address
): sender of the message (current call)msg.value
(uint
): number of wei sent with the messagetx.gasprice
(uint
): gas price of the transactiontx.origin
(address
): sender of the transaction (full call chain)assert(bool condition)
:如果条件为,则中止执行并还原状态更改false
(用于内部错误)require(bool condition)
:如果条件为,则中止执行并还原状态更改false
(用于错误的输入或外部组件中的错误)require(bool condition, string memory message)
:如果条件为,则中止执行并还原状态更改false
(用于错误的输入或外部组件中的错误)。还提供错误消息。revert()
:中止执行并还原状态更改revert(string memory message)
:中止执行并还原状态更改,提供解释字符串blockhash(uint blockNumber) returns (bytes32)
:给定块的哈希-仅适用于最近256个块keccak256(bytes memory) returns (bytes32)
:计算输入的keccak-256哈希sha256(bytes memory) returns (bytes32)
:计算输入的sha-256哈希ripemd160(bytes memory) returns (bytes20)
:计算输入的ripemd-160哈希ecrecover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) returns (address)
:从椭圆曲线签名恢复与公钥关联的地址,出错时返回零addmod(uint x, uint y, uint k) returns (uint)
:计算(x + y) % k
如果加法是以任意精度执行的,并且在2**256
.断言k != 0
从0.5.0版开始。mulmod(uint x, uint y, uint k) returns (uint)
:计算(x * y) % k
其中乘法是以任意精度执行的,而不是在2**256
.断言k != 0
从0.5.0版开始。this
(current contract's type): the current contract, explicitly convertible toaddress
oraddress payable
super
:继承层次结构中更高一级的协定selfdestruct(address payable recipient)
:销毁当前合同,将其资金发送到指定地址<address>.balance
(uint256
): balance of the 地址 in Wei<address>.code
(bytes memory
): code at the 地址 (can be empty)<address>.codehash
(bytes32
): the codehash of the 地址<address payable>.send(uint256 amount) returns (bool)
:将给定数量的wei发送到 地址 回报false
关于失败<address payable>.transfer(uint256 amount)
:将给定数量的wei发送到 地址 ,失败时引发type(C).name
(string
): the name of the contracttype(C).creationCode
(bytes memory
): creation bytecode of the given contract, see Type Information.type(C).runtimeCode
(bytes memory
): runtime bytecode of the given contract, see Type Information.type(I).interfaceId
(bytes4
): value containing the EIP-165 interface identifier of the given interface, see Type Information.type(T).min
(T
): the minimum value representable by the integer typeT
, see Type Information.type(T).max
(T
): the maximum value representable by the integer typeT
, see Type Information.
注解
当对合同进行离链评估时,而不是在挡路中包含的交易上下文中进行评估时,您不应假设 block.*
和 tx.*
指的是任何特定挡路或交易的价值。这些值由执行契约的EVM实现提供,可以是任意的。
注解
不要依赖 block.timestamp
或 blockhash
作为随机性的来源,除非你知道你在做什么。
时间戳和块散列在某种程度上都会受到矿工的影响。例如,采矿社区中的坏参与者可以在所选哈希上运行赌场付款函数,如果他们没有收到任何钱,只需重试其他哈希。
当前块时间戳必须严格大于最后一个块的时间戳,但唯一的保证是它将介于规范链中两个连续块的时间戳之间。
注解
由于可伸缩性的原因,块散列不能用于所有块。您只能访问最近256个块的哈希值,所有其他值都将为零。
注解
在0.5.0版中,删除了以下别名: suicide
作为别名 selfdestruct
, msg.gas
作为别名 gasleft
, block.blockhash
作为别名 blockhash
和 sha3
作为别名 keccak256
.
注解
在版本0.7.0中,别名 now
(用于 block.timestamp
)已删除。
函数可见性说明符
function myFunction() <visibility specifier> returns (bool) {
return true;
}
public
:外部和内部可见(创建 getter function 存储/状态变量)private
:仅在当前合同中可见external
:仅在外部可见(仅用于函数)-即只能调用消息(通过this.func
)internal
:仅在内部可见
修饰语
pure
对于函数:不允许修改或访问状态。view
对于函数:不允许修改状态。payable
对于功能:允许他们在接到电话的同时接收乙醚。constant
对于状态变量:不允许赋值(初始化除外),不占用存储槽。immutable
对于状态变量:在构造时只允许一个赋值,之后是常量。存储在代码中。anonymous
对于事件:不将事件签名存储为主题。indexed
对于事件参数:将参数存储为主题。virtual
对于函数和修饰符:允许在派生契约中更改函数或修饰符的行为。override
:声明此函数、修饰符或公共状态变量更改基协定中函数或修饰符的行为。
保留关键字
这些关键字保持一致。它们将来可能成为语法的一部分:
after
, alias
, apply
, auto
, byte
, case
, copyof
, default
, define
, final
, implements
, in
, inline
, let
, macro
, match
, mutable
, null
, of
, partial
, promise
, reference
, relocatable
, sealed
, sizeof
, static
, supports
, switch
, typedef
, typeof
, var
。