Skip to content

Commit

Permalink
Merge pull request #199 from decentrio/feat/transition-stake
Browse files Browse the repository at this point in the history
feat: transition stake
  • Loading branch information
trinitys7 authored Sep 5, 2024
2 parents d9e27c8 + 8292d43 commit cc54d3d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 7 deletions.
2 changes: 1 addition & 1 deletion contracts/consumer/virtual-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl VirtualStakingApi for VirtualStakingContract<'_> {
if max_cap.is_zero() {
let all_delegations = TokenQuerier::new(&deps.querier)
.all_delegations(env.contract.address.to_string(), config.max_retrieve)?;
if all_delegations.delegations.len() == 0 {
if all_delegations.delegations.is_empty() {
return Ok(resp.add_message(VirtualStakeMsg::DeleteAllScheduledTasks {}));
}
let mut msgs = vec![];
Expand Down
8 changes: 4 additions & 4 deletions contracts/provider/vault/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ library = []
mt = ["library", "sylvia/mt"]

[dependencies]
mesh-apis = { workspace = true }
mesh-sync = { workspace = true }
mesh-bindings = { workspace = true }
mesh-apis = { workspace = true }
mesh-sync = { workspace = true }
mesh-bindings = { workspace = true }
mesh-native-staking = { workspace = true }

sylvia = { workspace = true }
cosmwasm-schema = { workspace = true }
Expand All @@ -41,7 +42,6 @@ test-case = { workspace = true }
derivative = { workspace = true }
anyhow = { workspace = true }
mesh-external-staking = { workspace = true, features = ["mt"] }
mesh-native-staking = { workspace = true, features = ["mt"] }
mesh-native-staking-proxy = { workspace = true, features = ["mt"] }

[[bin]]
Expand Down
59 changes: 57 additions & 2 deletions contracts/provider/vault/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
coin, ensure, Addr, Binary, Coin, Decimal, DepsMut, Fraction, Order, Reply, Response,
StdResult, Storage, SubMsg, SubMsgResponse, Uint128, WasmMsg,
coin, ensure, to_json_binary, Addr, Binary, Coin, Decimal, DepsMut, Fraction, Order, Reply,
Response, StdResult, Storage, SubMsg, SubMsgResponse, Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw_storage_plus::{Bounder, Item, Map};
Expand Down Expand Up @@ -212,6 +212,61 @@ impl VaultContract<'_> {
Ok(resp)
}

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

let denom = self.config.load(ctx.deps.storage)?.denom;
ensure!(denom == amount.denom, ContractError::UnexpectedDenom(denom));

let mut user = self
.users
.may_load(ctx.deps.storage, &ctx.info.sender)?
.unwrap_or_default();
user.collateral += amount.amount;
self.users.save(ctx.deps.storage, &ctx.info.sender, &user)?;

let amt = amount.amount;
let mut resp = Response::new()
.add_attribute("action", "restake")
.add_attribute("sender", ctx.info.sender.clone().into_string())
.add_attribute("amount", amt.to_string());
let restake_msg = ProviderMsg::Restake {
delegator: ctx.info.sender.clone().into_string(),
validator: validator.clone(),
amount: amount.clone(),
};
resp = resp.add_message(restake_msg);

let config = self.config.load(ctx.deps.storage)?;
if let Some(local_staking) = self.local_staking.load(ctx.deps.storage)? {
self.stake(
&mut ctx,
&config,
&local_staking.contract.0,
local_staking.max_slash,
amount.clone(),
false,
)?;

let stake_msg = local_staking.contract.receive_stake(
ctx.info.sender.to_string(),
to_json_binary(&mesh_native_staking::msg::StakeMsg { validator }).unwrap(),
vec![amount],
)?;

resp = resp.add_message(stake_msg);
Ok(resp)
} else {
Err(ContractError::NoLocalStaking)
}
}

/// This assigns a claim of amount tokens to the remote contract, which can take some action with it
#[sv::msg(exec)]
fn stake_remote(
Expand Down
27 changes: 27 additions & 0 deletions packages/bindings/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ pub enum ProviderMsg {
validator: String,
amount: Coin,
},
/// Restake 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 restake
/// amount.amount tokens from staking module to local staking contract.
Restake {
delegator: String,
validator: String,
amount: Coin,
},
}

impl ProviderMsg {
Expand Down Expand Up @@ -171,6 +181,23 @@ impl ProviderMsg {
amount: coin,
}
}

pub fn restake(
denom: &str,
delegator: &str,
validator: &str,
amount: impl Into<Uint128>,
) -> ProviderMsg {
let coin = Coin {
amount: amount.into(),
denom: denom.into(),
};
ProviderMsg::Restake {
delegator: delegator.to_string(),
validator: validator.to_string(),
amount: coin,
}
}
}

impl From<ProviderMsg> for CosmosMsg<ProviderCustomMsg> {
Expand Down

0 comments on commit cc54d3d

Please sign in to comment.