Skip to content

Commit

Permalink
Add example for storage_address_from_base usage (#2705)
Browse files Browse the repository at this point in the history
<!-- Reference any GitHub issues resolved by this PR -->

Closes #2439 

## Introduced changes

<!-- A brief description of the changes -->

Update example for `storage_address_from_base` usage in storage
cheatcodes docs

## Checklist

<!-- Make sure all of these are complete -->

- [x] Linked relevant issue
- [x] Updated relevant documentation
- [x] Added relevant tests
- [x] Performed self-review of the code
- [x] Added changes to `CHANGELOG.md`
  • Loading branch information
franciszekjob authored Nov 26, 2024
1 parent 76ac22e commit 63447bb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#[starknet::interface]
pub trait ISimpleStorageContract<TState> {}
pub trait ISimpleStorageContract<TState> {
fn get_value(self: @TState, key: felt252) -> felt252;
}

#[starknet::contract]
mod SimpleStorageContract {
pub mod SimpleStorageContract {
use starknet::storage::Map;

#[storage]
Expand All @@ -16,4 +18,9 @@ mod SimpleStorageContract {
self.plain_felt.write(0x2137_felt252);
self.mapping.write('some_key', 'some_value');
}

#[external(v0)]
pub fn get_value(self: @ContractState, key: felt252) -> felt252 {
self.mapping.read(key)
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod felts_only;
pub mod complex_structures;
pub mod storage_address;
pub mod using_storage_address_from_base;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use starknet::storage::StorageAsPointer;
use starknet::storage::StoragePathEntry;

use snforge_std::{declare, ContractClassTrait, DeclareResultTrait, store, load};
use starknet::storage_access::{storage_address_from_base};

use direct_storage_access::felts_only::{
SimpleStorageContract, ISimpleStorageContractDispatcher, ISimpleStorageContractDispatcherTrait
};

#[test]
fn update_mapping() {
let key = 0;
let data = 100;
let (contract_address, _) = declare("SimpleStorageContract")
.unwrap()
.contract_class()
.deploy(@array![])
.unwrap();
let dispatcher = ISimpleStorageContractDispatcher { contract_address };
let mut state = SimpleStorageContract::contract_state_for_testing();

let storage_address = storage_address_from_base(
state.mapping.entry(key).as_ptr().__storage_pointer_address__.into()
);
let storage_value: Span<felt252> = array![data.into()].span();
store(contract_address, storage_address.into(), storage_value);

let read_data = dispatcher.get_value(key.into());
assert_eq!(read_data, data, "Storage update failed")
}
7 changes: 7 additions & 0 deletions docs/src/snforge-advanced-features/storage-cheatcodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ And perform a test checking `load` and `store` behavior in context of those stru
> 📝 **Note**
>
> The `load` cheatcode will return zeros for memory you haven't written into yet (it is a default storage value for Starknet contracts' storage).
## Example with `storage_address_from_base`
This example uses `storage_address_from_base` with entry's of the storage variable.

```rust
{{#include ../../listings/snforge_advanced_features/crates/direct_storage_access/tests/using_storage_address_from_base.cairo}}
```

0 comments on commit 63447bb

Please sign in to comment.