Skip to content

Commit 958166c

Browse files
authored
cw-storey 0.6.0 (#272)
2 parents 3fb469c + 4ffc9ef commit 958166c

File tree

15 files changed

+301
-151
lines changed

15 files changed

+301
-151
lines changed

docs-test-gen/Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs-test-gen/templates/ibc-packet.tpl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,14 @@ fn doctest() {
3737
}
3838
{
3939
use cw_storey::{containers::Item, CwStorage};
40-
const CHANNEL: Item<ChannelInfo> = Item::new(0);
41-
CHANNEL.access(&mut deps.storage).set(&channel_info).unwrap();
40+
use storey::containers::router;
41+
router! {
42+
router Root {
43+
0 -> channel: Item<ChannelInfo>,
44+
}
45+
}
46+
47+
Root::access(&mut deps.storage).channel_mut().set(&channel_info).unwrap();
4248
}
4349

4450

docs-test-gen/templates/sylvia/storey_contract.tpl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use sylvia::cw_schema::cw_serde;
88
use cw_utils::MsgInstantiateContractResponse;
99
use cw_storey::CwStorage;
1010
use cw_storey::containers::Item;
11+
use storey::containers::router;
1112
use std::marker::PhantomData;
1213

1314
pub mod external_contract {
@@ -47,16 +48,18 @@ fn doctest() {
4748
pub mod main_contract {
4849
use super::*;
4950
50-
pub struct Contract {
51-
remote: Item<Remote<'static, external_contract::ExternalContract>>
51+
router! {
52+
router Root {
53+
0 -> remote: Item<Remote<'static, external_contract::ExternalContract>>,
54+
}
5255
}
5356
57+
pub struct Contract;
58+
5459
#[contract]
5560
impl Contract {
5661
pub fn new() -> Self {
57-
Self {
58-
remote: Item::new(0)
59-
}
62+
Self
6063
}
6164
6265
#[sv::msg(instantiate)]

src/pages/ibc/diy-protocol/channel-lifecycle.mdx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const IBC_APP_VERSION: &str = "my-protocol-v1";
118118

119119
```rust filename="ibc.rs" template="core"
120120
use cw_storey::containers::Item;
121+
use storey::containers::router;
121122

122123
/// enforces ordering and versioning constraints
123124
#[cfg_attr(not(feature = "library"), entry_point)]
@@ -131,7 +132,7 @@ pub fn ibc_channel_open(
131132
// in this example, we only allow a single channel per contract instance
132133
// you can do more complex checks here
133134
ensure!(
134-
CHANNEL.access(&*deps.storage).get()?.is_none(),
135+
Root::access(&*deps.storage).channel().get()?.is_none(),
135136
StdError::generic_err("channel already exists")
136137
);
137138

@@ -152,7 +153,7 @@ pub fn ibc_channel_open(
152153

153154
// now, we save the channel ID to storage, so we can use it later
154155
// this also prevents any further channel openings
155-
CHANNEL.access(deps.storage).set(&ChannelInfo {
156+
Root::access(deps.storage).channel_mut().set(&ChannelInfo {
156157
channel_id: channel.endpoint.channel_id.clone(),
157158
finalized: false,
158159
})?;
@@ -170,7 +171,12 @@ struct ChannelInfo {
170171
finalized: bool,
171172
}
172173

173-
const CHANNEL: Item<ChannelInfo> = Item::new(0);
174+
router! {
175+
router Root {
176+
0 -> channel: Item<ChannelInfo>,
177+
}
178+
}
179+
174180
const IBC_APP_VERSION: &str = "my-protocol-v1";
175181
```
176182

@@ -361,6 +367,7 @@ const CHANNEL: Item<ChannelInfo> = Item::new("channel");
361367

362368
```rust filename="ibc.rs" template="core"
363369
use cw_storey::containers::Item;
370+
use storey::containers::router;
364371

365372
#[cfg_attr(not(feature = "library"), entry_point)]
366373
pub fn ibc_channel_connect(
@@ -372,8 +379,8 @@ pub fn ibc_channel_connect(
372379

373380
// in this example, we only allow a single channel per contract instance
374381
// you can do more complex checks here
375-
let mut channel_info = CHANNEL
376-
.access(&*deps.storage)
382+
let mut channel_info = Root::access(&*deps.storage)
383+
.channel()
377384
.get()?
378385
.ok_or_else(|| StdError::generic_err("channel not found"))?;
379386
ensure!(
@@ -387,7 +394,9 @@ pub fn ibc_channel_connect(
387394

388395
// at this point, we are finished setting up the channel and can mark it as finalized
389396
channel_info.finalized = true;
390-
CHANNEL.access(deps.storage).set(&channel_info)?;
397+
Root::access(deps.storage)
398+
.channel_mut()
399+
.set(&channel_info)?;
391400

392401
Ok(IbcBasicResponse::new())
393402
}
@@ -399,7 +408,11 @@ struct ChannelInfo {
399408
finalized: bool,
400409
}
401410

402-
const CHANNEL: Item<ChannelInfo> = Item::new(0);
411+
router! {
412+
router Root {
413+
0 -> channel: Item<ChannelInfo>,
414+
}
415+
}
403416
```
404417

405418
</Tabs.Tab>

src/pages/ibc/diy-protocol/packet-lifecycle.mdx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ Ok(Response::new().add_message(msg))
5757

5858
```rust template="ibc-packet"
5959
use cw_storey::containers::Item;
60+
use storey::containers::router;
6061

61-
const CHANNEL: Item<ChannelInfo> = Item::new(0);
62+
router! {
63+
router Root {
64+
0 -> channel: Item<ChannelInfo>,
65+
}
66+
}
6267

63-
let channel_id = CHANNEL
64-
.access(deps.storage)
68+
let channel_id = Root::access(deps.storage)
69+
.channel()
6570
.get()?
6671
.ok_or_else(|| StdError::generic_err("channel handshake hasn't started yet"))?
6772
.channel_id;
@@ -222,6 +227,13 @@ const ACK_LATER: Map<&(u64, String), String> = Map::new("ack_later");
222227

223228
```rust filename="ibc.rs" template="core"
224229
use cw_storey::containers::{Item, Map};
230+
use storey::containers::router;
231+
232+
router! {
233+
router Root {
234+
0 -> ack_later: Map<u64, Map<String, Item<String>>>,
235+
}
236+
}
225237

226238
#[cfg_attr(not(feature = "library"), entry_point)]
227239
pub fn ibc_packet_receive(
@@ -232,7 +244,8 @@ pub fn ibc_packet_receive(
232244
// save the data we need for the async acknowledgement in contract state
233245
// note: we are just saving a String here, but you can save any information
234246
// you need for the acknowledgement later
235-
ACK_LATER.access(deps.storage)
247+
Root::access(deps.storage)
248+
.ack_later_mut()
236249
.entry_mut(&msg.packet.sequence)
237250
.entry_mut(&msg.packet.dest.channel_id)
238251
.set(&"ack str".to_string())?;
@@ -250,7 +263,8 @@ pub fn async_ack(
250263
channel_id: String,
251264
) -> StdResult<Response> {
252265
// load data from contract state
253-
let ack_str = ACK_LATER.access(deps.storage)
266+
let ack_str = Root::access(deps.storage)
267+
.ack_later()
254268
.entry(&packet_sequence)
255269
.entry(&channel_id)
256270
.try_get()
@@ -263,9 +277,6 @@ pub fn async_ack(
263277
ack: IbcAcknowledgement::new(StdAck::success(ack_str.as_bytes())),
264278
}))
265279
}
266-
267-
const ACK_LATER_IX: u8 = 0;
268-
const ACK_LATER: Map<u64, Map<String, Item<String>>> = Map::new(ACK_LATER_IX);
269280
```
270281

271282
</Tabs.Tab>

src/pages/storey/containers.mdx

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ For more advanced use cases, you can build your own containers as described in t
2121

2222
## Namespace
2323

24-
To construct a container, you need to provide a single byte. This byte is used to distinguish
25-
between different "root" containers in the same storage space.
24+
To construct a container, you need to provide an ID in the 0-254 range. This byte is used to
25+
distinguish between different "root" containers in the same storage space.
2626

2727
```rust template="storage"
2828
use cw_storey::containers::{Item, Map};
29-
30-
let item: Item<u32> = Item::new(0); // byte 0 is used as the namespace
31-
let map: Map<String, Item<u32>> = Map::new(1); // byte 1 is used as the namespace
29+
use storey::containers::router;
30+
31+
router! {
32+
router Root {
33+
0 -> foo: Item<u32>,
34+
1 -> bar: Map<String, Item<u32>>,
35+
}
36+
}
3237
```
3338

3439
A container will assume it's free to save stuff under the given key (e.g. `A`), and any key that
@@ -53,15 +58,22 @@ reference to a storage backend.
5358

5459
```rust template="storage"
5560
use cw_storey::containers::Item;
61+
use storey::containers::router;
5662

57-
let item: Item<u32> = Item::new(0);
63+
router! {
64+
router Root {
65+
0 -> item: Item<u32>,
66+
}
67+
}
5868

59-
item.access(&mut storage)
69+
Root::access(&mut storage)
70+
.item_mut()
6071
.set(&42)
6172
.unwrap();
6273

6374
assert_eq!(
64-
item.access(&storage)
75+
Root::access(&storage)
76+
.item()
6577
.get()
6678
.unwrap(),
6779
Some(42)
@@ -99,17 +111,24 @@ assert_eq!(
99111

100112
```rust template="storage"
101113
use cw_storey::containers::{Map, Item};
114+
use storey::containers::router;
102115

103-
let map: Map<String, Map<String, Item<u32>>> = Map::new(0);
116+
router! {
117+
router Root {
118+
0 -> map: Map<String, Map<String, Item<u32>>>,
119+
}
120+
}
104121

105-
map.access(&mut storage)
122+
Root::access(&mut storage)
123+
.map_mut()
106124
.entry_mut("foo")
107125
.entry_mut("bar")
108126
.set(&42)
109127
.unwrap();
110128

111129
assert_eq!(
112-
map.access(&storage)
130+
Root::access(&storage)
131+
.map()
113132
.entry("foo")
114133
.entry("bar")
115134
.get()

src/pages/storey/containers/_meta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export default {
2+
routers: "Routers",
23
item: "Item",
34
map: "Map",
45
column: "Column",

0 commit comments

Comments
 (0)