Storable

Derive Macro Storable 

Source
#[derive(Storable)]
{
    // Attributes available to this derive:
    #[storable_arrays]
}
Expand description

Derives the Storable trait for structs with named fields.

This macro generates implementations for loading and storing multi-slot struct layout in EVM storage. Its packing and encoding schemes aim to be an exact representation of the storage model used by Solidity.

§Requirements

  • The struct must have named fields (not tuple structs or unit structs)
  • All fields must implement the Storable trait

§Generated Code

For each struct field, the macro generates sequential slot offsets. It implements the Storable trait methods:

  • load - Loads the struct from storage
  • store - Stores the struct to storage
  • delete - Uses default implementation (sets all slots to zero)

§Example

use precompiles::storage::Storable;
use alloy_primitives::{Address, U256};

#[derive(Storable)]
pub struct RewardStream {
    pub funder: Address,              // rel slot: 0 (20 bytes)
    pub start_time: u64,              // rel slot: 0 (8 bytes)
    pub end_time: u64,                // rel slot: 1 (8 bytes)
    pub rate_per_second_scaled: U256, // rel slot: 2 (32 bytes)
    pub amount_total: U256,           // rel slot: 3 (32 bytes)
}