Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Audit] I-8 - Avoid unecessary CPIs when amounts=0 #322

Merged
merged 6 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,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 @@ -382,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
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
24 changes: 15 additions & 9 deletions programs/uxd/src/instructions/rebalance_alloyx_vault_depository.rs
Original file line number Diff line number Diff line change
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
Loading