Store
trait enables types to be stored in and retrieved from Starknet’s contract storage.
Cairo implements Store
for most primitive types. However, collection types (arrays, dicts,
etc.) do not implement Store
directly. Instead, use specialized storage types, such as Vec
or Map
.
Signature
Derivation
To make a type storable in contract storage, simply derive theStore
trait:
Size
struct to be stored in a contract’s storage.
There’s no real reason to implement this trait yourself, as it can be trivially derived.
For efficiency purposes, consider manually implementing StorePacking
to optimize storage
usage.
Trait functions
read
Reads a value from storage at the given domain and base address.Arguments
address_domain
- The storage domain (currently only 0 is supported)base
- The base storage address to read from
Signature
write
Writes a value to storage at the given domain and base address.Arguments
address_domain
- The storage domain (currently only 0 is supported)base
- The base storage address to write tovalue
- The value to store
Signature
read_at_offset
Reads a value from storage at a base address plus an offset.Arguments
address_domain
- The storage domain (currently only 0 is supported)base
- The base storage addressoffset
- The offset from the base address where the value should be read
Signature
write_at_offset
Writes a value to storage at a base address plus an offset.Arguments
address_domain
- The storage domain (currently only 0 is supported)base
- The base storage addressoffset
- The offset from the base address where the value should be writtenvalue
- The value to store
Signature
size
Returns the size in storage for this type. This is bounded to 255, as the offset is a u8. As such, a single type can only take up to 255 slots in storage.Signature
scrub
Clears the storage area by writing zeroes to it.Arguments
address_domain
- The storage domainbase
- The base storage address to start clearingoffset
- The offset from the base address where clearing should start
size()
function.