Created
November 23, 2023 09:46
-
-
Save sodexx7/cdbb2fbaab50adc40435b4eaca63e71b to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.18+commit.87f61d96.js&optimize=false&runs=200&gist=
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: GPL-3.0 | |
pragma solidity ^0.8.17; | |
contract StorageArrayTest { | |
uint256[] bigArray; | |
// the bigArray's slot store the length of the bigArray. The initial value is zero. Firstly, the default value is zero. | |
function testFixedArrayLength() external view returns(uint) { | |
return bigArray.length; | |
} | |
function pushTest(uint num) external { | |
bigArray.push(num); | |
} | |
// 1. each time, should check the array's lengh. if equal 0, just revert. ZeroArray | |
// 2. after each pop, should update the array's length | |
function popTest() external { | |
bigArray.pop(); | |
} | |
// no matter read or write, Firstly should check the index is in the range of lenght. index is beginning from zero | |
// just based on the bigArray's storage mechniams, get the corrospending value. | |
/** | |
function readBigArrayLocation(uint256 index) | |
external | |
view | |
returns (uint256 ret) | |
{ | |
uint256 slot; | |
assembly { | |
slot := bigArray.slot | |
} | |
bytes32 location = keccak256(abi.encode(slot)); | |
assembly { | |
ret := sload(add(location, index)) | |
} | |
} | |
based on desgin, read and update the value. | |
https://github.com/RareSkills/Udemy-Yul-Code/blob/17aec03882dd951b0d15707d71b6ab38626d853c/Video-06-Storage-3.sol#L41 | |
**/ | |
function readTest(uint index) external view returns (uint) { | |
return bigArray[index]; | |
} | |
function writeTest(uint index,uint new_num) external { | |
bigArray[index] = new_num; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment