From 3db923e891b4bbed1df6ea70d310f354616034a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wo=C5=BAniak?= Date: Fri, 10 May 2024 20:10:31 +0200 Subject: [PATCH 1/6] Workaround to enable communication between `Empty` and `Custom` contracts in mt --- Cargo.lock | 8 +- Cargo.toml | 85 ++++++++++--------- contracts/consumer/converter/Cargo.toml | 35 ++++---- contracts/consumer/converter/src/contract.rs | 62 ++++++++++---- .../consumer/simple-price-feed/Cargo.toml | 31 +++---- .../simple-price-feed/src/contract.rs | 26 ++++-- .../consumer/virtual-staking/src/contract.rs | 1 - 7 files changed, 149 insertions(+), 99 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9f87aa4..7c98ea8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -607,6 +607,7 @@ dependencies = [ "cw2", "derivative", "mesh-apis", + "mesh-bindings", "mesh-burn", "mesh-simple-price-feed", "schemars", @@ -736,6 +737,7 @@ dependencies = [ "cw2", "derivative", "mesh-apis", + "mesh-bindings", "schemars", "serde", "sylvia", @@ -1188,8 +1190,7 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "sylvia" version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e64c8e892525ec035e5bdbeceec19309b1534734da7ff7bc69e9f639b077c497" +source = "git+https://github.com/CosmWasm/sylvia?branch=jawoznia/cosmwasm1/map_err_on_into_response#a5d3546eb287e3e7f003251c529ff3401784e1bb" dependencies = [ "anyhow", "cosmwasm-schema", @@ -1207,8 +1208,7 @@ dependencies = [ [[package]] name = "sylvia-derive" version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae5e0f41752efba2c6895514fa4caae742f69a2a73b4790bb6e365400c563bc1" +source = "git+https://github.com/CosmWasm/sylvia?branch=jawoznia/cosmwasm1/map_err_on_into_response#a5d3546eb287e3e7f003251c529ff3401784e1bb" dependencies = [ "convert_case", "itertools 0.12.1", diff --git a/Cargo.toml b/Cargo.toml index b15060e5..e5384b60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,58 +1,65 @@ [workspace] -members = ["packages/*", "contracts/provider/*", "contracts/consumer/*", "contracts/osmosis-price-provider"] +members = [ + "packages/*", + "contracts/provider/*", + "contracts/consumer/*", + "contracts/osmosis-price-provider", +] resolver = "2" [workspace.package] -edition = "2021" -version = "0.10.0-alpha.1" -license = "MIT" -repository = "https://github.com/osmosis-labs/mesh-security" +edition = "2021" +version = "0.10.0-alpha.1" +license = "MIT" +repository = "https://github.com/osmosis-labs/mesh-security" [workspace.dependencies] -mesh-apis = { path = "./packages/apis" } -mesh-bindings = { path = "./packages/bindings" } -mesh-burn = { path = "./packages/burn" } -mesh-sync = { path = "./packages/sync" } +mesh-apis = { path = "./packages/apis" } +mesh-bindings = { path = "./packages/bindings" } +mesh-burn = { path = "./packages/burn" } +mesh-sync = { path = "./packages/sync" } mesh-virtual-staking-mock = { path = "./packages/virtual-staking-mock" } -mesh-vault = { path = "./contracts/provider/vault" } -mesh-external-staking = { path = "./contracts/provider/external-staking" } -mesh-native-staking = { path = "./contracts/provider/native-staking" } -mesh-native-staking-proxy = { path = "./contracts/provider/native-staking-proxy" } - -mesh-converter = { path = "./contracts/consumer/converter" } -mesh-simple-price-feed = { path = "./contracts/consumer/simple-price-feed" } -mesh-virtual-staking = { path = "./contracts/consumer/virtual-staking" } - -sylvia = "0.10.1" -cosmwasm-schema = "1.5.4" -cosmwasm-std = { version = "1.5.4", features = ["ibc3", "cosmwasm_1_3"] } -cw-storage-plus = "1.2.0" -cw-utils = "1.0.3" -cw2 = "1.1.2" -osmosis-std = "0.20.1" -schemars = "0.8.17" -serde = { version = "1.0.199", default-features = false, features = ["derive"] } -thiserror = "1.0.59" +mesh-vault = { path = "./contracts/provider/vault" } +mesh-external-staking = { path = "./contracts/provider/external-staking" } +mesh-native-staking = { path = "./contracts/provider/native-staking" } +mesh-native-staking-proxy = { path = "./contracts/provider/native-staking-proxy" } + +mesh-converter = { path = "./contracts/consumer/converter" } +mesh-simple-price-feed = { path = "./contracts/consumer/simple-price-feed" } +mesh-virtual-staking = { path = "./contracts/consumer/virtual-staking" } + +sylvia = { git = "https://github.com/CosmWasm/sylvia", branch = "jawoznia/cosmwasm1/map_err_on_into_response", features = [ + "mt", +] } +cosmwasm-schema = "1.5.4" +cosmwasm-std = { version = "1.5.4", features = ["ibc3", "cosmwasm_1_3"] } +cw-storage-plus = "1.2.0" +cw-utils = "1.0.3" +cw2 = "1.1.2" +osmosis-std = "0.20.1" +schemars = "0.8.17" +serde = { version = "1.0.199", default-features = false, features = ["derive"] } +thiserror = "1.0.59" semver = "1.0.22" itertools = "0.12.1" # dev deps -anyhow = "1" +anyhow = "1" cw-multi-test = "0.20" -derivative = "2" -test-case = "3.3.1" +derivative = "2" +test-case = "3.3.1" [profile.release] -codegen-units = 1 -debug = false +codegen-units = 1 +debug = false debug-assertions = false -lto = true -panic = 'abort' -rpath = false -opt-level = 3 -overflow-checks = true +lto = true +panic = 'abort' +rpath = false +opt-level = 3 +overflow-checks = true [profile.release.package.mesh-vault] codegen-units = 1 -incremental = false +incremental = false diff --git a/contracts/consumer/converter/Cargo.toml b/contracts/consumer/converter/Cargo.toml index 547f896d..c60f0af3 100644 --- a/contracts/consumer/converter/Cargo.toml +++ b/contracts/consumer/converter/Cargo.toml @@ -3,8 +3,8 @@ name = "mesh-converter" description = "IBC connector on the Consumer side, converting their IBC messages into native tokens for virtual staking" version = { workspace = true } edition = { workspace = true } -license = { workspace = true } -repository = { workspace = true } +license = { workspace = true } +repository = { workspace = true } # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] @@ -19,28 +19,29 @@ library = [] mt = ["library", "sylvia/mt"] [dependencies] -mesh-apis = { workspace = true } +mesh-apis = { workspace = true } +mesh-bindings = { workspace = true } -sylvia = { workspace = true } -cosmwasm-schema = { workspace = true } -cosmwasm-std = { workspace = true } -cw-storage-plus = { workspace = true } -cw2 = { workspace = true } -cw-utils = { workspace = true } +sylvia = { workspace = true } +cosmwasm-schema = { workspace = true } +cosmwasm-std = { workspace = true } +cw-storage-plus = { workspace = true } +cw2 = { workspace = true } +cw-utils = { workspace = true } -schemars = { workspace = true } -serde = { workspace = true } -thiserror = { workspace = true } +schemars = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } [dev-dependencies] -mesh-burn = { workspace = true } +mesh-burn = { workspace = true } mesh-simple-price-feed = { workspace = true, features = ["mt"] } cw-multi-test = { workspace = true } -test-case = { workspace = true } -derivative = { workspace = true } -anyhow = { workspace = true } +test-case = { workspace = true } +derivative = { workspace = true } +anyhow = { workspace = true } [[bin]] name = "schema" -doc = false +doc = false diff --git a/contracts/consumer/converter/src/contract.rs b/contracts/consumer/converter/src/contract.rs index fc1d97cf..20203484 100644 --- a/contracts/consumer/converter/src/contract.rs +++ b/contracts/consumer/converter/src/contract.rs @@ -24,6 +24,16 @@ pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); const REPLY_ID_INSTANTIATE: u64 = 1; +#[cfg(not(any(test, feature = "mt")))] +pub type ConverterCustomMsg = cosmwasm_std::Empty; +#[cfg(any(test, feature = "mt"))] +pub type ConverterCustomMsg = mesh_bindings::VirtualStakeCustomMsg; + +#[cfg(not(any(test, feature = "mt")))] +pub type ConverterCustomQuery = cosmwasm_std::Empty; +#[cfg(any(test, feature = "mt"))] +pub type ConverterCustomQuery = mesh_bindings::VirtualStakeCustomQuery; + pub struct ConverterContract<'a> { pub config: Item<'a, Config>, pub virtual_stake: Item<'a, Addr>, @@ -33,6 +43,8 @@ pub struct ConverterContract<'a> { #[contract] #[sv::error(ContractError)] #[sv::messages(converter_api as ConverterApi)] +/// Workaround for lack of support in communication `Empty` <-> `Custom` Contracts. +#[sv::custom(query=ConverterCustomQuery, msg=ConverterCustomMsg)] impl ConverterContract<'_> { pub const fn new() -> Self { Self { @@ -51,7 +63,7 @@ impl ConverterContract<'_> { #[sv::msg(instantiate)] pub fn instantiate( &self, - ctx: InstantiateCtx, + ctx: InstantiateCtx, price_feed: String, discount: Decimal, remote_denom: String, @@ -94,7 +106,11 @@ impl ConverterContract<'_> { } #[sv::msg(reply)] - fn reply(&self, ctx: ReplyCtx, reply: Reply) -> Result { + fn reply( + &self, + ctx: ReplyCtx, + reply: Reply, + ) -> Result { match reply.id { REPLY_ID_INSTANTIATE => self.reply_init_callback(ctx.deps, reply.result.unwrap()), _ => Err(ContractError::InvalidReplyId(reply.id)), @@ -104,7 +120,7 @@ impl ConverterContract<'_> { /// Store virtual staking address fn reply_init_callback( &self, - deps: DepsMut, + deps: DepsMut, reply: SubMsgResponse, ) -> Result { let init_data = parse_instantiate_response_data(&reply.data.unwrap())?; @@ -118,7 +134,7 @@ impl ConverterContract<'_> { #[sv::msg(exec)] fn test_stake( &self, - ctx: ExecCtx, + ctx: ExecCtx, validator: String, stake: Coin, ) -> Result { @@ -139,7 +155,7 @@ impl ConverterContract<'_> { #[sv::msg(exec)] fn test_unstake( &self, - ctx: ExecCtx, + ctx: ExecCtx, validator: String, unstake: Coin, ) -> Result { @@ -160,7 +176,7 @@ impl ConverterContract<'_> { #[sv::msg(exec)] fn test_burn( &self, - ctx: ExecCtx, + ctx: ExecCtx, validators: Vec, burn: Coin, ) -> Result { @@ -177,7 +193,7 @@ impl ConverterContract<'_> { } #[sv::msg(query)] - fn config(&self, ctx: QueryCtx) -> Result { + fn config(&self, ctx: QueryCtx) -> Result { let config = self.config.load(ctx.deps.storage)?; let virtual_staking = self.virtual_stake.load(ctx.deps.storage)?.into_string(); Ok(ConfigResponse { @@ -191,7 +207,7 @@ impl ConverterContract<'_> { /// It is pulled out into a method, so it can also be called by test_stake for testing pub(crate) fn stake( &self, - deps: DepsMut, + deps: DepsMut, validator: String, stake: Coin, ) -> Result { @@ -215,7 +231,7 @@ impl ConverterContract<'_> { /// It is pulled out into a method, so it can also be called by test_unstake for testing pub(crate) fn unstake( &self, - deps: DepsMut, + deps: DepsMut, validator: String, unstake: Coin, ) -> Result { @@ -239,7 +255,7 @@ impl ConverterContract<'_> { /// It is pulled out into a method, so it can also be called by test_burn for testing pub(crate) fn burn( &self, - deps: DepsMut, + deps: DepsMut, validators: &[String], burn: Coin, ) -> Result { @@ -262,7 +278,11 @@ impl ConverterContract<'_> { Ok(Response::new().add_message(msg).add_event(event)) } - fn normalize_price(&self, deps: Deps, amount: Coin) -> Result { + fn normalize_price( + &self, + deps: Deps, + amount: Coin, + ) -> Result { let config = self.config.load(deps.storage)?; ensure_eq!( config.remote_denom, @@ -291,7 +311,11 @@ impl ConverterContract<'_> { }) } - fn invert_price(&self, deps: Deps, amount: Coin) -> Result { + fn invert_price( + &self, + deps: Deps, + amount: Coin, + ) -> Result { let config = self.config.load(deps.storage)?; ensure_eq!( config.local_denom, @@ -325,7 +349,7 @@ impl ConverterContract<'_> { pub(crate) fn transfer_rewards( &self, - deps: Deps, + deps: Deps, recipient: String, rewards: Coin, ) -> Result { @@ -351,7 +375,11 @@ impl ConverterContract<'_> { Ok(msg.into()) } - fn ensure_authorized(&self, deps: &DepsMut, info: &MessageInfo) -> Result<(), ContractError> { + fn ensure_authorized( + &self, + deps: &DepsMut, + info: &MessageInfo, + ) -> Result<(), ContractError> { let virtual_stake = self.virtual_stake.load(deps.storage)?; ensure_eq!(info.sender, virtual_stake, ContractError::Unauthorized {}); @@ -366,7 +394,7 @@ impl ConverterApi for ConverterContract<'_> { /// stakers who staked on this validator. This is tracked on the provider, so we send an IBC packet there. fn distribute_reward( &self, - mut ctx: ExecCtx, + mut ctx: ExecCtx, validator: String, ) -> Result { self.ensure_authorized(&ctx.deps, &ctx.info)?; @@ -391,7 +419,7 @@ impl ConverterApi for ConverterContract<'_> { /// in the native staking denom. fn distribute_rewards( &self, - mut ctx: ExecCtx, + mut ctx: ExecCtx, payments: Vec, ) -> Result { self.ensure_authorized(&ctx.deps, &ctx.info)?; @@ -431,7 +459,7 @@ impl ConverterApi for ConverterContract<'_> { #[allow(clippy::too_many_arguments)] fn valset_update( &self, - ctx: ExecCtx, + ctx: ExecCtx, additions: Vec, removals: Vec, updated: Vec, diff --git a/contracts/consumer/simple-price-feed/Cargo.toml b/contracts/consumer/simple-price-feed/Cargo.toml index d19c68b7..3122ac0d 100644 --- a/contracts/consumer/simple-price-feed/Cargo.toml +++ b/contracts/consumer/simple-price-feed/Cargo.toml @@ -3,8 +3,8 @@ name = "mesh-simple-price-feed" description = "Returns a fixed price for assets to the converter contract - for tests or gov-defined prices" version = { workspace = true } edition = { workspace = true } -license = { workspace = true } -repository = { workspace = true } +license = { workspace = true } +repository = { workspace = true } # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] @@ -19,25 +19,26 @@ library = [] mt = ["library", "sylvia/mt"] [dependencies] -mesh-apis = { workspace = true } +mesh-apis = { workspace = true } +mesh-bindings = { workspace = true } sylvia = { workspace = true } -cosmwasm-schema = { workspace = true } -cosmwasm-std = { workspace = true } -cw-storage-plus = { workspace = true } -cw2 = { workspace = true } -cw-utils = { workspace = true } +cosmwasm-schema = { workspace = true } +cosmwasm-std = { workspace = true } +cw-storage-plus = { workspace = true } +cw2 = { workspace = true } +cw-utils = { workspace = true } -schemars = { workspace = true } -serde = { workspace = true } -thiserror = { workspace = true } +schemars = { workspace = true } +serde = { workspace = true } +thiserror = { workspace = true } [dev-dependencies] cw-multi-test = { workspace = true } -test-case = { workspace = true } -derivative = { workspace = true } -anyhow = { workspace = true } +test-case = { workspace = true } +derivative = { workspace = true } +anyhow = { workspace = true } [[bin]] name = "schema" -doc = false +doc = false diff --git a/contracts/consumer/simple-price-feed/src/contract.rs b/contracts/consumer/simple-price-feed/src/contract.rs index 59896fc4..06aedf11 100644 --- a/contracts/consumer/simple-price-feed/src/contract.rs +++ b/contracts/consumer/simple-price-feed/src/contract.rs @@ -14,6 +14,16 @@ use crate::state::Config; pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); +#[cfg(not(any(test, feature = "mt")))] +pub type PriceFeedCustomMsg = cosmwasm_std::Empty; +#[cfg(any(test, feature = "mt"))] +pub type PriceFeedCustomMsg = mesh_bindings::VirtualStakeCustomMsg; + +#[cfg(not(any(test, feature = "mt")))] +pub type PriceFeedCustomQuery = cosmwasm_std::Empty; +#[cfg(any(test, feature = "mt"))] +pub type PriceFeedCustomQuery = mesh_bindings::VirtualStakeCustomQuery; + pub struct SimplePriceFeedContract<'a> { pub config: Item<'a, Config>, } @@ -21,7 +31,11 @@ pub struct SimplePriceFeedContract<'a> { #[cfg_attr(not(feature = "library"), sylvia::entry_points)] #[contract] #[sv::error(ContractError)] -#[sv::messages(price_feed_api as PriceFeedApi)] +#[sv::messages(price_feed_api as PriceFeedApi: custom(msg, query))] +// #[cfg_attr(any(test, feature = "mt"), sv::messages(price_feed_api as PriceFeedApi: custom(msg, query)))] +// #[cfg_attr(not(any(test, feature = "mt")), sv::messages(price_feed_api as PriceFeedApi))] +/// Workaround for lack of support in communication `Empty` <-> `Custom` Contracts. +#[sv::custom(query=PriceFeedCustomQuery, msg=PriceFeedCustomMsg)] impl SimplePriceFeedContract<'_> { pub const fn new() -> Self { Self { @@ -34,10 +48,10 @@ impl SimplePriceFeedContract<'_> { #[sv::msg(instantiate)] pub fn instantiate( &self, - ctx: InstantiateCtx, + ctx: InstantiateCtx, native_per_foreign: Decimal, owner: Option, - ) -> Result { + ) -> Result, ContractError> { nonpayable(&ctx.info)?; let owner = match owner { Some(owner) => ctx.deps.api.addr_validate(&owner)?, @@ -56,9 +70,9 @@ impl SimplePriceFeedContract<'_> { #[sv::msg(exec)] fn update_price( &self, - ctx: ExecCtx, + ctx: ExecCtx, native_per_foreign: Decimal, - ) -> Result { + ) -> Result, ContractError> { nonpayable(&ctx.info)?; let mut config = self.config.load(ctx.deps.storage)?; @@ -76,7 +90,7 @@ impl SimplePriceFeedContract<'_> { } #[sv::msg(query)] - fn config(&self, ctx: QueryCtx) -> Result { + fn config(&self, ctx: QueryCtx) -> Result { let config = self.config.load(ctx.deps.storage)?; Ok(ConfigResponse { owner: config.owner.into_string(), diff --git a/contracts/consumer/virtual-staking/src/contract.rs b/contracts/consumer/virtual-staking/src/contract.rs index 41f24682..42a53555 100644 --- a/contracts/consumer/virtual-staking/src/contract.rs +++ b/contracts/consumer/virtual-staking/src/contract.rs @@ -360,7 +360,6 @@ impl VirtualStakingApi for VirtualStakingContract<'_> { fn bond( &self, ctx: ExecCtx, - validator: String, amount: Coin, ) -> Result, Self::Error> { From 07fb1ca429e979047dec1fb0634b897f1d341679 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 13 May 2024 15:52:30 +0200 Subject: [PATCH 2/6] More generic, simple-price and converter pass tests --- contracts/consumer/converter/Cargo.toml | 2 + contracts/consumer/converter/src/contract.rs | 111 ++++++++++-------- contracts/consumer/converter/src/ibc.rs | 11 +- contracts/consumer/converter/src/multitest.rs | 27 +++-- .../src/multitest/virtual_staking_mock.rs | 52 +++++--- .../consumer/simple-price-feed/Cargo.toml | 2 + .../simple-price-feed/src/contract.rs | 47 +++++--- packages/apis/src/converter_api.rs | 20 ++-- packages/apis/src/price_feed_api.rs | 11 +- 9 files changed, 179 insertions(+), 104 deletions(-) diff --git a/contracts/consumer/converter/Cargo.toml b/contracts/consumer/converter/Cargo.toml index c60f0af3..a152fbeb 100644 --- a/contracts/consumer/converter/Cargo.toml +++ b/contracts/consumer/converter/Cargo.toml @@ -17,6 +17,8 @@ backtraces = ["cosmwasm-std/backtraces"] library = [] # enables generation of mt utilities mt = ["library", "sylvia/mt"] +# enable this for multi-tests where you need custom messages for compatibility with virtual staking +fake-custom = [ "mesh-simple-price-feed/fake-custom" ] [dependencies] mesh-apis = { workspace = true } diff --git a/contracts/consumer/converter/src/contract.rs b/contracts/consumer/converter/src/contract.rs index 20203484..a45887f2 100644 --- a/contracts/consumer/converter/src/contract.rs +++ b/contracts/consumer/converter/src/contract.rs @@ -24,15 +24,18 @@ pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); const REPLY_ID_INSTANTIATE: u64 = 1; -#[cfg(not(any(test, feature = "mt")))] -pub type ConverterCustomMsg = cosmwasm_std::Empty; -#[cfg(any(test, feature = "mt"))] -pub type ConverterCustomMsg = mesh_bindings::VirtualStakeCustomMsg; - -#[cfg(not(any(test, feature = "mt")))] -pub type ConverterCustomQuery = cosmwasm_std::Empty; -#[cfg(any(test, feature = "mt"))] -pub type ConverterCustomQuery = mesh_bindings::VirtualStakeCustomQuery; +#[cfg(not(feature = "fake-custom"))] +pub mod custom { + pub type ConverterMsg = cosmwasm_std::Empty; + pub type ConverterQuery = cosmwasm_std::Empty; + pub type Response = cosmwasm_std::Response; +} +#[cfg(feature = "fake-custom")] +pub mod custom { + pub type ConverterMsg = mesh_bindings::VirtualStakeCustomMsg; + pub type ConverterQuery = mesh_bindings::VirtualStakeCustomQuery; + pub type Response = cosmwasm_std::Response; +} pub struct ConverterContract<'a> { pub config: Item<'a, Config>, @@ -44,7 +47,7 @@ pub struct ConverterContract<'a> { #[sv::error(ContractError)] #[sv::messages(converter_api as ConverterApi)] /// Workaround for lack of support in communication `Empty` <-> `Custom` Contracts. -#[sv::custom(query=ConverterCustomQuery, msg=ConverterCustomMsg)] +#[sv::custom(query=custom::ConverterQuery, msg=custom::ConverterMsg)] impl ConverterContract<'_> { pub const fn new() -> Self { Self { @@ -63,13 +66,13 @@ impl ConverterContract<'_> { #[sv::msg(instantiate)] pub fn instantiate( &self, - ctx: InstantiateCtx, + ctx: InstantiateCtx, price_feed: String, discount: Decimal, remote_denom: String, virtual_staking_code_id: u64, admin: Option, - ) -> Result { + ) -> Result { nonpayable(&ctx.info)?; // validate args if discount >= Decimal::one() { @@ -108,9 +111,9 @@ impl ConverterContract<'_> { #[sv::msg(reply)] fn reply( &self, - ctx: ReplyCtx, + ctx: ReplyCtx, reply: Reply, - ) -> Result { + ) -> Result { match reply.id { REPLY_ID_INSTANTIATE => self.reply_init_callback(ctx.deps, reply.result.unwrap()), _ => Err(ContractError::InvalidReplyId(reply.id)), @@ -120,9 +123,9 @@ impl ConverterContract<'_> { /// Store virtual staking address fn reply_init_callback( &self, - deps: DepsMut, + deps: DepsMut, reply: SubMsgResponse, - ) -> Result { + ) -> Result { let init_data = parse_instantiate_response_data(&reply.data.unwrap())?; let virtual_staking = Addr::unchecked(init_data.contract_address); self.virtual_stake.save(deps.storage, &virtual_staking)?; @@ -134,10 +137,10 @@ impl ConverterContract<'_> { #[sv::msg(exec)] fn test_stake( &self, - ctx: ExecCtx, + ctx: ExecCtx, validator: String, stake: Coin, - ) -> Result { + ) -> Result { #[cfg(any(test, feature = "mt"))] { // This can only ever be called in tests @@ -155,10 +158,10 @@ impl ConverterContract<'_> { #[sv::msg(exec)] fn test_unstake( &self, - ctx: ExecCtx, + ctx: ExecCtx, validator: String, unstake: Coin, - ) -> Result { + ) -> Result { #[cfg(any(test, feature = "mt"))] { // This can only ever be called in tests @@ -176,10 +179,10 @@ impl ConverterContract<'_> { #[sv::msg(exec)] fn test_burn( &self, - ctx: ExecCtx, + ctx: ExecCtx, validators: Vec, burn: Coin, - ) -> Result { + ) -> Result { #[cfg(any(test, feature = "mt"))] { // This can only ever be called in tests @@ -193,7 +196,10 @@ impl ConverterContract<'_> { } #[sv::msg(query)] - fn config(&self, ctx: QueryCtx) -> Result { + fn config( + &self, + ctx: QueryCtx, + ) -> Result { let config = self.config.load(ctx.deps.storage)?; let virtual_staking = self.virtual_stake.load(ctx.deps.storage)?.into_string(); Ok(ConfigResponse { @@ -207,10 +213,10 @@ impl ConverterContract<'_> { /// It is pulled out into a method, so it can also be called by test_stake for testing pub(crate) fn stake( &self, - deps: DepsMut, + deps: DepsMut, validator: String, stake: Coin, - ) -> Result { + ) -> Result { let amount = self.normalize_price(deps.as_ref(), stake)?; let event = Event::new("mesh-bond") @@ -231,10 +237,10 @@ impl ConverterContract<'_> { /// It is pulled out into a method, so it can also be called by test_unstake for testing pub(crate) fn unstake( &self, - deps: DepsMut, + deps: DepsMut, validator: String, unstake: Coin, - ) -> Result { + ) -> Result { let amount = self.normalize_price(deps.as_ref(), unstake)?; let event = Event::new("mesh-unbond") @@ -255,10 +261,10 @@ impl ConverterContract<'_> { /// It is pulled out into a method, so it can also be called by test_burn for testing pub(crate) fn burn( &self, - deps: DepsMut, + deps: DepsMut, validators: &[String], burn: Coin, - ) -> Result { + ) -> Result { let amount = self.normalize_price(deps.as_ref(), burn)?; let event = Event::new("mesh-burn") @@ -280,7 +286,7 @@ impl ConverterContract<'_> { fn normalize_price( &self, - deps: Deps, + deps: Deps, amount: Coin, ) -> Result { let config = self.config.load(deps.storage)?; @@ -298,10 +304,13 @@ impl ConverterContract<'_> { // also see https://github.com/CosmWasm/sylvia/issues/181 to just store Remote in state use price_feed_api::sv::Querier; use sylvia::types::Remote; - // NOTE: Jan, this feels hacky... I'm not sure if this is the right way to do it - // Also, I am sticking in a random error type here, not what I will get (which is unknown) - let remote = - Remote::<&dyn price_feed_api::PriceFeedApi>::new(config.price_feed); + let remote = Remote::< + dyn price_feed_api::PriceFeedApi< + Error = StdError, + ExecC = custom::ConverterMsg, + QueryC = custom::ConverterQuery, + >, + >::new(config.price_feed); let price = remote.querier(&deps.querier).price()?.native_per_foreign; let converted = (amount.amount * price) * config.price_adjustment; @@ -313,7 +322,7 @@ impl ConverterContract<'_> { fn invert_price( &self, - deps: Deps, + deps: Deps, amount: Coin, ) -> Result { let config = self.config.load(deps.storage)?; @@ -330,10 +339,14 @@ impl ConverterContract<'_> { // also see https://github.com/CosmWasm/sylvia/issues/181 to just store Remote in state use price_feed_api::sv::Querier; use sylvia::types::Remote; - // NOTE: Jan, this feels hacky... I'm not sure if this is the right way to do it - // Also, I am sticking in a random error type here, not what I will get (which is unknown) - let remote = - Remote::<&dyn price_feed_api::PriceFeedApi>::new(config.price_feed); + // Note: it doesn't seem to matter which error type goes here... + let remote = Remote::< + dyn price_feed_api::PriceFeedApi< + Error = StdError, + ExecC = custom::ConverterMsg, + QueryC = custom::ConverterQuery, + >, + >::new(config.price_feed); let price = remote.querier(&deps.querier).price()?.native_per_foreign; let converted = (amount.amount * price.inv().ok_or(ContractError::InvalidPrice {})?) * config @@ -349,10 +362,10 @@ impl ConverterContract<'_> { pub(crate) fn transfer_rewards( &self, - deps: Deps, + deps: Deps, recipient: String, rewards: Coin, - ) -> Result { + ) -> Result, ContractError> { // ensure the address is proper let recipient = deps.api.addr_validate(&recipient)?; @@ -377,7 +390,7 @@ impl ConverterContract<'_> { fn ensure_authorized( &self, - deps: &DepsMut, + deps: &DepsMut, info: &MessageInfo, ) -> Result<(), ContractError> { let virtual_stake = self.virtual_stake.load(deps.storage)?; @@ -389,14 +402,16 @@ impl ConverterContract<'_> { impl ConverterApi for ConverterContract<'_> { type Error = ContractError; + type ExecC = custom::ConverterMsg; + type QueryC = custom::ConverterQuery; /// Rewards tokens (in native staking denom) are sent alongside the message, and should be distributed to all /// stakers who staked on this validator. This is tracked on the provider, so we send an IBC packet there. fn distribute_reward( &self, - mut ctx: ExecCtx, + mut ctx: ExecCtx, validator: String, - ) -> Result { + ) -> Result { self.ensure_authorized(&ctx.deps, &ctx.info)?; let config = self.config.load(ctx.deps.storage)?; @@ -419,9 +434,9 @@ impl ConverterApi for ConverterContract<'_> { /// in the native staking denom. fn distribute_rewards( &self, - mut ctx: ExecCtx, + mut ctx: ExecCtx, payments: Vec, - ) -> Result { + ) -> Result { self.ensure_authorized(&ctx.deps, &ctx.info)?; let config = self.config.load(ctx.deps.storage)?; @@ -459,7 +474,7 @@ impl ConverterApi for ConverterContract<'_> { #[allow(clippy::too_many_arguments)] fn valset_update( &self, - ctx: ExecCtx, + ctx: ExecCtx, additions: Vec, removals: Vec, updated: Vec, @@ -467,7 +482,7 @@ impl ConverterApi for ConverterContract<'_> { unjailed: Vec, tombstoned: Vec, mut slashed: Vec, - ) -> Result { + ) -> Result { self.ensure_authorized(&ctx.deps, &ctx.info)?; // Send over IBC to the Consumer diff --git a/contracts/consumer/converter/src/ibc.rs b/contracts/consumer/converter/src/ibc.rs index d711a2a5..c54a19dd 100644 --- a/contracts/consumer/converter/src/ibc.rs +++ b/contracts/consumer/converter/src/ibc.rs @@ -16,7 +16,10 @@ use mesh_apis::ibc::{ }; use sylvia::types::ExecCtx; -use crate::{contract::ConverterContract, error::ContractError}; +use crate::{ + contract::{custom, ConverterContract}, + error::ContractError, +}; /// This is the maximum version of the Mesh Security protocol that we support const SUPPORTED_IBC_PROTOCOL_VERSION: &str = "0.11.0"; @@ -184,10 +187,10 @@ pub fn ibc_channel_close( /// We cannot return any meaningful response value as we do not know the response value /// of execution. We just return ok if we dispatched, error if we failed to dispatch pub fn ibc_packet_receive( - deps: DepsMut, + deps: DepsMut, _env: Env, msg: IbcPacketReceiveMsg, -) -> Result { +) -> Result, ContractError> { let packet: ProviderPacket = from_json(msg.packet.data)?; let contract = ConverterContract::new(); let res = match packet { @@ -280,7 +283,7 @@ pub fn ibc_packet_timeout( } pub(crate) fn make_ibc_packet( - ctx: &mut ExecCtx, + ctx: &mut ExecCtx, packet: ConsumerPacket, ) -> Result { let channel = IBC_CHANNEL.load(ctx.deps.storage)?; diff --git a/contracts/consumer/converter/src/multitest.rs b/contracts/consumer/converter/src/multitest.rs index 883b38c9..8f20b7ad 100644 --- a/contracts/consumer/converter/src/multitest.rs +++ b/contracts/consumer/converter/src/multitest.rs @@ -1,7 +1,8 @@ mod virtual_staking_mock; use cosmwasm_std::{coin, coins, Addr, Decimal, StdError, Uint128, Validator}; -use cw_multi_test::App as MtApp; +use cw_multi_test::no_init; +use cw_multi_test::AppBuilder; use mesh_apis::converter_api::sv::mt::ConverterApiProxy; use mesh_apis::converter_api::RewardInfo; use mesh_simple_price_feed::contract::sv::mt::CodeId as PriceFeedCodeId; @@ -12,13 +13,15 @@ use virtual_staking_mock::VirtualStakingMock; use crate::contract::sv::mt::CodeId as ConverterCodeId; use crate::contract::sv::mt::ConverterContractProxy; -use crate::contract::ConverterContract; +use crate::contract::{custom, ConverterContract}; use crate::error::ContractError; use crate::error::ContractError::Unauthorized; use crate::multitest::virtual_staking_mock::sv::mt::VirtualStakingMockProxy; const JUNO: &str = "ujuno"; +pub type MtApp = cw_multi_test::BasicApp; + struct SetupArgs<'a> { owner: &'a str, admin: &'a str, @@ -32,6 +35,10 @@ struct SetupResponse<'a> { virtual_staking: Proxy<'a, MtApp, VirtualStakingMock<'a>>, } +fn new_app() -> App { + App::new(AppBuilder::new_custom().build(no_init)) +} + fn setup<'a>(app: &'a App, args: SetupArgs<'a>) -> SetupResponse<'a> { let SetupArgs { owner, @@ -80,7 +87,7 @@ fn setup<'a>(app: &'a App, args: SetupArgs<'a>) -> SetupResponse<'a> { #[test] fn instantiation() { - let app = App::default(); + let app = new_app(); let owner = "sunny"; // Owner of the staking contract (i. e. the vault contract) let admin = "theman"; @@ -122,7 +129,7 @@ fn instantiation() { #[test] fn ibc_stake_and_unstake() { - let app = App::default(); + let app = new_app(); let owner = "sunny"; // Owner of the staking contract (i. e. the vault contract) let admin = "theman"; @@ -208,7 +215,7 @@ fn ibc_stake_and_unstake() { #[test] fn ibc_stake_and_burn() { - let app = App::default(); + let app = new_app(); let owner = "sunny"; // Owner of the staking contract (i. e. the vault contract) let admin = "theman"; @@ -294,7 +301,7 @@ fn ibc_stake_and_burn() { #[test] fn valset_update_works() { - let app = App::default(); + let app = new_app(); let owner = "sunny"; // Owner of the staking contract (i. e. the vault contract) let admin = "theman"; @@ -365,7 +372,7 @@ fn valset_update_works() { #[test] fn unauthorized() { - let app = App::default(); + let app = new_app(); let SetupResponse { converter, .. } = setup( &app, @@ -415,7 +422,7 @@ fn distribute_rewards_invalid_amount_is_rejected() { let discount = Decimal::percent(10); // 1 OSMO worth of JUNO should give 0.9 OSMO of stake let native_per_foreign = Decimal::percent(40); // 1 JUNO is worth 0.4 OSMO - let app = App::default(); + let app = new_app(); let SetupResponse { price_feed: _, @@ -490,14 +497,14 @@ fn distribute_rewards_invalid_amount_is_rejected() { } #[test] -#[ignore = "unsupported by Sylvia"] +#[ignore = "IBC unsupported by Sylvia"] fn distribute_rewards_valid_amount() { let owner = "sunny"; let admin = "theman"; let discount = Decimal::percent(10); // 1 OSMO worth of JUNO should give 0.9 OSMO of stake let native_per_foreign = Decimal::percent(40); // 1 JUNO is worth 0.4 OSMO - let app = App::default(); + let app = new_app(); let SetupResponse { price_feed: _, diff --git a/contracts/consumer/converter/src/multitest/virtual_staking_mock.rs b/contracts/consumer/converter/src/multitest/virtual_staking_mock.rs index ac313247..3faaedf8 100644 --- a/contracts/consumer/converter/src/multitest/virtual_staking_mock.rs +++ b/contracts/consumer/converter/src/multitest/virtual_staking_mock.rs @@ -7,6 +7,8 @@ use mesh_apis::virtual_staking_api::{self, ValidatorSlash, VirtualStakingApi}; use sylvia::contract; use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx, SudoCtx}; +use crate::contract::custom; + #[cw_serde] pub struct Config { /// The denom we accept for staking @@ -43,6 +45,7 @@ pub struct VirtualStakingMock<'a> { #[contract] #[sv::error(ContractError)] #[sv::messages(virtual_staking_api as VirtualStakingApi)] +#[sv::custom(query=custom::ConverterQuery, msg=custom::ConverterMsg)] impl VirtualStakingMock<'_> { pub const fn new() -> Self { Self { @@ -52,7 +55,10 @@ impl VirtualStakingMock<'_> { } #[sv::msg(instantiate)] - pub fn instantiate(&self, ctx: InstantiateCtx) -> Result { + pub fn instantiate( + &self, + ctx: InstantiateCtx, + ) -> Result { nonpayable(&ctx.info)?; let denom = ctx.deps.querier.query_bonded_denom()?; let config = Config { @@ -64,7 +70,10 @@ impl VirtualStakingMock<'_> { } #[sv::msg(query)] - fn config(&self, ctx: QueryCtx) -> Result { + fn config( + &self, + ctx: QueryCtx, + ) -> Result { let cfg = self.config.load(ctx.deps.storage)?; let denom = cfg.denom; let converter = cfg.converter.into_string(); @@ -72,7 +81,11 @@ impl VirtualStakingMock<'_> { } #[sv::msg(query)] - fn stake(&self, ctx: QueryCtx, validator: String) -> Result { + fn stake( + &self, + ctx: QueryCtx, + validator: String, + ) -> Result { let stake = self .stake .may_load(ctx.deps.storage, &validator)? @@ -81,7 +94,10 @@ impl VirtualStakingMock<'_> { } #[sv::msg(query)] - fn all_stake(&self, ctx: QueryCtx) -> Result { + fn all_stake( + &self, + ctx: QueryCtx, + ) -> Result { let stakes = self .stake .range(ctx.deps.storage, None, None, cosmwasm_std::Order::Ascending) @@ -108,13 +124,18 @@ pub struct ConfigResponse { impl VirtualStakingApi for VirtualStakingMock<'_> { type Error = ContractError; - type ExecC = cosmwasm_std::Empty; - type QueryC = cosmwasm_std::Empty; + type ExecC = custom::ConverterMsg; + type QueryC = custom::ConverterQuery; /// Requests to bond tokens to a validator. This will be actually handled at the next epoch. /// If the virtual staking module is over the max cap, it will trigger a rebalance. /// If the max cap is 0, then this will immediately return an error. - fn bond(&self, ctx: ExecCtx, validator: String, amount: Coin) -> Result { + fn bond( + &self, + ctx: ExecCtx, + validator: String, + amount: Coin, + ) -> Result, Self::Error> { nonpayable(&ctx.info)?; let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(ctx.info.sender, cfg.converter, ContractError::Unauthorized); // only the converter can call this @@ -138,10 +159,10 @@ impl VirtualStakingApi for VirtualStakingMock<'_> { /// If the virtual staking contract doesn't have at least amount tokens staked to the given validator, this will return an error. fn unbond( &self, - ctx: ExecCtx, + ctx: ExecCtx, validator: String, amount: Coin, - ) -> Result { + ) -> Result, Self::Error> { nonpayable(&ctx.info)?; let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(ctx.info.sender, cfg.converter, ContractError::Unauthorized); // only the converter can call this @@ -165,10 +186,10 @@ impl VirtualStakingApi for VirtualStakingMock<'_> { /// If the virtual staking contract doesn't have at least amount tokens staked over the given validators, this will return an error. fn burn( &self, - ctx: ExecCtx, + ctx: ExecCtx, validators: Vec, amount: Coin, - ) -> Result { + ) -> Result, Self::Error> { nonpayable(&ctx.info)?; let cfg = self.config.load(ctx.deps.storage)?; // only the converter can call this @@ -225,7 +246,10 @@ impl VirtualStakingApi for VirtualStakingMock<'_> { /// as to perform a rebalance if needed (over the max cap). /// /// It should also withdraw all pending rewards here, and send them to the converter contract. - fn handle_epoch(&self, _ctx: SudoCtx) -> Result { + fn handle_epoch( + &self, + _ctx: SudoCtx, + ) -> Result, Self::Error> { unimplemented!() } @@ -238,7 +262,7 @@ impl VirtualStakingApi for VirtualStakingMock<'_> { /// - Permanent removal (i.e. tombstoning) of a validator from the active set. Implies slashing fn handle_valset_update( &self, - _ctx: SudoCtx, + _ctx: SudoCtx, _additions: Option>, _removals: Option>, _updated: Option>, @@ -246,7 +270,7 @@ impl VirtualStakingApi for VirtualStakingMock<'_> { _unjailed: Option>, _tombstoned: Option>, _slashed: Option>, - ) -> Result { + ) -> Result, Self::Error> { unimplemented!() } } diff --git a/contracts/consumer/simple-price-feed/Cargo.toml b/contracts/consumer/simple-price-feed/Cargo.toml index 3122ac0d..b32fe2e4 100644 --- a/contracts/consumer/simple-price-feed/Cargo.toml +++ b/contracts/consumer/simple-price-feed/Cargo.toml @@ -17,6 +17,8 @@ backtraces = ["cosmwasm-std/backtraces"] library = [] # enables generation of mt utilities mt = ["library", "sylvia/mt"] +# enable this for multi-tests where you need custom messages for compatibility with virtual staking +fake-custom = [] [dependencies] mesh-apis = { workspace = true } diff --git a/contracts/consumer/simple-price-feed/src/contract.rs b/contracts/consumer/simple-price-feed/src/contract.rs index 06aedf11..b3a5e2d8 100644 --- a/contracts/consumer/simple-price-feed/src/contract.rs +++ b/contracts/consumer/simple-price-feed/src/contract.rs @@ -14,15 +14,18 @@ use crate::state::Config; pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); -#[cfg(not(any(test, feature = "mt")))] -pub type PriceFeedCustomMsg = cosmwasm_std::Empty; -#[cfg(any(test, feature = "mt"))] -pub type PriceFeedCustomMsg = mesh_bindings::VirtualStakeCustomMsg; - -#[cfg(not(any(test, feature = "mt")))] -pub type PriceFeedCustomQuery = cosmwasm_std::Empty; -#[cfg(any(test, feature = "mt"))] -pub type PriceFeedCustomQuery = mesh_bindings::VirtualStakeCustomQuery; +#[cfg(not(feature = "fake-custom"))] +pub mod custom { + pub type PriceFeedMsg = cosmwasm_std::Empty; + pub type PriceFeedQuery = cosmwasm_std::Empty; + pub type Response = cosmwasm_std::Response; +} +#[cfg(feature = "fake-custom")] +pub mod custom { + pub type PriceFeedMsg = mesh_bindings::VirtualStakeCustomMsg; + pub type PriceFeedQuery = mesh_bindings::VirtualStakeCustomQuery; + pub type Response = cosmwasm_std::Response; +} pub struct SimplePriceFeedContract<'a> { pub config: Item<'a, Config>, @@ -31,11 +34,11 @@ pub struct SimplePriceFeedContract<'a> { #[cfg_attr(not(feature = "library"), sylvia::entry_points)] #[contract] #[sv::error(ContractError)] -#[sv::messages(price_feed_api as PriceFeedApi: custom(msg, query))] +#[sv::messages(price_feed_api as PriceFeedApi)] // #[cfg_attr(any(test, feature = "mt"), sv::messages(price_feed_api as PriceFeedApi: custom(msg, query)))] // #[cfg_attr(not(any(test, feature = "mt")), sv::messages(price_feed_api as PriceFeedApi))] /// Workaround for lack of support in communication `Empty` <-> `Custom` Contracts. -#[sv::custom(query=PriceFeedCustomQuery, msg=PriceFeedCustomMsg)] +#[sv::custom(query=custom::PriceFeedQuery, msg=custom::PriceFeedMsg)] impl SimplePriceFeedContract<'_> { pub const fn new() -> Self { Self { @@ -48,10 +51,10 @@ impl SimplePriceFeedContract<'_> { #[sv::msg(instantiate)] pub fn instantiate( &self, - ctx: InstantiateCtx, + ctx: InstantiateCtx, native_per_foreign: Decimal, owner: Option, - ) -> Result, ContractError> { + ) -> Result { nonpayable(&ctx.info)?; let owner = match owner { Some(owner) => ctx.deps.api.addr_validate(&owner)?, @@ -70,9 +73,9 @@ impl SimplePriceFeedContract<'_> { #[sv::msg(exec)] fn update_price( &self, - ctx: ExecCtx, + ctx: ExecCtx, native_per_foreign: Decimal, - ) -> Result, ContractError> { + ) -> Result { nonpayable(&ctx.info)?; let mut config = self.config.load(ctx.deps.storage)?; @@ -90,7 +93,10 @@ impl SimplePriceFeedContract<'_> { } #[sv::msg(query)] - fn config(&self, ctx: QueryCtx) -> Result { + fn config( + &self, + ctx: QueryCtx, + ) -> Result { let config = self.config.load(ctx.deps.storage)?; Ok(ConfigResponse { owner: config.owner.into_string(), @@ -101,10 +107,12 @@ impl SimplePriceFeedContract<'_> { impl PriceFeedApi for SimplePriceFeedContract<'_> { type Error = ContractError; + type ExecC = custom::PriceFeedMsg; + type QueryC = custom::PriceFeedQuery; /// Return the price of the foreign token. That is, how many native tokens /// are needed to buy one foreign token. - fn price(&self, ctx: QueryCtx) -> Result { + fn price(&self, ctx: QueryCtx) -> Result { let config = self.config.load(ctx.deps.storage)?; Ok(PriceResponse { native_per_foreign: config.native_per_foreign, @@ -112,7 +120,10 @@ impl PriceFeedApi for SimplePriceFeedContract<'_> { } /// Nothing needs to be done on the epoch - fn handle_epoch(&self, _ctx: SudoCtx) -> Result { + fn handle_epoch( + &self, + _ctx: SudoCtx, + ) -> Result, Self::Error> { Ok(Response::new()) } } diff --git a/packages/apis/src/converter_api.rs b/packages/apis/src/converter_api.rs index 2a26474d..22fb1dfe 100644 --- a/packages/apis/src/converter_api.rs +++ b/packages/apis/src/converter_api.rs @@ -1,5 +1,5 @@ use cosmwasm_schema::cw_serde; -use cosmwasm_std::{Coin, Response, StdError, Uint128, Validator}; +use cosmwasm_std::{Coin, CustomMsg, CustomQuery, Response, StdError, Uint128, Validator}; use sylvia::types::ExecCtx; use sylvia::{interface, schemars}; @@ -9,11 +9,17 @@ use sylvia::{interface, schemars}; #[interface] pub trait ConverterApi { type Error: From; + type ExecC: CustomMsg; + type QueryC: CustomQuery; /// Rewards tokens (in native staking denom) are sent alongside the message, and should be distributed to all /// stakers who staked on this validator. #[sv::msg(exec)] - fn distribute_reward(&self, ctx: ExecCtx, validator: String) -> Result; + fn distribute_reward( + &self, + ctx: ExecCtx, + validator: String, + ) -> Result, Self::Error>; /// This is a batch for of distribute_reward, including the payment for multiple validators. /// This is more efficient than calling distribute_reward multiple times, but also more complex. @@ -23,18 +29,18 @@ pub trait ConverterApi { #[sv::msg(exec)] fn distribute_rewards( &self, - ctx: ExecCtx, + ctx: ExecCtx, payments: Vec, - ) -> Result; + ) -> Result, Self::Error>; /// Valset updates. /// /// TODO: pubkeys need to be part of the Validator struct (requires CosmWasm support). - #[allow(clippy::too_many_arguments)] #[sv::msg(exec)] + #[allow(clippy::too_many_arguments)] fn valset_update( &self, - ctx: ExecCtx, + ctx: ExecCtx, additions: Vec, removals: Vec, updated: Vec, @@ -42,7 +48,7 @@ pub trait ConverterApi { unjailed: Vec, tombstoned: Vec, slashed: Vec, - ) -> Result; + ) -> Result, Self::Error>; } #[cw_serde] diff --git a/packages/apis/src/price_feed_api.rs b/packages/apis/src/price_feed_api.rs index 49723bf1..92679320 100644 --- a/packages/apis/src/price_feed_api.rs +++ b/packages/apis/src/price_feed_api.rs @@ -1,5 +1,5 @@ use cosmwasm_schema::cw_serde; -use cosmwasm_std::{Decimal, Response, StdError}; +use cosmwasm_std::{CustomMsg, CustomQuery, Decimal, Response, StdError}; use sylvia::types::{QueryCtx, SudoCtx}; use sylvia::{interface, schemars}; @@ -12,14 +12,19 @@ use sylvia::{interface, schemars}; #[interface] pub trait PriceFeedApi { type Error: From; + type ExecC: CustomMsg; + type QueryC: CustomQuery; /// Return the price of the foreign token. That is, how many native tokens /// are needed to buy one foreign token. #[sv::msg(query)] - fn price(&self, ctx: QueryCtx) -> Result; + fn price(&self, ctx: QueryCtx) -> Result; #[sv::msg(sudo)] - fn handle_epoch(&self, ctx: SudoCtx) -> Result; + fn handle_epoch( + &self, + ctx: SudoCtx, + ) -> Result, Self::Error>; } #[cw_serde] From 1009590f5895b761d42bc803ddafea2ac5e2dea2 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 13 May 2024 16:18:56 +0200 Subject: [PATCH 3/6] virtual-staking tests work --- contracts/consumer/converter/src/multitest.rs | 3 +-- .../consumer/remote-price-feed/src/contract.rs | 3 +++ contracts/consumer/virtual-staking/Cargo.toml | 4 ++-- .../consumer/virtual-staking/src/multitest.rs | 17 ++++++----------- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/contracts/consumer/converter/src/multitest.rs b/contracts/consumer/converter/src/multitest.rs index 8f20b7ad..22169d99 100644 --- a/contracts/consumer/converter/src/multitest.rs +++ b/contracts/consumer/converter/src/multitest.rs @@ -1,8 +1,7 @@ mod virtual_staking_mock; use cosmwasm_std::{coin, coins, Addr, Decimal, StdError, Uint128, Validator}; -use cw_multi_test::no_init; -use cw_multi_test::AppBuilder; +use cw_multi_test::{no_init, AppBuilder}; use mesh_apis::converter_api::sv::mt::ConverterApiProxy; use mesh_apis::converter_api::RewardInfo; use mesh_simple_price_feed::contract::sv::mt::CodeId as PriceFeedCodeId; diff --git a/contracts/consumer/remote-price-feed/src/contract.rs b/contracts/consumer/remote-price-feed/src/contract.rs index 9640a096..745b1ea4 100644 --- a/contracts/consumer/remote-price-feed/src/contract.rs +++ b/contracts/consumer/remote-price-feed/src/contract.rs @@ -84,6 +84,9 @@ impl RemotePriceFeedContract { impl PriceFeedApi for RemotePriceFeedContract { type Error = ContractError; + // FIXME: make these under a feature flag if we need virtual-staking multitest compatibility + type ExecC = cosmwasm_std::Empty; + type QueryC = cosmwasm_std::Empty; /// Return the price of the foreign token. That is, how many native tokens /// are needed to buy one foreign token. diff --git a/contracts/consumer/virtual-staking/Cargo.toml b/contracts/consumer/virtual-staking/Cargo.toml index 47e84cad..2952a33b 100644 --- a/contracts/consumer/virtual-staking/Cargo.toml +++ b/contracts/consumer/virtual-staking/Cargo.toml @@ -35,8 +35,8 @@ serde = { workspace = true } thiserror = { workspace = true } [dev-dependencies] -mesh-simple-price-feed = { workspace = true, features = ["mt"] } -mesh-converter = { workspace = true, features = ["mt"] } +mesh-simple-price-feed = { workspace = true, features = ["mt", "fake-custom"] } +mesh-converter = { workspace = true, features = ["mt", "fake-custom"] } cw-multi-test = { workspace = true } test-case = { workspace = true } derivative = { workspace = true } diff --git a/contracts/consumer/virtual-staking/src/multitest.rs b/contracts/consumer/virtual-staking/src/multitest.rs index 4baeedb0..54be3760 100644 --- a/contracts/consumer/virtual-staking/src/multitest.rs +++ b/contracts/consumer/virtual-staking/src/multitest.rs @@ -1,12 +1,12 @@ use cosmwasm_std::{Addr, Decimal, Validator}; use cw_multi_test::no_init; +use mesh_apis::virtual_staking_api::sv::mt::VirtualStakingApiProxy; use sylvia::multitest::Proxy; use mesh_converter::contract::sv::mt::ConverterContractProxy; use crate::contract; use crate::contract::sv::mt::VirtualStakingContractProxy; -use crate::contract::sv::ContractSudoMsg; const JUNO: &str = "ujuno"; @@ -120,7 +120,8 @@ fn instantiation() { } #[test] -#[ignore] // FIXME: Enable / finish this test once custom query support is added to sylvia +// FIXME: Enable / finish this test once custom query support is added to sylvia +#[ignore = "IBC Messages not supported yet"] fn valset_update_sudo() { let app = new_app(); @@ -160,8 +161,8 @@ fn valset_update_sudo() { ]; let rems = vec!["cosmosval2".to_string()]; let tombs = vec!["cosmosval3".to_string()]; - // See this as an example how we can make working directly with these genertaed enums nicer - let inner = mesh_apis::virtual_staking_api::sv::VirtualStakingApiSudoMsg::handle_valset_update( + + let res = virtual_staking.handle_valset_update( Some(adds), Some(rems), None, @@ -170,12 +171,6 @@ fn valset_update_sudo() { Some(tombs), None, ); - let msg = ContractSudoMsg::VirtualStakingApi(inner); - - let res = app - .app_mut() - .wasm_sudo(virtual_staking.contract_addr, &msg) - .unwrap(); - println!("res: {:?}", res); + res.unwrap(); } From 493826569b74db2765384169a4659558f9e46f85 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 13 May 2024 16:29:43 +0200 Subject: [PATCH 4/6] Test pass, clippy happy :) --- contracts/provider/native-staking-proxy/src/multitest.rs | 4 +--- contracts/provider/vault/src/multitest.rs | 1 + packages/apis/src/converter_api.rs | 4 +++- packages/apis/src/virtual_staking_api.rs | 5 ++++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/contracts/provider/native-staking-proxy/src/multitest.rs b/contracts/provider/native-staking-proxy/src/multitest.rs index 410f067c..02321a7d 100644 --- a/contracts/provider/native-staking-proxy/src/multitest.rs +++ b/contracts/provider/native-staking-proxy/src/multitest.rs @@ -293,7 +293,6 @@ fn unstaking() { ); } - #[test] fn burning() { let owner = "vault_admin"; @@ -336,7 +335,6 @@ fn burning() { coin(0, OSMO) ); - // Advance time until the unbonding period is over process_staking_unbondings(&app); @@ -551,8 +549,8 @@ fn process_staking_unbondings(app: &App) { // This is deprecated as unneeded, but tests fail if it isn't here. What's up??? app.app_mut() .sudo(cw_multi_test::SudoMsg::Staking( + #[allow(deprecated)] cw_multi_test::StakingSudo::ProcessQueue {}, )) .unwrap(); } - diff --git a/contracts/provider/vault/src/multitest.rs b/contracts/provider/vault/src/multitest.rs index 7b448b89..558bebf4 100644 --- a/contracts/provider/vault/src/multitest.rs +++ b/contracts/provider/vault/src/multitest.rs @@ -280,6 +280,7 @@ fn process_staking_unbondings(app: &App) { // This is deprecated as unneeded, but tests fail if it isn't here. What's up??? app.app_mut() .sudo(cw_multi_test::SudoMsg::Staking( + #[allow(deprecated)] cw_multi_test::StakingSudo::ProcessQueue {}, )) .unwrap(); diff --git a/packages/apis/src/converter_api.rs b/packages/apis/src/converter_api.rs index 22fb1dfe..8fc26412 100644 --- a/packages/apis/src/converter_api.rs +++ b/packages/apis/src/converter_api.rs @@ -1,3 +1,6 @@ +// only for valset_update but doesn't work for the autogenerated code +#![allow(clippy::too_many_arguments)] + use cosmwasm_schema::cw_serde; use cosmwasm_std::{Coin, CustomMsg, CustomQuery, Response, StdError, Uint128, Validator}; use sylvia::types::ExecCtx; @@ -37,7 +40,6 @@ pub trait ConverterApi { /// /// TODO: pubkeys need to be part of the Validator struct (requires CosmWasm support). #[sv::msg(exec)] - #[allow(clippy::too_many_arguments)] fn valset_update( &self, ctx: ExecCtx, diff --git a/packages/apis/src/virtual_staking_api.rs b/packages/apis/src/virtual_staking_api.rs index d820a668..d05013a2 100644 --- a/packages/apis/src/virtual_staking_api.rs +++ b/packages/apis/src/virtual_staking_api.rs @@ -1,3 +1,7 @@ +// only for handle_valset_update but doesn't work for the autogenerated code +#![allow(clippy::too_many_arguments)] + + use cosmwasm_schema::cw_serde; use cosmwasm_std::{Coin, Response, StdError, Uint128, Validator}; use sylvia::cw_std::{CustomMsg, CustomQuery}; @@ -66,7 +70,6 @@ pub trait VirtualStakingApi { /// - Temporary removal of a validator from the active set due to jailing. Implies slashing. /// - Addition of an existing validator to the active validator set. /// - Permanent removal (i.e. tombstoning) of a validator from the active set. Implies slashing - #[allow(clippy::too_many_arguments)] #[sv::msg(sudo)] fn handle_valset_update( &self, From 3092f958eefa4f6651b9e6771cdab917ac0558e1 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 13 May 2024 16:39:50 +0200 Subject: [PATCH 5/6] Wasm compilation proper again --- Cargo.lock | 6 ++++-- Cargo.toml | 5 ++--- contracts/consumer/converter/src/contract.rs | 4 ++-- packages/apis/src/virtual_staking_api.rs | 1 - scripts/optimizer.sh | 4 +++- 5 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7c98ea8f..0df67f14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1190,7 +1190,8 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" [[package]] name = "sylvia" version = "0.10.1" -source = "git+https://github.com/CosmWasm/sylvia?branch=jawoznia/cosmwasm1/map_err_on_into_response#a5d3546eb287e3e7f003251c529ff3401784e1bb" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e64c8e892525ec035e5bdbeceec19309b1534734da7ff7bc69e9f639b077c497" dependencies = [ "anyhow", "cosmwasm-schema", @@ -1208,7 +1209,8 @@ dependencies = [ [[package]] name = "sylvia-derive" version = "0.10.1" -source = "git+https://github.com/CosmWasm/sylvia?branch=jawoznia/cosmwasm1/map_err_on_into_response#a5d3546eb287e3e7f003251c529ff3401784e1bb" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae5e0f41752efba2c6895514fa4caae742f69a2a73b4790bb6e365400c563bc1" dependencies = [ "convert_case", "itertools 0.12.1", diff --git a/Cargo.toml b/Cargo.toml index e5384b60..3937a620 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,9 +29,8 @@ mesh-converter = { path = "./contracts/consumer/converter" } mesh-simple-price-feed = { path = "./contracts/consumer/simple-price-feed" } mesh-virtual-staking = { path = "./contracts/consumer/virtual-staking" } -sylvia = { git = "https://github.com/CosmWasm/sylvia", branch = "jawoznia/cosmwasm1/map_err_on_into_response", features = [ - "mt", -] } +sylvia = "0.10.1" + cosmwasm-schema = "1.5.4" cosmwasm-std = { version = "1.5.4", features = ["ibc3", "cosmwasm_1_3"] } cw-storage-plus = "1.2.0" diff --git a/contracts/consumer/converter/src/contract.rs b/contracts/consumer/converter/src/contract.rs index a45887f2..caedb92d 100644 --- a/contracts/consumer/converter/src/contract.rs +++ b/contracts/consumer/converter/src/contract.rs @@ -305,7 +305,7 @@ impl ConverterContract<'_> { use price_feed_api::sv::Querier; use sylvia::types::Remote; let remote = Remote::< - dyn price_feed_api::PriceFeedApi< + &dyn price_feed_api::PriceFeedApi< Error = StdError, ExecC = custom::ConverterMsg, QueryC = custom::ConverterQuery, @@ -341,7 +341,7 @@ impl ConverterContract<'_> { use sylvia::types::Remote; // Note: it doesn't seem to matter which error type goes here... let remote = Remote::< - dyn price_feed_api::PriceFeedApi< + &dyn price_feed_api::PriceFeedApi< Error = StdError, ExecC = custom::ConverterMsg, QueryC = custom::ConverterQuery, diff --git a/packages/apis/src/virtual_staking_api.rs b/packages/apis/src/virtual_staking_api.rs index d05013a2..982855ce 100644 --- a/packages/apis/src/virtual_staking_api.rs +++ b/packages/apis/src/virtual_staking_api.rs @@ -1,7 +1,6 @@ // only for handle_valset_update but doesn't work for the autogenerated code #![allow(clippy::too_many_arguments)] - use cosmwasm_schema::cw_serde; use cosmwasm_std::{Coin, Response, StdError, Uint128, Validator}; use sylvia::cw_std::{CustomMsg, CustomQuery}; diff --git a/scripts/optimizer.sh b/scripts/optimizer.sh index 2d196567..12d84b87 100755 --- a/scripts/optimizer.sh +++ b/scripts/optimizer.sh @@ -1,4 +1,6 @@ -: +#!/bin/bash + +set -x U="cosmwasm" V="0.15.0" From c5bbcc4c3ad9175c7cf750fa555b655b9fbc724b Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 13 May 2024 16:46:25 +0200 Subject: [PATCH 6/6] More clippy fixes --- contracts/consumer/virtual-staking/src/contract.rs | 8 ++++---- contracts/provider/vault/src/multitest.rs | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/contracts/consumer/virtual-staking/src/contract.rs b/contracts/consumer/virtual-staking/src/contract.rs index 42a53555..52af7d8b 100644 --- a/contracts/consumer/virtual-staking/src/contract.rs +++ b/contracts/consumer/virtual-staking/src/contract.rs @@ -1322,7 +1322,7 @@ mod tests { impl VirtualStakingExt for VirtualStakingContract<'_> { fn quick_inst(&self, deps: DepsMut) { self.instantiate(InstantiateCtx { - deps: deps, + deps, env: mock_env(), info: mock_info("me", &[]), }) @@ -1371,7 +1371,7 @@ mod tests { self.bond( ExecCtx { - deps: deps, + deps, env: mock_env(), info: mock_info("me", &[]), }, @@ -1386,7 +1386,7 @@ mod tests { self.unbond( ExecCtx { - deps: deps, + deps, env: mock_env(), info: mock_info("me", &[]), }, @@ -1406,7 +1406,7 @@ mod tests { self.burn( ExecCtx { - deps: deps, + deps, env: mock_env(), info: mock_info("me", &[]), }, diff --git a/contracts/provider/vault/src/multitest.rs b/contracts/provider/vault/src/multitest.rs index 558bebf4..49893402 100644 --- a/contracts/provider/vault/src/multitest.rs +++ b/contracts/provider/vault/src/multitest.rs @@ -111,6 +111,7 @@ fn setup_without_local_staking<'app>( (vault, external) } +#[allow(clippy::type_complexity)] fn setup_inner<'app>( app: &'app App, owner: &'app str,