Skip to content

Commit

Permalink
Merge branch 'feat/vesting-delegate-remake' of https://github.com/dec…
Browse files Browse the repository at this point in the history
…entrio/mesh-security into feat/native-immediate-unbonding
  • Loading branch information
neitdung committed Jul 22, 2024
2 parents a773d32 + 9999841 commit 4794d46
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 131 deletions.
2 changes: 1 addition & 1 deletion contracts/consumer/converter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod custom {
}
#[cfg(feature = "fake-custom")]
pub mod custom {
pub type ConverterMsg = mesh_bindings::VirtualStakeCustomMsg;
pub type ConverterMsg = mesh_bindings::MeshCustomMsg;
pub type ConverterQuery = mesh_bindings::VirtualStakeCustomQuery;
pub type Response = cosmwasm_std::Response<ConverterMsg>;
}
Expand Down
2 changes: 1 addition & 1 deletion contracts/consumer/simple-price-feed/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod custom {
}
#[cfg(feature = "fake-custom")]
pub mod custom {
pub type PriceFeedMsg = mesh_bindings::VirtualStakeCustomMsg;
pub type PriceFeedMsg = mesh_bindings::MeshCustomMsg;
pub type PriceFeedQuery = mesh_bindings::VirtualStakeCustomQuery;
pub type Response = cosmwasm_std::Response<PriceFeedMsg>;
}
Expand Down
46 changes: 23 additions & 23 deletions contracts/consumer/virtual-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cw_storage_plus::{Item, Map};
use cw_utils::nonpayable;
use mesh_apis::converter_api::{self, RewardInfo, ValidatorSlashInfo};
use mesh_bindings::{
TokenQuerier, VirtualStakeCustomMsg, VirtualStakeCustomQuery, VirtualStakeMsg,
TokenQuerier, VirtualStakeCustomQuery, MeshCustomMsg,
};
use sylvia::types::{ExecCtx, InstantiateCtx, QueryCtx, ReplyCtx, SudoCtx};
use sylvia::{contract, schemars};
Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct VirtualStakingContract<'a> {
#[sv::error(ContractError)]
#[sv::messages(virtual_staking_api as VirtualStakingApi)]
// FIXME: how to handle custom messages for sudo?
#[sv::custom(query=VirtualStakeCustomQuery, msg=VirtualStakeCustomMsg)]
#[sv::custom(query=VirtualStakeCustomQuery, msg=MeshCustomMsg)]
// #[sv::override_entry_point(sudo=sudo(SudoMsg))] // Disabled because lack of custom query support
impl VirtualStakingContract<'_> {
pub const fn new() -> Self {
Expand All @@ -74,7 +74,7 @@ impl VirtualStakingContract<'_> {
pub fn instantiate(
&self,
ctx: InstantiateCtx<VirtualStakeCustomQuery>,
) -> Result<Response<VirtualStakeCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
nonpayable(&ctx.info)?;
let denom = ctx.deps.querier.query_bonded_denom()?;
let config = Config {
Expand Down Expand Up @@ -134,7 +134,7 @@ impl VirtualStakingContract<'_> {
&self,
ctx: ReplyCtx<VirtualStakeCustomQuery>,
reply: Reply,
) -> Result<Response<VirtualStakeCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
match (reply.id, reply.result.into_result()) {
(REPLY_REWARDS_ID, Ok(_)) => self.reply_rewards(ctx.deps, ctx.env),
(REPLY_REWARDS_ID, Err(e)) => {
Expand All @@ -155,7 +155,7 @@ impl VirtualStakingContract<'_> {
&self,
mut deps: DepsMut<VirtualStakeCustomQuery>,
env: Env,
) -> Result<Response<VirtualStakeCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
const BATCH: ValidatorRewardsBatch = VALIDATOR_REWARDS_BATCH;

// Find the validator to assign the new reward to
Expand Down Expand Up @@ -236,7 +236,7 @@ fn calculate_rebalance(
current: Vec<(String, Uint128)>,
desired: Vec<(String, Uint128)>,
denom: &str,
) -> Vec<CosmosMsg<VirtualStakeCustomMsg>> {
) -> Vec<CosmosMsg<MeshCustomMsg>> {
let mut desired: BTreeMap<_, _> = desired.into_iter().collect();

// this will handle adjustments to all current validators
Expand All @@ -247,12 +247,12 @@ fn calculate_rebalance(
Ordering::Less => {
let unbond = prev - next;
let amount = coin(unbond.u128(), denom);
msgs.push(VirtualStakeMsg::Unbond { validator, amount }.into())
msgs.push(MeshCustomMsg::Unbond { validator, amount }.into())
}
Ordering::Greater => {
let bond = next - prev;
let amount = coin(bond.u128(), denom);
msgs.push(VirtualStakeMsg::Bond { validator, amount }.into())
msgs.push(MeshCustomMsg::Bond { validator, amount }.into())
}
Ordering::Equal => {}
}
Expand All @@ -261,7 +261,7 @@ fn calculate_rebalance(
// any new validators in the desired list need to be bonded
for (validator, bond) in desired {
let amount = coin(bond.u128(), denom);
msgs.push(VirtualStakeMsg::Bond { validator, amount }.into())
msgs.push(MeshCustomMsg::Bond { validator, amount }.into())
}

msgs
Expand Down Expand Up @@ -321,7 +321,7 @@ fn withdraw_reward_msgs<T: CustomQuery>(
deps: DepsMut<T>,
bonded: &[(String, Uint128)],
inactive: &[String],
) -> Vec<SubMsg<VirtualStakeCustomMsg>> {
) -> Vec<SubMsg<MeshCustomMsg>> {
// Filter out inactive validators
let inactive = inactive.iter().collect::<HashSet<_>>();
let bonded = bonded
Expand Down Expand Up @@ -352,7 +352,7 @@ fn withdraw_reward_msgs<T: CustomQuery>(
impl VirtualStakingApi for VirtualStakingContract<'_> {
type Error = ContractError;
type QueryC = VirtualStakeCustomQuery;
type ExecC = VirtualStakeCustomMsg;
type ExecC = MeshCustomMsg;

/// 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.
Expand All @@ -362,7 +362,7 @@ impl VirtualStakingApi for VirtualStakingContract<'_> {
ctx: ExecCtx<VirtualStakeCustomQuery>,
validator: String,
amount: Coin,
) -> Result<Response<VirtualStakeCustomMsg>, Self::Error> {
) -> Result<Response<Self::ExecC>, 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
Expand Down Expand Up @@ -392,7 +392,7 @@ impl VirtualStakingApi for VirtualStakingContract<'_> {
ctx: ExecCtx<VirtualStakeCustomQuery>,
validator: String,
amount: Coin,
) -> Result<Response<VirtualStakeCustomMsg>, Self::Error> {
) -> Result<Response<Self::ExecC>, 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
Expand Down Expand Up @@ -421,7 +421,7 @@ impl VirtualStakingApi for VirtualStakingContract<'_> {
ctx: ExecCtx<VirtualStakeCustomQuery>,
validators: Vec<String>,
amount: Coin,
) -> Result<Response<VirtualStakeCustomMsg>, Self::Error> {
) -> Result<Response<Self::ExecC>, 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
Expand Down Expand Up @@ -494,7 +494,7 @@ impl VirtualStakingApi for VirtualStakingContract<'_> {
fn handle_epoch(
&self,
ctx: SudoCtx<VirtualStakeCustomQuery>,
) -> Result<Response<VirtualStakeCustomMsg>, ContractError> {
) -> Result<Response<Self::ExecC>, ContractError> {
let SudoCtx { mut deps, env, .. } = ctx;

// withdraw rewards
Expand Down Expand Up @@ -574,7 +574,7 @@ impl VirtualStakingApi for VirtualStakingContract<'_> {
unjailed: Option<Vec<String>>,
tombstoned: Option<Vec<String>>,
slashed: Option<Vec<ValidatorSlash>>,
) -> Result<Response<VirtualStakeCustomMsg>, ContractError> {
) -> Result<Response<Self::ExecC>, ContractError> {
let SudoCtx { deps, .. } = ctx;

let additions = &additions.unwrap_or_default();
Expand Down Expand Up @@ -1299,7 +1299,7 @@ mod tests {
deps: DepsMut,
validator: &[&str],
amount: u128,
) -> Result<Response<VirtualStakeCustomMsg>, ContractError>;
) -> Result<Response<MeshCustomMsg>, ContractError>;
fn jail(
&self,
deps: DepsMut,
Expand Down Expand Up @@ -1401,7 +1401,7 @@ mod tests {
deps: DepsMut,
validators: &[&str],
amount: u128,
) -> Result<Response<VirtualStakeCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
let denom = self.config.load(deps.storage).unwrap().denom;

self.burn(
Expand Down Expand Up @@ -1590,20 +1590,20 @@ mod tests {
}

struct HitEpochResult {
virtual_stake_msgs: Vec<VirtualStakeMsg>,
virtual_stake_msgs: Vec<MeshCustomMsg>,
withdraw_reward_msgs: Vec<String>,
}

impl HitEpochResult {
fn new(data: Response<VirtualStakeCustomMsg>) -> Self {
fn new(data: Response<MeshCustomMsg>) -> Self {
use itertools::Either;
use itertools::Itertools as _;

let (virtual_stake_msgs, withdraw_reward_msgs) = data
.messages
.into_iter()
.partition_map(|SubMsg { msg, .. }| {
if let CosmosMsg::Custom(VirtualStakeCustomMsg::VirtualStake(msg)) = msg {
if let CosmosMsg::Custom(msg) = msg {
Either::Left(msg)
} else if let CosmosMsg::Distribution(
DistributionMsg::WithdrawDelegatorReward { validator },
Expand Down Expand Up @@ -1637,7 +1637,7 @@ mod tests {
self.virtual_stake_msgs
.iter()
.filter_map(|msg| {
if let VirtualStakeMsg::Bond { amount, validator } = msg {
if let MeshCustomMsg::Bond { amount, validator } = msg {
Some((
validator.as_str(),
(amount.amount.u128(), amount.denom.as_str()),
Expand All @@ -1653,7 +1653,7 @@ mod tests {
self.virtual_stake_msgs
.iter()
.filter_map(|msg| {
if let VirtualStakeMsg::Unbond { amount, validator } = msg {
if let MeshCustomMsg::Unbond { amount, validator } = msg {
Some((
validator.as_str(),
(amount.amount.u128(), amount.denom.as_str()),
Expand Down
2 changes: 1 addition & 1 deletion contracts/consumer/virtual-staking/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const JUNO: &str = "ujuno";

// Trying to figure out how to work with the generic types
type MtApp = cw_multi_test::BasicApp<
mesh_bindings::VirtualStakeCustomMsg,
mesh_bindings::MeshCustomMsg,
mesh_bindings::VirtualStakeCustomQuery,
>;
type App = sylvia::multitest::App<MtApp>;
Expand Down
24 changes: 12 additions & 12 deletions contracts/provider/vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use mesh_apis::local_staking_api::{
sv::LocalStakingApiQueryMsg, LocalStakingApiHelper, SlashRatioResponse,
};
use mesh_apis::vault_api::{self, SlashInfo, VaultApi};
use mesh_bindings::{ProviderCustomMsg, ProviderMsg};
use mesh_bindings::MeshCustomMsg;
use mesh_sync::Tx::InFlightStaking;
use mesh_sync::{max_range, ValueRange};

Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct VaultContract<'a> {
#[sv::error(ContractError)]
#[sv::messages(vault_api as VaultApi)]
/// Workaround for lack of support in communication `Empty` <-> `Custom` Contracts.
#[sv::custom(msg=ProviderCustomMsg)]
#[sv::custom(msg=MeshCustomMsg)]
impl VaultContract<'_> {
pub fn new() -> Self {
Self {
Expand All @@ -94,7 +94,7 @@ impl VaultContract<'_> {
ctx: InstantiateCtx,
denom: String,
local_staking: Option<LocalStakingInfo>,
) -> Result<Response<ProviderCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
nonpayable(&ctx.info)?;

let config = Config { denom };
Expand Down Expand Up @@ -143,7 +143,7 @@ impl VaultContract<'_> {
}

#[sv::msg(exec)]
fn bond(&self, ctx: ExecCtx, amount: Coin) -> Result<Response<ProviderCustomMsg>, ContractError> {
fn bond(&self, ctx: ExecCtx, amount: Coin) -> Result<Response<MeshCustomMsg>, ContractError> {
nonpayable(&ctx.info)?;

let denom = self.config.load(ctx.deps.storage)?.denom;
Expand All @@ -156,7 +156,7 @@ impl VaultContract<'_> {
user.collateral += amount.amount;
self.users.save(ctx.deps.storage, &ctx.info.sender, &user)?;
let amt = amount.amount;
let msg = ProviderMsg::Bond { delegator: ctx.info.sender.clone().into_string(), amount};
let msg = MeshCustomMsg::Deposit { delegator: ctx.info.sender.clone().into_string(), amount};
let resp = Response::new()
.add_message(msg)
.add_attribute("action", "unbond")
Expand All @@ -167,7 +167,7 @@ impl VaultContract<'_> {
}

#[sv::msg(exec)]
fn unbond(&self, ctx: ExecCtx, amount: Coin) -> Result<Response<ProviderCustomMsg>, ContractError> {
fn unbond(&self, ctx: ExecCtx, amount: Coin) -> Result<Response<MeshCustomMsg>, ContractError> {
nonpayable(&ctx.info)?;

let denom = self.config.load(ctx.deps.storage)?.denom;
Expand All @@ -187,7 +187,7 @@ impl VaultContract<'_> {
user.collateral -= amount.amount;
self.users.save(ctx.deps.storage, &ctx.info.sender, &user)?;
let amt = amount.amount;
let msg = ProviderMsg::Unbond { delegator: ctx.info.sender.clone().into_string(), amount};
let msg = MeshCustomMsg::Withdraw { delegator: ctx.info.sender.clone().into_string(), amount};
let resp = Response::new()
.add_message(msg)
.add_attribute("action", "unbond")
Expand All @@ -208,7 +208,7 @@ impl VaultContract<'_> {
amount: Coin,
// action to take with that stake
msg: Binary,
) -> Result<Response<ProviderCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
nonpayable(&ctx.info)?;

let config = self.config.load(ctx.deps.storage)?;
Expand Down Expand Up @@ -255,7 +255,7 @@ impl VaultContract<'_> {
amount: Coin,
// action to take with that stake
msg: Binary,
) -> Result<Response<ProviderCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
nonpayable(&ctx.info)?;

let config = self.config.load(ctx.deps.storage)?;
Expand Down Expand Up @@ -492,7 +492,7 @@ impl VaultContract<'_> {
}

#[sv::msg(reply)]
fn reply(&self, ctx: ReplyCtx, reply: Reply) -> Result<Response<ProviderCustomMsg>, ContractError> {
fn reply(&self, ctx: ReplyCtx, reply: Reply) -> Result<Response<MeshCustomMsg>, ContractError> {
match reply.id {
REPLY_ID_INSTANTIATE => self.reply_init_callback(ctx.deps, reply.result.unwrap()),
_ => Err(ContractError::InvalidReplyId(reply.id)),
Expand All @@ -503,7 +503,7 @@ impl VaultContract<'_> {
&self,
deps: DepsMut,
reply: SubMsgResponse,
) -> Result<Response<ProviderCustomMsg>, ContractError> {
) -> Result<Response<MeshCustomMsg>, ContractError> {
let init_data = parse_instantiate_response_data(&reply.data.unwrap())?;
let local_staking = Addr::unchecked(init_data.contract_address);

Expand Down Expand Up @@ -983,7 +983,7 @@ impl Default for VaultContract<'_> {

impl VaultApi for VaultContract<'_> {
type Error = ContractError;
type ExecC = ProviderCustomMsg;
type ExecC = MeshCustomMsg;

/// This must be called by the remote staking contract to release this claim
fn release_cross_stake(
Expand Down
2 changes: 1 addition & 1 deletion packages/bindings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod msg;
mod query;

pub use msg::{VirtualStakeCustomMsg, VirtualStakeMsg, ProviderCustomMsg, ProviderMsg};
pub use msg::MeshCustomMsg;
pub use query::{
BondStatusResponse, SlashRatioResponse, TokenQuerier, VirtualStakeCustomQuery,
VirtualStakeQuery,
Expand Down
Loading

0 comments on commit 4794d46

Please sign in to comment.