You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add example using storage_address_from_base for store cheatcode (#2175)
## Introduced changes
<!-- A brief description of the changes -->
- Add an example using `storage_address_from_base` for `store` cheatcode
## Checklist
<!-- Make sure all of these are complete -->
- [ ] Linked relevant issue
- [x] Updated relevant documentation
- [ ] Added relevant tests
- [ ] Performed self-review of the code
- [ ] Added changes to `CHANGELOG.md`
---------
Co-authored-by: Tomasz Rejowski <[email protected]>
> The `load` cheatcodewillreturnzerosformemoryyouhaven'twrittenintoyet (itisadefaultstoragevalueforStarknetcontracts' storage).
161
162
162
163
164
+
## Example with `storage_address_from_base`
165
+
166
+
This example uses `storage_address_from_base` with `address` function of the [storage variable](https://book.cairo-lang.org/ch14-01-contract-storage.html#addresses-of-storage-variables).
167
+
168
+
To retrieve storage address of a given `field`, you need to import `{field_name}ContractMemberStateTrait` from the contract.
169
+
170
+
```rust
171
+
#[starknet::contract]
172
+
mod Contract {
173
+
#[storage]
174
+
struct Storage {
175
+
map: LegacyMap::<(u8, u32), u32>,
176
+
}
177
+
}
178
+
179
+
// ...
180
+
use starknet::storage_access::storage_address_from_base;
181
+
use snforge_std::{ store, load };
182
+
use Contract::mapContractMemberStateTrait;
183
+
184
+
#[test]
185
+
fn update_mapping() {
186
+
let key = (1_u8, 10_u32);
187
+
let data = 42_u32;
188
+
189
+
// ...
190
+
let mut state = Contract::contract_state_for_testing();
191
+
let storage_address: felt252 = storage_address_from_base(
192
+
state.map.address(key)
193
+
)
194
+
.into();
195
+
let storage_value: Span<felt252> = array![data.into()].span();
0 commit comments