Skip to content

Commit

Permalink
align examples with cw-storey 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Feb 13, 2025
1 parent 53d3675 commit ff1631c
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 116 deletions.
31 changes: 22 additions & 9 deletions docs-test-gen/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs-test-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sha2 = "0.10.8"
cosmos-sdk-proto = { version = "0.24.0", default-features = false } # Used in IBC code
ibc = "0.54.0" # Used in IBC code
serde = "*"
cw-storey = "*"
schemars = "0.8.21" # Used in entrypoint example
thiserror = "1.0.65"
storey = "*"
cw-storey = "0.5"
storey = "0.4"
2 changes: 1 addition & 1 deletion docs-test-gen/templates/ibc-packet.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn doctest() {
{
use cw_storey::{containers::Item, CwStorage};
const CHANNEL: Item<ChannelInfo> = Item::new(0);
CHANNEL.access(&mut CwStorage(&mut deps.storage)).set(&channel_info).unwrap();
CHANNEL.access(&mut deps.storage).set(&channel_info).unwrap();
}


Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions src/pages/ibc/diy-protocol/channel-lifecycle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const IBC_APP_VERSION: &str = "my-protocol-v1";
<Tabs.Tab>

```rust filename="ibc.rs" template="core"
use cw_storey::{containers::Item, CwStorage};
use cw_storey::containers::Item;

/// enforces ordering and versioning constraints
#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -126,14 +126,12 @@ pub fn ibc_channel_open(
env: Env,
msg: IbcChannelOpenMsg,
) -> StdResult<IbcChannelOpenResponse> {
let mut storage = CwStorage(deps.storage);

let channel = msg.channel();

// in this example, we only allow a single channel per contract instance
// you can do more complex checks here
ensure!(
CHANNEL.access(&storage).get()?.is_none(),
CHANNEL.access(&mut *deps.storage).get()?.is_none(),
StdError::generic_err("channel already exists")
);

Expand All @@ -154,7 +152,7 @@ pub fn ibc_channel_open(

// now, we save the channel ID to storage, so we can use it later
// this also prevents any further channel openings
CHANNEL.access(&mut storage).set(&ChannelInfo {
CHANNEL.access(deps.storage).set(&ChannelInfo {
channel_id: channel.endpoint.channel_id.clone(),
finalized: false,
})?;
Expand Down Expand Up @@ -362,22 +360,20 @@ const CHANNEL: Item<ChannelInfo> = Item::new("channel");
<Tabs.Tab>

```rust filename="ibc.rs" template="core"
use cw_storey::{containers::Item, CwStorage};
use cw_storey::containers::Item;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_channel_connect(
deps: DepsMut,
env: Env,
msg: IbcChannelConnectMsg,
) -> StdResult<IbcBasicResponse> {
let mut storage = CwStorage(deps.storage);

let channel = msg.channel();

// in this example, we only allow a single channel per contract instance
// you can do more complex checks here
let mut channel_info = CHANNEL
.access(&storage)
.access(&mut *deps.storage)
.get()?
.ok_or_else(|| StdError::generic_err("channel not found"))?;
ensure!(
Expand All @@ -391,7 +387,7 @@ pub fn ibc_channel_connect(

// at this point, we are finished setting up the channel and can mark it as finalized
channel_info.finalized = true;
CHANNEL.access(&mut storage).set(&channel_info)?;
CHANNEL.access(deps.storage).set(&channel_info)?;

Ok(IbcBasicResponse::new())
}
Expand Down
9 changes: 4 additions & 5 deletions src/pages/ibc/diy-protocol/packet-lifecycle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ Ok(Response::new().add_message(msg))
<Tabs.Tab>

```rust template="ibc-packet"
use cw_storey::{containers::Item, CwStorage};
use cw_storey::containers::Item;

const CHANNEL: Item<ChannelInfo> = Item::new(0);

let channel_id = CHANNEL
.access(&mut CwStorage(deps.storage))
.access(deps.storage)
.get()?
.ok_or_else(|| StdError::generic_err("channel handshake hasn't started yet"))?
.channel_id;
Expand Down Expand Up @@ -222,7 +222,6 @@ const ACK_LATER: Map<&(u64, String), String> = Map::new("ack_later");

```rust filename="ibc.rs" template="core"
use cw_storey::containers::{Item, Map};
use cw_storey::CwStorage;

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_receive(
Expand All @@ -233,7 +232,7 @@ pub fn ibc_packet_receive(
// save the data we need for the async acknowledgement in contract state
// note: we are just saving a String here, but you can save any information
// you need for the acknowledgement later
ACK_LATER.access(&mut CwStorage(deps.storage))
ACK_LATER.access(deps.storage)
.entry_mut(&msg.packet.sequence)
.entry_mut(&msg.packet.dest.channel_id)
.set(&"ack str".to_string())?;
Expand All @@ -251,7 +250,7 @@ pub fn async_ack(
channel_id: String,
) -> StdResult<Response> {
// load data from contract state
let ack_str = ACK_LATER.access(&CwStorage(deps.storage))
let ack_str = ACK_LATER.access(deps.storage)
.entry(&packet_sequence)
.entry(&channel_id)
.try_get()
Expand Down
10 changes: 4 additions & 6 deletions src/pages/storey/containers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@ reference to a storage backend.

```rust template="storage"
use cw_storey::containers::Item;
use cw_storey::CwStorage;

let item: Item<u32> = Item::new(0);

item.access(&mut CwStorage(&mut storage))
item.access(&mut storage)
.set(&42)
.unwrap();

assert_eq!(
item.access(&CwStorage(&storage))
item.access(&storage)
.get()
.unwrap(),
Some(42)
Expand Down Expand Up @@ -100,18 +99,17 @@ assert_eq!(

```rust template="storage"
use cw_storey::containers::{Map, Item};
use cw_storey::CwStorage;

let map: Map<String, Map<String, Item<u32>>> = Map::new(0);

map.access(&mut CwStorage(&mut storage))
map.access(&mut storage)
.entry_mut("foo")
.entry_mut("bar")
.set(&42)
.unwrap();

assert_eq!(
map.access(&CwStorage(&storage))
map.access(&storage)
.entry("foo")
.entry("bar")
.get()
Expand Down
49 changes: 22 additions & 27 deletions src/pages/storey/containers/column.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,39 @@ This change will also break existing contracts that rely on the current `Column`

Let's say you want to store a log of messages. You can do this with a `Column<String>`.

```rust template="storage" showLineNumbers {6,8,10,12}
```rust template="storage" showLineNumbers {5,6,8,10}
use cw_storey::containers::Column;
use cw_storey::CwStorage;

const MESSAGES_IX: u8 = 1;

let messages: Column<String> = Column::new(MESSAGES_IX);
let mut cw_storage = CwStorage(&mut storage);
let mut access = messages.access(&mut cw_storage);
let mut access = messages.access(&mut storage);

access.push(&"Hello, world!".to_string()).unwrap();

assert_eq!(access.get(0).unwrap(), Some("Hello, world!".to_string()));
assert_eq!(access.get(1).unwrap(), Some("Hello, world!".to_string()));
```

- _line 6:_ Here we construct the `Column` facade. The constructor takes a key, which is the prefix
- _line 5:_ Here we construct the `Column` facade. The constructor takes a key, which is the prefix
of the keys in the underlying storage backend.
- _line 8:_ The [`access`] method returns a [`ColumnAccess`] entity, which allows manipulating the
- _line 6:_ The [`access`] method returns a [`ColumnAccess`] entity, which allows manipulating the
column.
- _line 10:_ Here we push a message to the column.
- _line 12:_ We check that the message is stored correctly.
- _line 8:_ Here we push a message to the column.
- _line 10:_ We check that the message is stored correctly.

#### Iterating over the messages

As with [`Map`], we can iterate over all messages.

```rust template="storage" showLineNumbers {4, 17}
```rust template="storage" showLineNumbers {3, 15}
use cw_storey::containers::{Column, Item};
use cw_storey::CwStorage;

use storey::containers::IterableAccessor as _;

const MESSAGES_IX: u8 = 1;

let messages: Column<String> = Column::new(MESSAGES_IX);
let mut cw_storage = CwStorage(&mut storage);
let mut access = messages.access(&mut cw_storage);
let mut access = messages.access(&mut storage);

// populate the column
access.push(&"Hello, world!".to_string()).unwrap();
Expand All @@ -70,14 +66,14 @@ access.push(&"Hell is empty and all the devils are here.".to_string()).unwrap();
let messages: Vec<_> = access.pairs().collect::<Result<_, _>>().unwrap();

assert_eq!(messages, vec![
(0, "Hello, world!".to_string()),
(1, "My name is Bob.".to_string()),
(2, "Hell is empty and all the devils are here.".to_string()),
(1, "Hello, world!".to_string()),
(2, "My name is Bob.".to_string()),
(3, "Hell is empty and all the devils are here.".to_string()),
]);
```

- _line 4:_ We import the `IterableAccessor` trait to use iteration methods.
- _line 17:_ The `pairs` method produces an iterator over tuples of `(index, value)`. In this case,
- _line 3:_ We import the `IterableAccessor` trait to use iteration methods.
- _line 15:_ The `pairs` method produces an iterator over tuples of `(index, value)`. In this case,
we collect the iterator into a `Vec` for ease of making our assertion later.

Similarly to [`Map`], you can use the [`keys`], [`values`], and [`pairs`] methods to iterate over
Expand All @@ -89,7 +85,7 @@ If you want to perform bounded iteration, it is possible. This time you need the
[`BoundedIterableAccessor`] trait, and the relevant methods are [`bounded_pairs`], [`bounded_keys`],
and [`bounded_values`].

The following example is the same as the previous one except for the bounds found in line 17, and
The following example is the same as the previous one except for the bounds found in line 16, and
the limited results.

<Callout>
Expand All @@ -100,28 +96,27 @@ bounds as suits you.

</Callout>

```rust template="storage" showLineNumbers {4, 17}
use cw_storey::containers::{Column, Item};
use cw_storey::CwStorage;
```rust template="storage" showLineNumbers {4, 16}
use std::ops::Bound;

use cw_storey::containers::{Column, Item};
use storey::containers::BoundedIterableAccessor as _;

const MESSAGES_IX: u8 = 1;

let messages: Column<String> = Column::new(MESSAGES_IX);
let mut cw_storage = CwStorage(&mut storage);
let mut access = messages.access(&mut cw_storage);
let mut access = messages.access(&mut storage);

// populate the column
access.push(&"Hello, world!".to_string()).unwrap();
access.push(&"My name is Bob.".to_string()).unwrap();
access.push(&"Hell is empty and all the devils are here.".to_string()).unwrap();

let messages: Vec<_> = access.bounded_pairs(Some(0), Some(2)).collect::<Result<_, _>>().unwrap();
let messages: Vec<_> = access.bounded_pairs(Bound::Included(1), Bound::Excluded(3)).collect::<Result<_, _>>().unwrap();

assert_eq!(messages, vec![
(0, "Hello, world!".to_string()),
(1, "My name is Bob.".to_string()),
(1, "Hello, world!".to_string()),
(2, "My name is Bob.".to_string()),
]);
```

Expand Down
Loading

0 comments on commit ff1631c

Please sign in to comment.