Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Generic support #253

Merged
merged 15 commits into from
Nov 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: Move messages method out of EnumMsg
jawoznia committed Nov 13, 2023
commit a1bdc2a6201f3f3831b77aeb1167138905b18a42
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ jobs:
run: cargo clippy --all-targets -- -D warnings
- name: Fmt check project
run: cargo fmt --check

- name: Test examples
working-directory: examples
run: cargo test --locked
@@ -75,6 +76,7 @@ jobs:
- name: Fmt check examples
working-directory: examples
run: cargo fmt --check

- name: Build cw20-base example
working-directory: examples/contracts/cw20-base
run: cargo build --release --target wasm32-unknown-unknown --locked --lib
@@ -90,10 +92,18 @@ jobs:
- name: Build custom
working-directory: examples/contracts/custom
run: cargo build --release --target wasm32-unknown-unknown --locked --lib
- name: Build generic_contract
working-directory: examples/contracts/generic_contract
run: cargo build --release --target wasm32-unknown-unknown --locked --lib
- name: Build generic_iface_on_contract
working-directory: examples/contracts/generic_iface_on_contract
run: cargo build --release --target wasm32-unknown-unknown --locked --lib

- name: Install cosmwasm-check
run: cargo install cosmwasm-check --force
- name: Check contracts
run: find examples/target/wasm32-unknown-unknown/release/ -type f -name "*.wasm" -exec cosmwasm-check {} \;

- name: Cw1-whitelist schema
working-directory: examples/contracts/cw1-whitelist/
run: cargo schema
@@ -109,6 +119,13 @@ jobs:
- name: Custom schema
working-directory: examples/contracts/custom
run: cargo schema
- name: Generic_contract schema
working-directory: examples/contracts/generic_contract
run: cargo schema
- name: generic_iface_on_contract schema
working-directory: examples/contracts/generic_iface_on_contract
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
@@ -124,6 +141,13 @@ jobs:
- name: Custom ts-codegen
working-directory: examples/contracts/custom/
run: cosmwasm-ts-codegen generate --plugin client --schema ./schema --out ./ts --name custom --no-bundle
- name: Generic_contract ts-codegen
working-directory: examples/contracts/generic_contract/
run: cosmwasm-ts-codegen generate --plugin client --schema ./schema --out ./ts --name custom --no-bundle
- name: Generic_iface_on_contract ts-codegen
working-directory: examples/contracts/generic_iface_on_contract
run: cosmwasm-ts-codegen generate --plugin client --schema ./schema --out ./ts --name custom --no-bundle

- name: Archive schema artifats
uses: actions/upload-artifact@v3
with:
@@ -134,6 +158,8 @@ jobs:
examples/contracts/cw20-base/schema/cw20-base.json
examples/contracts/entry-points-overriding/schema/entry-points-overriding.json
examples/contracts/custom/schema/custom.json
examples/contracts/generic_contract/schema/generic_contract.json
examples/contracts/generic_iface_on_contract/schema/generic_iface_on_contract.json

coverage:
name: Code coverage
58 changes: 58 additions & 0 deletions examples/Cargo.lock

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

28 changes: 16 additions & 12 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
[workspace]
members = [
# Contract intefaces
"interfaces/cw1",
"interfaces/cw4",
"interfaces/cw20-allowances",
"interfaces/cw20-minting",
"interfaces/cw20-marketing",
# Contract intefaces
"interfaces/cw1",
"interfaces/cw4",
"interfaces/cw20-allowances",
"interfaces/cw20-minting",
"interfaces/cw20-marketing",
"interfaces/custom-and-generic",
"interfaces/generic",

# Contracts
"contracts/cw1-whitelist",
"contracts/cw1-subkeys",
"contracts/cw20-base",
"contracts/entry-points-overriding",
"contracts/custom",
# Contracts
"contracts/cw1-whitelist",
"contracts/cw1-subkeys",
"contracts/cw20-base",
"contracts/entry-points-overriding",
"contracts/custom",
"contracts/generic_contract",
"contracts/generic_iface_on_contract",
]
resolver = "2"

6 changes: 6 additions & 0 deletions examples/contracts/generic_contract/.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"
34 changes: 34 additions & 0 deletions examples/contracts/generic_contract/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "generic_contract"
version = { workspace = true }
authors = ["Jan Woźniak <jan@confio.gmbh>"]
edition = { workspace = true }
description = "Example of generic contract"
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.3", 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" }
cw1 = { path = "../../interfaces/cw1" }
generic = { path = "../../interfaces/generic" }
custom-and-generic = { path = "../../interfaces/custom-and-generic" }

[dev-dependencies]
anyhow = "1.0"
cw-multi-test = "0.16"
sylvia = { path = "../../../sylvia", features = ["mt"] }
13 changes: 13 additions & 0 deletions examples/contracts/generic_contract/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use cosmwasm_schema::write_api;

#[cfg(not(tarpaulin_include))]
fn main() {
use generic_contract::contract::{ContractExecMsg, ContractQueryMsg, InstantiateMsg};
use sylvia::types::SvCustomMsg;

write_api! {
instantiate: InstantiateMsg<SvCustomMsg>,
execute: ContractExecMsg<SvCustomMsg>,
query: ContractQueryMsg<SvCustomMsg>,
}
}
114 changes: 114 additions & 0 deletions examples/contracts/generic_contract/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use cosmwasm_std::{Reply, Response, StdResult};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use sylvia::types::{
CustomMsg, ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, ReplyCtx, SvCustomMsg,
};
use sylvia::{contract, schemars};

pub struct GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType>(
std::marker::PhantomData<(
InstantiateParam,
ExecParam,
QueryParam,
MigrateParam,
RetType,
)>,
);

#[contract]
#[messages(cw1 as Cw1: custom(msg))]
#[messages(generic<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg> as Generic: custom(msg))]
#[messages(custom_and_generic<SvCustomMsg, SvCustomMsg, sylvia::types::SvCustomMsg> as CustomAndGeneric)]
#[sv::custom(msg=SvCustomMsg)]
impl<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType>
GenericContract<InstantiateParam, ExecParam, QueryParam, MigrateParam, RetType>
where
for<'msg_de> InstantiateParam: CustomMsg + Deserialize<'msg_de> + 'msg_de,
ExecParam: CustomMsg + DeserializeOwned + 'static,
QueryParam: CustomMsg + DeserializeOwned + 'static,
MigrateParam: CustomMsg + DeserializeOwned + 'static,
RetType: CustomMsg + DeserializeOwned + 'static,
{
pub const fn new() -> Self {
Self(std::marker::PhantomData)
}

#[msg(instantiate)]
pub fn instantiate(
&self,
_ctx: InstantiateCtx,
_msg: InstantiateParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}

#[msg(exec)]
pub fn contract_execute(
&self,
_ctx: ExecCtx,
_msg: ExecParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}

#[msg(query)]
pub fn contract_query(
&self,
_ctx: QueryCtx,
_msg: QueryParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}

#[msg(migrate)]
pub fn migrate(
&self,
_ctx: MigrateCtx,
_msg: MigrateParam,
) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}

#[allow(dead_code)]
#[msg(reply)]
fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult<Response<SvCustomMsg>> {
Ok(Response::new())
}
}

#[cfg(test)]
mod tests {
use super::multitest_utils::CodeId;
use sylvia::multitest::App;
use sylvia::types::SvCustomMsg;

#[test]
fn generic_contract() {
let app = App::<cw_multi_test::BasicApp<SvCustomMsg>>::custom(|_, _, _| {});
let code_id: CodeId<
SvCustomMsg,
SvCustomMsg,
SvCustomMsg,
super::SvCustomMsg,
super::SvCustomMsg,
_,
> = CodeId::store_code(&app);

let owner = "owner";

let contract = code_id
.instantiate(SvCustomMsg {})
.with_label("GenericContract")
.with_admin(owner)
.call(owner)
.unwrap();

contract.contract_execute(SvCustomMsg).call(owner).unwrap();
contract.contract_query(SvCustomMsg).unwrap();
contract
.migrate(SvCustomMsg)
.call(owner, code_id.code_id())
.unwrap();
}
}
Loading