### Install Solidity via Homebrew Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Update Homebrew, tap the ethereum repository, and install the Solidity compiler. This method builds from source. ```bash brew update brew upgrade brew tap ethereum/ethereum brew install solidity ``` -------------------------------- ### Install Dependencies for solc-js Source: https://github.com/argotorg/solidity/wiki/Building-solc‐js-from-source Run this command to install all necessary project dependencies. ```sh npm install ``` -------------------------------- ### Install Latest Development Solc via Snap Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Install the latest development version of Solidity with the most recent changes using the --edge flag with snap. ```bash sudo snap install solc --edge ``` -------------------------------- ### Install Specific Solidity Version via Homebrew Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Clone the Homebrew Ethereum repository, checkout a specific commit hash, and install a particular Solidity version. ```bash git clone https://github.com/ethereum/homebrew-ethereum.git cd homebrew-ethereum git checkout brew unlink solidity # eg. Install 0.4.8 brew install solidity.rb ``` -------------------------------- ### ABI Encode Example Source: https://github.com/argotorg/solidity/blob/develop/docs/units-and-global-variables.md Shows how to ABI-encode arguments using abi.encode. ```Solidity abi.encode(...) ``` -------------------------------- ### Install Latest Stable Solc via Snap Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Use this command to install the latest stable version of the Solidity compiler using the snap package manager. ```bash sudo snap install solc ``` -------------------------------- ### Get Compiler Help Source: https://github.com/argotorg/solidity/blob/develop/docs/using-the-compiler.md Run `solc --help` to see all available compiler options and their explanations. ```bash solc --help ``` -------------------------------- ### ABI Decode Example Source: https://github.com/argotorg/solidity/blob/develop/docs/units-and-global-variables.md Demonstrates how to ABI-decode data using abi.decode with specified types. ```Solidity (uint a, uint[2] memory b, bytes memory c) = abi.decode(data, (uint, uint[2], bytes)) ``` -------------------------------- ### Solidity Library Example: Set Implementation Source: https://github.com/argotorg/solidity/blob/develop/docs/contracts/libraries.rst This example demonstrates how to implement a Set data structure using a Solidity library. It shows how library functions can operate on storage variables of the calling contract by accepting a storage reference as the first parameter. Note that calls to these library functions are compiled as DELEGATECALLs. ```solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.9.0; // We define a new struct datatype that will be used to // hold its data in the calling contract. struct Data { mapping(uint => bool) flags; } library Set { // Note that the first parameter is of type "storage // reference" and thus only its storage address and not // its contents is passed as part of the call. This is a // special feature of library functions. It is idiomatic // to call the first parameter `self`, if the function can // be seen as a method of that object. function insert(Data storage self, uint value) public returns (bool) { if (self.flags[value]) return false; // already there self.flags[value] = true; return true; } function remove(Data storage self, uint value) public returns (bool) { if (!self.flags[value]) return false; // not there self.flags[value] = false; return true; } function contains(Data storage self, uint value) public view returns (bool) { return self.flags[value]; } } contract C { Data knownValues; function register(uint value) public { // The library functions can be called without a // specific instance of the library, since the // "instance" will be the current contract. require(Set.insert(knownValues, value)); } // In this contract, we can also directly access knownValues.flags, if we want. } ``` -------------------------------- ### ABI EncodeWithSelector Example Source: https://github.com/argotorg/solidity/blob/develop/docs/units-and-global-variables.md Demonstrates ABI-encoding arguments and prepending a four-byte selector using abi.encodeWithSelector. ```Solidity abi.encodeWithSelector(bytes4 selector, ...) ``` -------------------------------- ### Inline Assembly Example Source: https://github.com/argotorg/solidity/blob/develop/Changelog.md Demonstrates the use of inline assembly within Solidity code. This allows for low-level manipulation and optimization. ```solidity assembly { // inline assembly code } ``` -------------------------------- ### Get Help for Soltest Options Source: https://github.com/argotorg/solidity/blob/develop/docs/contributing.md Access extensive help documentation for all available options in the soltest executable. ```bash ./build/test/soltest --help ``` -------------------------------- ### String Concat Example Source: https://github.com/argotorg/solidity/blob/develop/docs/units-and-global-variables.md Shows how to concatenate multiple string arguments into a single string using string.concat. ```Solidity string.concat(...) ``` -------------------------------- ### Solidity Contract Example Source: https://github.com/argotorg/solidity/blob/develop/docs/analysing-compilation-output.md A simple Solidity contract used to demonstrate compiler output analysis. ```solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.5.0 <0.9.0; contract C { function one() public pure returns (uint) { return 1; } } ``` -------------------------------- ### ABI EncodeWithSignature Example Source: https://github.com/argotorg/solidity/blob/develop/docs/units-and-global-variables.md Shows how to ABI-encode arguments with a signature using abi.encodeWithSignature. ```Solidity abi.encodeWithSignature(string memory signature, ...) ``` -------------------------------- ### Solidity Contract Inheritance Example Source: https://github.com/argotorg/solidity/blob/develop/docs/contracts/inheritance.rst Demonstrates multiple inheritance, virtual functions, overriding, and constructor arguments in Solidity. ```solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.7.0 <0.9.0; contract Owned { address payable owner; constructor() { owner = payable(msg.sender); } } // Use `is` to derive from another contract. Derived // contracts can access all non-private members including // internal functions and state variables. These cannot be // accessed externally via `this`, though. contract Emittable is Owned { event Emitted(); // The keyword `virtual` means that the function can change // its behavior in derived classes ("overriding"). function emitEvent() virtual public { if (msg.sender == owner) emit Emitted(); } } // These abstract contracts are only provided to make the // interface known to the compiler. Note the function // without body. If a contract does not implement all // functions it can only be used as an interface. abstract contract Config { function lookup(uint id) public virtual returns (address adr); } abstract contract NameReg { function register(bytes32 name) public virtual; function unregister() public virtual; } // Multiple inheritance is possible. Note that `Owned` is // also a base class of `Emittable`, yet there is only a single // instance of `Owned` (as for virtual inheritance in C++). contract Named is Owned, Emittable { constructor(bytes32 name) { Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970); NameReg(config.lookup(1)).register(name); } // Functions can be overridden by another function with the same name and // the same number/types of inputs. If the overriding function has different // types of output parameters, that causes an error. // Both local and message-based function calls take these overrides // into account. // If you want the function to override, you need to use the // `override` keyword. You need to specify the `virtual` keyword again // if you want this function to be overridden again. function emitEvent() public virtual override { if (msg.sender == owner) { Config config = Config(0xD5f9D8D94886E70b06E474c3fB14Fd43E2f23970); NameReg(config.lookup(1)).unregister(); // It is still possible to call a specific // overridden function. Emittable.emitEvent(); } } } // If a constructor takes an argument, it needs to be // provided in the header or modifier-invocation-style at // the constructor of the derived contract (see below). contract PriceFeed is Owned, Emittable, Named("GoldFeed") { uint info; function updateInfo(uint newInfo) public { if (msg.sender == owner) info = newInfo; } // Here, we only specify `override` and not `virtual`. // This means that contracts deriving from `PriceFeed` // cannot change the behavior of `emitEvent` anymore. function emitEvent() public override(Emittable, Named) { Named.emitEvent(); } function get() public view returns(uint r) { return info; } } ``` -------------------------------- ### Initialize Git Submodule Source: https://github.com/argotorg/solidity/blob/develop/test/ethdebugSchemaTests/README.md Initialize the ethdebug-format git submodule. This is a required setup step before running the tests. ```bash git submodule update --init test/ethdebugSchemaTests/ethdebug-format ``` -------------------------------- ### Immutable References Example Source: https://github.com/argotorg/solidity/blob/develop/docs/using-the-compiler.md Shows how immutable variables in the contract are referenced in the deployed bytecode. Each reference includes its starting position and length. ```json { "immutableReferences": { "3": [{ "start": 42, "length": 32 }, { "start": 80, "length": 32 }] } } ``` -------------------------------- ### Build Solidity on Windows with Visual Studio Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Use these commands to create a Visual Studio solution for building Solidity on Windows. ```bash mkdir build cd build cmake -G "Visual Studio 16 2019" .. ``` -------------------------------- ### Direct import examples in Solidity Source: https://github.com/argotorg/solidity/blob/develop/docs/path-resolution.md Demonstrates direct imports in Solidity, where the import path is directly used as the source unit name after applying any remappings. Paths not starting with './' or '../' are considered direct imports. ```solidity import "/project/lib/util.sol"; // source unit name: /project/lib/util.sol import "lib/util.sol"; // source unit name: lib/util.sol import "@openzeppelin/address.sol"; // source unit name: @openzeppelin/address.sol import "https://example.com/token.sol"; // source unit name: https://example.com/token.sol ``` -------------------------------- ### Solidity Contract Storage Layout Example Source: https://github.com/argotorg/solidity/blob/develop/docs/internals/layout_in_storage.md Demonstrates inheritance and custom storage layout in Solidity. Transient, constant, and immutable variables do not affect the storage layout. Dynamic arrays and mappings reserve a slot for their data address. Value types are packed tightly, with structs and arrays starting new slots. ```Solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.29; struct S { int32 x; bool y; } contract A { uint a; uint128 transient b; uint constant c = 10; uint immutable d = 12; } contract B { uint8[] e; mapping(uint => S) f; uint16 g; uint16 h; bytes16 transient i; S s; int8 k; } contract C is A, B layout at 42 { bytes21 l; uint8[10] m; bytes5[8] n; bytes5 o; } ``` -------------------------------- ### Build Solidity Docs Source: https://github.com/argotorg/solidity/blob/develop/docs/README.md Navigate to the docs directory and execute the script to install dependencies and build the project. The generated HTML files will be located in the _build/html directory. ```sh cd docs ./docs.sh ``` -------------------------------- ### Install Coreutils on macOS Source: https://github.com/argotorg/solidity/blob/develop/docs/contributing.md On macOS, install GNU coreutils using Homebrew if needed by testing scripts. ```bash brew install coreutils ``` -------------------------------- ### AFL Instrumentation Output Example Source: https://github.com/argotorg/solidity/blob/develop/docs/contributing.md Example output indicating successful instrumentation of the 'solfuzzer' binary by AFL. ```text Scanning dependencies of target solfuzzer [ 98%] Building CXX object test/tools/CMakeFiles/solfuzzer.dir/fuzzer.cpp.o afl-cc 2.52b by afl-as 2.52b by [+] Instrumented 1949 locations (64-bit, non-hardened mode, ratio 100%). [100%] Linking CXX executable solfuzzer ``` -------------------------------- ### Build Solidity on Windows Command Line Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Compile Solidity using the Release configuration from the command line on Windows. ```bash cmake --build . --config Release ``` -------------------------------- ### Install Windows Dependencies Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Run this PowerShell script to install required external dependencies for Windows builds, including Boost and CMake. ```powershell scripts\install_deps.ps1 ``` -------------------------------- ### Build Solidity with Build Script Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Easily build Solidity on Linux and macOS using the provided build script, which installs binaries to /usr/local/bin. ```bash #note: this will install binaries solc and soltest at usr/local/bin ./scripts/build.sh ``` -------------------------------- ### Example Pragma Version Source: https://github.com/argotorg/solidity/blob/develop/docs/contributing.md Ensure all code examples begin with a pragma version that spans the largest range where the contract code is valid. ```solidity pragma solidity >=0.4.0 <0.9.0; ``` -------------------------------- ### Serve Solidity Docs Locally Source: https://github.com/argotorg/solidity/blob/develop/docs/README.md Use Python's http.server module to serve the generated HTML documentation from the _build/html directory on port 8080. Access the documentation via http://localhost:8080. ```python python3 -m http.server -d _build/html --cgi 8080 ``` -------------------------------- ### List Available CMake Options Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Run this command to see all available CMake configuration options for Solidity. ```bash cmake .. -LH ``` -------------------------------- ### Install solcjs via npm Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Use npm to install the solcjs package globally. Note that the command-line executable is named solcjs, and its options are not compatible with solc. ```bash npm install --global solc ``` -------------------------------- ### Get Winner Name in Solidity Source: https://github.com/argotorg/solidity/blob/develop/docs/examples/voting.rst Retrieves the name of the winning proposal by first calling `winningProposal()` to get the winner's index and then accessing the name from the proposals array. ```solidity function winnerName() external view returns (bytes32 winnerName_) { winnerName_ = proposals[winningProposal()].name; } ``` -------------------------------- ### Solidity Contract Example Source: https://github.com/argotorg/solidity/blob/develop/docs/ir-breaking-changes.md This example demonstrates a Solidity contract with inline assembly. It highlights differences in return values between the old and new code generators due to cleanup operations. ```solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.1; contract C { function f(uint8 a) public pure returns (uint r1, uint r2) { a = ~a; assembly { r1 := a } r2 = a; } } ``` -------------------------------- ### Yul Verbatim Bytecode Example Source: https://github.com/argotorg/solidity/blob/develop/docs/yul.md Use verbatim_1i_1o to create custom bytecode sequences that the optimizer will not modify. This example multiplies an input by two, ensuring the constant 'two' remains untouched. ```yul let x := calldataload(0) let double := verbatim_1i_1o(hex"600202", x) ``` -------------------------------- ### Hello World Contract in Solidity Source: https://github.com/argotorg/solidity/blob/develop/README.md A basic 'Hello World' smart contract example. It defines a function that returns a string. Use the latest Solidity version for development. ```solidity // SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; contract HelloWorld { function helloWorld() external pure returns (string memory) { return "Hello, World!"; } } ``` -------------------------------- ### Yul Example: Memory Knowledge Preservation Source: https://github.com/argotorg/solidity/blob/develop/docs/internals/optimizer.md This Yul example shows how the optimizer preserves knowledge about memory location `x` even after an `add` operation, because `add` does not write to the memory range of `x`. ```yul let x := calldataload(0) mstore(x, 100) // Current knowledge memory location x -> 100 let y := add(x, 32) // Does not clear the knowledge that x -> 100, since y does not write to [x, x + 32) mstore(y, 200) // This Keccak-256 can now be evaluated let value := keccak256(x, 32) ``` -------------------------------- ### Full ABI Encoding Example Source: https://github.com/argotorg/solidity/blob/develop/docs/abi-spec.md Provides a comprehensive example of ABI encoding for a function with nested dynamic arrays of integers and strings. It includes function signature, offsets, element counts, and the encoded data for each element. ```none 0x2289b18c - function signature 0 - f - offset of [[1, 2], [3]] 1 - g - offset of ["one", "two", "three"] 2 - 0000000000000000000000000000000000000000000000000000000000000002 - count for [[1, 2], [3]] 3 - 0000000000000000000000000000000000000000000000000000000000000040 - offset of [1, 2] 4 - 00000000000000000000000000000000000000000000000000000000000000a0 - offset of [3] 5 - 0000000000000000000000000000000000000000000000000000000000000002 - count for [1, 2] 6 - 0000000000000000000000000000000000000000000000000000000000000001 - encoding of 1 7 - 0000000000000000000000000000000000000000000000000000000000000002 - encoding of 2 8 - 0000000000000000000000000000000000000000000000000000000000000001 - count for [3] 9 - 0000000000000000000000000000000000000000000000000000000000000003 - encoding of 3 10 - 0000000000000000000000000000000000000000000000000000000000000003 - count for ["one", "two", "three"] 11 - 0000000000000000000000000000000000000000000000000000000000000060 - offset for "one" 12 - 00000000000000000000000000000000000000000000000000000000000000a0 - offset for "two" 13 - 00000000000000000000000000000000000000000000000000000000000000e0 - offset for "three" 14 - 0000000000000000000000000000000000000000000000000000000000000003 - count for "one" 15 - 6f6e650000000000000000000000000000000000000000000000000000000000 - encoding of "one" 16 - 0000000000000000000000000000000000000000000000000000000000000003 - count for "two" 17 - 74776f0000000000000000000000000000000000000000000000000000000000 - encoding of "two" 18 - 0000000000000000000000000000000000000000000000000000000000000005 - count for "three" 19 - 7468726565000000000000000000000000000000000000000000000000000000 - encoding of "three" ``` -------------------------------- ### Compile Solidity files using Docker Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Compile Solidity files on the host machine using a Docker container. This example mounts a local directory for input and output, specifying the contract to compile along with ABI and binary output. ```bash docker run \ --volume "/tmp/some/local/path/:/sources/" \ ghcr.io/argotorg/solc:stable \ /sources/Contract.sol \ --abi \ --bin \ --output-dir /sources/output/ ``` -------------------------------- ### Yul Object Structure Example Source: https://github.com/argotorg/solidity/blob/develop/docs/yul.md Illustrates the structure of a Yul object, including nested objects, code sections, and data sections. This example shows how to define a contract with constructor logic and runtime code, as well as embedded contracts. ```yul object "Contract1" { // This is the constructor code of the contract. code { function allocate(size) -> ptr { ptr := mload(0x40) // Note that Solidity generated IR code reserves memory offset ``0x60`` as well, but a pure Yul object is free to use memory as it chooses. if iszero(ptr) { ptr := 0x60 } mstore(0x40, add(ptr, size)) } // first create "Contract2" let size := datasize("Contract2") let offset := allocate(size) // This will turn into codecopy for EVM datacopy(offset, dataoffset("Contract2"), size) // constructor parameter is a single number 0x1234 mstore(add(offset, size), 0x1234) pop(create(0, offset, add(size, 32))) // now return the runtime object (the currently // executing code is the constructor code) size := datasize("Contract1_deployed") offset := allocate(size) // This will turn into a codecopy for EVM datacopy(offset, dataoffset("Contract1_deployed"), size) return(offset, size) } data "Table2" hex"4123" object "Contract1_deployed" { code { function allocate(size) -> ptr { ptr := mload(0x40) // Note that Solidity generated IR code reserves memory offset ``0x60`` as well, but a pure Yul object is free to use memory as it chooses. if iszero(ptr) { ptr := 0x60 } mstore(0x40, add(ptr, size)) } // runtime code mstore(0, "Hello, World!") return(0, 0x20) } } // Embedded object. Use case is that the outside is a factory contract, // and Contract2 is the code to be created by the factory object "Contract2" { code { // code here ... } object "Contract2_deployed" { code { // code here ... } } data "Table1" hex"4123" } } ``` -------------------------------- ### Path Normalization Examples for Imports Source: https://github.com/argotorg/solidity/blob/develop/docs/path-resolution.md Illustrates how Solidity's import path resolution algorithm handles non-normalized paths, including redundant segments like './', '//', and '../'. The resulting source unit names show the effect of normalization. ```solidity import "./util/./util.sol"; // source unit name: lib/src/../util/util.sol ``` ```solidity import "./util//util.sol"; // source unit name: lib/src/../util/util.sol ``` ```solidity import "../util/../array/util.sol"; // source unit name: lib/src/array/util.sol ``` ```solidity import "../.././../util.sol"; // source unit name: util.sol ``` ```solidity import "../../.././../util.sol"; // source unit name: util.sol ``` -------------------------------- ### Configure and Build Solidity with CMake Source: https://github.com/argotorg/solidity/blob/develop/docs/installing-solidity.md Build the Solidity project using CMake. This involves creating a build directory, configuring with CMake, and then compiling with make. Ensure external dependencies are installed first. ```bash mkdir build cd build cmake .. && make ``` -------------------------------- ### String Offsets Calculation Source: https://github.com/argotorg/solidity/blob/develop/docs/abi-spec.md Illustrates how offsets for dynamic string elements are calculated within the ABI encoding. Offsets point to the start of the string's content, which is typically 96 bytes after the start of the encoded data for the first string. ```none 0 - c - offset for "one" 1 - d - offset for "two" 2 - e - offset for "three" 3 - 0000000000000000000000000000000000000000000000000000000000000003 - count for "one" 4 - 6f6e650000000000000000000000000000000000000000000000000000000000 - encoding of "one" 5 - 0000000000000000000000000000000000000000000000000000000000000003 - count for "two" 6 - 74776f0000000000000000000000000000000000000000000000000000000000 - encoding of "two" 7 - 0000000000000000000000000000000000000000000000000000000000000005 - count for "three" 8 - 7468726565000000000000000000000000000000000000000000000000000000 - encoding of "three" ``` -------------------------------- ### Reading and Writing to Storage Variables in Assembly Source: https://github.com/argotorg/solidity/blob/develop/docs/assembly.md This example shows how to read from and write to local and state storage variables using their `.slot` property within an inline assembly block. Note that this example will produce a warning due to ignoring the storage slot offset. ```Solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.28 <0.9.0; // This will report a warning contract C { bool transient a; uint b; function f(uint x) public returns (uint r) { assembly { // We ignore the storage slot offset, we know it is zero // in this special case. r := mul(x, sload(b.slot)) tstore(a.slot, true) } } } ``` -------------------------------- ### Initializing Dynamically-Sized Memory Arrays in Solidity Source: https://github.com/argotorg/solidity/blob/develop/docs/types/reference-types.rst Demonstrates the correct method for initializing dynamically-sized memory arrays by first allocating the array with 'new' and then assigning individual elements. Direct assignment from array literals is not supported for dynamically-sized arrays. ```solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.4.16 <0.9.0; contract C { function f() public pure { uint[] memory x = new uint[](3); x[0] = 1; x[1] = 3; x[2] = 4; } } ``` -------------------------------- ### Using Solidity Libraries for Data Structures Source: https://github.com/argotorg/solidity/blob/develop/docs/contracts.md Demonstrates how to implement a set data structure using a Solidity library. Note that library functions with storage reference parameters are compiled as DELEGATECALLs. ```solidity // SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.6.0 <0.9.0; // We define a new struct datatype that will be used to // hold its data in the calling contract. struct Data { mapping(uint => bool) flags; } library Set { // Note that the first parameter is of type "storage // reference" and thus only its storage address and not // its contents is passed as part of the call. This is a // special feature of library functions. It is idiomatic // to call the first parameter `self`, if the function can // be seen as a method of that object. function insert(Data storage self, uint value) public returns (bool) { if (self.flags[value]) return false; // already there self.flags[value] = true; return true; } function remove(Data storage self, uint value) public returns (bool) { if (!self.flags[value]) return false; // not there self.flags[value] = false; return true; } function contains(Data storage self, uint value) public view returns (bool) { return self.flags[value]; } } contract C { Data knownValues; function register(uint value) public { // The library functions can be called without a // specific instance of the library, since the // "instance" will be the current contract. require(Set.insert(knownValues, value)); } // In this contract, we can also directly access knownValues.flags, if we want. } ``` -------------------------------- ### Download and Test Specific soljson.js Version Source: https://github.com/argotorg/solidity/wiki/Building-solc‐js-from-source This example shows how to download a specific soljson.js version using wget and then run the tests. The build process is automatically handled by the test script. ```sh cd solc-js wget https://output.circle-artifacts.com/output/job/6318ee25-a6ee-4134-b535-5541854e2e8a/artifacts/0/soljson.js -O soljson.js # the test script automatically runs the build. npm run test ``` -------------------------------- ### Method Identifiers Example Source: https://github.com/argotorg/solidity/blob/develop/docs/using-the-compiler.md Lists the function hashes for the contract. This is useful for identifying functions by their signature. ```json { "methodIdentifiers": { "delegate(address)": "5c19a95c" } } ```