-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for
storage_address_from_base
usage (#2705)
<!-- 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
1 parent
76ac22e
commit 63447bb
Showing
4 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
docs/listings/snforge_advanced_features/crates/direct_storage_access/tests/lib.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
31 changes: 31 additions & 0 deletions
31
...dvanced_features/crates/direct_storage_access/tests/using_storage_address_from_base.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters