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

VLT-273. add strategy max debt validation for direct deposit #63

Merged
merged 1 commit into from
Dec 28, 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
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.",
};
Loading