foundry-rs / starknet-foundry

Blazing fast toolkit for developing Starknet contracts.
https://foundry-rs.github.io/starknet-foundry/
MIT License
311 stars 152 forks source link

Improve `load` and `store` interfaces with new traits from cairo `2.7.0` #2299

Open Arcticae opened 3 months ago

Arcticae commented 3 months ago

Which components does the task require to be changed? (think hard pls)

snforge

Description

It should be easier to construct something like those helpers, when Store trait is available for usage:

fn load<T, +Store<T>>(target: ContractAddress, base_address: felt252) -> T {
    Store::<T>::read(0, base_address).unwrap()
}

fn load_path<T>(base_address: felt252) -> StoragePath<T> {
    StoragePath::<T> { pedersen::HashState {base_address} }
}

Then the usage of the cheats would be more intuitive since we would not have to specify the sizes:

// Given storage:
struct Storage {
   a: Map<u8,u8>,
   b: MyStruct
}

// Construct load/store paths with addresses and bind to types:
let path_a = load_path::<Map<u8,u8>>(selector!("a"));
let path_b = load_path::<MyStruct>(selector!("b"));

// Retrieve values:
path_a.entry(5).read();
path_b.read();
cptartur commented 1 month ago

Blocked by #2347