-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Override generated entry_points
- Loading branch information
Showing
14 changed files
with
361 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[alias] | ||
wasm = "build --release --target wasm32-unknown-unknown --lib" | ||
wasm-debug = "build --target wasm32-unknown-unknown --lib" | ||
unit-test = "test --lib" | ||
integration-test = "test --test integration" | ||
schema = "run --bin schema" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[package] | ||
name = "entry-points-overriding" | ||
version = { workspace = true } | ||
authors = ["Jan Woźniak <[email protected]>"] | ||
edition = { workspace = true } | ||
description = "Example usage of sudo and entry point overriding." | ||
license = "Apache-2.0" | ||
repository = "https://github.com/CosmWasm/sylvia" | ||
homepage = "https://cosmwasm.com" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[features] | ||
library = [] | ||
tests = ["library", "cw-multi-test", "anyhow"] | ||
|
||
[dependencies] | ||
anyhow = { version = "1.0", optional = true } | ||
cosmwasm-schema = "1.2" | ||
cosmwasm-std = { version = "1.2", features = ["staking"] } | ||
cw-multi-test = { version = "0.16", optional = true } | ||
cw-storage-plus = "1.0" | ||
cw-utils = "1.0" | ||
serde = { version = "1.0", default-features = false, features = ["derive"] } | ||
sylvia = { path = "../../../sylvia" } | ||
|
||
[dev-dependencies] | ||
anyhow = "1.0" | ||
cw-multi-test = "0.16" | ||
sylvia = { path = "../../../sylvia", features = ["mt"] } |
15 changes: 15 additions & 0 deletions
15
examples/contracts/entry-points-overriding/src/bin/schema.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use cosmwasm_schema::write_api; | ||
|
||
use entry_points_overriding::contract::{ContractQueryMsg, InstantiateMsg}; | ||
use entry_points_overriding::messages::CustomExecMsg; | ||
use entry_points_overriding::messages::SudoMsg; | ||
|
||
#[cfg(not(tarpaulin_include))] | ||
fn main() { | ||
write_api! { | ||
instantiate: InstantiateMsg, | ||
execute: CustomExecMsg, | ||
query: ContractQueryMsg, | ||
sudo: SudoMsg | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
examples/contracts/entry-points-overriding/src/contract.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use cosmwasm_std::{Response, StdError, StdResult}; | ||
use cw_storage_plus::Item; | ||
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; | ||
use sylvia::{contract, schemars}; | ||
|
||
#[cfg(not(feature = "library"))] | ||
use sylvia::entry_points; | ||
|
||
use crate::messages::CountResponse; | ||
|
||
pub struct CounterContract { | ||
pub(crate) counter: Item<'static, u32>, | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_points)] | ||
#[contract] | ||
#[sv::override_entry_point(sudo=crate::entry_points::sudo(crate::messages::SudoMsg))] | ||
#[sv::override_entry_point(exec=crate::entry_points::execute(crate::messages::CustomExecMsg))] | ||
impl CounterContract { | ||
pub const fn new() -> Self { | ||
Self { | ||
counter: Item::new("counter"), | ||
} | ||
} | ||
|
||
#[msg(instantiate)] | ||
pub fn instantiate(&self, ctx: InstantiateCtx) -> StdResult<Response> { | ||
self.counter.save(ctx.deps.storage, &0)?; | ||
Ok(Response::new()) | ||
} | ||
|
||
#[msg(query)] | ||
pub fn count(&self, ctx: QueryCtx) -> StdResult<CountResponse> { | ||
let count = self.counter.load(ctx.deps.storage)?; | ||
Ok(CountResponse { count }) | ||
} | ||
|
||
#[msg(exec)] | ||
pub fn increase_by_two(&self, ctx: ExecCtx) -> StdResult<Response> { | ||
self.counter | ||
.update(ctx.deps.storage, |count| -> Result<u32, StdError> { | ||
Ok(count + 2) | ||
})?; | ||
Ok(Response::new()) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/contracts/entry-points-overriding/src/entry_points.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response, StdResult}; | ||
|
||
use crate::contract::CounterContract; | ||
use crate::messages::{CustomExecMsg, SudoMsg}; | ||
|
||
#[entry_point] | ||
pub fn sudo(deps: DepsMut, _env: Env, _msg: SudoMsg) -> StdResult<Response> { | ||
CounterContract::new().counter.save(deps.storage, &3)?; | ||
Ok(Response::new()) | ||
} | ||
|
||
#[entry_point] | ||
pub fn execute( | ||
deps: DepsMut, | ||
env: Env, | ||
info: MessageInfo, | ||
msg: CustomExecMsg, | ||
) -> StdResult<Response> { | ||
msg.dispatch((deps, env, info)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub mod contract; | ||
pub mod entry_points; | ||
pub mod messages; | ||
#[cfg(any(test, feature = "tests"))] | ||
pub mod multitest; |
44 changes: 44 additions & 0 deletions
44
examples/contracts/entry-points-overriding/src/messages.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdError, StdResult}; | ||
use sylvia::types::ExecCtx; | ||
|
||
use crate::contract::{ContractExecMsg, CounterContract}; | ||
|
||
#[cw_serde] | ||
pub struct CountResponse { | ||
pub count: u32, | ||
} | ||
|
||
#[cw_serde] | ||
pub enum SudoMsg { | ||
SetCountToThree {}, | ||
} | ||
|
||
#[cw_serde] | ||
pub enum UserExecMsg { | ||
IncreaseByOne {}, | ||
} | ||
|
||
pub fn increase_by_one(ctx: ExecCtx) -> StdResult<Response> { | ||
CounterContract::new() | ||
.counter | ||
.update(ctx.deps.storage, |count| -> Result<u32, StdError> { | ||
Ok(count + 1) | ||
})?; | ||
Ok(Response::new()) | ||
} | ||
|
||
#[cw_serde] | ||
pub enum CustomExecMsg { | ||
ContractExec(ContractExecMsg), | ||
CustomExec(UserExecMsg), | ||
} | ||
|
||
impl CustomExecMsg { | ||
pub fn dispatch(self, ctx: (DepsMut, Env, MessageInfo)) -> StdResult<Response> { | ||
match self { | ||
CustomExecMsg::ContractExec(msg) => msg.dispatch(&CounterContract::new(), ctx), | ||
CustomExecMsg::CustomExec(_) => increase_by_one(ctx.into()), | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
examples/contracts/entry-points-overriding/src/multitest.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#[cfg(test)] | ||
mod test { | ||
use crate::{ | ||
contract::{multitest_utils::CodeId, ContractExecMsg, ExecMsg}, | ||
messages::{CustomExecMsg, SudoMsg, UserExecMsg}, | ||
}; | ||
use cosmwasm_std::Addr; | ||
use cw_multi_test::Executor; | ||
use sylvia::multitest::App; | ||
|
||
#[test] | ||
fn overriden_entry_points_in_mt() { | ||
let app = App::default(); | ||
let code_id = CodeId::store_code(&app); | ||
|
||
let owner = "owner"; | ||
|
||
let contract = code_id | ||
.instantiate() | ||
.with_label("Contract") | ||
.with_admin(Some(owner)) | ||
.call(owner) | ||
.unwrap(); | ||
|
||
let count = contract.count().unwrap().count; | ||
assert_eq!(count, 0); | ||
|
||
let msg = SudoMsg::SetCountToThree {}; | ||
|
||
contract | ||
.app | ||
.app_mut() | ||
.wasm_sudo(contract.contract_addr.clone(), &msg) | ||
.unwrap(); | ||
|
||
let count = contract.count().unwrap().count; | ||
assert_eq!(count, 3); | ||
|
||
// custom ExecMsg | ||
let msg = CustomExecMsg::CustomExec(UserExecMsg::IncreaseByOne {}); | ||
(*contract.app) | ||
.app_mut() | ||
.execute_contract( | ||
Addr::unchecked(owner), | ||
contract.contract_addr.clone(), | ||
&msg, | ||
&[], | ||
) | ||
.unwrap(); | ||
|
||
let count = contract.count().unwrap().count; | ||
assert_eq!(count, 4); | ||
|
||
// custom ExecMsg | ||
let msg = CustomExecMsg::ContractExec(ContractExecMsg::CounterContract( | ||
ExecMsg::increase_by_two(), | ||
)); | ||
(*contract.app) | ||
.app_mut() | ||
.execute_contract( | ||
Addr::unchecked(owner), | ||
contract.contract_addr.clone(), | ||
&msg, | ||
&[], | ||
) | ||
.unwrap(); | ||
|
||
let count = contract.count().unwrap().count; | ||
assert_eq!(count, 6); | ||
} | ||
} |
Oops, something went wrong.