From 8a2422b84039a10b6708480cf5a490ed8f0a6b91 Mon Sep 17 00:00:00 2001 From: DavitMkhitaryan Date: Fri, 20 Dec 2024 17:43:05 +0400 Subject: [PATCH] Integration tests: added rest of the direct deposit tests --- .../vaultUserOperations/directDeposit.test.ts | 430 +++++++++++++++++- tests/utils/constants.ts | 6 +- 2 files changed, 419 insertions(+), 17 deletions(-) diff --git a/tests/integration/tests/vaultUserOperations/directDeposit.test.ts b/tests/integration/tests/vaultUserOperations/directDeposit.test.ts index 1b7dfb3..9008719 100644 --- a/tests/integration/tests/vaultUserOperations/directDeposit.test.ts +++ b/tests/integration/tests/vaultUserOperations/directDeposit.test.ts @@ -1183,15 +1183,6 @@ describe("Vault User Operations: Direct Deposit Tests", () => { nonVerifiedUser.publicKey ); - const kycVerified = anchor.web3.PublicKey.findProgramAddressSync( - [ - Buffer.from("user_role"), - nonVerifiedUser.publicKey.toBuffer(), - ROLES_BUFFER.KYC_VERIFIED, - ], - accessControlProgram.programId - )[0]; - await vaultProgram.methods .directDeposit(new BN(depositAmount)) .accounts({ @@ -1205,13 +1196,6 @@ describe("Vault User Operations: Direct Deposit Tests", () => { tokenProgram: token.TOKEN_PROGRAM_ID, }) .signers([nonVerifiedUser]) - .remainingAccounts([ - { - pubkey: kycVerified, - isWritable: false, - isSigner: false, - }, - ]) .rpc(); nonVerifiedUserCurrentAmount -= depositAmount; @@ -1232,4 +1216,418 @@ describe("Vault User Operations: Direct Deposit Tests", () => { strategyCurrentDebtAmountExpected: depositAmount, }); }); + + it("Directly depositing valid amount into direct deposit disabled vault should revert", async () => { + const depositAmount = 5000000000; + + accountantConfigAccount = await accountantProgram.account.config.fetch( + accountantConfig + ); + const accountantIndex = + accountantConfigAccount.nextAccountantIndex.toNumber(); + + const accountant = anchor.web3.PublicKey.findProgramAddressSync( + [ + Buffer.from( + new Uint8Array(new BigUint64Array([BigInt(accountantIndex)]).buffer) + ), + ], + accountantProgram.programId + )[0]; + + await accountantProgram.methods + .initAccountant(accountantType) + .accounts({ + signer: generalAdmin.publicKey, + }) + .signers([generalAdmin]) + .rpc(); + + const vaultConfig = { + depositLimit: new BN(100000000000), + minUserDeposit: new BN(100000000), + accountant: accountant, + profitMaxUnlockTime: new BN(0), + kycVerifiedOnly: false, + directDepositEnabled: false, + whitelistedOnly: false, + }; + + const sharesConfig = { + name: "Test Roles and Permissions One", + symbol: "TRPV1", + uri: "https://gist.githubusercontent.com/vito-kovalione/08b86d3c67440070a8061ae429572494/raw/833e3d5f5988c18dce2b206a74077b2277e13ab6/PVT.json", + }; + + const [vault, sharesMint, metadataAccount, vaultTokenAccount] = + await initializeVault({ + vaultProgram, + underlyingMint, + signer: generalAdmin, + vaultConfig: vaultConfig, + sharesConfig: sharesConfig, + }); + + const strategyConfig = new SimpleStrategyConfig({ + depositLimit: new BN(100000000000), + performanceFee: new BN(1000), + feeManager: generalAdmin.publicKey, + }); + + const [strategy, strategyTokenAccount] = await initializeSimpleStrategy({ + strategyProgram, + vault: vault, + underlyingMint, + signer: generalAdmin, + config: strategyConfig, + }); + + await vaultProgram.methods + .addStrategy(new BN(100000000000)) + .accounts({ + vault: vault, + strategy: strategy, + signer: generalAdmin.publicKey, + }) + .signers([generalAdmin]) + .rpc(); + + await accountantProgram.methods + .initTokenAccount() + .accounts({ + accountant: accountant, + signer: generalAdmin.publicKey, + mint: sharesMint, + }) + .signers([generalAdmin]) + .rpc(); + + await accountantProgram.methods + .initTokenAccount() + .accounts({ + accountant: accountant, + signer: generalAdmin.publicKey, + mint: underlyingMint, + }) + .signers([generalAdmin]) + .rpc(); + + const userSharesAccount = await token.createAccount( + provider.connection, + nonVerifiedUser, + sharesMint, + nonVerifiedUser.publicKey + ); + + try { + await vaultProgram.methods + .directDeposit(new BN(depositAmount)) + .accounts({ + vault: vault, + accountant: accountant, + user: nonVerifiedUser.publicKey, + userTokenAccount: nonVerifiedUserTokenAccount, + userSharesAccount: userSharesAccount, + underlyingMint: underlyingMint, + strategy: strategy, + tokenProgram: token.TOKEN_PROGRAM_ID, + }) + .signers([nonVerifiedUser]) + .rpc(); + } catch (err) { + expect(err.message).to.contain(errorStrings.directDepositDisabled); + } + + await validateDirectDeposit({ + userTokenAccount: nonVerifiedUserTokenAccount, + userTokenAccountAmountExpected: nonVerifiedUserCurrentAmount, + userSharesAccount: userSharesAccount, + userSharesAccountAmountExpected: 0, + vaultTokenAccount: vaultTokenAccount, + vaultTokenAccountAmountExpected: 0, + vault: vault, + vaultTotalDebtAmountExpected: 0, + vaultTotalSharesAmountExpected: 0, + strategyTokenAccount: strategyTokenAccount, + strategyTokenAccountAmountExpected: 0, + strategy: strategy, + strategyCurrentDebtAmountExpected: 0, + }); + }); + + it.skip("Directly depositing more than strategy deposit limit into direct deposit enabled vault should revert", async () => { + const depositAmount = 10000000001; + + accountantConfigAccount = await accountantProgram.account.config.fetch( + accountantConfig + ); + const accountantIndex = + accountantConfigAccount.nextAccountantIndex.toNumber(); + + const accountant = anchor.web3.PublicKey.findProgramAddressSync( + [ + Buffer.from( + new Uint8Array(new BigUint64Array([BigInt(accountantIndex)]).buffer) + ), + ], + accountantProgram.programId + )[0]; + + await accountantProgram.methods + .initAccountant(accountantType) + .accounts({ + signer: generalAdmin.publicKey, + }) + .signers([generalAdmin]) + .rpc(); + + const vaultConfig = { + depositLimit: new BN(100000000000), + minUserDeposit: new BN(100000000), + accountant: accountant, + profitMaxUnlockTime: new BN(0), + kycVerifiedOnly: false, + directDepositEnabled: true, + whitelistedOnly: false, + }; + + const sharesConfig = { + name: "Test Roles and Permissions One", + symbol: "TRPV1", + uri: "https://gist.githubusercontent.com/vito-kovalione/08b86d3c67440070a8061ae429572494/raw/833e3d5f5988c18dce2b206a74077b2277e13ab6/PVT.json", + }; + + const [vault, sharesMint, metadataAccount, vaultTokenAccount] = + await initializeVault({ + vaultProgram, + underlyingMint, + signer: generalAdmin, + vaultConfig: vaultConfig, + sharesConfig: sharesConfig, + }); + + const strategyConfig = new SimpleStrategyConfig({ + depositLimit: new BN(10000000000), + performanceFee: new BN(1000), + feeManager: generalAdmin.publicKey, + }); + + const [strategy, strategyTokenAccount] = await initializeSimpleStrategy({ + strategyProgram, + vault: vault, + underlyingMint, + signer: generalAdmin, + config: strategyConfig, + }); + + await vaultProgram.methods + .addStrategy(new BN(100000000000)) + .accounts({ + vault: vault, + strategy: strategy, + signer: generalAdmin.publicKey, + }) + .signers([generalAdmin]) + .rpc(); + + await accountantProgram.methods + .initTokenAccount() + .accounts({ + accountant: accountant, + signer: generalAdmin.publicKey, + mint: sharesMint, + }) + .signers([generalAdmin]) + .rpc(); + + await accountantProgram.methods + .initTokenAccount() + .accounts({ + accountant: accountant, + signer: generalAdmin.publicKey, + mint: underlyingMint, + }) + .signers([generalAdmin]) + .rpc(); + + const userSharesAccount = await token.createAccount( + provider.connection, + nonVerifiedUser, + sharesMint, + nonVerifiedUser.publicKey + ); + + try { + await vaultProgram.methods + .directDeposit(new BN(depositAmount)) + .accounts({ + vault: vault, + accountant: accountant, + user: nonVerifiedUser.publicKey, + userTokenAccount: nonVerifiedUserTokenAccount, + userSharesAccount: userSharesAccount, + underlyingMint: underlyingMint, + strategy: strategy, + tokenProgram: token.TOKEN_PROGRAM_ID, + }) + .signers([nonVerifiedUser]) + .rpc(); + } catch (err) { + expect(err.message).to.contain(errorStrings.maxDepositReached); + } + + await validateDirectDeposit({ + userTokenAccount: nonVerifiedUserTokenAccount, + userTokenAccountAmountExpected: nonVerifiedUserCurrentAmount, + userSharesAccount: userSharesAccount, + userSharesAccountAmountExpected: 0, + vaultTokenAccount: vaultTokenAccount, + vaultTokenAccountAmountExpected: 0, + vault: vault, + vaultTotalDebtAmountExpected: 0, + vaultTotalSharesAmountExpected: 0, + strategyTokenAccount: strategyTokenAccount, + strategyTokenAccountAmountExpected: 0, + strategy: strategy, + strategyCurrentDebtAmountExpected: 0, + }); + }); + + 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( + accountantConfig + ); + const accountantIndex = + accountantConfigAccount.nextAccountantIndex.toNumber(); + + const accountant = anchor.web3.PublicKey.findProgramAddressSync( + [ + Buffer.from( + new Uint8Array(new BigUint64Array([BigInt(accountantIndex)]).buffer) + ), + ], + accountantProgram.programId + )[0]; + + await accountantProgram.methods + .initAccountant(accountantType) + .accounts({ + signer: generalAdmin.publicKey, + }) + .signers([generalAdmin]) + .rpc(); + + const vaultConfig = { + depositLimit: new BN(100000000000), + minUserDeposit: new BN(100000000), + accountant: accountant, + profitMaxUnlockTime: new BN(0), + kycVerifiedOnly: false, + directDepositEnabled: true, + whitelistedOnly: false, + }; + + const sharesConfig = { + name: "Test Roles and Permissions One", + symbol: "TRPV1", + uri: "https://gist.githubusercontent.com/vito-kovalione/08b86d3c67440070a8061ae429572494/raw/833e3d5f5988c18dce2b206a74077b2277e13ab6/PVT.json", + }; + + const [vault, sharesMint, metadataAccount, vaultTokenAccount] = + await initializeVault({ + vaultProgram, + underlyingMint, + signer: generalAdmin, + vaultConfig: vaultConfig, + sharesConfig: sharesConfig, + }); + + const strategyConfig = new SimpleStrategyConfig({ + depositLimit: new BN(100000000000), + performanceFee: new BN(1000), + feeManager: generalAdmin.publicKey, + }); + + const [strategy, strategyTokenAccount] = await initializeSimpleStrategy({ + strategyProgram, + vault: vault, + underlyingMint, + signer: generalAdmin, + config: strategyConfig, + }); + + await vaultProgram.methods + .addStrategy(new BN(10000000000)) + .accounts({ + vault: vault, + strategy: strategy, + signer: generalAdmin.publicKey, + }) + .signers([generalAdmin]) + .rpc(); + + await accountantProgram.methods + .initTokenAccount() + .accounts({ + accountant: accountant, + signer: generalAdmin.publicKey, + mint: sharesMint, + }) + .signers([generalAdmin]) + .rpc(); + + await accountantProgram.methods + .initTokenAccount() + .accounts({ + accountant: accountant, + signer: generalAdmin.publicKey, + mint: underlyingMint, + }) + .signers([generalAdmin]) + .rpc(); + + const userSharesAccount = await token.createAccount( + provider.connection, + nonVerifiedUser, + sharesMint, + nonVerifiedUser.publicKey + ); + + try { + await vaultProgram.methods + .directDeposit(new BN(depositAmount)) + .accounts({ + vault: vault, + accountant: accountant, + user: nonVerifiedUser.publicKey, + userTokenAccount: nonVerifiedUserTokenAccount, + userSharesAccount: userSharesAccount, + underlyingMint: underlyingMint, + strategy: strategy, + tokenProgram: token.TOKEN_PROGRAM_ID, + }) + .signers([nonVerifiedUser]) + .rpc(); + } catch (err) { + console.log(err.message); + } + + await validateDirectDeposit({ + userTokenAccount: nonVerifiedUserTokenAccount, + userTokenAccountAmountExpected: nonVerifiedUserCurrentAmount, + userSharesAccount: userSharesAccount, + userSharesAccountAmountExpected: 0, + vaultTokenAccount: vaultTokenAccount, + vaultTokenAccountAmountExpected: 0, + vault: vault, + vaultTotalDebtAmountExpected: 0, + vaultTotalSharesAmountExpected: 0, + strategyTokenAccount: strategyTokenAccount, + strategyTokenAccountAmountExpected: 0, + strategy: strategy, + strategyCurrentDebtAmountExpected: 0, + }); + }); }); diff --git a/tests/utils/constants.ts b/tests/utils/constants.ts index 2d4f206..2611674 100644 --- a/tests/utils/constants.ts +++ b/tests/utils/constants.ts @@ -53,5 +53,9 @@ export const errorStrings = { zeroValue: "Error Code: ZeroValue. Error Number: 6001. Error Message: Zero value.", vaultShutdown: - " Error Code: VaultShutdown. Error Number: 6000. Error Message: Vault was shutdown.", + "Error Code: VaultShutdown. Error Number: 6000. Error Message: Vault was shutdown.", + directDepositDisabled: + "Error Code: DirectDepositDisabled. Error Number: 6021. Error Message: Direct deposit is disabled.", + maxDepositReached: + "Error Code: MaxDepositReached. Error Number: 6005. Error Message: Max deposit reached.", };