Solidity 编译器

Clicking the Solidity icon in the icon panel brings you to the Solidity Compiler. The default view of the Solidity Compiler shows the basic configuration. To open the Advanced Configuration panel, click the Advanced Configuration button (C. in fig. 1). For details on advanced features - see below.

Solidity compiler basics

选择一个合约进行编译

在文件管理器中打开一个文件即可进行编译。如果打开了多个文件,请确保你想编译的文件在编辑器中处于选中状态。

If there is not an active file in the editor or a file has not already been compiled, then the Solidity Compiler will look like this:

触发编译

当你这么做的时候会触发编译:

  • 点击编译按钮 (下面图1中的D.)

  • 使用快捷键 control + s.

  • 在文件管理器中右键单击文件并选择编译选项

自动编译

If the auto compile checkbox (B. in fig. 1 above) is checked, compilation will occur every few seconds (when the file is auto-saved) as well as when another file is selected. If a contract has a lot of dependencies, it can take a while to compile - so you use autocompilation at your discretion.

Solidity 版本 & Remix 功能

编译器下拉列表中已经选中了一个编译器版本(见上图1中的A.)。

您可以编译(且部署)比 0.4.12 更老的 Solidity 版本的合约。但是这些更老的编译器使用了传统的抽象语法树 — 我们已经不再支持。因此,有些插件可能无法使用,有些功能 — 比如编辑器语法高亮可能只有部分还能工作。

Using the Contract selectbox

Because a Solidity file can include multiple contracts and because contracts can import other contracts, multiple contracts are often compiled. However, only 1 contract's compilation details can be retrieved at a time.

To select the desired contract, use the Contract selectbox (F. in fig. 1). Forgetting to select the right contract is a common mistake - so remember to verify that the contract you want is selected.

编译细节和发布

Using the publish button, you can upload your contract to IPFS or Swarm (only non abstract contracts can be published to Swarm).

当发布一个引用了其他合约的合约时,主合约和所有引用合约都会被发布 - 每一个都会有对应的地址。

发布的数据包含合约的元数据和 solidity 源码。

After either Publish on IPFS or Publish on Swarm is clicked a modal will pop up. This modal contains the contract's address as well as the addresses of the contracts that it imported and the address of the contract's metadata.

点击 "Compilation Details" 按钮 (见图1中的G.),会弹出一个对话框展示包含当前选中合约详情信息。

如果你只想获取 ABIBytecode - 点击对应的按钮即可。见图1中的H.

被动合约验证

When you publish your metadata to IPFS and deploy your code to the mainnet or a public testnet, the contract verification service Sourcify, will verify your contracts without you needing to do anything.

编译且执行脚本

The Compile and Run script button (E. in fig. 1) is for compiling and then immediately running a script. It's a time saver so that you can write some code, automatically run script that sets state of the contract - thus allowing you to quickly understand how the code is working. Learn more about Compile and Run.

编译错误和警告

编译错误和警告展示在合约区域的下方。每次编译,静态分析选项卡都会生成一份报告。

It is important to address reported issues even if the compiler doesn't complain. (more about static analysis)

Advanced compiler configurations

点击高级编译配置会打开如下面板 (见下图2中的M.)。

这里有一个单选按钮可以选择通过交互界面配置编译器 (见图2中的N) 还是用JSON 文件进行配置 (见图2中的R)。

Solidity or Yul

自 Solidity 0.5.7 版本后就可以编译 Yul 文件。请阅读(solidity 文档中关于 Yul 的内容),里面包含了代码示例。您可以在语言下拉框 (见图2中的O.) 中切换语言。Yul 选项只对大于等于 0.5.7 版本的编译器有效。

选择 EVM 版本

EVM 下拉选框 (图2中的P.) 可以让您对特定的以太坊硬分叉版本进行编译。compiler default对应使用默认硬分叉版本作为给定版本。

To see the name of the hard fork used in the current compilation, click the "Compilation Details" button and in the Metadata section there will be a sub-section called settings. Open up the settings to see the EVM version's name.

启用优化

根据Solidity的文档,“优化器试图简化复杂的表达式,从而减少代码大小和执行成本,也就是说,它可以减少合约部署所需的gas,以及对该合约进行外部调用所需的gas。”

For recent versions of Solidity, it is recommended to enable optimization.

想要深入了解优化, (图2中的Q.) 可以访问 Solidity 文档中关于优化器的内容

To the right of the Enable optimization checkbox is the box to input the number of Optimization runs. The default value is 200.

你可能会问 - “对于我的合约来说,正确的次数应该是多少?” Solidity 文档是这么说的:

如果你希望部署合约的时候油费便宜一点,且以后执行合约方法更贵一点,你可以设置 --optimize-runs=1 。如果你预料到这个合约以后会有很多交易,且并不在乎高昂的部署费用和输出尺寸,那就设置 --optimize-runs 为一个更高的值。

想了解更多关于优化次数的细节,请访问 Solidity 文档中关于优化器选项的内容

JSON 文件的编译器配置

Selecting the radio button next to Use configuration file will let you set the configuration using a JSON file (T. in fig 2). When you switch to compile with a config file, a sample compiler config file is created. This file can be edited with all the available options.

Clicking the config file's name will open it up in the Editor. To change the config file click the Change button. If you update the text box with a file name of a file that does not exist, a new file will be created containing the default file's contents.

使用 .json 文件配置是不会有错误检测的,所以请确保您的配置文件是正确的。

使用自定义 Solidity 编译器

For those writing your own custom Solidity Compiler, you can import that by clicking the + button (X. in fig 1) to open a modal where you can input the URL of the compiler to be loaded.