Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Last active October 9, 2024 16:45
Show Gist options
  • Save Turupawn/f557a56c4e4cf547c77e81d8a57294cc to your computer and use it in GitHub Desktop.
Save Turupawn/f557a56c4e4cf547c77e81d8a57294cc to your computer and use it in GitHub Desktop.
L1Sload Advanced structures
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract L1ArrayDemo {
uint[5] myArray;
constructor() {
myArray[0] = 10;
myArray[1] = 20;
myArray[2] = 30;
myArray[3] = 40;
myArray[4] = 50;
}
}
contract L2Storage {
address constant L1_BLOCKS_ADDRESS = 0x5300000000000000000000000000000000000001;
address constant L1_SLOAD_ADDRESS = 0x0000000000000000000000000000000000000101;
function getNum(address contractAddress, uint arraySlot, uint arrayIndex) public view returns(uint) {
bool success;
bytes memory returnValue;
(success, returnValue) = L1_SLOAD_ADDRESS.staticcall(abi.encodePacked(contractAddress, arraySlot + arrayIndex));
if(!success)
{
revert("L1SLOAD failed");
}
return abi.decode(returnValue, (uint));
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract L1Contract {
uint[] public myArray;
constructor() {
myArray.push(10);
myArray.push(20);
myArray.push(30);
}
}
contract L2Storage {
address constant L1_BLOCKS_ADDRESS = 0x5300000000000000000000000000000000000001;
address constant L1_SLOAD_ADDRESS = 0x0000000000000000000000000000000000000101;
function retrieveArrayElement(address contractAddress, uint arraySlot, uint arrayIndex) public view returns(uint) {
require(arrayIndex < getArrayLength(contractAddress, arraySlot), "Out of bounds index");
uint accountBalanceSlot = uint(
keccak256(
abi.encodePacked(arraySlot)
)
) + arrayIndex;
bool success;
bytes memory returnValue;
(success, returnValue) = L1_SLOAD_ADDRESS.staticcall(abi.encodePacked(contractAddress, accountBalanceSlot));
if(!success)
{
revert("L1SLOAD failed");
}
return abi.decode(returnValue, (uint));
}
function getArrayLength(address contractAddress, uint arraySlot) public view returns(uint) {
bool success;
bytes memory returnValue;
(success, returnValue) = L1_SLOAD_ADDRESS.staticcall(abi.encodePacked(contractAddress, arraySlot));
if(!success)
{
revert("L1SLOAD failed");
}
return abi.decode(returnValue, (uint));
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;
struct MyStruct {
uint a;// 32 bytes (256 bits)
address b; // 20 bytes (160 bits)
bool c; // 1 byte (8 bits)
uint d; // 32 bytes (256 bits)
}
contract L1Contract {
MyStruct myStruct;
constructor() {
myStruct = MyStruct(10,address(this),true,20);
}
}
// This contract reads the balance of any holder on L1
contract L2Storage {
address constant L1_BLOCKS_ADDRESS = 0x5300000000000000000000000000000000000001;
address constant L1_SLOAD_ADDRESS = 0x0000000000000000000000000000000000000101;
function getArrayLength(address contractAddress, uint structSlot) public view returns(MyStruct memory) {
bool success;
bytes memory returnValue;
(success, returnValue) = L1_SLOAD_ADDRESS.staticcall(abi.encodePacked(contractAddress, structSlot, structSlot+1, structSlot+2));
if(!success)
{
revert("L1SLOAD failed");
}
(uint256 slot0, uint256 slot1, uint slot2) = abi.decode(returnValue, (uint, uint, uint));
address b = address(uint160(slot1));
bool c = (slot1 >> 160) == 1;
return MyStruct(slot0, b, c, slot2);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract DynamicArrayExample {
mapping(uint => uint[] myArray) arrayMapping;
constructor() {
arrayMapping[0].push(10);
arrayMapping[0].push(20);
arrayMapping[0].push(30);
arrayMapping[1].push(100);
arrayMapping[1].push(200);
arrayMapping[1].push(300);
}
}
contract L2Storage {
address constant L1_BLOCKS_ADDRESS = 0x5300000000000000000000000000000000000001;
address constant L1_SLOAD_ADDRESS = 0x0000000000000000000000000000000000000101;
function getArrayLength(address contractAddress, uint mappingSlot, uint mappingKey, uint arrayIndex) public view returns(uint) {
uint accountBalanceSlot = uint(
keccak256(
abi.encodePacked(
keccak256(abi.encodePacked(mappingKey,
mappingSlot)
)
)
)) + arrayIndex;
bool success;
bytes memory returnValue;
(success, returnValue) = L1_SLOAD_ADDRESS.staticcall(abi.encodePacked(contractAddress, accountBalanceSlot));
if(!success)
{
revert("L1SLOAD failed");
}
return abi.decode(returnValue, (uint));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment