Skip to content

Commit

Permalink
implement execute_instant_unbondings
Browse files Browse the repository at this point in the history
  • Loading branch information
jununifi committed Dec 8, 2023
1 parent cdbfcaf commit 9d47467
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
7 changes: 6 additions & 1 deletion contracts/strategy-osmosis/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use crate::error::ContractError;
use crate::execute::epoch::epoch::execute_epoch;
use crate::execute::stake::execute_stake;
use crate::execute::superfluid::execute_superfluid_delegate;
use crate::execute::unstake::{execute_unstake, execute_update_unbonding_recipients};
use crate::execute::unstake::{
execute_instant_unbondings, execute_unstake, execute_update_unbonding_recipients,
};
use crate::execute::update_params::execute_update_params;
use crate::msgs::{ExecuteMsg, InstantiateMsg, MigrateMsg, Phase, PhaseStep, QueryMsg};
use crate::query::amounts::query_amounts;
Expand Down Expand Up @@ -142,6 +144,9 @@ pub fn execute(
ExecuteMsg::UpdateLegacyUnbondingRecipients(msg) => {
execute_update_unbonding_recipients(deps, env, info, msg)
}
ExecuteMsg::ProcessInstantUnbondings(msg) => {
execute_instant_unbondings(deps, env, info, msg)
}
}
}

Expand Down
59 changes: 57 additions & 2 deletions contracts/strategy-osmosis/src/execute/unstake.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::error::{ContractError, NoDeposit};
use crate::msgs::UpdateLegacyUnbondingRecipientsMsg;
use crate::msgs::{ProcessInstantUnbondingsMsg, UpdateLegacyUnbondingRecipientsMsg};
use crate::query::unbondings::{query_unbondings, UNBONDING_ITEM_LIMIT};
use crate::state::{
DepositInfo, Unbonding, DEPOSITS, HOST_LP_RATE_MULTIPLIER, PARAMS, STAKE_RATE_MULTIPLIER,
STATE, UNBONDINGS,
};

use cosmwasm_std::{DepsMut, Env, MessageInfo, Response, StdResult, Uint128};
use cosmwasm_std::{
coins, BankMsg, CosmosMsg, DepsMut, Env, MessageInfo, Response, StdResult, Uint128,
};
use strategy::v1::msgs::UnstakeMsg;
use ununifi_binding::v1::binding::UnunifiMsg;

Expand Down Expand Up @@ -114,3 +116,56 @@ pub fn execute_update_unbonding_recipients(
let rsp = Response::new().add_attribute("action", "update_unbonding");
Ok(rsp)
}

pub fn execute_instant_unbondings(
deps: DepsMut,
_: Env,
info: MessageInfo,
msg: ProcessInstantUnbondingsMsg,
) -> Result<Response<UnunifiMsg>, ContractError> {
let params = PARAMS.load(deps.storage)?;
if info.sender != params.authority {
return Err(ContractError::Unauthorized {});
}

let mut state = STATE.load(deps.storage)?;
let mut rsp = Response::new().add_attribute("action", "instant_unbondings");
for update in msg.unbondings {
let unbonding = UNBONDINGS.load(deps.storage, update.unbonding_id)?;
// decrease total unbonding lp amount
state.unbonding_lp_amount -= unbonding.amount;

// remove unbonding
UNBONDINGS.remove(deps.storage, update.unbonding_id);

// update the share amount on the sender
let share_receiver = deps.api.addr_validate(update.share_receiver.as_str())?;
DEPOSITS.update(
deps.storage,
share_receiver.to_string(),
|deposit: Option<DepositInfo>| -> StdResult<_> {
if let Some(unwrapped) = deposit {
return Ok(DepositInfo {
sender: share_receiver.clone(),
share: unwrapped.share.checked_add(update.share_recover_amount)?,
});
}
Ok(DepositInfo {
sender: share_receiver.clone(),
share: update.share_recover_amount,
})
},
)?;

// token send to unbonding recipient
let bank_send_msg = CosmosMsg::Bank(BankMsg::Send {
to_address: unbonding.sender.to_string(),
amount: coins(update.withdrawal.u128(), &params.controller_deposit_denom),
});
rsp = rsp.add_message(bank_send_msg)
}

STATE.save(deps.storage, &state)?;

Ok(rsp)
}
15 changes: 14 additions & 1 deletion contracts/strategy-osmosis/src/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum ExecuteMsg {
SuperfluidDelegate(SuperfluidDelegateMsg),
Epoch(EpochMsg),
UpdateLegacyUnbondingRecipients(UpdateLegacyUnbondingRecipientsMsg),
ProcessInstantUnbondings(ProcessInstantUnbondingsMsg),
}

#[cw_serde]
Expand Down Expand Up @@ -61,10 +62,22 @@ pub struct UpdateUnbondingRecipient {

#[cw_serde]
pub struct UpdateLegacyUnbondingRecipientsMsg {
pub authority: Option<String>,
pub updates: Vec<UpdateUnbondingRecipient>,
}

#[cw_serde]
pub struct InstantUnbonding {
pub unbonding_id: u64,
pub withdrawal: Uint128,
pub share_recover_amount: Uint128,
pub share_receiver: String,
}

#[cw_serde]
pub struct ProcessInstantUnbondingsMsg {
pub unbondings: Vec<InstantUnbonding>,
}

#[cw_serde]
pub struct IcqBalanceCallbackMsg {
pub coins: Vec<Coin>,
Expand Down

0 comments on commit 9d47467

Please sign in to comment.