DEV Community

Ahmed Castro for Filosofía Código EN

Posted on

How to use L1SLOAD, the Keystores backbone

Seamless cross-chain account abstraction features will be possible thanks to Keystores. Were users will be able to control multiple smart contract accounts, on multiple chains, with a single key. This will bring rollups closer and provide the so long waited good UX for end users in a rollup centric Ethereum.

In order to make this happen, we need to be able to read the L1 data from L2 rollups which is currently a very expensive process. That's why Scroll recently introduced the L1SLOAD precompile that is able to read the L1 State fast and cheap. Safe wallet is already creating a proof of concept introduced at Safecon Berlin 2024 of this work and I think this is just the begining: DeFi, gamming, social and many more types of corss-chain applications are possible with this.

Let's now learn, with examples, the basics of this new primitive that is set to open the door to a new way of interacting with Ethereum.

1. Connect your wallet to the devnet

Currently, L1SLOAD is available only on the Scroll Devnet. Please don't confuse it with the Scroll Sepolia Testnet. Although both are deployed on top of Sepolia Testnet, they are separate chains.

Let's start by connecting our wallet to Scroll Devnet:

  • Name: Scroll Devnet
  • RPC: https://l1sload-rpc.scroll.io
  • Chain id: 222222
  • Symbol: Sepolia ETH
  • Explorer: https://l1sload-blockscout.scroll.io

Connect to Scroll Devnet

2. Get some funds on the L2 devnet

There are two methods for obtaining funds on the Scroll Devnet. Choose whichever option you prefer.

Telegram faucet bot (recommended)

Join this telegram group and type /drop YOURADDRESS (e.g. /drop 0xd8da6bf26964af9d7eed9e03e53415d37aa96045) to receive funds directly to your account.

Sepolia Bridge

You can bridge Sepolia ETH from Sepolia Testnet to Sepolia Devnet through the Scroll Messenger. There are different ways of achieving this but in this case we're going to use Remix.

Let's start by connecting your wallet with Sepolia ETH to Sepolia Testnet. Remember you can get some Sepolia ETH for free from a faucet.

Now compile the following interface.

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

interface ScrollMessanger {
    function sendMessage(address to, uint value, bytes memory message, uint gasLimit) external payable;
}
Enter fullscreen mode Exit fullscreen mode

Next, on the Deploy & Run tab connect the following contract address: 0x561894a813b7632B9880BB127199750e93aD2cd5.

Connect Scroll Messenger Interface on Remix

You can now send ETH by calling the sendMessage function. As explained below:

  • to: Your EOA wallet address. The the ETH recipient on L2
  • value: The amount you wish to receieve on L2 in wei. For example, if you want to send 0.01 ETH you should pass 10000000000000000
  • message: Leave this empty, just pass 0x00
  • gasLimit: 1000000 should be fine

Also remember to pass some value to your transaction. And add some extra ETH to pay for fees on L2, 0.001 should be more than enough. So if for example you sent 0.01 ETH on the bridge, send a transaction with 0.011 ETH to cover the fees.

Send ETH from Sepolia to Scroll Devnet

Click the transact button and your funds should be available in around 15 mins.


2. Deploy a contract on L1

As mentioned earlier, L1SLOAD reads L1 contract state from L2. Let's deploy a simple L1 contract with a number variable and later access it from L2.

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.20;

/**
 * @title Storage
 * @dev Store & retrieve value in a variable
 */
contract L1Storage {

    uint256 public number;

    /**
     * @dev Store value in variable
     * @param num value to store
     */
    function store(uint256 num) public {
        number = num;
    }

    /**
     * @dev Return value
     * @return value of 'number'
     */
    function retrieve() public view returns (uint256){
        return number;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now call the store(uint256 num) function and pass a new value. For example let's pass 42.

Store a value on L1

3. Retrieve a Slot from L2

Now let's deploy the following contract on L2 by passing the L1 contract address we just deployed as constructor param.

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.20;

interface IL1Blocks {
    function latestBlockNumber() external view returns (uint256);
}

contract L2Storage {
    address constant L1_BLOCKS_ADDRESS = 0x5300000000000000000000000000000000000001;
    address constant L1_SLOAD_ADDRESS = 0x0000000000000000000000000000000000000101;
    uint256 constant NUMBER_SLOT = 0;
    address immutable l1StorageAddr;

    uint public l1Number;

    constructor(address _l1Storage) {
        l1StorageAddr = _l1Storage;
    }

    function latestL1BlockNumber() public view returns (uint256) {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        return l1BlockNum;
    }

    function retrieveFromL1() public {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        bytes memory input = abi.encodePacked(l1BlockNum, l1StorageAddr, NUMBER_SLOT);
        bool success;
        bytes memory ret;
        (success, ret) = L1_SLOAD_ADDRESS.call(input);
        if (success) {
            (l1Number) = abi.decode(ret, (uint256));
        } else {
            revert("L1SLOAD failed");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Notice this contract first calls latestL1BlockNumber() to get the latest L1 block that L2 has visibility on. And then calls L1SLOAD (opcode 0x101) by passing the L1 contract address and the slot 0 where the uint number is stored.

Now we can call retrieveFromL1() to get the value we previously stored.

L2SLOAD L1 State red from L2

Example #2: Reading other variable types

Luckily for us, Solidity stores the slots in the same order as they were declared. For example, in the following contract account will be stored on slot #0, number slot #1 and text on slot #2.

// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

contract AdvancedL1Storage {
    address public account;
    uint public number;
    string public text;
}
Enter fullscreen mode Exit fullscreen mode

So, you can notice on the following example how you can query the different slots and decode accordingly to uint256, address, etc... The only different native type that needs special decoding is the string type.

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.20;

interface IL1Blocks {
    function latestBlockNumber() external view returns (uint256);
}

contract L2Storage {
    address constant L1_BLOCKS_ADDRESS = 0x5300000000000000000000000000000000000001;
    address constant L1_SLOAD_ADDRESS = 0x0000000000000000000000000000000000000101;
    address immutable l1ContractAddress;

    address public account;
    uint public number;
    string public test;

    constructor(address _l1ContractAddress) { //0x5555158Ea3aB5537Aa0012AdB93B055584355aF3
        l1ContractAddress = _l1ContractAddress;
    }

    // Internal functions

    function latestL1BlockNumber() internal view returns (uint256) {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        return l1BlockNum;
    }

    function retrieveSlotFromL1(uint blockNumber, address l1StorageAddress, uint slot) internal returns (bytes memory) {
        bool success;
        bytes memory returnValue;
        (success, returnValue) = L1_SLOAD_ADDRESS.call(abi.encodePacked(blockNumber, l1StorageAddress, slot));
        if(!success)
        {
            revert("L1SLOAD failed");
        }
        return returnValue;
    }

    function decodeStringSlot(bytes memory encodedString) internal pure returns (string memory) {
        uint length = 0;
        while (length < encodedString.length && encodedString[length] != 0x00) {
            length++;
        }
        bytes memory data = new bytes(length);
        for (uint i = 0; i < length; i++) {
            data[i] = encodedString[i];
        }
        return string(data);
    }

    // Public functions

    function retrieveAddress() public {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        account = abi.decode(retrieveSlotFromL1(l1BlockNum, l1ContractAddress, 0), (address));
    }

    function retrieveNumber() public {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        number = abi.decode(retrieveSlotFromL1(l1BlockNum, l1ContractAddress, 1), (uint));
    }

    function retrieveString() public {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        test = decodeStringSlot(retrieveSlotFromL1(l1BlockNum, l1ContractAddress, 2));
    }

    function retrieveAll() public {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        account = abi.decode(retrieveSlotFromL1(l1BlockNum, l1ContractAddress, 0), (address));
        number = abi.decode(retrieveSlotFromL1(l1BlockNum, l1ContractAddress, 1), (uint));
        test = decodeStringSlot(retrieveSlotFromL1(l1BlockNum, l1ContractAddress, 2));
    }
}
Enter fullscreen mode Exit fullscreen mode

Example #3: Reading ERC20 token balance from L1

Let's start by deploying the following very simple ERC20 token.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SimpleToken is ERC20 {
    constructor(
        string memory name,
        string memory symbol,
        uint256 initialSupply
    ) ERC20(name, symbol) {
        _mint(msg.sender, initialSupply * 1 ether);
    }
}
Enter fullscreen mode Exit fullscreen mode

Next, we can deploy the following contract on L2 by passing the L1 token address as parameter.

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.20;

interface IL1Blocks {
    function latestBlockNumber() external view returns (uint256);
}

contract L2Storage {
    address constant L1_BLOCKS_ADDRESS = 0x5300000000000000000000000000000000000001;
    address constant L1_SLOAD_ADDRESS = 0x0000000000000000000000000000000000000101;
    address immutable l1ContractAddress;

    uint public l1Balance;

    constructor(address _l1ContractAddress) {
        l1ContractAddress = _l1ContractAddress;
    }

    // Internal functions
    function latestL1BlockNumber() public view returns (uint256) {
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        return l1BlockNum;
    }

    function retrieveSlotFromL1(uint blockNumber, address l1StorageAddress, uint slot) internal returns (bytes memory) {
        bool success;
        bytes memory returnValue;
        (success, returnValue) = L1_SLOAD_ADDRESS.call(abi.encodePacked(blockNumber, l1StorageAddress, slot));
        if(!success)
        {
            revert("L1SLOAD failed");
        }
        return returnValue;
    }

    // Public functions
    function retrieveL1Balance(address account) public {
        uint slotNumber = 0;
        uint256 l1BlockNum = IL1Blocks(L1_BLOCKS_ADDRESS).latestBlockNumber();
        l1Balance = abi.decode(retrieveSlotFromL1(
            l1BlockNum,
            l1ContractAddress,
            uint(keccak256(
                abi.encodePacked(uint160(account),slotNumber)
                )
            )
            ), (uint));
    }
}
Enter fullscreen mode Exit fullscreen mode

OpenZeppelin contracts conveniently places the balances mapping on Slot 0. So you can call retrieveL1Balance() by passing the account address as paramater and the token balance will be stored on the l1Balance variable. As you can see on the code, it works by converting the account to uint160 and then hashing it with the mapping slot which is 0. This is because that's the way the Solidity implemented mappings.

Thanks for reading this guide!

Follow Filosofía Código on dev.to and in Youtube for everything related to Blockchain development.

Top comments (0)