Skip to content

cw-storey 0.6.0 #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 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 @@ -28,5 +28,5 @@ ibc = "0.54.0" # Used in IBC code
serde = "*"
schemars = "0.8.21" # Used in entrypoint example
thiserror = "1.0.65"
cw-storey = "*"
storey = "*"
cw-storey = { git = "https://github.com/CosmWasm/storey", rev = "5e520bd" }
storey = { git = "https://github.com/CosmWasm/storey", rev = "5e520bd" }
10 changes: 8 additions & 2 deletions docs-test-gen/templates/ibc-packet.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,14 @@ fn doctest() {
}
{
use cw_storey::{containers::Item, CwStorage};
const CHANNEL: Item<ChannelInfo> = Item::new(0);
CHANNEL.access(&mut deps.storage).set(&channel_info).unwrap();
use storey::containers::router;
router! {
router Root {
0 -> channel: Item<ChannelInfo>,
}
}

Root::access(&mut deps.storage).channel_mut().set(&channel_info).unwrap();
}


Expand Down
13 changes: 8 additions & 5 deletions docs-test-gen/templates/sylvia/storey_contract.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sylvia::cw_schema::cw_serde;
use cw_utils::MsgInstantiateContractResponse;
use cw_storey::CwStorage;
use cw_storey::containers::Item;
use storey::containers::router;
use std::marker::PhantomData;

pub mod external_contract {
Expand Down Expand Up @@ -47,16 +48,18 @@ fn doctest() {
pub mod main_contract {
use super::*;

pub struct Contract {
remote: Item<Remote<'static, external_contract::ExternalContract>>
router! {
router Root {
0 -> remote: Item<Remote<'static, external_contract::ExternalContract>>,
}
}

pub struct Contract;

#[contract]
impl Contract {
pub fn new() -> Self {
Self {
remote: Item::new(0)
}
Self
}

#[sv::msg(instantiate)]
Expand Down
7 changes: 3 additions & 4 deletions src/pages/core/entrypoints/query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Callout } from "nextra/components";

# Query

In the previous section we talked about the [`execute`](./execute.mdx) endpoint. The `query` endpoint is
actually pretty similar to its sibling `execute`, but with one key difference: The storage is only
accessible immutably.
In the previous section we talked about the [`execute`](./execute.mdx) endpoint. The `query`
endpoint is actually pretty similar to its sibling `execute`, but with one key difference: The
storage is only accessible immutably.

This means you can only _read_ from the storage but not _write_ to it.

Expand Down Expand Up @@ -55,4 +55,3 @@ pub fn query(
Ok(QueryResponse::default())
}
```

Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ More details about the [Item](../../../cw-storage-plus/containers/item.mdx) type
integer representing a counter value.

More detailed explanation of the smart contract structure written in Sylvia can be found in
[Sylvia documentation](../../../sylvia/basics/contract-structure.mdx), but for testing purposes we will
just mention the key elements of this contract.
[Sylvia documentation](../../../sylvia/basics/contract-structure.mdx), but for testing purposes we
will just mention the key elements of this contract.

#### constructor

Expand Down
27 changes: 20 additions & 7 deletions src/pages/ibc/diy-protocol/channel-lifecycle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const IBC_APP_VERSION: &str = "my-protocol-v1";

```rust filename="ibc.rs" template="core"
use cw_storey::containers::Item;
use storey::containers::router;

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

Expand All @@ -152,7 +153,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(deps.storage).set(&ChannelInfo {
Root::access(deps.storage).channel_mut().set(&ChannelInfo {
channel_id: channel.endpoint.channel_id.clone(),
finalized: false,
})?;
Expand All @@ -170,7 +171,12 @@ struct ChannelInfo {
finalized: bool,
}

const CHANNEL: Item<ChannelInfo> = Item::new(0);
router! {
router Root {
0 -> channel: Item<ChannelInfo>,
}
}

const IBC_APP_VERSION: &str = "my-protocol-v1";
```

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

```rust filename="ibc.rs" template="core"
use cw_storey::containers::Item;
use storey::containers::router;

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

// 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(&*deps.storage)
let mut channel_info = Root::access(&*deps.storage)
.channel()
.get()?
.ok_or_else(|| StdError::generic_err("channel not found"))?;
ensure!(
Expand All @@ -387,7 +394,9 @@ 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(deps.storage).set(&channel_info)?;
Root::access(deps.storage)
.channel_mut()
.set(&channel_info)?;

Ok(IbcBasicResponse::new())
}
Expand All @@ -399,7 +408,11 @@ struct ChannelInfo {
finalized: bool,
}

const CHANNEL: Item<ChannelInfo> = Item::new(0);
router! {
router Root {
0 -> channel: Item<ChannelInfo>,
}
}
```

</Tabs.Tab>
Expand Down
27 changes: 19 additions & 8 deletions src/pages/ibc/diy-protocol/packet-lifecycle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ Ok(Response::new().add_message(msg))

```rust template="ibc-packet"
use cw_storey::containers::Item;
use storey::containers::router;

const CHANNEL: Item<ChannelInfo> = Item::new(0);
router! {
router Root {
0 -> channel: Item<ChannelInfo>,
}
}

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

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

router! {
router Root {
0 -> ack_later: Map<u64, Map<String, Item<String>>>,
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn ibc_packet_receive(
Expand All @@ -232,7 +244,8 @@ 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(deps.storage)
Root::access(deps.storage)
.ack_later_mut()
.entry_mut(&msg.packet.sequence)
.entry_mut(&msg.packet.dest.channel_id)
.set(&"ack str".to_string())?;
Expand All @@ -250,7 +263,8 @@ pub fn async_ack(
channel_id: String,
) -> StdResult<Response> {
// load data from contract state
let ack_str = ACK_LATER.access(deps.storage)
let ack_str = Root::access(deps.storage)
.ack_later()
.entry(&packet_sequence)
.entry(&channel_id)
.try_get()
Expand All @@ -263,9 +277,6 @@ pub fn async_ack(
ack: IbcAcknowledgement::new(StdAck::success(ack_str.as_bytes())),
}))
}

const ACK_LATER_IX: u8 = 0;
const ACK_LATER: Map<u64, Map<String, Item<String>>> = Map::new(ACK_LATER_IX);
```

</Tabs.Tab>
Expand Down
41 changes: 30 additions & 11 deletions src/pages/storey/containers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ For more advanced use cases, you can build your own containers as described in t

## Namespace

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

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

let item: Item<u32> = Item::new(0); // byte 0 is used as the namespace
let map: Map<String, Item<u32>> = Map::new(1); // byte 1 is used as the namespace
use storey::containers::router;

router! {
router Root {
0 -> foo: Item<u32>,
1 -> bar: Map<String, Item<u32>>,
}
}
```

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

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

let item: Item<u32> = Item::new(0);
router! {
router Root {
0 -> item: Item<u32>,
}
}

item.access(&mut storage)
Root::access(&mut storage)
.item_mut()
.set(&42)
.unwrap();

assert_eq!(
item.access(&storage)
Root::access(&storage)
.item()
.get()
.unwrap(),
Some(42)
Expand Down Expand Up @@ -99,17 +111,24 @@ assert_eq!(

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

let map: Map<String, Map<String, Item<u32>>> = Map::new(0);
router! {
router Root {
0 -> map: Map<String, Map<String, Item<u32>>>,
}
}

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

assert_eq!(
map.access(&storage)
Root::access(&storage)
.map()
.entry("foo")
.entry("bar")
.get()
Expand Down
Loading
Loading