From 4e724a4ad8bc01b45c5293ca4e2b26f5f7c5199d Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 30 May 2024 12:56:15 +0200 Subject: [PATCH] Update sylvia so all execute messages payable --- Cargo.lock | 4 +- Cargo.toml | 2 +- .../remote-price-feed/src/price_keeper.rs | 6 +++ .../provider/external-staking/src/crdt.rs | 6 +++ contracts/provider/vault/src/lib.rs | 3 ++ contracts/provider/vault/src/orch.rs | 54 +++++++++++++++++++ contracts/provider/vault/src/test.rs | 38 +++++++++++++ 7 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 contracts/provider/vault/src/orch.rs create mode 100644 contracts/provider/vault/src/test.rs diff --git a/Cargo.lock b/Cargo.lock index 4d940b72..992efb58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1509,7 +1509,7 @@ dependencies = [ [[package]] name = "sylvia" version = "0.10.1" -source = "git+https://github.com/AbstractSDK/sylvia.git?rev=a4168a242d444cc97d6a6363304d99d630dcbef3#a4168a242d444cc97d6a6363304d99d630dcbef3" +source = "git+https://github.com/AbstractSDK/sylvia.git?rev=76ed9ace11ce79f541d94e5d03eb9d0b9deed509#76ed9ace11ce79f541d94e5d03eb9d0b9deed509" dependencies = [ "anyhow", "cosmwasm-schema", @@ -1528,7 +1528,7 @@ dependencies = [ [[package]] name = "sylvia-derive" version = "0.10.1" -source = "git+https://github.com/AbstractSDK/sylvia.git?rev=a4168a242d444cc97d6a6363304d99d630dcbef3#a4168a242d444cc97d6a6363304d99d630dcbef3" +source = "git+https://github.com/AbstractSDK/sylvia.git?rev=76ed9ace11ce79f541d94e5d03eb9d0b9deed509#76ed9ace11ce79f541d94e5d03eb9d0b9deed509" dependencies = [ "convert_case", "itertools 0.12.1", diff --git a/Cargo.toml b/Cargo.toml index db297939..e96251c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ mesh-virtual-staking = { path = "./contracts/consumer/virtual-staking" } # sylvia = "0.10.1" # from branch update/clean-add-cw-orch -sylvia = { git = "https://github.com/AbstractSDK/sylvia.git", rev = "a4168a242d444cc97d6a6363304d99d630dcbef3", features = ["orch"] } +sylvia = { git = "https://github.com/AbstractSDK/sylvia.git", rev = "76ed9ace11ce79f541d94e5d03eb9d0b9deed509", features = ["orch"] } # need this fork of cw-orch as well... cw-orch = { git = "https://github.com/abstractsdk/cw-orchestrator", branch = "update/relax-bound-on-derive" } # cw-orch = "0.22.1" diff --git a/contracts/consumer/remote-price-feed/src/price_keeper.rs b/contracts/consumer/remote-price-feed/src/price_keeper.rs index 50b5d2de..f88d9d24 100644 --- a/contracts/consumer/remote-price-feed/src/price_keeper.rs +++ b/contracts/consumer/remote-price-feed/src/price_keeper.rs @@ -9,6 +9,12 @@ pub struct PriceKeeper { pub price_info_ttl_in_secs: Item<'static, u64>, } +impl Default for PriceKeeper { + fn default() -> Self { + Self::new() + } +} + impl PriceKeeper { pub const fn new() -> Self { Self { diff --git a/contracts/provider/external-staking/src/crdt.rs b/contracts/provider/external-staking/src/crdt.rs index 838ded4a..26dc551f 100644 --- a/contracts/provider/external-staking/src/crdt.rs +++ b/contracts/provider/external-staking/src/crdt.rs @@ -96,6 +96,12 @@ pub struct CrdtState<'a> { validators: Map<'a, &'a str, ValidatorState>, } +impl<'a> Default for CrdtState<'a> { + fn default() -> Self { + Self::new() + } +} + impl<'a> CrdtState<'a> { pub const fn new() -> Self { CrdtState { diff --git a/contracts/provider/vault/src/lib.rs b/contracts/provider/vault/src/lib.rs index 77d7a460..829c4b95 100644 --- a/contracts/provider/vault/src/lib.rs +++ b/contracts/provider/vault/src/lib.rs @@ -3,5 +3,8 @@ pub mod error; pub mod msg; #[cfg(test)] mod multitest; +pub mod orch; mod state; +#[cfg(test)] +mod test; pub mod txs; diff --git a/contracts/provider/vault/src/orch.rs b/contracts/provider/vault/src/orch.rs new file mode 100644 index 00000000..b0f1faec --- /dev/null +++ b/contracts/provider/vault/src/orch.rs @@ -0,0 +1,54 @@ +// This contains all code we needed to manually add to make it work well with cw-orch +// In the future, hopefully some of this can me auto-generated. But let's get it to work now. +use crate::contract::entry_points::{execute, instantiate, query}; +use crate::contract::sv::{ContractExecMsg, ContractQueryMsg, ContractSudoMsg, InstantiateMsg}; +use cw_orch::prelude::*; + +// Maybe uploadable can be autogenerated? +// But this is fine to include in the client code + +#[cw_orch::interface(InstantiateMsg, ContractExecMsg, ContractQueryMsg, Empty)] +pub struct MeshVault; + +impl Uploadable for MeshVault { + /// Return the path to the wasm file corresponding to the contract + fn wasm(_chain: &ChainInfoOwned) -> WasmPath { + artifacts_dir_from_workspace!() + .find_wasm_path("mesh_vault") + .unwrap() + } + /// Returns a CosmWasm contract wrapper + fn wrapper() -> Box> { + Box::new(ContractWrapper::new_with_empty(execute, instantiate, query)) + } +} + +// Autogenerating these Froms caused all kinds of rust compiler issues. +// For now, I will just manually implement them to show what could work. +// The following is done in Sylvia, ideally the other (interface) variants could be added there too: +// impl From for ContractExecMsg + +// Since we know the regular structure here, this may be easy enough to +// generate inside the `sv::messages` macro rather than the `contract` macro, eg. +// #[sv::messages(cw20_marketing as Marketing)] +// #[sv::messages(cw20_minting as Minting)] + +use mesh_apis::vault_api::sv::{VaultApiExecMsg, VaultApiQueryMsg, VaultApiSudoMsg}; + +impl From for ContractExecMsg { + fn from(msg: VaultApiExecMsg) -> Self { + ContractExecMsg::VaultApi(msg) + } +} + +impl From for ContractQueryMsg { + fn from(msg: VaultApiQueryMsg) -> Self { + ContractQueryMsg::VaultApi(msg) + } +} + +impl From for ContractSudoMsg { + fn from(msg: VaultApiSudoMsg) -> Self { + ContractSudoMsg::VaultApi(msg) + } +} diff --git a/contracts/provider/vault/src/test.rs b/contracts/provider/vault/src/test.rs new file mode 100644 index 00000000..452a062f --- /dev/null +++ b/contracts/provider/vault/src/test.rs @@ -0,0 +1,38 @@ +use cosmwasm_std::coin; +// use cw_orch::environment::IndexResponse; +use cw_orch::prelude::*; + +use crate::orch::MeshVault; + +use crate::contract::sv::{ExecMsgFns, InstantiateMsg, QueryMsgFns}; + +// TODO: shared variable +const BECH_PREFIX: &str = "osmo"; + +#[test] +fn happy_path_works() { + let denom = "uosmo"; + let chain = MockBech32::new(BECH_PREFIX); + chain + .add_balance(&chain.sender(), vec![coin(1_000_000, denom)]) + .unwrap(); + + let contract = MeshVault::new("vault", chain.clone()); + contract.upload().unwrap(); + let msg = InstantiateMsg { + denom: denom.to_string(), + local_staking: None, + }; + contract.instantiate(&msg, None, None).unwrap(); + + let cfg = contract.config().unwrap(); + println!("{:?}", cfg); + + let account = contract.account(chain.sender().into()).unwrap(); + assert_eq!(account.bonded.u128(), 0u128); + + contract.bond(&[coin(400_000, denom)]).unwrap(); + + let account = contract.account(chain.sender().into()).unwrap(); + assert_eq!(account.bonded.u128(), 400_000u128); +}