Skip to content

Commit

Permalink
Merge branch 'develop' into vbrunet/2023_12_23-audit-i5
Browse files Browse the repository at this point in the history
  • Loading branch information
crypto-vincent authored Jan 1, 2024
2 parents d201ce6 + 14eba90 commit e0108af
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::error::UxdError;
use crate::events::CollectProfitsOfCredixLpDepositoryEvent;
use crate::state::controller::Controller;
use crate::state::credix_lp_depository::CredixLpDepository;
use crate::utils::checked_as_u64;
use crate::utils::compute_decrease;
use crate::utils::compute_increase;
use crate::utils::compute_shares_amount_for_value_floor;
Expand Down Expand Up @@ -178,37 +179,30 @@ pub(crate) fn handler(ctx: Context<CollectProfitsOfCredixLpDepository>) -> Resul
)?;

// How much collateral can we withdraw as profits
let profits_value: u128 = {
let profits_value = {
// Compute the set of liabilities owed to the users
let liabilities_value: u128 = ctx
.accounts
.depository
.load()?
.redeemable_amount_under_management;
let liabilities_value = checked_as_u64(
ctx.accounts
.depository
.load()?
.redeemable_amount_under_management,
)?;
msg!(
"[collect_profits_of_credix_lp_depository:liabilities_value:{}]",
liabilities_value
);
// Compute the set of assets owned in the LP
let assets_value: u128 = owned_shares_value_before.into();
let assets_value = owned_shares_value_before;
msg!(
"[collect_profits_of_credix_lp_depository:assets_value:{}]",
assets_value
);
// Compute the amount of profits that we can safely withdraw
assets_value
.checked_sub(liabilities_value)
.ok_or(UxdError::MathOverflow)?
assets_value.saturating_sub(liabilities_value)
};
msg!(
"[collect_profits_of_credix_lp_depository:profits_value:{}]",
profits_value
);

// Assumes and enforce a collateral/redeemable 1:1 relationship on purpose
let collateral_amount_before_precision_loss: u64 = u64::try_from(profits_value)
.ok()
.ok_or(UxdError::MathOverflow)?;
let collateral_amount_before_precision_loss: u64 = profits_value;
msg!(
"[collect_profits_of_credix_lp_depository:collateral_amount_before_precision_loss:{}]",
collateral_amount_before_precision_loss
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::state::mercurial_vault_depository::MercurialVaultDepository;
use crate::utils::calculate_router_depositories_target_redeemable_amount;
use crate::utils::checked_add;
use crate::utils::checked_as_u64;
use crate::utils::checked_sub;
use crate::utils::compute_value_for_shares_amount_floor;
use crate::validate_is_program_frozen;
use crate::CONTROLLER_NAMESPACE;
Expand Down Expand Up @@ -183,7 +182,7 @@ pub(crate) fn handler(
total_shares_supply,
total_shares_value,
)?;
checked_sub(owned_shares_value, redeemable_amount_under_management)?
owned_shares_value.saturating_sub(redeemable_amount_under_management)
};

let overflow_value = {
Expand All @@ -196,14 +195,8 @@ pub(crate) fn handler(
&ctx.accounts.alloyx_vault_depository,
)?
.credix_lp_depository_target_redeemable_amount;
if redeemable_amount_under_management < redeemable_amount_under_management_target_amount {
0
} else {
checked_sub(
redeemable_amount_under_management,
redeemable_amount_under_management_target_amount,
)?
}
redeemable_amount_under_management
.saturating_sub(redeemable_amount_under_management_target_amount)
};

// ---------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@ pub(crate) fn handler(
.redeemable_amount_under_management,
)?;

let profits_collateral_amount = checked_sub(
owned_shares_value_before,
redeemable_amount_under_management,
)?;
let profits_collateral_amount =
owned_shares_value_before.saturating_sub(redeemable_amount_under_management);
msg!(
"[rebalance_redeem_withdraw_request_from_credix_lp_depository:profits_collateral_amount:{}]",
profits_collateral_amount
Expand All @@ -283,14 +281,8 @@ pub(crate) fn handler(
&ctx.accounts.alloyx_vault_depository,
)?
.credix_lp_depository_target_redeemable_amount;
if redeemable_amount_under_management < redeemable_amount_under_management_target_amount {
0
} else {
checked_sub(
redeemable_amount_under_management,
redeemable_amount_under_management_target_amount,
)?
}
redeemable_amount_under_management
.saturating_sub(redeemable_amount_under_management_target_amount)
};
msg!(
"[rebalance_redeem_withdraw_request_from_credix_lp_depository:overflow_value:{}]",
Expand Down Expand Up @@ -350,18 +342,18 @@ pub(crate) fn handler(
total_shares_value_before,
)?;

if withdrawal_total_shares_amount == 0 {
msg!("[rebalance_redeem_withdraw_request_from_credix_lp_depository:no_withdrawable_liquidity]",);
return Ok(());
}

let withdrawal_total_collateral_amount_after_precision_loss =
compute_value_for_shares_amount_floor(
withdrawal_total_shares_amount,
total_shares_supply_before,
total_shares_value_before,
)?;

if withdrawal_total_collateral_amount_after_precision_loss == 0 {
msg!("[rebalance_redeem_withdraw_request_from_credix_lp_depository:no_withdrawable_liquidity]",);
return Ok(());
}

// Precision loss should be taken from the profits, not the overflow
// Otherwise this means that the precision loss would take out of the backing value
let withdrawal_overflow_value_after_precision_loss = withdrawal_overflow_value;
Expand Down Expand Up @@ -390,23 +382,27 @@ pub(crate) fn handler(
"[rebalance_redeem_withdraw_request_from_credix_lp_depository:profits_beneficiary_collateral_transfer:{}]",
withdrawal_profits_collateral_amount_after_precision_loss
);
token::transfer(
ctx.accounts
.into_transfer_depository_collateral_to_profits_beneficiary_collateral_context()
.with_signer(depository_pda_signer),
withdrawal_profits_collateral_amount_after_precision_loss,
)?;
if withdrawal_profits_collateral_amount_after_precision_loss > 0 {
token::transfer(
ctx.accounts
.into_transfer_depository_collateral_to_profits_beneficiary_collateral_context()
.with_signer(depository_pda_signer),
withdrawal_profits_collateral_amount_after_precision_loss,
)?;
}

msg!(
"[rebalance_redeem_withdraw_request_from_credix_lp_depository:identity_depository_collateral_transfer:{}]",
withdrawal_overflow_value_after_precision_loss
);
token::transfer(
ctx.accounts
.into_transfer_depository_collateral_to_identity_depository_collateral_context()
.with_signer(depository_pda_signer),
withdrawal_overflow_value_after_precision_loss,
)?;
if withdrawal_overflow_value_after_precision_loss > 0 {
token::transfer(
ctx.accounts
.into_transfer_depository_collateral_to_identity_depository_collateral_context()
.with_signer(depository_pda_signer),
withdrawal_overflow_value_after_precision_loss,
)?;
}

// Refresh account states after withdrawal
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,16 @@ pub(crate) fn handler(ctx: Context<CollectProfitsOfMercurialVaultDepository>) ->
]];

// 5 - withdraw collateral from mercurial vault for LP tokens
mercurial_vault::cpi::withdraw(
ctx.accounts
.into_withdraw_collateral_from_mercurial_vault_context()
.with_signer(depository_signer_seed),
lp_token_amount_to_match_collectable_profits_value,
// Do not check slippage here
0,
)?;
if lp_token_amount_to_match_collectable_profits_value > 0 {
mercurial_vault::cpi::withdraw(
ctx.accounts
.into_withdraw_collateral_from_mercurial_vault_context()
.with_signer(depository_signer_seed),
lp_token_amount_to_match_collectable_profits_value,
// Do not check slippage here
0,
)?;
}

// 6 - Reload accounts impacted by the withdraw (We need updated numbers for further calculation)
ctx.accounts.depository_lp_token_vault.reload()?;
Expand Down Expand Up @@ -251,9 +253,7 @@ impl<'info> CollectProfitsOfMercurialVaultDepository<'info> {
.ok()
.ok_or(UxdError::MathOverflow)?;

Ok(owned_lp_tokens_value
.checked_sub(redeemable_amount_under_management)
.ok_or(UxdError::MathOverflow)?)
Ok(owned_lp_tokens_value.saturating_sub(redeemable_amount_under_management))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,16 @@ pub(crate) fn handler(
)?;

// 3 - Redeem collateral from mercurial vault for lp tokens
mercurial_vault::cpi::withdraw(
ctx.accounts
.into_withdraw_collateral_from_mercurial_vault_context()
.with_signer(depository_signer_seed),
lp_token_amount_to_match_collateral_amount_less_fees,
// Do not check slippage here
0,
)?;
if lp_token_amount_to_match_collateral_amount_less_fees > 0 {
mercurial_vault::cpi::withdraw(
ctx.accounts
.into_withdraw_collateral_from_mercurial_vault_context()
.with_signer(depository_signer_seed),
lp_token_amount_to_match_collateral_amount_less_fees,
// Do not check slippage here
0,
)?;
}

// 4 - Reload accounts impacted by the withdraw (We need updated numbers for further calculation)
ctx.accounts.depository_lp_token_vault.reload()?;
Expand Down
28 changes: 17 additions & 11 deletions programs/uxd/src/instructions/rebalance_alloyx_vault_depository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ pub(crate) fn handler(ctx: Context<RebalanceAlloyxVaultDepository>, vault_id: &s
alloyx_vault_depository.redeemable_amount_under_management,
deposited_underflow_collateral,
)?;
// If we deposited, we can stop here
return Ok(());
}
// If we deposited, we can stop here (no need to withdraw)
return Ok(());
}

// ---------------------------------------------------------------------
Expand Down Expand Up @@ -406,15 +406,18 @@ impl<'info> RebalanceAlloyxVaultDepository<'info> {
collateral_mint.as_ref(),
&[self.alloyx_vault_depository.load()?.bump],
]];

token::transfer(
self.into_transfer_identity_depository_collateral_to_alloyx_vault_depository_collateral_context()
.with_signer(identity_depository_pda_signer),
collateral_amount_after_precision_loss,
)?;
token::transfer(
self.into_transfer_payer_collateral_to_alloyx_vault_depository_collateral_context(),
collateral_amount_delta_precision_loss,
)?;
)?;
if collateral_amount_delta_precision_loss > 0 {
token::transfer(
self.into_transfer_payer_collateral_to_alloyx_vault_depository_collateral_context(),
collateral_amount_delta_precision_loss,
)?;
}
alloyx_cpi::cpi::deposit(
self.into_deposit_alloyx_vault_depository_to_alloyx_vault_context()
.with_signer(alloyx_vault_depository_pda_signer),
Expand Down Expand Up @@ -580,16 +583,19 @@ impl<'info> RebalanceAlloyxVaultDepository<'info> {
collateral_mint.as_ref(),
&[self.alloyx_vault_depository.load()?.bump],
]];

alloyx_cpi::cpi::withdraw(
self.into_withdraw_from_alloyx_vault_to_alloyx_vault_depository_collateral_context()
.with_signer(alloyx_vault_depository_pda_signer),
vault_id.to_owned(),
shares_amount,
)?;
token::transfer(
self.into_transfer_payer_collateral_to_identity_depository_collateral_context(),
collateral_amount_delta_precision_loss,
)?;
if collateral_amount_delta_precision_loss > 0 {
token::transfer(
self.into_transfer_payer_collateral_to_identity_depository_collateral_context(),
collateral_amount_delta_precision_loss,
)?;
}
token::transfer(
self.into_transfer_alloyx_vault_depository_collateral_to_identity_depository_collateral_context()
.with_signer(alloyx_vault_depository_pda_signer),
Expand Down
2 changes: 1 addition & 1 deletion programs/uxd/src/instructions/redeem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub(crate) fn handler(ctx: Context<Redeem>, redeemable_amount: u64) -> Result<()
let new_epoch_outflow_amount = {
// How long ago was the last outflow
let last_outflow_elapsed_slots = std::cmp::min(
checked_as_u64(checked_sub(current_slot, controller.last_outflow_slot)?)?,
checked_sub(current_slot, controller.last_outflow_slot)?,
controller.slots_per_epoch,
);
// How much was unlocked by waiting since last redeem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,13 @@ pub fn calculate_depositories_mint_collateral_amount(
.iter()
.map(|depository| {
if !depository.directly_mintable {
return Ok(0);
return 0;
}
if depository.target_redeemable_amount <= depository.redeemable_amount_under_management
{
return Ok(0);
}
checked_sub(
depository.target_redeemable_amount,
depository.redeemable_amount_under_management,
)
depository
.target_redeemable_amount
.saturating_sub(depository.redeemable_amount_under_management)
})
.collect::<Result<Vec<u64>>>()?;
.collect::<Vec<u64>>();

let total_under_target_redeemable_amount =
calculate_depositories_sum_value(&depositories_under_target_redeemable_amount)?;
Expand All @@ -63,23 +58,17 @@ pub fn calculate_depositories_mint_collateral_amount(
.iter()
.map(|depository| {
if !depository.directly_mintable {
return Ok(0);
return 0;
}
let depository_after_target_redeemable_amount = std::cmp::max(
depository.redeemable_amount_under_management,
depository.target_redeemable_amount,
);
if depository.redeemable_amount_under_management_cap
<= depository_after_target_redeemable_amount
{
return Ok(0);
}
checked_sub(
depository.redeemable_amount_under_management_cap,
depository_after_target_redeemable_amount,
)
depository
.redeemable_amount_under_management_cap
.saturating_sub(depository_after_target_redeemable_amount)
})
.collect::<Result<Vec<u64>>>()?;
.collect::<Vec<u64>>();

let total_under_cap_redeemable_amount =
calculate_depositories_sum_value(&depositories_under_cap_redeemable_amount)?;
Expand Down
Loading

0 comments on commit e0108af

Please sign in to comment.