### Install SSTORE2 with yarn Source: https://github.com/0xsequence/sstore2/blob/master/README.md Install the sstore2 package using yarn from the GitHub repository. ```bash yarn add https://github.com/0xsequence/sstore2 ``` -------------------------------- ### Install SSTORE2 with npm Source: https://github.com/0xsequence/sstore2/blob/master/README.md Install the sstore2 package using npm from the GitHub repository. ```bash npm install --save https://github.com/0xsequence/sstore2 ``` -------------------------------- ### SSTORE2 for immutables: valid and broken examples Source: https://github.com/0xsequence/sstore2/blob/master/README.md Shows how to emulate variable-size immutables using SSTORE2 with an immutable pointer, since Solidity 0.8.9 does not support non-primitive immutable types. Includes a Broken contract that fails to build because it tries to use a bytes immutable variable. ```solidity pragma solidity ^0.8.0; import "@0xsequence/sstore2/contracts/SSTORE2.sol"; contract Demo { address private immutable dataPointer; constructor(bytes memory _data) { dataPointer = SSTORE2.write(_data); } function getData() external view returns (bytes memory) { return SSTORE2.read(dataPointer); } } contract Broken { // Fails to build, non-primite types // can't be used as immutable variables bytes private immutable data; constructor(bytes memory _data) { data = _data; } } ``` -------------------------------- ### Read data slices with SSTORE2.read Source: https://github.com/0xsequence/sstore2/blob/master/README.md Demonstrates SSTORE2.read with start and end parameters to read slices of stored data, mirroring JavaScript's slice(start, end). The goodSlices function shows valid slices, including out-of-range end values that clamp to the data length, while badSlies shows that a start greater than end reverts. ```solidity pragma solidity ^0.8.0; import "@0xsequence/sstore2/contracts/SSTORE2.sol"; contract Demo { event Sliced(bytes _data); function goodSlices() external { address pointer = SSTORE2.write(hex"11_22_33_44"); // 0x223344 emit Sliced( SSTORE2.read(pointer, 1) ); // 0x2233 emit Sliced( SSTORE2.read(pointer, 1, 3) ); // 0x emit Sliced( SSTORE2.read(pointer, 3, 3) ); // 0x3344 emit Sliced( SSTORE2.read(pointer, 2, 42000) ); // 0x emit Sliced( SSTORE2.read(pointer, 41000, 42000) ); } function badSlies() external { address pointer = SSTORE2.write(hex"11_22_33_44"); // This reverts // start must be equal or lower than end emit Sliced( SSTORE2.read(pointer, 3, 2) ); } } ``` -------------------------------- ### SSTORE2 basic usage: write and read data Source: https://github.com/0xsequence/sstore2/blob/master/README.md Demonstrates a basic Solidity contract that uses SSTORE2.write to store a string and SSTORE2.read to retrieve it. Calling write generates a new pointer address, and reading an invalid pointer may return an empty bytes array or contract bytecode. ```solidity pragma solidity ^0.8.0; import "@0xsequence/sstore2/contracts/SSTORE2.sol"; contract Demo { address private pointer; function setText(string calldata _text) external { pointer = SSTORE2.write(bytes(_text)); } function getText() external view returns (string memory) { return string(SSTORE2.read(pointer)); } } ``` -------------------------------- ### SSTORE2Map write and read with bytes32 key Source: https://github.com/0xsequence/sstore2/blob/master/README.md Shows how to use SSTORE2Map with a fixed bytes32 key. The contract writes an ABI-encoded array of hashes and later reads it back. Reading a key that was never written returns an empty array of bytes. ```solidity pragma solidity ^0.8.0; import "@0xsequence/sstore2/contracts/SSTORE2Map.sol"; contract Demo { bytes32 private constant KEY = 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3; function setHashes(bytes32[] calldata _hashes) external { SSTORE2Map.write(KEY, abi.encode(_hashes)); } function getHashes() external view returns (bytes32[] memory) { return abi.decode(SSTORE2Map.read(KEY), (bytes32[])); } } ``` -------------------------------- ### SSTORE2Map multiple keys and write-once failure Source: https://github.com/0xsequence/sstore2/blob/master/README.md Demonstrates using multiple keys with SSTORE2Map. Writing to distinct keys succeeds, but writing twice with the same key reverts because the map store is write-once. Strings are internally mapped as keccak256(bytes()). ```solidity pragma solidity ^0.8.0; import "@0xsequence/sstore2/contracts/SSTORE2Map.sol"; contract Demo { // This works function good() external { SSTORE2Map.write(string("@key-1"), bytes("hola")); SSTORE2Map.write(string("@key-2"), bytes("mundo")); } // This reverts function bad() external { SSTORE2Map.write(string("@key-3"), bytes("adios")); SSTORE2Map.write(string("@key-3"), bytes("mundo")); } } ``` -------------------------------- ### SSTORE2 Library Functions Source: https://github.com/0xsequence/sstore2/blob/master/README.md Documentation for the SSTORE2 library's write and read functions, which allow storing and retrieving data using auto-generated address pointers. ```APIDOC ## [SSTORE2.write] write(bytes data) returns (address pointer) ### Description Writes data to a new contract address and returns a pointer to that address. The pointer can later be used with `SSTORE2.read` to retrieve the data. Each call generates a new pointer, and pointers cannot be deleted. ### Method SSTORE2.write ### Parameters #### Function Parameters - **data** (bytes) - Required - The data to be stored. ### Returns - **pointer** (address) - The address of the contract where the data is stored. ### Example ```solidity pointer = SSTORE2.write(bytes(_text)); ``` ## [SSTORE2.read] read(address pointer) returns (bytes memory) ### Description Reads the data stored at the given pointer address. If the pointer is invalid, it may return an empty bytes array or contract bytecode. ### Method SSTORE2.read ### Parameters #### Function Parameters - **pointer** (address) - Required - The address of the contract where the data is stored. ### Returns - **data** (bytes) - The data stored at the pointer address. ### Example ```solidity string(SSTORE2.read(pointer)); ``` ``` -------------------------------- ### MIT License Source: https://github.com/0xsequence/sstore2/blob/master/README.md The MIT License text for the project, specifying copyright and permission terms. ```text MIT License Copyright (c) [2018] [Ismael Ramos Silvan] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.