Skip to content

Commit

Permalink
VLT-273. add strategy max debt validation for direct deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
vito-kovalione committed Dec 27, 2024
1 parent 669d33e commit 16d5f8d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions programs/tokenized_vault/src/instructions/direct_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use strategy::program::Strategy;

use crate::constants::{SHARES_SEED, STRATEGY_DATA_SEED, UNDERLYING_SEED, ONE_SHARE_TOKEN, USER_DATA_SEED};

use crate::errors::ErrorCode;
use crate::events::{VaultDepositEvent, UpdatedCurrentDebtForStrategyEvent};
use crate::state::{UserData, Vault, StrategyData};
use crate::utils::{accountant, strategy as strategy_utils, token, vault};
Expand Down Expand Up @@ -117,6 +118,16 @@ pub fn handle_direct_deposit<'info>(ctx: Context<'_, '_, '_, 'info, DirectDeposi
amount_to_deposit
)?;

let new_debt = ctx.accounts.strategy_data.current_debt + amount;
if new_debt > ctx.accounts.strategy_data.max_debt {
return Err(ErrorCode::DebtHigherThanMaxDebt.into());
}

let max_strategy_deposit = strategy_utils::get_max_deposit(&ctx.accounts.strategy.to_account_info())?;
if amount > max_strategy_deposit {
return Err(ErrorCode::ExceedDepositLimit.into());
}

let mut shares = ctx.accounts.vault.load()?.convert_to_shares(amount_to_deposit);

token::transfer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1478,7 +1478,7 @@ describe("Vault User Operations: Direct Deposit Tests", () => {
.signers([nonVerifiedUser])
.rpc();
} catch (err) {
expect(err.message).to.contain(errorStrings.maxDepositReached);
expect(err.message).to.contain(errorStrings.exceedDepositLimit);
}

await validateDirectDeposit({
Expand All @@ -1498,7 +1498,7 @@ describe("Vault User Operations: Direct Deposit Tests", () => {
});
});

it.skip("Directly depositing more than strategy max debt into direct deposit enabled vault should revert", async () => {
it("Directly depositing more than strategy max debt into direct deposit enabled vault should revert", async () => {
const depositAmount = 10000000001;

accountantConfigAccount = await accountantProgram.account.config.fetch(
Expand Down Expand Up @@ -1617,7 +1617,7 @@ describe("Vault User Operations: Direct Deposit Tests", () => {
.signers([nonVerifiedUser])
.rpc();
} catch (err) {
console.log(err.message);
expect(err.message).to.contain(errorStrings.debtHigherThanMaxDebt);
}

await validateDirectDeposit({
Expand Down
2 changes: 2 additions & 0 deletions tests/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,6 @@ export const errorStrings = {
"Error Code: DirectDepositDisabled. Error Number: 6021. Error Message: Direct deposit is disabled.",
maxDepositReached:
"Error Code: MaxDepositReached. Error Number: 6005. Error Message: Max deposit reached.",
debtHigherThanMaxDebt:
"Error Code: DebtHigherThanMaxDebt. Error Number: 6007. Error Message: Debt cannot be higher than max debt.",
};

0 comments on commit 16d5f8d

Please sign in to comment.