Skip to content

Commit

Permalink
feat: Generate sudo entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Nov 30, 2023
1 parent 941bdd0 commit 6d48cea
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 48 deletions.
17 changes: 17 additions & 0 deletions sylvia-derive/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,8 @@ impl<'a> EntryPoints<'a> {
.iter()
.map(|variant| variant.function_name.clone())
.next();
let sudo_variants =
MsgVariants::new(source.as_variants(), MsgType::Sudo, generics, where_clause);
let contract_generics = match &attrs.generics {
Some(generics) => quote! { ::< #generics > },
None => quote! {},
Expand Down Expand Up @@ -1742,6 +1744,19 @@ impl<'a> EntryPoints<'a> {
_ => quote! {},
});

let sudo = override_entry_points
.get_entry_point(MsgType::Sudo)
.map(|_| quote! {})
.unwrap_or_else(|| {
sudo_variants.emit_default_entry_point(
&custom_msg,
&custom_query,
name,
error,
&attrs.generics,
)
});

quote! {
pub mod entry_points {
use super::*;
Expand All @@ -1751,6 +1766,8 @@ impl<'a> EntryPoints<'a> {
#migrate

#reply_ep

#sudo
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion sylvia/tests/dispatching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod contract {
use cosmwasm_std::{Addr, Response, StdError, StdResult};
use cw_storage_plus::{Item, Map};
use sylvia::types::{InstantiateCtx, SudoCtx};
use sylvia_derive::contract;
use sylvia_derive::{contract, entry_points};

use crate::QueryResponse;

Expand All @@ -147,6 +147,7 @@ mod contract {
pub(crate) data: Map<'static, Addr, QueryResponse>,
}

#[entry_points]
#[allow(dead_code)]
#[cfg(not(tarpaulin_include))]
#[contract]
Expand Down
160 changes: 114 additions & 46 deletions sylvia/tests/entry_points.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,55 @@
use contract::sv::ContractExecMsg;
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Coin;
use cw_storage_plus::Item;

#[cw_serde]
pub struct CountResponse {
pub count: u32,
}

#[cw_serde]
pub enum SudoMsg {
MoveFunds {
recipient: String,
amount: Vec<Coin>,
},
pub mod sudo {
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Coin, DepsMut, Env, Response, StdError, StdResult};
use sylvia::types::SudoCtx;

#[cw_serde]
pub enum SudoMsg {
MoveFunds {
recipient: String,
amount: Vec<Coin>,
},
}

impl SudoMsg {
pub fn dispatch(
self,
contract: &crate::contract::Contract,
ctx: SudoCtx,
) -> StdResult<Response> {
contract
.sudos
.update(ctx.deps.storage, |count| -> Result<u32, StdError> {
Ok(count + 1)
})?;
Ok(Response::new())
}
}

#[cw_serde]
pub enum SudoWrapperMsg {
CustomSudo(SudoMsg),
ContractSudo(crate::contract::sv::ContractSudoMsg),
}

impl SudoWrapperMsg {
pub fn dispatch(self, ctx: (DepsMut, Env)) -> StdResult<Response> {
use SudoWrapperMsg::*;

match self {
ContractSudo(msg) => msg.dispatch(&crate::contract::Contract::new(), ctx),
CustomSudo(msg) => msg.dispatch(&crate::contract::Contract::new(), Into::into(ctx)),
}
}
}
}

pub mod migrate {
Expand All @@ -28,15 +64,19 @@ pub mod exec {
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdError, StdResult};
use sylvia::types::ExecCtx;

use crate::contract::Contract;

#[cw_serde]
pub enum UserExecMsg {
IncreaseByOne {},
}

pub fn increase_by_one(ctx: ExecCtx) -> StdResult<Response> {
crate::COUNTER.update(ctx.deps.storage, |count| -> Result<u32, StdError> {
Ok(count + 1)
})?;
Contract::new()
.execs
.update(ctx.deps.storage, |count| -> Result<u32, StdError> {
Ok(count + 1)
})?;
Ok(Response::new())
}

Expand All @@ -58,24 +98,26 @@ pub mod exec {
}
}

const COUNTER: Item<u32> = Item::new("counter");

pub mod entry_points {
use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response, StdResult};
use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response, StdError, StdResult};

use crate::contract::Contract;
use crate::exec::CustomExecMsg;
use crate::migrate::MigrateMsg;
use crate::{SudoMsg, COUNTER};
use crate::sudo::SudoWrapperMsg;

#[entry_point]
pub fn sudo(deps: DepsMut, _env: Env, _msg: SudoMsg) -> StdResult<Response> {
COUNTER.save(deps.storage, &3)?;
Ok(Response::new())
pub fn sudo(deps: DepsMut, env: Env, msg: SudoWrapperMsg) -> StdResult<Response> {
msg.dispatch((deps, env))
}

#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
COUNTER.save(deps.storage, &5)?;
Contract::new()
.migrates
.update(deps.storage, |count| -> Result<u32, StdError> {
Ok(count + 1)
})?;
Ok(Response::new())
}

Expand All @@ -92,29 +134,40 @@ pub mod entry_points {

mod contract {
use cosmwasm_std::{Response, StdError, StdResult};
use cw_storage_plus::Item;
use sylvia::contract;
use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx};
use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, SudoCtx};

use crate::{CountResponse, COUNTER};
use crate::CountResponse;

pub struct Contract {}
pub struct Contract {
pub(crate) execs: Item<'static, u32>,
pub(crate) sudos: Item<'static, u32>,
pub(crate) migrates: Item<'static, u32>,
}

#[cfg(not(tarpaulin_include))]
#[contract]
#[sv::override_entry_point(sudo=crate::entry_points::sudo(crate::SudoMsg))]
#[sv::override_entry_point(sudo=crate::entry_points::sudo(crate::sudo::SudoWrapperMsg))]
#[sv::override_entry_point(migrate=crate::entry_points::migrate(crate::migrate::MigrateMsg))]
#[sv::override_entry_point(exec=crate::entry_points::execute(crate::exec::CustomExecMsg))]
#[allow(dead_code)]
impl Contract {
#[allow(clippy::new_without_default)]
#[allow(dead_code)]
pub fn new() -> Self {
Self {}
Self {
execs: Item::new("execs"),
sudos: Item::new("sudos"),
migrates: Item::new("migrates"),
}
}

#[msg(instantiate)]
pub fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response> {
COUNTER.save(ctx.deps.storage, &0)?;
self.execs.save(ctx.deps.storage, &0)?;
self.migrates.save(ctx.deps.storage, &0)?;
self.sudos.save(ctx.deps.storage, &0)?;
Ok(Response::new())
}

Expand All @@ -123,17 +176,35 @@ mod contract {
Ok(Response::new())
}

#[msg(exec)]
pub fn increase_by_two(&self, ctx: ExecCtx) -> StdResult<Response> {
self.execs
.update(ctx.deps.storage, |count| -> Result<u32, StdError> {
Ok(count + 2)
})?;
Ok(Response::new())
}

#[msg(query)]
pub fn count(&self, ctx: QueryCtx) -> StdResult<CountResponse> {
let count = COUNTER.load(ctx.deps.storage)?;
pub fn execs(&self, ctx: QueryCtx) -> StdResult<CountResponse> {
let count = self.execs.load(ctx.deps.storage)?;
Ok(CountResponse { count })
}

#[msg(exec)]
pub fn increase_by_two(&self, ctx: ExecCtx) -> StdResult<Response> {
crate::COUNTER.update(ctx.deps.storage, |count| -> Result<u32, StdError> {
Ok(count + 2)
})?;
#[msg(query)]
pub fn sudos(&self, ctx: QueryCtx) -> StdResult<CountResponse> {
let count = self.sudos.load(ctx.deps.storage)?;
Ok(CountResponse { count })
}

#[msg(query)]
pub fn migrates(&self, ctx: QueryCtx) -> StdResult<CountResponse> {
let count = self.migrates.load(ctx.deps.storage)?;
Ok(CountResponse { count })
}

#[msg(sudo)]
pub fn sudo(&self, _ctx: SudoCtx) -> StdResult<Response> {
Ok(Response::new())
}
}
Expand All @@ -148,7 +219,7 @@ mod tests {
use crate::contract::sv::multitest_utils::CodeId;
use crate::contract::sv::{ContractExecMsg, ExecMsg};
use crate::exec::{CustomExecMsg, UserExecMsg};
use crate::SudoMsg;
use crate::sudo::SudoWrapperMsg;

#[test]
fn overriden_entry_points_in_mt() {
Expand All @@ -164,26 +235,23 @@ mod tests {
.call(owner)
.unwrap();

let count = contract.count().unwrap().count;
assert_eq!(count, 0);

let msg = SudoMsg::MoveFunds {
let msg = SudoWrapperMsg::CustomSudo(crate::sudo::SudoMsg::MoveFunds {
recipient: "recipient".to_string(),
amount: vec![],
};
});

contract
.app
.app_mut()
.wasm_sudo(contract.contract_addr.clone(), &msg)
.unwrap();

let count = contract.count().unwrap().count;
assert_eq!(count, 3);
let count = contract.sudos().unwrap().count;
assert_eq!(count, 1);

contract.migrate().call(owner, code_id.code_id()).unwrap();
let count = contract.count().unwrap().count;
assert_eq!(count, 5);
let count = contract.migrates().unwrap().count;
assert_eq!(count, 1);

// custom ExecMsg
let msg = CustomExecMsg::CustomExec(UserExecMsg::IncreaseByOne {});
Expand All @@ -197,8 +265,8 @@ mod tests {
)
.unwrap();

let count = contract.count().unwrap().count;
assert_eq!(count, 6);
let count = contract.execs().unwrap().count;
assert_eq!(count, 1);

// custom ExecMsg
let msg =
Expand All @@ -213,7 +281,7 @@ mod tests {
)
.unwrap();

let count = contract.count().unwrap().count;
assert_eq!(count, 8);
let count = contract.execs().unwrap().count;
assert_eq!(count, 3);
}
}
3 changes: 2 additions & 1 deletion sylvia/tests/messages_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,6 @@ fn entry_points_generation() {
entry_points::query,
)
.with_migrate(entry_points::migrate)
.with_reply(entry_points::reply);
.with_reply(entry_points::reply)
.with_sudo(entry_points::sudo);
}

0 comments on commit 6d48cea

Please sign in to comment.