Skip to content

Commit

Permalink
Consistency fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Dec 15, 2022
1 parent 260c91b commit ae82e02
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/basics/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use schemars;
use sylvia::contract;
#pub struct AdminContract<'a> {
# pub(crate) admins: Map<'static, &'a Addr, Empty>,
# pub(crate) admins: Map<'a, &'a Addr, Empty>,
#}
#
#[contract]
Expand Down
4 changes: 2 additions & 2 deletions src/basics/execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ use schemars;
use sylvia::contract;
#pub struct AdminContract<'a> {
# pub(crate) admins: Map<'static, &'a Addr, Empty>,
# pub(crate) admins: Map<'a, &'a Addr, Empty>,
#}
#[contract]
Expand Down Expand Up @@ -290,7 +290,7 @@ Now let's add a simple unit test for `execute` message.
#use sylvia::contract;
#
#pub struct AdminContract<'a> {
# pub(crate) admins: Map<'static, &'a Addr, Empty>,
# pub(crate) admins: Map<'a, &'a Addr, Empty>,
#}
#
# #[contract]
Expand Down
17 changes: 5 additions & 12 deletions src/basics/first-messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdResult};
use schemars;
use sylvia::contract;
pub struct AdminContract {}
pub struct AdminContract;
pub type ContractError = cosmwasm_std::StdError;
Expand Down Expand Up @@ -148,16 +148,12 @@ use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdResult};
use schemars;
use sylvia::contract;
pub struct AdminContract {}
pub struct AdminContract;
pub type ContractError = cosmwasm_std::StdError;
#[contract]
impl AdminContract {
pub const fn new() -> AdminContract {
AdminContract {}
}
#[msg(instantiate)]
pub fn instantiate(&self, _ctx: (DepsMut, Env, MessageInfo)) -> StdResult<Response> {
Ok(Response::new())
Expand All @@ -174,21 +170,18 @@ use cosmwasm_std::{entry_point, DepsMut, Empty, Env, MessageInfo, Response, StdR
use crate::contract::{InstantiateMsg, AdminContract};
const CONTRACT: AdminContract = AdminContract::new();
#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
msg.dispatch(&CONTRACT, (deps, env, info))
msg.dispatch(&AdminContract, (deps, env, info))
}
```

We have created `const CONTRACT`. It will be an internal value in our smart contract on which we
will dispatch all the messages. `Empty` is now changed to `InstantiateMsg` generated by `sylvia`.
Now we dispatch the message, and it will trigger `instatiation` method.
`Empty` is now changed to `InstantiateMsg` generated by `sylvia`.
We dispatch the message, and it will trigger `instatiation` method.
For now, it is just returning an empty Response, but let's change it in the following chapters,
where we will introduce states and queries.
8 changes: 4 additions & 4 deletions src/basics/funds.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use schemars;
use sylvia::contract;
pub struct AdminContract<'a> {
pub(crate) admins: Map<'static, &'a Addr, Empty>,
pub(crate) donation_denom: Item<'static, String>,
pub(crate) admins: Map<'a, &'a Addr, Empty>,
pub(crate) donation_denom: Item<'a, String>,
}
#[contract]
Expand Down Expand Up @@ -236,8 +236,8 @@ use schemars;
use sylvia::contract;
pub struct AdminContract<'a> {
pub(crate) admins: Map<'static, &'a Addr, Empty>,
pub(crate) donation_denom: Item<'static, String>,
pub(crate) admins: Map<'a, &'a Addr, Empty>,
pub(crate) donation_denom: Item<'a, String>,
}
#[contract]
Expand Down
2 changes: 1 addition & 1 deletion src/basics/query-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ the unit test. This approach is simple and doesn't require much knowledge beside
#use sylvia::contract;
#
#pub struct AdminContract<'a> {
# pub(crate) admins: Map<'static, &'a Addr, Empty>,
# pub(crate) admins: Map<'a, &'a Addr, Empty>,
#}
#
# #[contract]
Expand Down
4 changes: 2 additions & 2 deletions src/basics/reusability.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ use sylvia::contract;
use crate::{donation, error::ContractError, responses::AdminListResp};
#pub struct AdminContract<'a> {
# pub(crate) admins: Map<'static, &'a Addr, Empty>,
# pub(crate) donation_denom: Item<'static, String>,
# pub(crate) admins: Map<'a, &'a Addr, Empty>,
# pub(crate) donation_denom: Item<'a, String>,
#}
#
#[contract]
Expand Down
34 changes: 31 additions & 3 deletions src/basics/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use schemars;
use sylvia::contract;
pub struct AdminContract<'a> {
pub(crate) admins: Map<'static, &'a Addr, Empty>,
pub(crate) admins: Map<'a, &'a Addr, Empty>,
}
#[contract]
Expand Down Expand Up @@ -70,7 +70,7 @@ New types:
- [`Empty`](https://docs.rs/cosmwasm-std/0.16.0/cosmwasm_std/struct.Empty.html) - an empty struct
that serves as a placeholder.

We declared state `admins` as immutable `Map<'static, &'a Addr, Empty>`.
We declared state `admins` as immutable `Map<'a, &'a Addr, Empty>`.
It might seem weird that we created `Map` with an `Empty` value containing no information. Still,
our alternative would be to store it as `Vec<Addr>`, forcing us to load whole the `Vec` to
alternate it or read a single element which would be a costly operation.
Expand All @@ -91,6 +91,9 @@ intelligently. The key to the `Map` doesn't matter to us - it would be figured o
based on a unique string passed to the
[`new`](https://docs.rs/cw-storage-plus/0.13.4/cw_storage_plus/struct.Item.html#method.new) method.

Last new thing. We crated the `new` method for the `AdminContract` to hide the instantiation of
the fields.

## Initializing the state

Now that the state field has been added we can improve our instantiate. We will make it possible for
Expand All @@ -103,7 +106,7 @@ use schemars;
use sylvia::contract;
#
#pub struct AdminContract<'a> {
# pub(crate) admins: Map<'static, &'a Addr, Empty>,
# pub(crate) admins: Map<'a, &'a Addr, Empty>,
#}
#[contract]
Expand Down Expand Up @@ -186,5 +189,30 @@ actual blockchain storage. As emphasized, the `Map` object stores nothing and is
It determines how to store the data in the storage given to it. The second argument is the
serializable data to be stored, and the last one is the value which in our case is `Empty`.

With the state added to our contract, let's also update the entry_point. Go to `src/lib.rs`:

```rust,noplayground
pub mod contract;
use cosmwasm_std::{entry_point, DepsMut, Empty, Env, MessageInfo, Response, StdResult};
use crate::contract::{InstantiateMsg, AdminContract};
const CONTRACT: AdminContract = AdminContract::new();
#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
msg.dispatch(&CONTRACT, (deps, env, info))
}
```

Instead of passing the `&AdminContract` to the `dispatch` method, we first create the inner value
`CONTRACT` by calling `AdminContract::new()`.

Nice, we now have the state initialized on our contract, but we can't validate if the data is
stored correctly. Let's change it in the next chapter, in which we will introduce `query`.

0 comments on commit ae82e02

Please sign in to comment.