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
Copy file name to clipboardExpand all lines: packages/storage/Readme.md
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,52 +30,52 @@ This is the simplest storage object in this toolkit. It based on the similarly n
30
30
31
31
This object is meant to be initialized as a static constant in `state.rs`. However, it would also work perfectly fine if it was initialized during run time with a variable key (in this case though, you'd have to remind it what type of object is stored and its serde). Import it using the following lines:
The way to read/write to/from strorage is to use its methods. These methods are `save`, `load`, `may_load`, `remove`, `update`. Here is an example usecase for each in execution inside `contract.rs`:
58
58
59
-
```rust
59
+
```ignore
60
60
// The compiler knows that owner_addr is HumanAddr
> ❗ Initializing the object as const instead of static will also work but be less efficient since the variable won't be able to cache length data.
109
109
110
110
Often times we need these storage objects to be associated to a user address or some other key that is variable. In this case, you need not initialize a completely new AppendStore inside `contract.rs`. Instead, you can create a new AppendStore by adding a suffix to an already existing AppendStore. This has the benefit of preventing you from having to rewrite the signature of the AppendStore. For example
111
111
112
-
```rust
112
+
```ignore
113
113
// The compiler knows that user_count_store is AppendStore<i32, Bincode2>
114
114
let user_count_store = COUNT_STORE.add_suffix(env.message.sender.to_string().as_bytes());
115
115
```
@@ -122,13 +122,13 @@ The main user facing methods to read/write to AppendStore are `pop`, `push`, `ge
122
122
123
123
AppendStore also implements a readonly iterator feature. This feature is also used to create a paging wrapper method called `paging`. The way you create the iterator is:
124
124
125
-
```rust
125
+
```ignore
126
126
let iter = user_count_store.iter(&deps.storage)?;
127
127
```
128
128
129
129
More examples can be found in the unit tests. And the paging wrapper is used in the following manner:
130
130
131
-
```rust
131
+
```ignore
132
132
let start_page: u32 = 0;
133
133
let page_size: u32 = 5;
134
134
// The compiler knows that values is Vec<i32>
@@ -143,11 +143,11 @@ This is a storage wrapper based on AppendStore that replicates a double ended li
143
143
144
144
To import and intialize this storage object as a static constant in `state.rs`, do the following:
@@ -189,7 +189,7 @@ If you need to associate a keymap to a user address (or any other variable), the
189
189
190
190
For example suppose that in your contract, a user can make multiple bets. Then, you'd want a Keymap to be associated to each user. You would achieve this my doing the following during execution in `contract.rs`.
191
191
192
-
```rust
192
+
```ignore
193
193
// The compiler knows that user_bet_store is AppendStore<u32, BetInfo>
194
194
let user_count_store = BET_STORE.add_suffix(env.message.sender.to_string().as_bytes());
195
195
```
@@ -200,7 +200,7 @@ You can find more examples of using keymaps in the unit tests of Keymap in `keym
200
200
201
201
To insert, remove, read from the keymap, do the following:
202
202
203
-
```rust
203
+
```ignore
204
204
let user_addr: HumanAddr = env.message.sender;
205
205
206
206
let foo = Foo {
@@ -224,7 +224,7 @@ Keymap also has two paging methods, these are `.paging` and `.paging_keys`. `pag
224
224
225
225
Here are some select examples from the unit tests:
0 commit comments