⌘K

Icon SunFilledIcon MoonStars

Icon LinkDefining The Storage Block

Next, we'll introduce the storage block. This is where you store all persistent state variables in your contract.

Variables declared within a function and not saved in the storage block will be discarded once the function completes its execution. Add the storage block below to your main.sw file:

storage {
    // counter for total items listed
    item_counter: u64 = 0,
 
    // map of item IDs to Items
    item_map: StorageMap<u64, Item> = StorageMap {},
 
    // owner of the contract
    owner: Option<Identity> = Option::None,
}

The first variable we've stored is item_counter, a number initialized to 0. This counter can be used to track the total number of items listed.

Icon LinkStorageMap

A StorageMap is a unique type that permits the saving of key-value pairs within a storage block.

To define a storage map, you need to specify the types for both the key and the value. For instance, in the example below, the key type is u64, and the value type is an Item struct.

// map of item IDs to Items
item_map: StorageMap<u64, Item> = StorageMap {},

Here, we are creating a mapping from the item's ID to the Item struct. Using this, we can retrieve information about an item using its ID.

Icon LinkOptions

Here, we are defining the owner variable as one that can either be None or hold an Identity.

// owner of the contract
owner: Option<Identity> = Option::None,

If you want a value to be potentially null or undefined under specific conditions, you can employ the Option type. It's an enum that can take on either Some(value) or None. The keyword None indicates the absence of a value, while Some signifies the presence of a stored value.