Created
March 29, 2023 10:13
-
-
Save KolevDarko/1abb0d30de305f79048cbe9af264eb8b to your computer and use it in GitHub Desktop.
Storage basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: MIT | |
pragma solidity 0.8.19; | |
contract StorageBasics { | |
uint256 x = 1; | |
uint256 y = 23; | |
uint256 z = 48; | |
uint128 a = 1; | |
uint128 b = 2; | |
function getXYul() external view returns (bytes32 ret) { | |
assembly { | |
ret := sload(x.slot) | |
} | |
} | |
function getSlot(uint256 slot) external view returns (bytes32 ret) { | |
assembly { | |
ret := sload(slot) | |
} | |
} | |
// dangerous operation, never allow external actors to set arbitrary slot values | |
function setSlot(uint256 slot, uint256 value) external { | |
assembly { | |
sstore(slot, value) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment