From da87f16dba80c7e649a99d80f8c38f223a574e01 Mon Sep 17 00:00:00 2001 From: Trinity Date: Thu, 5 Sep 2024 16:59:02 +0700 Subject: [PATCH] Remove virtual staking mock --- Cargo.lock | 13 -- Cargo.toml | 1 - packages/virtual-staking-mock/Cargo.toml | 16 -- packages/virtual-staking-mock/src/lib.rs | 204 ----------------------- 4 files changed, 234 deletions(-) delete mode 100644 packages/virtual-staking-mock/Cargo.toml delete mode 100644 packages/virtual-staking-mock/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 580371e2..901cc99e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -811,19 +811,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "mesh-virtual-staking-mock" -version = "0.10.0-alpha.1" -dependencies = [ - "anyhow", - "cosmwasm-std", - "cw-multi-test", - "cw-storage-plus", - "mesh-bindings", - "schemars", - "serde", -] - [[package]] name = "num-traits" version = "0.2.17" diff --git a/Cargo.toml b/Cargo.toml index 3937a620..aa9857ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,6 @@ 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" } diff --git a/packages/virtual-staking-mock/Cargo.toml b/packages/virtual-staking-mock/Cargo.toml deleted file mode 100644 index 5909adbd..00000000 --- a/packages/virtual-staking-mock/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "mesh-virtual-staking-mock" -edition.workspace = true -version.workspace = true -license.workspace = true -repository.workspace = true -publish = false - -[dependencies] -anyhow = { workspace = true } -cosmwasm-std = { workspace = true } -cw-multi-test = { workspace = true } -cw-storage-plus = { workspace = true } -mesh-bindings = { workspace = true } -schemars = { workspace = true } -serde = { workspace = true } diff --git a/packages/virtual-staking-mock/src/lib.rs b/packages/virtual-staking-mock/src/lib.rs deleted file mode 100644 index 20652cd2..00000000 --- a/packages/virtual-staking-mock/src/lib.rs +++ /dev/null @@ -1,204 +0,0 @@ -use anyhow::Result as AnyResult; -use cosmwasm_std::{ - coin, - testing::{MockApi, MockStorage}, - to_json_binary, Addr, AllDelegationsResponse, Api, Binary, BlockInfo, CustomQuery, Empty, - Querier, QuerierWrapper, Storage, Uint128, -}; -use cw_multi_test::{AppResponse, BankKeeper, Module, WasmKeeper}; -use cw_storage_plus::{Item, Map}; -use mesh_bindings::{ - BondStatusResponse, SlashRatioResponse, VirtualStakeCustomMsg, VirtualStakeCustomQuery, -}; -use schemars::JsonSchema; -use serde::de::DeserializeOwned; - -pub type App = cw_multi_test::App< - BankKeeper, - MockApi, - MockStorage, - VirtualStakingModule, - WasmKeeper, ->; - -pub struct VirtualStakingModule { - /// virtual-staking contract -> max cap - caps: Map<'static, Addr, Uint128>, - /// (virtual-staking contract, validator) -> bonded amount - bonds: Map<'static, (Addr, Addr), Uint128>, - slash_ratio: Item<'static, SlashRatioResponse>, -} - -impl VirtualStakingModule { - pub fn new() -> Self { - Self { - caps: Map::new("virtual_staking_caps"), - bonds: Map::new("virtual_staking_bonds"), - slash_ratio: Item::new("virtual_staking_slash_ratios"), - } - } - - pub fn init_slash_ratios( - &self, - storage: &mut dyn Storage, - slash_for_downtime: impl Into, - slash_for_double_sign: impl Into, - ) -> AnyResult<()> { - self.slash_ratio.save( - storage, - &SlashRatioResponse { - slash_fraction_downtime: slash_for_downtime.into(), - slash_fraction_double_sign: slash_for_double_sign.into(), - }, - )?; - - Ok(()) - } - - fn bonded_for_contract(&self, storage: &dyn Storage, contract: Addr) -> AnyResult { - Ok(self - .bonds - .range(storage, None, None, cosmwasm_std::Order::Ascending) - .collect::, _>>()? - .into_iter() - .filter_map(|((c, _), amt)| if c == contract { Some(amt) } else { None }) - .sum()) - } -} - -impl Default for VirtualStakingModule { - fn default() -> Self { - Self::new() - } -} - -impl Module for VirtualStakingModule { - type ExecT = VirtualStakeCustomMsg; - - type QueryT = VirtualStakeCustomQuery; - - type SudoT = Empty; - - fn execute( - &self, - _api: &dyn Api, - storage: &mut dyn Storage, - _router: &dyn cw_multi_test::CosmosRouter, - _block: &BlockInfo, - sender: Addr, - msg: Self::ExecT, - ) -> AnyResult - where - ExecC: std::fmt::Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static, - QueryC: CustomQuery + DeserializeOwned + 'static, - { - let VirtualStakeCustomMsg::VirtualStake(msg) = msg; - - let cap = self.caps.load(storage, sender.clone())?; - - match msg { - mesh_bindings::VirtualStakeMsg::Bond { amount, validator } => { - let all_bonded = self.bonded_for_contract(storage, sender.clone())?; - - if all_bonded + amount.amount <= cap { - let current_bonded = self - .bonds - .may_load(storage, (sender.clone(), Addr::unchecked(&validator)))? - .unwrap_or(Uint128::zero()); - - self.bonds.save( - storage, - (sender, Addr::unchecked(validator)), - &(current_bonded + amount.amount), - )?; - - Ok(AppResponse::default()) - } else { - Err(anyhow::anyhow!("cap exceeded")) - } - } - mesh_bindings::VirtualStakeMsg::Unbond { amount, validator } => { - let current_bonded = self - .bonds - .may_load(storage, (sender.clone(), Addr::unchecked(&validator)))? - .unwrap_or(Uint128::zero()); - - if current_bonded - amount.amount >= Uint128::zero() { - self.bonds.save( - storage, - (sender, Addr::unchecked(validator)), - &(current_bonded - amount.amount), - )?; - - Ok(AppResponse::default()) - } else { - Err(anyhow::anyhow!("bonded amount exceeded")) - } - } - mesh_bindings::VirtualStakeMsg::UpdateDelegation { .. } => Ok(AppResponse::default()), - mesh_bindings::VirtualStakeMsg::DeleteAllScheduledTasks { .. } => { - Ok(AppResponse::default()) - } - } - } - - fn sudo( - &self, - _api: &dyn Api, - _storage: &mut dyn Storage, - _router: &dyn cw_multi_test::CosmosRouter, - _block: &BlockInfo, - _msg: Self::SudoT, - ) -> AnyResult - where - ExecC: std::fmt::Debug + Clone + PartialEq + JsonSchema + DeserializeOwned + 'static, - QueryC: CustomQuery + DeserializeOwned + 'static, - { - Err(anyhow::anyhow!( - "sudo not implemented for the virtual staking module" - )) - } - - fn query( - &self, - _api: &dyn Api, - storage: &dyn Storage, - querier: &dyn Querier, - _block: &BlockInfo, - request: Self::QueryT, - ) -> AnyResult { - let VirtualStakeCustomQuery::VirtualStake(query) = request; - - let result = match query { - mesh_bindings::VirtualStakeQuery::BondStatus { contract } => { - let denom = - QuerierWrapper::::new(querier).query_bonded_denom()?; - - let cap = self.caps.load(storage, Addr::unchecked(&contract))?; - let bonded = self.bonded_for_contract(storage, Addr::unchecked(contract))?; - - to_json_binary(&BondStatusResponse { - cap: coin(cap.u128(), &denom), - delegated: coin(bonded.u128(), denom), - })? - } - mesh_bindings::VirtualStakeQuery::SlashRatio {} => { - to_json_binary(&self.slash_ratio.load(storage)?)? - } - mesh_bindings::VirtualStakeQuery::TotalDelegation { - contract, - validator, - } => to_json_binary(&self.bonds.load( - storage, - (Addr::unchecked(&contract), Addr::unchecked(&validator)), - )?)?, - mesh_bindings::VirtualStakeQuery::AllDelegations { .. } => { - to_json_binary(&AllDelegationsResponse { - delegations: vec![], - })? - } - }; - - Ok(to_json_binary(&result)?) - } -}