2017-02-02 20:52:59 +00:00
***** ***** ***** ***
2021-02-12 13:09:09 +00:00
Using the Compiler
2017-02-02 20:52:59 +00:00
***** ***** ***** ***
2021-03-15 12:20:03 +00:00
.. index :: ! commandline compiler, compiler;commandline, ! solc
2016-06-10 15:25:51 +00:00
.. _commandline-compiler:
Using the Commandline Compiler
***** ***** ***** ***** ***** *****
2018-02-26 23:36:01 +00:00
.. note ::
2018-09-25 16:43:27 +00:00
This section does not apply to :ref: `solcjs <solcjs>` , not even if it is used in commandline mode.
2018-02-26 23:36:01 +00:00
2021-02-12 13:09:09 +00:00
Basic Usage
2020-11-14 00:20:38 +00:00
-----------
2016-06-10 15:25:51 +00:00
One of the build targets of the Solidity repository is `` solc `` , the solidity commandline compiler.
Using `` solc --help `` provides you with an explanation of all options. The compiler can produce various outputs, ranging from simple binaries and assembly over an abstract syntax tree (parse tree) to estimations of gas usage.
2021-02-23 11:39:19 +00:00
If you only want to compile a single file, you run it as `` solc --bin sourceFile.sol `` and it will print the binary. If you want to get some of the more advanced output variants of `` solc `` , it is probably better to tell it to output everything to separate files using `` solc -o outputDirectory --bin --ast-compact-json --asm sourceFile.sol `` .
2018-05-02 15:07:12 +00:00
2021-02-12 13:09:09 +00:00
Optimizer Options
2020-11-14 00:20:38 +00:00
-----------------
2018-09-25 16:43:27 +00:00
Before you deploy your contract, activate the optimizer when compiling using `` solc --optimize --bin sourceFile.sol `` .
2019-02-26 12:14:41 +00:00
By default, the optimizer will optimize the contract assuming it is called 200 times across its lifetime
(more specifically, it assumes each opcode is executed around 200 times).
2018-09-25 16:43:27 +00:00
If you want the initial contract deployment to be cheaper and the later function executions to be more expensive,
2019-01-22 17:36:26 +00:00
set it to `` --optimize-runs=1 `` . If you expect many transactions and do not care for higher deployment cost and
output size, set `` --optimize-runs `` to a high number.
2019-02-26 12:14:41 +00:00
This parameter has effects on the following (this might change in the future):
2021-07-14 15:55:05 +00:00
- the size of the binary search in the function dispatch routine
- the way constants like large numbers or strings are stored
2016-06-10 15:25:51 +00:00
2021-08-20 17:17:44 +00:00
.. index :: allowed paths, --allow-paths, base path, --base-path, include paths, --include-path
2021-03-15 12:20:03 +00:00
Base Path and Import Remapping
------------------------------
2020-11-14 00:20:38 +00:00
2016-06-10 15:25:51 +00:00
The commandline compiler will automatically read imported files from the filesystem, but
2021-03-15 12:20:03 +00:00
it is also possible to provide :ref: `path redirects <import-remapping>` using `` prefix=path `` in the following way:
2016-06-10 15:25:51 +00:00
2021-07-14 16:52:39 +00:00
.. code-block :: bash
2016-06-10 15:25:51 +00:00
2018-08-09 18:46:22 +00:00
solc github.com/ethereum/dapp-bin/=/usr/local/lib/dapp-bin/ file.sol
2016-06-10 15:25:51 +00:00
This essentially instructs the compiler to search for anything starting with
2018-08-09 18:46:22 +00:00
`` github.com/ethereum/dapp-bin/ `` under `` /usr/local/lib/dapp-bin `` .
2021-03-15 12:20:03 +00:00
When accessing the filesystem to search for imports, :ref:`paths that do not start with ./
2021-08-20 17:17:44 +00:00
or ../ <direct-imports>` are treated as relative to the directories specified using
`` --base-path `` and `` --include-path `` options (or the current working directory if base path is not specified).
Furthermore, the part of the path added via these options will not appear in the contract metadata.
2021-03-15 12:20:03 +00:00
2021-07-21 18:19:28 +00:00
For security reasons the compiler has :ref: `restrictions on what directories it can access <allowed-paths>` .
2021-03-15 12:20:03 +00:00
Directories of source files specified on the command line and target paths of
remappings are automatically allowed to be accessed by the file reader, but everything
else is rejected by default.
Additional paths (and their subdirectories) can be allowed via the
`` --allow-paths /sample/path,/another/sample/path `` switch.
2020-05-13 11:51:49 +00:00
Everything inside the path specified via `` --base-path `` is always allowed.
2021-03-15 12:20:03 +00:00
The above is only a simplification of how the compiler handles import paths.
For a detailed explanation with examples and discussion of corner cases please refer to the section on
:ref: `path resolution <path-resolution>` .
.. index :: ! linker, ! --link, ! --libraries
2020-11-14 00:20:38 +00:00
.. _library-linking:
2021-02-12 13:09:09 +00:00
Library Linking
2020-11-14 00:20:38 +00:00
---------------
2018-10-08 13:08:12 +00:00
If your contracts use :ref: `libraries <libraries>` , you will notice that the bytecode contains substrings of the form `` __$53aea86b7d70b31448b230b20ae141a537$__ `` . These are placeholders for the actual library addresses.
The placeholder is a 34 character prefix of the hex encoding of the keccak256 hash of the fully qualified library name.
The bytecode file will also contain lines of the form `` // <placeholder> -> <fq library name> `` at the end to help
identify which libraries the placeholders represent. Note that the fully qualified library name
is the path of its source file and the library name separated by `` : `` .
2018-10-08 10:18:56 +00:00
You can use `` solc `` as a linker meaning that it will insert the library addresses for you at those points:
2016-06-10 15:25:51 +00:00
2021-01-02 09:24:26 +00:00
Either add `` --libraries "file.sol:Math=0x1234567890123456789012345678901234567890 file.sol:Heap=0xabCD567890123456789012345678901234567890" `` to your command to provide an address for each library (use commas or spaces as separators) or store the string in a file (one library per line) and run `` solc `` using `` --libraries fileName `` .
.. note ::
Starting Solidity 0.8.1 accepts `` = `` as separator between library and address, and `` : `` as a separator is deprecated. It will be removed in the future. Currently `` --libraries "file.sol:Math:0x1234567890123456789012345678901234567890 file.sol:Heap:0xabCD567890123456789012345678901234567890" `` will work too.
2016-06-10 15:25:51 +00:00
2021-03-15 12:20:03 +00:00
.. index :: --standard-json, --base-path
2019-11-21 18:51:28 +00:00
If `` solc `` is called with the option `` --standard-json `` , it will expect a JSON input (as explained below) on the standard input, and return a JSON output on the standard output. This is the recommended interface for more complex and especially automated uses. The process will always terminate in a "success" state and report any errors via the JSON output.
2020-05-13 11:51:49 +00:00
The option `` --base-path `` is also processed in standard-json mode.
2017-04-24 10:25:24 +00:00
2020-11-14 00:20:38 +00:00
If `` solc `` is called with the option `` --link `` , all input files are interpreted to be unlinked binaries (hex-encoded) in the `` __$53aea86b7d70b31448b230b20ae141a537$__ `` -format given above and are linked in-place (if the input is read from stdin, it is written to stdout). All options except `` --libraries `` are ignored (including `` -o `` ) in this case.
.. warning ::
Manually linking libraries on the generated bytecode is discouraged because it does not update
contract metadata. Since metadata contains a list of libraries specified at the time of
compilation and bytecode contains a metadata hash, you will get different binaries, depending
on when linking is performed.
You should ask the compiler to link the libraries at the time a contract is compiled by either
using the `` --libraries `` option of `` solc `` or the `` libraries `` key if you use the
standard-JSON interface to the compiler.
2018-10-08 10:18:56 +00:00
.. note ::
The library placeholder used to be the fully qualified name of the library itself
instead of the hash of it. This format is still supported by `` solc --link `` but
the compiler will no longer output it. This change was made to reduce
the likelihood of a collision between libraries, since only the first 36 characters
of the fully qualified library name could be used.
2018-08-13 14:46:46 +00:00
.. _evm-version:
.. index :: ! EVM version, compile target
2021-02-12 13:09:09 +00:00
Setting the EVM Version to Target
2018-08-13 14:46:46 +00:00
***** ***** ***** ***** ***** ***** ***
When you compile your contract code you can specify the Ethereum virtual machine
version to compile for to avoid particular features or behaviours.
.. warning ::
Compiling for the wrong EVM version can result in wrong, strange and failing
behaviour. Please ensure, especially if running a private chain, that you
use matching EVM versions.
2018-09-25 16:43:27 +00:00
On the command line, you can select the EVM version as follows:
2018-08-13 14:46:46 +00:00
.. code-block :: shell
solc --evm-version <VERSION> contract.sol
2018-09-25 16:43:27 +00:00
In the :ref: `standard JSON interface <compiler-api>` , use the `` "evmVersion" ``
key in the `` "settings" `` field:
2018-08-13 14:46:46 +00:00
2021-07-14 16:53:15 +00:00
.. code-block :: javascript
2018-08-13 14:46:46 +00:00
2021-07-14 16:56:55 +00:00
{
2021-07-14 16:53:15 +00:00
"sources": {/* ... * /},
2021-07-14 16:56:55 +00:00
"settings": {
2021-07-14 16:53:15 +00:00
"optimizer": {/* ... * /},
2021-07-14 16:56:55 +00:00
"evmVersion": "<VERSION>"
}
2018-09-25 16:43:27 +00:00
}
2018-08-13 14:46:46 +00:00
2021-02-12 13:09:09 +00:00
Target Options
2018-08-13 14:46:46 +00:00
--------------
Below is a list of target EVM versions and the compiler-relevant changes introduced
at each version. Backward compatibility is not guaranteed between each version.
2020-04-20 16:24:31 +00:00
- `` homestead ``
- (oldest version)
2018-08-13 14:46:46 +00:00
- `` tangerineWhistle ``
2019-02-25 14:51:26 +00:00
- Gas cost for access to other accounts increased, relevant for gas estimation and the optimizer.
- All gas sent by default for external calls, previously a certain amount had to be retained.
2018-08-13 14:46:46 +00:00
- `` spuriousDragon ``
2019-02-25 14:51:26 +00:00
- Gas cost for the `` exp `` opcode increased, relevant for gas estimation and the optimizer.
2019-03-04 11:43:27 +00:00
- `` byzantium ``
2019-02-25 14:51:26 +00:00
- Opcodes `` returndatacopy `` , `` returndatasize `` and `` staticcall `` are available in assembly.
- The `` staticcall `` opcode is used when calling non-library view or pure functions, which prevents the functions from modifying state at the EVM level, i.e., even applies when you use invalid type conversions.
- It is possible to access dynamic data returned from function calls.
2018-08-13 14:46:46 +00:00
- `` revert `` opcode introduced, which means that `` revert() `` will not waste gas.
2019-02-25 14:51:26 +00:00
- `` constantinople ``
- Opcodes `` create2 ` , ` ` extcodehash `` , `` shl `` , `` shr `` and `` sar `` are available in assembly.
- Shifting operators use shifting opcodes and thus need less gas.
2019-12-04 09:42:14 +00:00
- `` petersburg ``
2019-02-25 14:51:26 +00:00
- The compiler behaves the same way as with constantinople.
2021-04-13 07:15:50 +00:00
- `` istanbul ``
2019-09-19 09:00:43 +00:00
- Opcodes `` chainid `` and `` selfbalance `` are available in assembly.
2021-08-09 08:06:53 +00:00
- `` berlin ``
2021-04-13 07:15:50 +00:00
- Gas costs for `` SLOAD `` , `` *CALL `` , `` BALANCE `` , `` EXT* `` and `` SELFDESTRUCT `` increased. The
compiler assumes cold gas costs for such operations. This is relevant for gas estimation and
the optimizer.
2021-08-09 08:06:53 +00:00
- `` london `` (**default** )
- The block's base fee (`EIP-3198 <https://eips.ethereum.org/EIPS/eip-3198> `_ and `EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559> `_ ) can be accessed via the global `` block.basefee `` or `` basefee() `` in inline assembly.
2019-02-25 14:51:26 +00:00
2018-08-13 14:46:46 +00:00
2021-03-15 12:20:03 +00:00
.. index :: ! standard JSON, ! --standard-json
2017-02-02 20:52:59 +00:00
.. _compiler-api:
2016-06-10 15:25:51 +00:00
2017-02-02 20:52:59 +00:00
Compiler Input and Output JSON Description
***** ***** ***** ***** ***** ***** ***** ***** **
2018-09-25 16:43:27 +00:00
The recommended way to interface with the Solidity compiler especially for
more complex and automated setups is the so-called JSON-input-output interface.
The same interface is provided by all distributions of the compiler.
The fields are generally subject to change,
some are optional (as noted), but we try to only make backwards compatible changes.
2017-02-02 20:52:59 +00:00
2017-02-08 19:22:40 +00:00
The compiler API expects a JSON formatted input and outputs the compilation result in a JSON formatted output.
2019-11-21 18:51:28 +00:00
The standard error output is not used and the process will always terminate in a "success" state, even
if there were errors. Errors are always reported as part of the JSON output.
2017-02-08 19:22:40 +00:00
2018-09-25 16:43:27 +00:00
The following subsections describe the format through an example.
2016-06-10 15:25:51 +00:00
Comments are of course not permitted and used here only for explanatory purposes.
Input Description
-----------------
2021-07-14 16:53:15 +00:00
.. code-block :: javascript
2016-12-02 13:46:56 +00:00
2016-06-10 15:25:51 +00:00
{
2017-04-21 14:34:40 +00:00
// Required: Source code language. Currently supported are "Solidity" and "Yul".
2019-01-11 16:47:06 +00:00
"language": "Solidity",
2017-02-02 13:54:21 +00:00
// Required
2019-01-11 16:47:06 +00:00
"sources":
2016-06-10 15:25:51 +00:00
{
2017-02-02 13:54:21 +00:00
// The keys here are the "global" names of the source files,
// imports can use other files via remappings (see below).
"myFile.sol":
{
// Optional: keccak256 hash of the source file
2017-02-02 21:10:19 +00:00
// It is used to verify the retrieved content if imported via URLs.
2017-02-02 13:54:21 +00:00
"keccak256": "0x123...",
// Required (unless "content" is used, see below): URL(s) to the source file.
// URL(s) should be imported in this order and the result checked against the
// keccak256 hash (if available). If the hash doesn't match or none of the
// URL(s) result in success, an error should be raised.
2019-01-11 16:47:06 +00:00
// Using the commandline interface only filesystem paths are supported.
// With the JavaScript interface the URL will be passed to the user-supplied
// read callback, so any URL supported by the callback can be used.
2017-02-02 13:54:21 +00:00
"urls":
[
"bzzr://56ab...",
"ipfs://Qma...",
2019-01-11 16:47:06 +00:00
"/tmp/path/to/file.sol"
2018-09-03 13:11:35 +00:00
// If files are used, their directories should be added to the command line via
// `--allow-paths <path>` .
2017-02-02 13:54:21 +00:00
]
},
2020-01-08 08:17:59 +00:00
"destructible":
2017-02-02 13:54:21 +00:00
{
// Optional: keccak256 hash of the source file
"keccak256": "0x234...",
// Required (unless "urls" is used): literal contents of the source file
2020-01-08 08:17:59 +00:00
"content": "contract destructible is owned { function shutdown() { if (msg.sender == owner) selfdestruct(owner); } }"
2017-02-02 13:54:21 +00:00
}
2016-06-10 15:25:51 +00:00
},
2017-02-02 13:58:30 +00:00
// Optional
2019-01-11 16:47:06 +00:00
"settings":
2016-06-10 15:25:51 +00:00
{
2020-07-08 20:08:50 +00:00
// Optional: Stop compilation after the given stage. Currently only "parsing" is valid here
"stopAfter": "parsing",
2017-02-02 13:58:30 +00:00
// Optional: Sorted list of remappings
2019-03-28 19:01:41 +00:00
"remappings": [ ":g=/dir" ],
2018-05-02 15:07:12 +00:00
// Optional: Optimizer settings
2019-01-11 16:47:06 +00:00
"optimizer": {
2021-06-14 15:15:40 +00:00
// Disabled by default.
// NOTE: enabled=false still leaves some optimizations on. See comments below.
// WARNING: Before version 0.8.6 omitting the 'enabled' key was not equivalent to setting
// it to false and would actually disable all the optimizations.
2019-01-11 16:47:06 +00:00
"enabled": true,
2018-05-02 15:07:12 +00:00
// Optimize for how many times you intend to run the code.
2019-07-15 12:33:50 +00:00
// Lower values will optimize more for initial deployment cost, higher
// values will optimize more for high-frequency usage.
2019-02-21 16:39:47 +00:00
"runs": 200,
// Switch optimizer components on or off in detail.
// The "enabled" switch above provides two defaults which can be
// tweaked here. If "details" is given, "enabled" can be omitted.
"details": {
2019-07-15 12:33:50 +00:00
// The peephole optimizer is always on if no details are given,
// use details to switch it off.
2019-02-21 16:39:47 +00:00
"peephole": true,
2021-01-14 12:02:14 +00:00
// The inliner is always on if no details are given,
// use details to switch it off.
"inliner": true,
2019-07-15 12:33:50 +00:00
// The unused jumpdest remover is always on if no details are given,
// use details to switch it off.
2019-02-21 16:39:47 +00:00
"jumpdestRemover": true,
// Sometimes re-orders literals in commutative operations.
"orderLiterals": false,
// Removes duplicate code blocks
"deduplicate": false,
// Common subexpression elimination, this is the most complicated step but
// can also provide the largest gain.
"cse": false,
// Optimize representation of literal numbers and strings in code.
"constantOptimizer": false,
2020-10-29 18:40:09 +00:00
// The new Yul optimizer. Mostly operates on the code of ABI coder v2
2020-01-02 22:01:50 +00:00
// and inline assembly.
// It is activated together with the global optimizer setting
// and can be deactivated here.
// Before Solidity 0.6.0 it had to be activated through this switch.
2019-02-21 16:39:47 +00:00
"yul": false,
2019-02-26 18:55:13 +00:00
// Tuning options for the Yul optimizer.
"yulDetails": {
// Improve allocation of stack slots for variables, can free up stack slots early.
// Activated by default if the Yul optimizer is activated.
2020-05-04 13:45:44 +00:00
"stackAllocation": true,
// Select optimization steps to be applied.
// Optional, the optimizer will use the default sequence if omitted.
"optimizerSteps": "dhfoDgvulfnTUtnIf..."
2019-02-26 18:55:13 +00:00
}
2019-02-21 16:39:47 +00:00
}
2017-02-02 13:58:30 +00:00
},
2019-07-15 12:33:50 +00:00
// Version of the EVM to compile for.
// Affects type checking and code generation. Can be homestead,
2019-08-26 12:09:55 +00:00
// tangerineWhistle, spuriousDragon, byzantium, constantinople, petersburg, istanbul or berlin
2019-07-15 12:33:50 +00:00
"evmVersion": "byzantium",
2020-11-12 01:31:06 +00:00
// Optional: Change compilation pipeline to go through the Yul intermediate representation.
// This is a highly EXPERIMENTAL feature, not to be used for production. This is false by default.
"viaIR": true,
2019-09-18 14:44:36 +00:00
// Optional: Debugging settings
"debug": {
// How to treat revert (and require) reason strings. Settings are
// "default", "strip", "debug" and "verboseDebug".
// "default" does not inject compiler-generated revert strings and keeps user-supplied ones.
// "strip" removes all revert strings (if possible, i.e. if literals are used) keeping side-effects
2020-01-22 14:48:56 +00:00
// "debug" injects strings for compiler-generated internal reverts, implemented for ABI encoders V1 and V2 for now.
2019-09-18 14:44:36 +00:00
// "verboseDebug" even appends further information to user-supplied revert strings (not yet implemented)
"revertStrings": "default"
2021-07-14 16:53:15 +00:00
},
2017-02-02 15:00:01 +00:00
// Metadata settings (optional)
2019-01-11 16:47:06 +00:00
"metadata": {
2017-02-02 15:00:01 +00:00
// Use only literal content and not URLs (false by default)
2019-09-06 17:11:07 +00:00
"useLiteralContent": true,
// Use the given hash method for the metadata hash that is appended to the bytecode.
// The metadata hash can be removed from the bytecode via option "none".
// The other options are "ipfs" and "bzzr1".
// If the option is omitted, "ipfs" is used by default.
"bytecodeHash": "ipfs"
2017-02-02 15:00:01 +00:00
},
2019-07-15 12:33:50 +00:00
// Addresses of the libraries. If not all libraries are given here,
// it can result in unlinked objects whose output data is different.
2019-01-11 16:47:06 +00:00
"libraries": {
2017-02-02 13:58:30 +00:00
// The top level key is the the name of the source file where the library is used.
2019-07-15 12:33:50 +00:00
// If remappings are used, this source file should match the global path
// after remappings were applied.
2017-02-02 13:58:30 +00:00
// If this key is an empty string, that refers to a global level.
"myFile.sol": {
"MyLib": "0x123123..."
}
2021-07-14 16:53:15 +00:00
},
2018-12-11 12:58:02 +00:00
// The following can be used to select desired outputs based
// on file and contract names.
// If this field is omitted, then the compiler loads and does type checking,
// but will not generate any outputs apart from errors.
// The first level key is the file name and the second level key is the contract name.
// An empty contract name is used for outputs that are not tied to a contract
// but to the whole source file like the AST.
// A star as contract name refers to all contracts in the file.
// Similarly, a star as a file name matches all files.
// To select all outputs the compiler can possibly generate, use
// "outputSelection: { "*": { "* ": [ "*" ], "": [ "* " ] } }"
// but note that this might slow down the compilation process needlessly.
2017-02-02 14:57:07 +00:00
//
// The available output types are as follows:
2018-12-11 12:58:02 +00:00
//
// File level (needs empty string as contract name):
2017-05-22 13:47:09 +00:00
// ast - AST of all source files
2018-12-11 12:58:02 +00:00
//
// Contract level (needs the contract name or "*"):
// abi - ABI
2017-02-02 14:57:07 +00:00
// devdoc - Developer documentation (natspec)
// userdoc - User documentation (natspec)
// metadata - Metadata
2017-04-21 14:34:40 +00:00
// ir - Yul intermediate representation of the code before optimization
// irOptimized - Intermediate representation after optimization
2019-10-30 17:11:33 +00:00
// storageLayout - Slots, offsets and types of the contract's state variables.
2017-04-21 14:34:40 +00:00
// evm.assembly - New assembly format
2017-04-07 13:59:13 +00:00
// evm.legacyAssembly - Old-style assembly format in JSON
2020-05-07 12:46:47 +00:00
// evm.bytecode.functionDebugData - Debugging information at function level
2017-03-29 21:30:00 +00:00
// evm.bytecode.object - Bytecode object
// evm.bytecode.opcodes - Opcodes list
// evm.bytecode.sourceMap - Source mapping (useful for debugging)
// evm.bytecode.linkReferences - Link references (if unlinked object)
2020-09-28 13:38:28 +00:00
// evm.bytecode.generatedSources - Sources generated by the compiler
2020-04-06 09:22:07 +00:00
// evm.deployedBytecode* - Deployed bytecode (has all the options that evm.bytecode has)
// evm.deployedBytecode.immutableReferences - Map from AST ids to bytecode ranges that reference immutables
2017-02-02 14:57:07 +00:00
// evm.methodIdentifiers - The list of function hashes
// evm.gasEstimates - Function gas estimates
2020-10-30 19:44:59 +00:00
// ewasm.wast - Ewasm in WebAssembly S-expressions format
// ewasm.wasm - Ewasm in WebAssembly binary format
2017-04-07 14:34:38 +00:00
//
// Note that using a using `evm` , `evm.bytecode` , `ewasm` , etc. will select every
2017-10-19 13:24:33 +00:00
// target part of that output. Additionally, `*` can be used as a wildcard to request everything.
2017-04-07 14:34:38 +00:00
//
2019-01-11 16:47:06 +00:00
"outputSelection": {
2017-02-02 14:57:07 +00:00
"*": {
2019-01-11 16:47:06 +00:00
"*": [
"metadata", "evm.bytecode" // Enable the metadata and bytecode outputs of every single contract.
, "evm.bytecode.sourceMap" // Enable the source map output of every single contract.
2019-02-05 15:33:55 +00:00
],
"": [
"ast" // Enable the AST output of every single file.
2019-01-11 16:47:06 +00:00
]
2017-02-02 14:57:07 +00:00
},
// Enable the abi and opcodes output of MyContract defined in file def.
"def": {
2018-01-21 08:35:59 +00:00
"MyContract": [ "abi", "evm.bytecode.opcodes" ]
2017-02-02 14:57:07 +00:00
}
2020-10-14 15:44:40 +00:00
},
2021-04-14 10:56:43 +00:00
// The modelChecker object is experimental and subject to changes.
2020-12-09 14:15:49 +00:00
"modelChecker":
{
2021-04-15 10:31:45 +00:00
// Chose which contracts should be analyzed as the deployed one.
2021-07-14 16:53:15 +00:00
"contracts":
2021-04-15 10:31:45 +00:00
{
"source1.sol": ["contract1"],
"source2.sol": ["contract2", "contract3"]
},
2021-07-15 15:39:01 +00:00
// Choose whether division and modulo operations should be replaced by
// multiplication with slack variables. Default is `true` .
// Using `false` here is recommended if you are using the CHC engine
// and not using Spacer as the Horn solver (using Eldarica, for example).
// See the Formal Verification section for a more detailed explanation of this option.
"divModWithSlacks": true,
2020-12-09 14:15:49 +00:00
// Choose which model checker engine to use: all (default), bmc, chc, none.
"engine": "chc",
2021-07-28 16:58:03 +00:00
// Choose whether to output all unproved targets. The default is `false` .
"showUnproved": true,
2021-08-06 15:40:45 +00:00
// Choose which solvers should be used, if available.
// See the Formal Verification section for the solvers description.
"solvers": ["cvc4", "smtlib2", "z3"],
2021-04-14 10:56:43 +00:00
// Choose which targets should be checked: constantCondition,
2021-03-23 18:15:14 +00:00
// underflow, overflow, divByZero, balance, assert, popEmptyArray, outOfBounds.
2021-07-07 10:31:09 +00:00
// If the option is not given all targets are checked by default,
// except underflow/overflow for Solidity >=0.8.7.
2021-01-19 11:56:22 +00:00
// See the Formal Verification section for the targets description.
2021-04-14 10:56:43 +00:00
"targets": ["underflow", "overflow", "assert"],
2020-12-09 14:15:49 +00:00
// Timeout for each SMT query in milliseconds.
// If this option is not given, the SMTChecker will use a deterministic
// resource limit by default.
// A given timeout of 0 means no resource/time restrictions for any query.
"timeout": 20000
}
2016-06-10 15:25:51 +00:00
}
}
2016-11-16 13:05:24 +00:00
2017-02-02 20:52:59 +00:00
Output Description
------------------
2016-11-16 13:05:24 +00:00
2021-07-14 16:53:15 +00:00
.. code-block :: javascript
2016-11-16 13:05:24 +00:00
{
2021-06-30 12:48:45 +00:00
// Optional: not present if no errors/warnings/infos were encountered
2019-01-11 16:47:06 +00:00
"errors": [
2017-02-02 21:18:09 +00:00
{
2017-02-02 21:35:07 +00:00
// Optional: Location within the source file.
2019-01-11 16:47:06 +00:00
"sourceLocation": {
"file": "sourceFile.sol",
"start": 0,
"end": 100
2021-07-14 16:53:15 +00:00
},
2019-07-03 12:21:09 +00:00
// Optional: Further locations (e.g. places of conflicting declarations)
"secondarySourceLocations": [
{
"file": "sourceFile.sol",
"start": 64,
"end": 92,
"message": "Other declaration is here:"
}
],
2017-10-20 04:51:13 +00:00
// Mandatory: Error type, such as "TypeError", "InternalCompilerError", "Exception", etc.
// See below for complete list of types.
2019-01-11 16:47:06 +00:00
"type": "TypeError",
2017-05-02 13:49:13 +00:00
// Mandatory: Component where the error originated, such as "general", "ewasm", etc.
2019-01-11 16:47:06 +00:00
"component": "general",
2021-06-30 12:48:45 +00:00
// Mandatory ("error", "warning" or "info", but please note that this may be extended in the future)
2019-01-11 16:47:06 +00:00
"severity": "error",
2020-05-29 23:42:36 +00:00
// Optional: unique code for the cause of the error
"errorCode": "3141",
2017-02-02 21:18:09 +00:00
// Mandatory
2020-05-29 23:42:36 +00:00
"message": "Invalid keyword",
2017-04-07 14:37:11 +00:00
// Optional: the message formatted with source location
2019-01-11 16:47:06 +00:00
"formattedMessage": "sourceFile.sol:100: Invalid keyword"
2017-02-02 21:18:09 +00:00
}
],
2019-07-15 12:33:50 +00:00
// This contains the file-level outputs.
// It can be limited/filtered by the outputSelection settings.
2019-01-11 16:47:06 +00:00
"sources": {
2017-02-02 22:06:10 +00:00
"sourceFile.sol": {
2018-12-11 14:35:32 +00:00
// Identifier of the source (used in source maps)
2019-01-11 16:47:06 +00:00
"id": 1,
2017-02-02 22:06:10 +00:00
// The AST object
2021-07-14 16:53:15 +00:00
"ast": {}
2017-02-02 22:06:10 +00:00
}
},
2019-07-15 12:33:50 +00:00
// This contains the contract-level outputs.
// It can be limited/filtered by the outputSelection settings.
2019-01-11 16:47:06 +00:00
"contracts": {
2017-02-02 21:27:12 +00:00
"sourceFile.sol": {
2017-02-08 19:00:07 +00:00
// If the language used has no contract names, this field should equal to an empty string.
2017-02-02 21:27:12 +00:00
"ContractName": {
// The Ethereum Contract ABI. If empty, it is represented as an empty array.
2020-11-18 14:20:34 +00:00
// See https://docs.soliditylang.org/en/develop/abi-spec.html
2019-01-11 16:47:06 +00:00
"abi": [],
2017-03-29 21:33:14 +00:00
// See the Metadata Output documentation (serialised JSON string)
2021-07-14 16:53:15 +00:00
"metadata": "{/* ... * /}",
2017-03-29 21:23:35 +00:00
// User documentation (natspec)
2019-01-11 16:47:06 +00:00
"userdoc": {},
2017-03-29 21:23:35 +00:00
// Developer documentation (natspec)
2019-01-11 16:47:06 +00:00
"devdoc": {},
2017-04-07 13:48:35 +00:00
// Intermediate representation (string)
2019-01-11 16:47:06 +00:00
"ir": "",
2019-10-30 17:11:33 +00:00
// See the Storage Layout documentation.
2021-07-14 16:53:15 +00:00
"storageLayout": {"storage": [/* ... * /], "types": {/* ... * /} },
2017-03-29 21:23:35 +00:00
// EVM-related outputs
2019-01-11 16:47:06 +00:00
"evm": {
2017-02-02 21:52:35 +00:00
// Assembly (string)
2019-01-11 16:47:06 +00:00
"assembly": "",
2017-04-07 13:59:13 +00:00
// Old-style assembly (object)
2019-01-11 16:47:06 +00:00
"legacyAssembly": {},
2017-02-02 21:33:46 +00:00
// Bytecode and related details.
2019-01-11 16:47:06 +00:00
"bytecode": {
2020-05-07 12:46:47 +00:00
// Debugging data at the level of functions.
"functionDebugData": {
// Now follows a set of functions including compiler-internal and
// user-defined function. The set does not have to be complete.
"@mint_13": { // Internal name of the function
"entryPoint": 128, // Byte offset into the bytecode where the function starts (optional)
"id": 13, // AST ID of the function definition or null for compiler-internal functions (optional)
"parameterSlots": 2, // Number of EVM stack slots for the function parameters (optional)
"returnSlots": 1 // Number of EVM stack slots for the return values (optional)
}
},
2017-02-02 21:33:46 +00:00
// The bytecode as a hex string.
2019-01-11 16:47:06 +00:00
"object": "00fe",
2017-03-29 21:21:33 +00:00
// Opcodes list (string)
2019-01-11 16:47:06 +00:00
"opcodes": "",
2017-02-02 21:33:46 +00:00
// The source mapping as a string. See the source mapping definition.
2019-01-11 16:47:06 +00:00
"sourceMap": "",
2020-09-28 13:38:28 +00:00
// Array of sources generated by the compiler. Currently only
// contains a single Yul file.
"generatedSources": [{
// Yul AST
2021-07-14 16:53:15 +00:00
"ast": {/* ... * /},
2020-09-28 13:38:28 +00:00
// Source file in its text form (may contain comments)
"contents":"{ function abi_decode(start, end) -> data { data := calldataload(start) } }",
// Source file ID, used for source references, same "namespace" as the Solidity source files
"id": 2,
"language": "Yul",
"name": "#utility.yul"
2021-07-14 16:53:15 +00:00
}],
2017-02-02 21:33:46 +00:00
// If given, this is an unlinked object.
2019-01-11 16:47:06 +00:00
"linkReferences": {
2017-02-02 21:33:46 +00:00
"libraryFile.sol": {
2019-07-15 12:33:50 +00:00
// Byte offsets into the bytecode.
// Linking replaces the 20 bytes located there.
2017-02-02 21:35:57 +00:00
"Library1": [
2019-01-11 16:47:06 +00:00
{ "start": 0, "length": 20 },
{ "start": 200, "length": 20 }
2017-02-02 21:35:57 +00:00
]
2017-02-02 21:33:46 +00:00
}
}
2017-03-29 21:21:33 +00:00
},
2020-04-06 09:22:07 +00:00
"deployedBytecode": {
2021-07-14 16:53:15 +00:00
/* ..., * / // The same layout as above.
2020-08-12 23:16:10 +00:00
"immutableReferences": {
2020-04-06 09:22:07 +00:00
// There are two references to the immutable with AST ID 3, both 32 bytes long. One is
// at bytecode offset 42, the other at bytecode offset 80.
"3": [{ "start": 42, "length": 32 }, { "start": 80, "length": 32 }]
2020-08-12 23:16:10 +00:00
}
2020-04-06 09:22:07 +00:00
},
2017-02-02 21:52:35 +00:00
// The list of function hashes
2019-01-11 16:47:06 +00:00
"methodIdentifiers": {
2017-03-29 21:25:05 +00:00
"delegate(address)": "5c19a95c"
2017-02-02 21:52:35 +00:00
},
// Function gas estimates
2019-01-11 16:47:06 +00:00
"gasEstimates": {
"creation": {
"codeDepositCost": "420000",
"executionCost": "infinite",
"totalCost": "infinite"
2017-02-02 21:52:35 +00:00
},
2019-01-11 16:47:06 +00:00
"external": {
2017-04-27 12:13:49 +00:00
"delegate(address)": "25000"
2017-02-02 21:52:35 +00:00
},
2019-01-11 16:47:06 +00:00
"internal": {
2017-04-27 12:13:49 +00:00
"heavyLifting()": "infinite"
2017-02-02 21:52:35 +00:00
}
}
2017-02-02 21:27:12 +00:00
},
2020-10-30 19:44:59 +00:00
// Ewasm related outputs
2019-01-11 16:47:06 +00:00
"ewasm": {
2017-02-02 21:52:35 +00:00
// S-expressions format
2019-01-11 16:47:06 +00:00
"wast": "",
2017-02-02 21:52:35 +00:00
// Binary format (hex string)
2019-01-11 16:47:06 +00:00
"wasm": ""
2017-03-29 21:23:35 +00:00
}
2017-02-02 21:27:12 +00:00
}
2016-11-16 13:05:24 +00:00
}
2017-05-02 13:49:13 +00:00
}
2016-11-16 13:05:24 +00:00
}
2017-10-20 04:51:13 +00:00
2021-02-12 13:09:09 +00:00
Error Types
2017-10-20 04:51:13 +00:00
~~~~~~~~~~~
1. `` JSONError `` : JSON input doesn't conform to the required format, e.g. input is not a JSON object, the language is not supported, etc.
2. `` IOError `` : IO and import processing errors, such as unresolvable URL or hash mismatch in supplied sources.
3. `` ParserError `` : Source code doesn't conform to the language rules.
4. `` DocstringParsingError `` : The NatSpec tags in the comment block cannot be parsed.
5. `` SyntaxError `` : Syntactical error, such as `` continue `` is used outside of a `` for `` loop.
6. `` DeclarationError `` : Invalid, unresolvable or clashing identifier names. e.g. `` Identifier not found ``
7. `` TypeError `` : Error within the type system, such as invalid type conversions, invalid assignments, etc.
8. `` UnimplementedFeatureError `` : Feature is not supported by the compiler, but is expected to be supported in future versions.
9. `` InternalCompilerError `` : Internal bug triggered in the compiler - this should be reported as an issue.
10. `` Exception `` : Unknown failure during compilation - this should be reported as an issue.
11. `` CompilerError `` : Invalid use of the compiler stack - this should be reported as an issue.
12. `` FatalError `` : Fatal error not processed correctly - this should be reported as an issue.
13. `` Warning `` : A warning, which didn't stop the compilation, but should be addressed if possible.
2021-06-30 12:48:45 +00:00
14. `` Info `` : Information that the compiler thinks the user might find useful, but is not dangerous and does not necessarily need to be addressed.
2019-12-09 16:01:31 +00:00
.. _compiler-tools:
2021-02-12 13:09:09 +00:00
Compiler Tools
2019-12-09 16:01:31 +00:00
***** ***** *** *
solidity-upgrade
----------------
`` solidity-upgrade `` can help you to semi-automatically upgrade your contracts
to breaking language changes. While it does not and cannot implement all
required changes for every breaking release, it still supports the ones, that
would need plenty of repetitive manual adjustments otherwise.
.. note ::
`` solidity-upgrade `` carries out a large part of the work, but your
contracts will most likely need further manual adjustments. We recommend
using a version control system for your files. This helps reviewing and
eventually rolling back the changes made.
.. warning ::
`` solidity-upgrade `` is not considered to be complete or free from bugs, so
please use with care.
2021-02-12 13:09:09 +00:00
How it Works
2019-12-09 16:01:31 +00:00
~~~~~~~~~~~~
You can pass (a) Solidity source file(s) to `` solidity-upgrade [files] `` . If
these make use of `` import `` statement which refer to files outside the
current source file's directory, you need to specify directories that
are allowed to read and import files from, by passing
`` --allow-paths [directory] `` . You can ignore missing files by passing
`` --ignore-missing `` .
`` solidity-upgrade `` is based on `` libsolidity `` and can parse, compile and
analyse your source files, and might find applicable source upgrades in them.
Source upgrades are considered to be small textual changes to your source code.
They are applied to an in-memory representation of the source files
given. The corresponding source file is updated by default, but you can pass
`` --dry-run `` to simulate to whole upgrade process without writing to any file.
The upgrade process itself has two phases. In the first phase source files are
parsed, and since it is not possible to upgrade source code on that level,
errors are collected and can be logged by passing `` --verbose `` . No source
upgrades available at this point.
In the second phase, all sources are compiled and all activated upgrade analysis
modules are run alongside compilation. By default, all available modules are
activated. Please read the documentation on
:ref: `available modules <upgrade-modules>` for further details.
This can result in compilation errors that may
be fixed by source upgrades. If no errors occur, no source upgrades are being
reported and you're done.
If errors occur and some upgrade module reported a source upgrade, the first
reported one gets applied and compilation is triggered again for all given
source files. The previous step is repeated as long as source upgrades are
reported. If errors still occur, you can log them by passing `` --verbose `` .
If no errors occur, your contracts are up to date and can be compiled with
the latest version of the compiler.
.. _upgrade-modules:
2021-02-12 13:09:09 +00:00
Available Upgrade Modules
2019-12-09 16:01:31 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~
2020-06-24 09:43:31 +00:00
+----------------------------+---------+--------------------------------------------------+
| Module | Version | Description |
+============================+=========+==================================================+
| `` constructor `` | 0.5.0 | Constructors must now be defined using the |
| | | `` constructor `` keyword. |
+----------------------------+---------+--------------------------------------------------+
| `` visibility `` | 0.5.0 | Explicit function visibility is now mandatory, |
| | | defaults to `` public `` . |
+----------------------------+---------+--------------------------------------------------+
| `` abstract `` | 0.6.0 | The keyword `` abstract `` has to be used if a |
| | | contract does not implement all its functions. |
+----------------------------+---------+--------------------------------------------------+
| `` virtual `` | 0.6.0 | Functions without implementation outside an |
| | | interface have to be marked `` virtual `` . |
+----------------------------+---------+--------------------------------------------------+
| `` override `` | 0.6.0 | When overriding a function or modifier, the new |
| | | keyword `` override `` must be used. |
+----------------------------+---------+--------------------------------------------------+
| `` dotsyntax `` | 0.7.0 | The following syntax is deprecated: |
| | | `` f.gas(...)() `` , `` f.value(...)() `` and |
| | | `` (new C).value(...)() `` . Replace these calls by |
| | | `` f{gas: ..., value: ...}() `` and |
| | | `` (new C){value: ...}() `` . |
+----------------------------+---------+--------------------------------------------------+
| `` now `` | 0.7.0 | The `` now `` keyword is deprecated. Use |
| | | `` block.timestamp `` instead. |
+----------------------------+---------+--------------------------------------------------+
| `` constructor-visibility `` | 0.7.0 | Removes visibility of constructors. |
| | | |
+----------------------------+---------+--------------------------------------------------+
2019-12-09 16:01:31 +00:00
2020-04-30 08:05:29 +00:00
Please read :doc: `0.5.0 release notes <050-breaking-changes>` ,
2020-09-23 10:29:00 +00:00
:doc: `0.6.0 release notes <060-breaking-changes>` ,
:doc: `0.7.0 release notes <070-breaking-changes>` and :doc: `0.8.0 release notes <080-breaking-changes>` for further details.
2019-12-09 16:01:31 +00:00
Synopsis
~~~~~~~~
.. code-block :: none
Usage: solidity-upgrade [options] contract.sol
Allowed options:
--help Show help message and exit.
--version Show version and exit.
--allow-paths path(s)
Allow a given path for imports. A list of paths can be
supplied by separating them with a comma.
--ignore-missing Ignore missing files.
--modules module(s) Only activate a specific upgrade module. A list of
modules can be supplied by separating them with a comma.
--dry-run Apply changes in-memory only and don't write to input
file.
--verbose Print logs, errors and changes. Shortens output of
upgrade patches.
--unsafe Accept *unsafe* changes.
2021-02-12 13:09:09 +00:00
Bug Reports / Feature Requests
2019-12-09 16:01:31 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If you found a bug or if you have a feature request, please
`file an issue <https://github.com/ethereum/solidity/issues/new/choose> `_ on Github.
Example
~~~~~~~
2020-07-08 15:41:45 +00:00
Assume that you have the following contract in `` Source.sol `` :
2019-12-09 16:01:31 +00:00
2020-08-06 11:58:14 +00:00
.. code-block :: Solidity
2019-12-09 16:01:31 +00:00
2020-07-08 15:41:45 +00:00
pragma solidity >=0.6.0 <0.6.4;
// This will not compile after 0.7.0
2020-05-13 15:45:58 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-07-08 15:41:45 +00:00
contract C {
// FIXME: remove constructor visibility and make the contract abstract
constructor() internal {}
2019-12-09 16:01:31 +00:00
}
2020-07-08 15:41:45 +00:00
contract D {
uint time;
function f() public payable {
// FIXME: change now to block.timestamp
time = now;
}
2019-12-09 16:01:31 +00:00
}
2020-07-08 15:41:45 +00:00
contract E {
D d;
2019-12-09 16:01:31 +00:00
2020-07-08 15:41:45 +00:00
// FIXME: remove constructor visibility
constructor() public {}
2019-12-09 16:01:31 +00:00
2020-07-08 15:41:45 +00:00
function g() public {
// FIXME: change .value(5) => {value: 5}
d.f.value(5)();
}
2019-12-09 16:01:31 +00:00
}
2020-07-08 15:41:45 +00:00
2021-02-12 13:09:09 +00:00
Required Changes
2019-12-09 16:01:31 +00:00
^^^^^^^^^^^^^^^^
2020-07-08 15:41:45 +00:00
The above contract will not compile starting from 0.7.0. To bring the contract up to date with the
current Solidity version, the following upgrade modules have to be executed:
`` constructor-visibility `` , `` now `` and `` dotsyntax `` . Please read the documentation on
:ref: `available modules <upgrade-modules>` for further details.
2019-12-09 16:01:31 +00:00
2021-02-12 13:09:09 +00:00
Running the Upgrade
2019-12-09 16:01:31 +00:00
^^^^^^^^^^^^^^^^^^^
2020-07-08 15:41:45 +00:00
It is recommended to explicitly specify the upgrade modules by using `` --modules `` argument.
2019-12-09 16:01:31 +00:00
2021-07-14 17:05:42 +00:00
.. code-block :: bash
2019-12-09 16:01:31 +00:00
2021-07-14 17:05:42 +00:00
solidity-upgrade --modules constructor-visibility,now,dotsyntax Source.sol
2019-12-09 16:01:31 +00:00
2020-07-08 15:41:45 +00:00
The command above applies all changes as shown below. Please review them carefully (the pragmas will
have to be updated manually.)
2019-12-09 16:01:31 +00:00
2020-08-06 11:58:14 +00:00
.. code-block :: Solidity
2019-12-09 16:01:31 +00:00
2020-07-08 15:41:45 +00:00
// SPDX-License-Identifier: GPL-3.0
2020-09-29 07:53:50 +00:00
pragma solidity >=0.7.0 <0.9.0;
2020-07-08 15:41:45 +00:00
abstract contract C {
// FIXME: remove constructor visibility and make the contract abstract
constructor() {}
2019-12-09 16:01:31 +00:00
}
2020-07-08 15:41:45 +00:00
contract D {
uint time;
function f() public payable {
// FIXME: change now to block.timestamp
time = block.timestamp;
}
2019-12-09 16:01:31 +00:00
}
2020-07-08 15:41:45 +00:00
contract E {
D d;
2019-12-09 16:01:31 +00:00
2020-07-08 15:41:45 +00:00
// FIXME: remove constructor visibility
constructor() {}
2019-12-09 16:01:31 +00:00
2020-07-08 15:41:45 +00:00
function g() public {
// FIXME: change .value(5) => {value: 5}
d.f{value: 5}();
}
2019-12-09 16:01:31 +00:00
}