diff --git a/Cargo.lock b/Cargo.lock index d7b714d8..580371e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -678,6 +678,7 @@ dependencies = [ "cw2", "derivative", "mesh-apis", + "mesh-bindings", "mesh-burn", "mesh-native-staking", "mesh-vault", diff --git a/contracts/provider/external-staking/src/multitest.rs b/contracts/provider/external-staking/src/multitest.rs index ed8298a2..84092f45 100644 --- a/contracts/provider/external-staking/src/multitest.rs +++ b/contracts/provider/external-staking/src/multitest.rs @@ -6,7 +6,7 @@ use cosmwasm_std::{coin, coins, to_json_binary, Decimal, Uint128}; use cw_multi_test::App as MtApp; use mesh_native_staking::contract::sv::mt::CodeId as NativeStakingCodeId; use mesh_native_staking::contract::sv::InstantiateMsg as NativeStakingInstantiateMsg; -use mesh_native_staking_proxy::contract::sv::mt::CodeId as NativeStakingProxyCodeId; +use mesh_native_staking_proxy::mock::sv::mt::CodeId as NativeStakingProxyCodeId; use mesh_vault::mock::sv::mt::{CodeId as VaultCodeId, VaultMockProxy}; use mesh_vault::mock::VaultMock; use mesh_vault::msg::{LocalStakingInfo, StakingInitInfo}; diff --git a/contracts/provider/native-staking-proxy/Cargo.toml b/contracts/provider/native-staking-proxy/Cargo.toml index a8210310..6328e09e 100644 --- a/contracts/provider/native-staking-proxy/Cargo.toml +++ b/contracts/provider/native-staking-proxy/Cargo.toml @@ -21,6 +21,7 @@ mt = ["library", "sylvia/mt"] [dependencies] mesh-apis = { workspace = true } mesh-burn = { workspace = true } +mesh-bindings = { workspace = true } sylvia = { workspace = true } cosmwasm-schema = { workspace = true } diff --git a/contracts/provider/native-staking-proxy/src/contract.rs b/contracts/provider/native-staking-proxy/src/contract.rs index 89aa6240..fcbc52c8 100644 --- a/contracts/provider/native-staking-proxy/src/contract.rs +++ b/contracts/provider/native-staking-proxy/src/contract.rs @@ -7,6 +7,7 @@ use cw2::set_contract_version; use cw_storage_plus::Item; use cw_utils::{must_pay, nonpayable}; +use mesh_bindings::{ProviderCustomMsg, ProviderMsg}; use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; use sylvia::{contract, schemars}; @@ -26,6 +27,7 @@ pub struct NativeStakingProxyContract<'a> { #[cfg_attr(not(feature = "library"), sylvia::entry_points)] #[contract] #[sv::error(ContractError)] +#[sv::custom(msg=ProviderCustomMsg)] impl NativeStakingProxyContract<'_> { pub const fn new() -> Self { Self { @@ -43,7 +45,7 @@ impl NativeStakingProxyContract<'_> { denom: String, owner: String, validator: String, - ) -> Result { + ) -> Result, ContractError> { let config = Config { denom, parent: ctx.info.sender.clone(), @@ -76,7 +78,11 @@ impl NativeStakingProxyContract<'_> { /// Stakes the tokens from `info.funds` to the given validator. /// Can only be called by the parent contract #[sv::msg(exec)] - fn stake(&self, ctx: ExecCtx, validator: String) -> Result { + fn stake( + &self, + ctx: ExecCtx, + validator: String, + ) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.parent, ctx.info.sender, ContractError::Unauthorized {}); @@ -97,7 +103,7 @@ impl NativeStakingProxyContract<'_> { ctx: ExecCtx, validator: Option, amount: Coin, - ) -> Result { + ) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.parent, ctx.info.sender, ContractError::Unauthorized {}); @@ -186,7 +192,7 @@ impl NativeStakingProxyContract<'_> { src_validator: String, dst_validator: String, amount: Coin, - ) -> Result { + ) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); @@ -213,7 +219,7 @@ impl NativeStakingProxyContract<'_> { ctx: ExecCtx, proposal_id: u64, vote: VoteOption, - ) -> Result { + ) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); @@ -230,7 +236,7 @@ impl NativeStakingProxyContract<'_> { ctx: ExecCtx, proposal_id: u64, vote: Vec, - ) -> Result { + ) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); @@ -247,7 +253,7 @@ impl NativeStakingProxyContract<'_> { /// send the tokens to the caller. /// NOTE: must make sure not to release unbonded tokens #[sv::msg(exec)] - fn withdraw_rewards(&self, ctx: ExecCtx) -> Result { + fn withdraw_rewards(&self, ctx: ExecCtx) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); @@ -276,7 +282,7 @@ impl NativeStakingProxyContract<'_> { ctx: ExecCtx, validator: String, amount: Coin, - ) -> Result { + ) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); @@ -288,7 +294,11 @@ impl NativeStakingProxyContract<'_> { ContractError::InvalidDenom(amount.denom) ); - let msg = StakingMsg::Undelegate { validator, amount }; + let msg = ProviderMsg::Unstake { + delegator: ctx.info.sender.to_string(), + validator, + amount, + }; Ok(Response::new().add_message(msg)) } @@ -296,7 +306,7 @@ impl NativeStakingProxyContract<'_> { /// This will go back to the parent via `release_proxy_stake`. /// Errors if the proxy doesn't have any liquid tokens #[sv::msg(exec)] - fn release_unbonded(&self, ctx: ExecCtx) -> Result { + fn release_unbonded(&self, ctx: ExecCtx) -> Result, ContractError> { let cfg = self.config.load(ctx.deps.storage)?; ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); diff --git a/contracts/provider/native-staking-proxy/src/lib.rs b/contracts/provider/native-staking-proxy/src/lib.rs index 684e3b37..54c54224 100644 --- a/contracts/provider/native-staking-proxy/src/lib.rs +++ b/contracts/provider/native-staking-proxy/src/lib.rs @@ -1,5 +1,6 @@ pub mod contract; pub mod error; +pub mod mock; pub mod msg; #[cfg(test)] mod multitest; diff --git a/contracts/provider/native-staking-proxy/src/mock.rs b/contracts/provider/native-staking-proxy/src/mock.rs new file mode 100644 index 00000000..9fc5b3d6 --- /dev/null +++ b/contracts/provider/native-staking-proxy/src/mock.rs @@ -0,0 +1,502 @@ +use cosmwasm_std::WasmMsg::Execute; +use cosmwasm_std::{ + coin, ensure_eq, to_json_binary, Coin, DistributionMsg, GovMsg, Response, StakingMsg, + VoteOption, WeightedVoteOption, +}; +use cw2::set_contract_version; +use cw_storage_plus::Item; + +use cw_utils::{must_pay, nonpayable}; +use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx}; +use sylvia::{contract, schemars}; + +use crate::error::ContractError; +use crate::msg::{ConfigResponse, OwnerMsg}; +use crate::native_staking_callback; +use crate::state::Config; + +pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); +pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); + +pub struct NativeStakingProxyMock<'a> { + config: Item<'a, Config>, + burned: Item<'a, u128>, +} + +#[contract] +#[sv::error(ContractError)] +impl NativeStakingProxyMock<'_> { + pub const fn new() -> Self { + Self { + config: Item::new("config"), + burned: Item::new("burned"), + } + } + + /// The caller of the instantiation will be the native-staking contract. + /// We stake `funds.info` on the given validator + #[sv::msg(instantiate)] + pub fn instantiate( + &self, + ctx: InstantiateCtx, + denom: String, + owner: String, + validator: String, + ) -> Result { + let config = Config { + denom, + parent: ctx.info.sender.clone(), + owner: ctx.deps.api.addr_validate(&owner)?, + }; + self.config.save(ctx.deps.storage, &config)?; + set_contract_version(ctx.deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + + // Set burned stake to zero + self.burned.save(ctx.deps.storage, &0)?; + + // Stake info.funds on validator + let exec_ctx = ExecCtx { + deps: ctx.deps, + env: ctx.env, + info: ctx.info, + }; + let res = self.stake(exec_ctx, validator)?; + + // Set owner as recipient of future withdrawals + let set_withdrawal = DistributionMsg::SetWithdrawAddress { + address: config.owner.into_string(), + }; + + // Pass owner to caller's reply handler + let owner_msg = to_json_binary(&OwnerMsg { owner })?; + Ok(res.add_message(set_withdrawal).set_data(owner_msg)) + } + + /// Stakes the tokens from `info.funds` to the given validator. + /// Can only be called by the parent contract + #[sv::msg(exec)] + fn stake(&self, ctx: ExecCtx, validator: String) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.parent, ctx.info.sender, ContractError::Unauthorized {}); + + let amount = must_pay(&ctx.info, &cfg.denom)?; + + let amount = coin(amount.u128(), cfg.denom); + let msg = StakingMsg::Delegate { validator, amount }; + + Ok(Response::new().add_message(msg)) + } + + /// Burn `amount` tokens from the given validator, if set. + /// If `validator` is not set, undelegate evenly from all validators the user has stake. + /// Can only be called by the parent contract + #[sv::msg(exec)] + fn burn( + &self, + ctx: ExecCtx, + validator: Option, + amount: Coin, + ) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.parent, ctx.info.sender, ContractError::Unauthorized {}); + + nonpayable(&ctx.info)?; + + // Check denom + ensure_eq!( + amount.denom, + cfg.denom, + ContractError::InvalidDenom(amount.denom) + ); + + let delegations = match validator { + Some(validator) => { + match ctx + .deps + .querier + .query_delegation(ctx.env.contract.address.clone(), validator)? + .map(|full_delegation| { + ( + full_delegation.validator, + full_delegation.amount.amount.u128(), + ) + }) { + Some(delegation) => vec![delegation], + None => vec![], + } + } + None => ctx + .deps + .querier + .query_all_delegations(ctx.env.contract.address.clone())? + .iter() + .map(|delegation| { + ( + delegation.validator.clone(), + delegation.amount.amount.u128(), + ) + }) + .collect::>(), + }; + + // Error if no validators + if delegations.is_empty() { + return Err(ContractError::InsufficientDelegations( + ctx.env.contract.address.to_string(), + amount.amount, + )); + } + + let (burned, burns) = mesh_burn::distribute_burn(&delegations, amount.amount.u128()); + + // Bail if we don't have enough delegations + if burned < amount.amount.u128() { + return Err(ContractError::InsufficientDelegations( + ctx.env.contract.address.to_string(), + amount.amount, + )); + } + + // Build undelegate messages + // FIXME: Use an "immediate unbonding" message for undelegation + let mut undelegate_msgs = vec![]; + for (validator, burn_amount) in burns { + let undelegate_msg = StakingMsg::Undelegate { + validator: validator.to_string(), + amount: coin(burn_amount, &cfg.denom), + }; + undelegate_msgs.push(undelegate_msg); + } + + // Accounting trick to avoid burning stake + self.burned.update(ctx.deps.storage, |old| { + Ok::<_, ContractError>(old + amount.amount.u128()) + })?; + + Ok(Response::new().add_messages(undelegate_msgs)) + } + + /// Re-stakes the given amount from the one validator to another on behalf of the calling user. + /// Returns an error if the user doesn't have such stake + #[sv::msg(exec)] + fn restake( + &self, + ctx: ExecCtx, + src_validator: String, + dst_validator: String, + amount: Coin, + ) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); + + nonpayable(&ctx.info)?; + + ensure_eq!( + amount.denom, + cfg.denom, + ContractError::InvalidDenom(amount.denom) + ); + + let msg = StakingMsg::Redelegate { + src_validator, + dst_validator, + amount, + }; + Ok(Response::new().add_message(msg)) + } + + /// Vote with the user's stake (over all delegations) + #[sv::msg(exec)] + fn vote( + &self, + ctx: ExecCtx, + proposal_id: u64, + vote: VoteOption, + ) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); + + nonpayable(&ctx.info)?; + + let msg = GovMsg::Vote { proposal_id, vote }; + Ok(Response::new().add_message(msg)) + } + + /// Vote with the user's stake (over all delegations) + #[sv::msg(exec)] + fn vote_weighted( + &self, + ctx: ExecCtx, + proposal_id: u64, + vote: Vec, + ) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); + + nonpayable(&ctx.info)?; + + let msg = GovMsg::VoteWeighted { + proposal_id, + options: vote, + }; + Ok(Response::new().add_message(msg)) + } + + /// If the caller has any delegations, withdraw all rewards from those delegations and + /// send the tokens to the caller. + /// NOTE: must make sure not to release unbonded tokens + #[sv::msg(exec)] + fn withdraw_rewards(&self, ctx: ExecCtx) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); + + nonpayable(&ctx.info)?; + + // Withdraw all delegations to the owner (already set as withdrawal address in instantiate) + let msgs: Vec<_> = ctx + .deps + .querier + .query_all_delegations(ctx.env.contract.address)? + .into_iter() + .map(|delegation| DistributionMsg::WithdrawDelegatorReward { + validator: delegation.validator, + }) + .collect(); + let res = Response::new().add_messages(msgs); + Ok(res) + } + + /// Unstakes the given amount from the given validator on behalf of the calling user. + /// Returns an error if the user doesn't have such stake. + /// After the unbonding period, it will allow the user to claim the tokens (returning to vault) + #[sv::msg(exec)] + fn unstake( + &self, + ctx: ExecCtx, + validator: String, + amount: Coin, + ) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); + + nonpayable(&ctx.info)?; + + ensure_eq!( + amount.denom, + cfg.denom, + ContractError::InvalidDenom(amount.denom) + ); + + let msg = StakingMsg::Undelegate { validator, amount }; + Ok(Response::new().add_message(msg)) + } + + /// Releases any tokens that have fully unbonded from a previous unstake. + /// This will go back to the parent via `release_proxy_stake`. + /// Errors if the proxy doesn't have any liquid tokens + #[sv::msg(exec)] + fn release_unbonded(&self, ctx: ExecCtx) -> Result { + let cfg = self.config.load(ctx.deps.storage)?; + ensure_eq!(cfg.owner, ctx.info.sender, ContractError::Unauthorized {}); + + nonpayable(&ctx.info)?; + + // Simply assumes all of our liquid assets are from unbondings + let balance = ctx + .deps + .querier + .query_balance(ctx.env.contract.address, cfg.denom)?; + // But discount burned stake + // FIXME: this is not accurate, as it doesn't take into account the unbonding period + let burned = self.burned.load(ctx.deps.storage)?; + let balance = coin(balance.amount.u128().saturating_sub(burned), &balance.denom); + + // Short circuit if there are no funds to send + if balance.amount.is_zero() { + return Ok(Response::new()); + } + + // Send them to the parent contract via `release_proxy_stake` + let msg = to_json_binary(&native_staking_callback::sv::ExecMsg::ReleaseProxyStake {})?; + + let wasm_msg = Execute { + contract_addr: cfg.parent.to_string(), + msg, + funds: vec![balance], + }; + Ok(Response::new().add_message(wasm_msg)) + } + + #[sv::msg(query)] + fn config(&self, ctx: QueryCtx) -> Result { + Ok(self.config.load(ctx.deps.storage)?) + } +} + +// Some unit tests, due to mt limitations / unsupported msgs +#[cfg(test)] +mod tests { + use super::*; + use cosmwasm_std::DistributionMsg::SetWithdrawAddress; + use cosmwasm_std::GovMsg::{Vote, VoteWeighted}; + use cosmwasm_std::{CosmosMsg, Decimal, DepsMut}; + + use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; + use cosmwasm_std::VoteOption::Yes; + use cw_utils::PaymentError; + + static OSMO: &str = "uosmo"; + static CREATOR: &str = "staking"; // The creator of the proxy contract(s) is the staking contract + static OWNER: &str = "user"; + static VALIDATOR: &str = "validator"; + + fn do_instantiate(deps: DepsMut) -> (ExecCtx, NativeStakingProxyMock) { + let contract = NativeStakingProxyMock::new(); + let mut ctx = InstantiateCtx { + deps, + env: mock_env(), + info: mock_info(CREATOR, &[coin(100, OSMO)]), + }; + contract + .instantiate( + ctx.branch(), + OSMO.to_owned(), + OWNER.to_owned(), + VALIDATOR.to_owned(), + ) + .unwrap(); + let exec_ctx = ExecCtx { + deps: ctx.deps, + info: mock_info(OWNER, &[]), + env: ctx.env, + }; + (exec_ctx, contract) + } + + // Extra checks of instantiate returned messages and data + #[test] + fn instantiating() { + let mut deps = mock_dependencies(); + let contract = NativeStakingProxyMock::new(); + let mut ctx = InstantiateCtx { + deps: deps.as_mut(), + env: mock_env(), + info: mock_info(CREATOR, &[coin(100, OSMO)]), + }; + let res = contract + .instantiate( + ctx.branch(), + OSMO.to_owned(), + OWNER.to_owned(), + VALIDATOR.to_owned(), + ) + .unwrap(); + + // Assert returned messages + assert_eq!( + res.messages[0].msg, + CosmosMsg::Staking(StakingMsg::Delegate { + validator: VALIDATOR.to_owned(), + amount: coin(100, OSMO) + }) + ); + assert_eq!( + res.messages[1].msg, + CosmosMsg::Distribution(SetWithdrawAddress { + address: OWNER.to_owned(), + }) + ); + + // Assert data payload + assert_eq!( + res.data.unwrap(), + to_json_binary(&OwnerMsg { + owner: OWNER.to_owned(), + }) + .unwrap() + ); + } + + #[test] + fn voting() { + let mut deps = mock_dependencies(); + let (mut ctx, contract) = do_instantiate(deps.as_mut()); + + // The owner can vote + let proposal_id = 1; + let vote = Yes; + let res = contract + .vote(ctx.branch(), proposal_id, vote.clone()) + .unwrap(); + assert_eq!(1, res.messages.len()); + // assert it's a governance vote + assert_eq!( + res.messages[0].msg, + cosmwasm_std::CosmosMsg::Gov(Vote { + proposal_id, + vote: vote.clone() + }) + ); + + // But not send funds + ctx.info = mock_info(OWNER, &[coin(1, OSMO)]); + let res = contract.vote(ctx.branch(), proposal_id, vote.clone()); + assert!(matches!( + res.unwrap_err(), + ContractError::Payment(PaymentError::NonPayable {}) + )); + + // Nobody else can vote + ctx.info = mock_info("somebody", &[]); + let res = contract.vote(ctx.branch(), proposal_id, vote.clone()); + assert!(matches!(res.unwrap_err(), ContractError::Unauthorized {})); + + // Not even the creator + ctx.info = mock_info(CREATOR, &[]); + let res = contract.vote(ctx, proposal_id, vote); + assert!(matches!(res.unwrap_err(), ContractError::Unauthorized {})); + } + + #[test] + fn weighted_voting() { + let mut deps = mock_dependencies(); + let (mut ctx, contract) = do_instantiate(deps.as_mut()); + + // The owner can weighted vote + let proposal_id = 2; + let vote = vec![WeightedVoteOption { + option: Yes, + weight: Decimal::percent(50), + }]; + let res = contract + .vote_weighted(ctx.branch(), proposal_id, vote.clone()) + .unwrap(); + assert_eq!(1, res.messages.len()); + // Assert it's a weighted governance vote + assert_eq!( + res.messages[0].msg, + cosmwasm_std::CosmosMsg::Gov(VoteWeighted { + proposal_id, + options: vote.clone() + }) + ); + + // But not send funds + ctx.info = mock_info(OWNER, &[coin(1, OSMO)]); + let res = contract.vote_weighted(ctx.branch(), proposal_id, vote.clone()); + assert!(matches!( + res.unwrap_err(), + ContractError::Payment(PaymentError::NonPayable {}) + )); + + // Nobody else can vote + ctx.info = mock_info("somebody", &[]); + let res = contract.vote_weighted(ctx.branch(), proposal_id, vote.clone()); + assert!(matches!(res.unwrap_err(), ContractError::Unauthorized {})); + + // Not even the creator + ctx.info = mock_info(CREATOR, &[]); + let res = contract.vote_weighted(ctx, proposal_id, vote); + assert!(matches!(res.unwrap_err(), ContractError::Unauthorized {})); + } +} diff --git a/contracts/provider/native-staking-proxy/src/multitest.rs b/contracts/provider/native-staking-proxy/src/multitest.rs index 90baa916..b0e684f4 100644 --- a/contracts/provider/native-staking-proxy/src/multitest.rs +++ b/contracts/provider/native-staking-proxy/src/multitest.rs @@ -10,9 +10,8 @@ use mesh_vault::mock::sv::mt::VaultMockProxy; use mesh_vault::mock::VaultMock; use mesh_vault::msg::LocalStakingInfo; -use crate::contract; -use crate::contract::sv::mt::NativeStakingProxyContractProxy; -use crate::contract::NativeStakingProxyContract; +use crate::mock::sv::mt::NativeStakingProxyMockProxy; +use crate::mock::NativeStakingProxyMock; use crate::msg::ConfigResponse; const OSMO: &str = "uosmo"; @@ -62,7 +61,7 @@ fn setup<'app>( ) -> AnyResult>> { let vault_code = mesh_vault::mock::sv::mt::CodeId::store_code(app); let staking_code = mesh_native_staking::contract::sv::mt::CodeId::store_code(app); - let staking_proxy_code = contract::sv::mt::CodeId::store_code(app); + let staking_proxy_code = crate::mock::sv::mt::CodeId::store_code(app); // Instantiate vault msg let staking_init_info = mesh_vault::msg::StakingInitInfo { @@ -126,7 +125,7 @@ fn instantiation() { setup(&app, owner, user, &[validator]).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Check config @@ -170,7 +169,7 @@ fn staking() { let vault = setup(&app, owner, user, &[validator]).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Stake some more @@ -216,7 +215,7 @@ fn restaking() { setup(&app, owner, user, &[validator]).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Restake 30% to a different validator @@ -255,7 +254,7 @@ fn unstaking() { setup(&app, owner, user, &[validator]).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Unstake 50% @@ -310,7 +309,7 @@ fn burning() { setup(&app, owner, user, &[validator]).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Burn 10%, from validator @@ -377,7 +376,7 @@ fn burning_multiple_delegations() { setup(&app, owner, user, &validators).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Burn 15%, no validator specified @@ -458,7 +457,7 @@ fn releasing_unbonded() { let vault = setup(&app, owner, user, &[validator]).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Unstake 100% @@ -514,7 +513,7 @@ fn withdrawing_rewards() { let original_user_funds = app.app().wrap().query_balance(user, OSMO).unwrap(); // Access staking proxy instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // Advance time enough for rewards to accrue diff --git a/contracts/provider/native-staking/src/multitest.rs b/contracts/provider/native-staking/src/multitest.rs index fde0c43a..1463a9e8 100644 --- a/contracts/provider/native-staking/src/multitest.rs +++ b/contracts/provider/native-staking/src/multitest.rs @@ -6,10 +6,10 @@ use cw_multi_test::{App as MtApp, StakingInfo}; use sylvia::multitest::{App, Proxy}; use mesh_apis::local_staking_api::sv::mt::LocalStakingApiProxy; -use mesh_native_staking_proxy::contract::sv::mt::{ - CodeId as NativeStakingProxyCodeId, NativeStakingProxyContractProxy, +use mesh_native_staking_proxy::mock::sv::mt::{ + CodeId as NativeStakingProxyCodeId, NativeStakingProxyMockProxy, }; -use mesh_native_staking_proxy::contract::NativeStakingProxyContract; +use mesh_native_staking_proxy::mock::NativeStakingProxyMock; use mesh_sync::ValueRange; use mesh_vault::mock::sv::mt::VaultMockProxy; use mesh_vault::msg::LocalStakingInfo; @@ -291,7 +291,7 @@ fn releasing_proxy_stake() { ); // Access staking instance - let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyContract<'_>> = + let staking_proxy: Proxy<'_, MtApp, NativeStakingProxyMock<'_>> = Proxy::new(Addr::unchecked(proxy_addr), &app); // User bonds some funds to the vault diff --git a/contracts/provider/vault/src/multitest.rs b/contracts/provider/vault/src/multitest.rs index 9e8b897a..fc2f9485 100644 --- a/contracts/provider/vault/src/multitest.rs +++ b/contracts/provider/vault/src/multitest.rs @@ -8,8 +8,8 @@ use mesh_external_staking::state::SlashRatio; use mesh_external_staking::state::Stake; use mesh_native_staking::contract::sv::mt::NativeStakingContractProxy; use mesh_native_staking::contract::NativeStakingContract; -use mesh_native_staking_proxy::contract::sv::mt::NativeStakingProxyContractProxy; -use mesh_native_staking_proxy::contract::NativeStakingProxyContract; +use mesh_native_staking_proxy::mock::sv::mt::NativeStakingProxyMockProxy; +use mesh_native_staking_proxy::mock::NativeStakingProxyMock; use mesh_sync::Tx::InFlightStaking; use mesh_sync::{Tx, ValueRange}; use sylvia::multitest::{App, Proxy}; @@ -126,7 +126,7 @@ fn setup_inner<'app>( let staking_init_info = if local_staking { let native_staking_code = mesh_native_staking::contract::sv::mt::CodeId::store_code(app); let native_staking_proxy_code = - mesh_native_staking_proxy::contract::sv::mt::CodeId::store_code(app); + mesh_native_staking_proxy::mock::sv::mt::CodeId::store_code(app); let native_staking_inst_msg = mesh_native_staking::contract::sv::InstantiateMsg { denom: OSMO.to_string(), @@ -262,7 +262,7 @@ fn proxy_for_user<'a>( local_staking: &Proxy<'_, MtApp, NativeStakingContract<'_>>, user: &str, app: &'a App, -) -> Proxy<'a, MtApp, NativeStakingProxyContract<'a>> { +) -> Proxy<'a, MtApp, NativeStakingProxyMock<'a>> { let proxy_addr = local_staking .proxy_by_owner(user.to_string()) .unwrap() diff --git a/packages/bindings/src/msg.rs b/packages/bindings/src/msg.rs index b897c850..0e74a3d7 100644 --- a/packages/bindings/src/msg.rs +++ b/packages/bindings/src/msg.rs @@ -120,6 +120,16 @@ pub enum ProviderMsg { /// If these conditions are met, it will instantly unbond /// amount.amount tokens from the vault contract. Unbond { delegator: String, amount: Coin }, + /// Unstake ensures that amount.denom is the native staking denom and + /// the calling contract is the native staking proxy contract. + /// + /// If these conditions are met, it will instantly unstake + /// amount.amount tokens from the native staking proxy contract. + Unstake { + delegator: String, + validator: String, + amount: Coin, + }, } impl ProviderMsg { @@ -144,6 +154,23 @@ impl ProviderMsg { amount: coin, } } + + pub fn unstake( + denom: &str, + delegator: &str, + validator: &str, + amount: impl Into, + ) -> ProviderMsg { + let coin = Coin { + amount: amount.into(), + denom: denom.into(), + }; + ProviderMsg::Unstake { + delegator: delegator.to_string(), + validator: validator.to_string(), + amount: coin, + } + } } impl From for CosmosMsg {