diff --git a/src/basics/events.md b/src/basics/events.md index 760d5c9..94ded94 100644 --- a/src/basics/events.md +++ b/src/basics/events.md @@ -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] diff --git a/src/basics/execute.md b/src/basics/execute.md index 9793067..7dd4398 100644 --- a/src/basics/execute.md +++ b/src/basics/execute.md @@ -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] @@ -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] diff --git a/src/basics/first-messages.md b/src/basics/first-messages.md index 12bae4e..5e04d4f 100644 --- a/src/basics/first-messages.md +++ b/src/basics/first-messages.md @@ -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; @@ -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 { Ok(Response::new()) @@ -174,8 +170,6 @@ 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, @@ -183,12 +177,11 @@ pub fn instantiate( info: MessageInfo, msg: InstantiateMsg, ) -> StdResult { - 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. diff --git a/src/basics/funds.md b/src/basics/funds.md index 35fe6cc..9c779f5 100644 --- a/src/basics/funds.md +++ b/src/basics/funds.md @@ -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] @@ -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] diff --git a/src/basics/query-testing.md b/src/basics/query-testing.md index 3afc353..e4ccf75 100644 --- a/src/basics/query-testing.md +++ b/src/basics/query-testing.md @@ -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] diff --git a/src/basics/reusability.md b/src/basics/reusability.md index ffc27bc..620ac0f 100644 --- a/src/basics/reusability.md +++ b/src/basics/reusability.md @@ -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] diff --git a/src/basics/state.md b/src/basics/state.md index 600e748..ef08d69 100644 --- a/src/basics/state.md +++ b/src/basics/state.md @@ -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] @@ -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`, forcing us to load whole the `Vec` to alternate it or read a single element which would be a costly operation. @@ -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 @@ -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] @@ -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 { + 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`.