备忘单

运算符的优先顺序

以下是按计算顺序列出的运算符的优先顺序。

优先

描述

算符

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

逗号运算符

,

全局变量

  • 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 to abi.encodeWithSelector(bytes4(keccak256(bytes(signature)), ...)`

  • bytes.concat(...) returns (bytes memory): Concatenates variable number of arguments to one byte array

  • block.basefee (uint): current block's base fee (EIP-3198 and EIP-1559)

  • block.chainid (uint): current chain id

  • block.coinbase (address payable): current block miner's address

  • block.difficulty (uint): current block difficulty

  • block.gaslimit (uint): current block gaslimit

  • block.number (uint): current block number

  • block.timestamp (uint): current block timestamp

  • gasleft() returns (uint256) :剩余气体

  • msg.data (bytes): complete calldata

  • msg.sender (address): sender of the message (current call)

  • msg.value (uint): number of wei sent with the message

  • tx.gasprice (uint): gas price of the transaction

  • tx.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 to address or address 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 contract

  • type(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 type T, see Type Information.

  • type(T).max (T): the maximum value representable by the integer type T, see Type Information.

注解

当对合同进行离链评估时,而不是在挡路中包含的交易上下文中进行评估时,您不应假设 block.*tx.* 指的是任何特定挡路或交易的价值。这些值由执行契约的EVM实现提供,可以是任意的。

注解

不要依赖 block.timestampblockhash 作为随机性的来源,除非你知道你在做什么。

时间戳和块散列在某种程度上都会受到矿工的影响。例如,采矿社区中的坏参与者可以在所选哈希上运行赌场付款函数,如果他们没有收到任何钱,只需重试其他哈希。

当前块时间戳必须严格大于最后一个块的时间戳,但唯一的保证是它将介于规范链中两个连续块的时间戳之间。

注解

由于可伸缩性的原因,块散列不能用于所有块。您只能访问最近256个块的哈希值,所有其他值都将为零。

注解

在0.5.0版中,删除了以下别名: suicide 作为别名 selfdestructmsg.gas 作为别名 gasleftblock.blockhash 作为别名 blockhashsha3 作为别名 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 :声明此函数、修饰符或公共状态变量更改基协定中函数或修饰符的行为。

保留关键字

这些关键字保持一致。它们将来可能成为语法的一部分:

afteraliasapplyautobytecasecopyofdefaultdefinefinalimplementsininlineletmacromatchmutablenullofpartialpromisereferencerelocatablesealedsizeofstaticsupportsswitchtypedeftypeofvar