diff --git a/programs/tokenized_vault/src/instructions/direct_deposit.rs b/programs/tokenized_vault/src/instructions/direct_deposit.rs index 19ddb14..32b9362 100644 --- a/programs/tokenized_vault/src/instructions/direct_deposit.rs +++ b/programs/tokenized_vault/src/instructions/direct_deposit.rs @@ -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}; @@ -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( diff --git a/tests/integration/tests/vaultUserOperations/directDeposit.test.ts b/tests/integration/tests/vaultUserOperations/directDeposit.test.ts index 38076e2..396aef4 100644 --- a/tests/integration/tests/vaultUserOperations/directDeposit.test.ts +++ b/tests/integration/tests/vaultUserOperations/directDeposit.test.ts @@ -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({ @@ -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( @@ -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({ diff --git a/tests/utils/constants.ts b/tests/utils/constants.ts index 2611674..8b8fcbe 100644 --- a/tests/utils/constants.ts +++ b/tests/utils/constants.ts @@ -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.", };