Skip to content

Commit

Permalink
feat: Override generated entry_points
Browse files Browse the repository at this point in the history
  • Loading branch information
jawoznia committed Jul 31, 2023
1 parent e2ab7e8 commit ae8e6e1
Show file tree
Hide file tree
Showing 14 changed files with 361 additions and 46 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ jobs:
- name: Build cw1-whitelist
working-directory: examples/contracts/cw1-whitelist
run: cargo build --release --target wasm32-unknown-unknown --locked --lib
- name: Build entry-points-overriding
working-directory: examples/contracts/entry-points-overriding
run: cargo build --release --target wasm32-unknown-unknown --locked --lib
- name: Install cosmwasm-check
run: cargo install cosmwasm-check --force
- name: Check contracts
Expand All @@ -97,6 +100,9 @@ jobs:
- name: Cw20-base schema
working-directory: examples/contracts/cw20-base
run: cargo schema
- name: Entry-points-overriding schema
working-directory: examples/contracts/entry-points-overriding
run: cargo schema
- name: Cw1-whitelist ts-codegen
working-directory: examples/contracts/cw1-whitelist/
run: cosmwasm-ts-codegen generate --plugin client --schema ./schema --out ./ts --name cw1-whitelist --no-bundle
Expand All @@ -106,6 +112,9 @@ jobs:
- name: Cw20-base ts-codegen
working-directory: examples/contracts/cw20-base/
run: cosmwasm-ts-codegen generate --plugin client --schema ./schema --out ./ts --name cw20-base --no-bundle
- name: Entry-points-overriding ts-codegen
working-directory: examples/contracts/entry-points-overriding
run: cosmwasm-ts-codegen generate --plugin client --schema ./schema --out ./ts --name entry-points-overriding --no-bundle
- name: Archive schema artifats
uses: actions/upload-artifact@v3
with:
Expand All @@ -114,6 +123,7 @@ jobs:
examples/contracts/cw1-subkeys/schema/cw1-subkeys.json
examples/contracts/cw1-whitelist/schema/cw1-whitelist.json
examples/contracts/cw20-base/schema/cw20-base.json
examples/contracts/entry-points-overriding/schema/entry-points-overriding.json
coverage:
name: Code coverage
Expand Down
6 changes: 1 addition & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
[workspace]
members = [
"sylvia",
"sylvia-derive",

]
members = ["sylvia", "sylvia-derive"]
exclude = ["examples/*"]
resolver = "2"

Expand Down
14 changes: 14 additions & 0 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ members = [
"contracts/cw1-whitelist",
"contracts/cw1-subkeys",
"contracts/cw20-base",
"contracts/entry-points-overriding",
]
resolver = "2"

[workspace.package]
version = "0.5.0"
edition = "2021"

6 changes: 6 additions & 0 deletions examples/contracts/entry-points-overriding/.cargo/config
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"
31 changes: 31 additions & 0 deletions examples/contracts/entry-points-overriding/Cargo.toml
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 examples/contracts/entry-points-overriding/src/bin/schema.rs
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 examples/contracts/entry-points-overriding/src/contract.rs
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 examples/contracts/entry-points-overriding/src/entry_points.rs
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))
}
5 changes: 5 additions & 0 deletions examples/contracts/entry-points-overriding/src/lib.rs
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 examples/contracts/entry-points-overriding/src/messages.rs
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 examples/contracts/entry-points-overriding/src/multitest.rs
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);
}
}
Loading

0 comments on commit ae8e6e1

Please sign in to comment.