Skip to content

Commit

Permalink
Provide unbond handler address to liquidate when handling liquidations
Browse files Browse the repository at this point in the history
  • Loading branch information
kerber0x committed May 9, 2022
1 parent c7ca901 commit 963814f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 20 deletions.
17 changes: 6 additions & 11 deletions contracts/luna-vault/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
attr, from_binary, to_binary, Addr, Binary, CosmosMsg, Decimal, Deps, DepsMut, Env,
MessageInfo, Order, ReplyOn, Response, SubMsg, Uint128, WasmMsg,
MessageInfo, ReplyOn, Response, SubMsg, Uint128, WasmMsg,
};
use cw20::{Cw20ExecuteMsg, Cw20ReceiveMsg};
use terraswap::asset::{Asset, AssetInfo};
Expand All @@ -23,7 +23,6 @@ use crate::queries::{query_unbond_handler_expiration_time, query_withdrawable_un
use crate::state::{
UnbondDataCache, ADMIN, DEPOSIT_INFO, FEE, POOL_INFO, PROFIT, STATE, UNBOND_CACHE,
UNBOND_HANDLERS_ASSIGNED, UNBOND_HANDLERS_AVAILABLE, UNBOND_HANDLER_EXPIRATION_TIMES,
UNBOND_HANDLER_EXPIRATION_TIMES_READ_LIMIT,
};

/// handler function invoked when the luna-vault contract receives
Expand Down Expand Up @@ -418,18 +417,14 @@ pub fn withdraw_unbonded(
deps: DepsMut,
msg_info: MessageInfo,
liquidation: bool,
liquidate_addr: Option<String>,
) -> VaultResult<Response> {
let unbond_handler = if liquidation {
let (index, _) = UNBOND_HANDLER_EXPIRATION_TIMES
.prefix(())
.range(deps.storage, None, None, Order::Ascending)
.take(UNBOND_HANDLER_EXPIRATION_TIMES_READ_LIMIT as usize)
.find_position(|&item| *item?.1 > env.block.time.seconds())
.ok_or(LunaVaultError::NoExpiredUnbondHandlers {})?;

UNBOND_HANDLER_EXPIRATION_TIMES[index]
// validate liquidate_addr
let liquidate_addr = liquidate_addr.ok_or(LunaVaultError::UnbondHandlerError {})?;
deps.api.addr_validate(&liquidate_addr)?
} else {
// get handler assigned to user
// get unbond handler assigned to user
UNBOND_HANDLERS_ASSIGNED
.may_load(deps.storage, msg_info.sender.clone())?
.ok_or(LunaVaultError::NoUnbondHandlerAssigned {})?
Expand Down
8 changes: 4 additions & 4 deletions contracts/luna-vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn execute(
ExecuteMsg::ProvideLiquidity { asset } => {
commands::provide_liquidity(deps, env, info, asset)
}
ExecuteMsg::WithdrawUnbonded {} => commands::withdraw_unbonded(deps, info, false),
ExecuteMsg::WithdrawUnbonded {} => commands::withdraw_unbonded(deps, info, false, None),
ExecuteMsg::SetAdmin { admin } => commands::set_admin(deps, info, admin),
ExecuteMsg::SetFee {
flash_loan_fee,
Expand Down Expand Up @@ -213,9 +213,9 @@ pub fn execute(
),
ExecuteMsg::Callback(msg) => flashloan::_handle_callback(deps, env, info, msg),
ExecuteMsg::UnbondHandler(msg) => commands::handle_unbond_handler_msg(deps, info, msg),
ExecuteMsg::LiquidateExpiredUnbondHandler {} => {
commands::withdraw_unbonded(deps, info, true)
}
ExecuteMsg::LiquidateExpiredUnbondHandler {
liquidate_unbond_handler_addr,
} => commands::withdraw_unbonded(deps, info, true, Some(liquidate_unbond_handler_addr)),
}
}

Expand Down
3 changes: 0 additions & 3 deletions contracts/luna-vault/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ pub enum LunaVaultError {
#[error("Couldn't get unbond handler.")]
UnbondHandlerError {},

#[error("No expired unbond handlers found.")]
NoExpiredUnbondHandlers {},

#[error("Last balance is non-zero, you can only call this function once.")]
Nonzero {},
}
Expand Down
6 changes: 4 additions & 2 deletions packages/white_whale/src/luna_vault/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ pub enum ExecuteMsg {
Callback(CallbackMsg),
/// Messages sent by unbond handlers to the vault
UnbondHandler(UnbondHandlerMsg),
/// Liquidates expired unbond handlers if any
LiquidateExpiredUnbondHandler {},
/// Liquidates the given unbond handler addr if it's expired
LiquidateExpiredUnbondHandler {
liquidate_unbond_handler_addr: String,
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down

0 comments on commit 963814f

Please sign in to comment.