In Sway, StorageMap is a data structure used to store key-value pairs within smart contracts. It can be used to store, retrieve, update, and delete data.
basic_examples
function, we demonstrate how to perform these basic operations.nested_examples
function, we demonstrate how to perform read, insert, and remove operations on a nested StorageMap.In the provided code, we define a smart contract named MyContract
with two functions named basic_examples
and nested_examples
, each performing operations on StorageMap.
balance_of
, which stores a mapping from Identity
to u64
values.allowance
, which stores a mapping from a pair of Identity
to u64
values.ADDR
constant to create a b256
value, which is used as a key in the allowance
StorageMap.The basic_examples
function demonstrates how to perform insert, read, update, and remove operations on the balance_of
StorageMap. The nested_examples
function demonstrates how to perform read, insert, and remove operations on the allowance
StorageMap.
This simple example is intended to show you how to use StorageMap in Sway to store and manipulate key-value pair data. We hope this tutorial helps you better understand StorageMap in Sway.
All Examplescontract; use std::{storage::storage_vec::*}; // storage vector, heap abi MyContract { #[storage(read, write)] fn storage_vec_examples(); fn heap_vec_examples(); } storage { nums: StorageVec= StorageVec {}, } impl MyContract for Contract { #[storage(read, write)] fn storage_vec_examples() { // push // pop // get // set // remove - moves all elements down by one // swap remove - remove element, move last element // len // clear - sets length to zero // loop // push storage.nums.push(100); storage.nums.push(200); storage.nums.push(300); storage.nums.push(400); storage.nums.push(500); storage.nums.push(600); // pop - remove last - returns Option let last = storage.nums.pop(); // get let first = storage.nums.get(0).unwrap(); let none = storage.nums.get(1000); // set storage.nums.set(0, 123); // remove - Returns value removed // Before remove [100, 200, 300, 400] // After remove [100, 300, 400] let removed_val = storage.nums.remove(1); // swap remove // Before swap_remove [100, 300, 400, 500] // After swap_remove [100, 500, 400] storage.nums.swap_remove(1); let len = storage.nums.len(); // clear - sets length to zero storage.nums.clear(); // Loop example let mut i = 0; while i < len { let val = storage.nums.get(i).unwrap(); i += 1; } } fn heap_vec_examples() { // new // push // pop // remove // get // set // len let mut v: Vec = Vec::new(); v.push(100); v.push(200); v.push(300); v.push(400); v.push(500); // Returns Option v.pop(); // Before remove [100, 200, 300, 400] // After remove [100, 300, 400] // Returns removed element v.remove(1); let val = v.get(1).unwrap(); v.set(1, val + 1); let len = v.len(); } }