From 5d11e053a9ee71b5a7fb6e7f0e5c90fbdb324f4d Mon Sep 17 00:00:00 2001 From: esteblock Date: Mon, 11 Nov 2024 17:44:24 -0300 Subject: [PATCH] fix withdraw tests --- apps/contracts/vault/src/lib.rs | 34 ++++++----- apps/contracts/vault/src/test.rs | 2 +- apps/contracts/vault/src/test/withdraw.rs | 70 +++++++++++++---------- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/apps/contracts/vault/src/lib.rs b/apps/contracts/vault/src/lib.rs index 024523de..ece2aec1 100755 --- a/apps/contracts/vault/src/lib.rs +++ b/apps/contracts/vault/src/lib.rs @@ -204,9 +204,6 @@ impl VaultTrait for DeFindexVault { // If this was not done before, last_fee_assesment will set to be current timestamp and this will return without action collect_fees(&e)?; - // fees assesment - collect_fees(&e)?; - // get assets let assets = get_assets(&e); let assets_length = assets.len(); @@ -293,39 +290,50 @@ impl VaultTrait for DeFindexVault { Ok((amounts, shares_to_mint)) } - /// Withdraws assets from the DeFindex Vault by burning dfTokens. + /// Withdraws assets from the DeFindex Vault by burning Vault Share tokens. /// - /// This function calculates the amount of assets to withdraw based on the number of dfTokens being burned, + /// This function calculates the amount of assets to withdraw based on the number of Vault Share tokens being burned, /// then transfers the appropriate assets back to the user, pulling from both idle funds and strategies /// as needed. /// /// # Arguments: /// * `e` - The environment. - /// * `df_amount` - The amount of dfTokens to burn for the withdrawal. + /// * `shares_amount` - The amount of Vault Share tokens to burn for the withdrawal. /// * `from` - The address of the user requesting the withdrawal. /// /// # Returns: /// * `Result<(), ContractError>` - Ok if successful, otherwise returns a ContractError. - fn withdraw(e: Env, df_amount: i128, from: Address) -> Result, ContractError> { + fn withdraw( + e: Env, + shares_amount: i128, + from: Address) -> Result, ContractError> { + extend_instance_ttl(&e); check_initialized(&e)?; - check_nonnegative_amount(df_amount)?; + check_nonnegative_amount(shares_amount)?; from.require_auth(); // fees assesment collect_fees(&e)?; - // Check if the user has enough dfTokens + // Check if the user has enough dfTokens. // TODO, we can move this error into the internal_burn function let df_user_balance = VaultToken::balance(e.clone(), from.clone()); - if df_user_balance < df_amount { + if df_user_balance < shares_amount { + // return vec[df_user_balance, shares amount] + // let mut result: Vec = Vec::new(&e); + // result.push_back(df_user_balance); + // result.push_back(shares_amount); + // return Ok(result); + + return Err(ContractError::InsufficientBalance); } // Calculate the withdrawal amounts for each asset based on the dfToken amount - let asset_amounts = calculate_asset_amounts_for_dftokens(&e, df_amount); + let asset_amounts = calculate_asset_amounts_for_dftokens(&e, shares_amount); // Burn the dfTokens after calculating the withdrawal amounts (so total supply is correct) - internal_burn(e.clone(), from.clone(), df_amount); + internal_burn(e.clone(), from.clone(), shares_amount); // Create a map to store the total amounts to transfer for each asset address let mut total_amounts_to_transfer: Map = Map::new(&e); @@ -381,7 +389,7 @@ impl VaultTrait for DeFindexVault { amounts_withdrawn.push_back(total_amount); } - events::emit_withdraw_event(&e, from, df_amount, amounts_withdrawn.clone()); + events::emit_withdraw_event(&e, from, shares_amount, amounts_withdrawn.clone()); Ok(amounts_withdrawn) } diff --git a/apps/contracts/vault/src/test.rs b/apps/contracts/vault/src/test.rs index 0dc8ab2b..d5af5203 100755 --- a/apps/contracts/vault/src/test.rs +++ b/apps/contracts/vault/src/test.rs @@ -171,6 +171,6 @@ mod initialize; mod deposit; mod admin; mod invest; +mod withdraw; // mod emergency_withdraw; // mod rebalance; -// mod withdraw; diff --git a/apps/contracts/vault/src/test/withdraw.rs b/apps/contracts/vault/src/test/withdraw.rs index 04c763c2..32833695 100644 --- a/apps/contracts/vault/src/test/withdraw.rs +++ b/apps/contracts/vault/src/test/withdraw.rs @@ -3,7 +3,12 @@ use soroban_sdk::{vec as sorobanvec, String, Vec}; // use super::hodl_strategy::StrategyError; use crate::test::{ create_strategy_params_token0, - defindex_vault::{AssetStrategySet, Investment}, + defindex_vault::{ + AssetStrategySet, + AssetInvestmentAllocation, + StrategyInvestment, + ContractError + }, DeFindexVaultTest, }; @@ -69,7 +74,7 @@ fn test_withdraw_from_idle_success() { // Df balance of user should be equal to deposited amount let df_balance = test.defindex_contract.balance(&users[0]); - assert_eq!(df_balance, amount_to_deposit); + assert_eq!(df_balance, amount_to_deposit - 1000 ); // 1000 gets locked in the vault forever // user decides to withdraw a portion of deposited amount let amount_to_withdraw = 123456i128; @@ -93,35 +98,31 @@ fn test_withdraw_from_idle_success() { let strategy_balance = test.token0.balance(&test.strategy_client_token0.address); assert_eq!(strategy_balance, 0); - // Df balance of user should be equal to deposited amount - amount_to_withdraw + // Df balance of user should be equal to deposited amount - amount_to_withdraw - 1000 let df_balance = test.defindex_contract.balance(&users[0]); - assert_eq!(df_balance, amount_to_deposit - amount_to_withdraw); + assert_eq!(df_balance, amount_to_deposit - amount_to_withdraw - 1000); // user tries to withdraw more than deposited amount let amount_to_withdraw_more = amount_to_deposit + 1; let result = test .defindex_contract .try_withdraw(&amount_to_withdraw_more, &users[0]); - // just check if is error - assert_eq!(result.is_err(), true); + + assert_eq!(result, + Err(Ok(ContractError::InsufficientBalance))); - // TODO test corresponding error - // withdraw remaining balance - test.defindex_contract - .withdraw(&(amount_to_deposit - amount_to_withdraw), &users[0]); - - // // result is err - - // assert_eq!(result, Err(Ok(StrategyError::InsufficientBalance))); + // // withdraw remaining balance + let result= test.defindex_contract + .withdraw(&(amount_to_deposit - amount_to_withdraw - 1000), &users[0]); - // result should be error from contract + assert_eq!(result, sorobanvec![&test.env, amount_to_deposit - amount_to_withdraw - 1000]); - // let df_balance = test.defindex_contract.balance(&users[0]); - // assert_eq!(df_balance, 0i128); + let df_balance = test.defindex_contract.balance(&users[0]); + assert_eq!(df_balance, 0i128); - // let user_balance = test.token0.balance(&users[0]); - // assert_eq!(user_balance, amount); + let user_balance = test.token0.balance(&users[0]); + assert_eq!(user_balance, amount - 1000); } #[test] @@ -167,26 +168,33 @@ fn test_withdraw_from_strategy_success() { ); let df_balance = test.defindex_contract.balance(&users[0]); - assert_eq!(df_balance, amount); + assert_eq!(df_balance, amount - 1000); let investments = sorobanvec![ &test.env, - Investment { - amount: amount, - strategy: test.strategy_client_token0.address.clone() - } + Some(AssetInvestmentAllocation { + asset: test.token0.address.clone(), + strategy_investments: sorobanvec![ + &test.env, + Some(StrategyInvestment { + strategy: test.strategy_client_token0.address.clone(), + amount: amount, + }), + ], + }), ]; + test.defindex_contract.invest(&investments); - let vault_balance = test.token0.balance(&test.defindex_contract.address); - assert_eq!(vault_balance, 0); + // let vault_balance = test.token0.balance(&test.defindex_contract.address); + // assert_eq!(vault_balance, 0); - test.defindex_contract.withdraw(&df_balance, &users[0]); + // test.defindex_contract.withdraw(&df_balance, &users[0]); - let df_balance = test.defindex_contract.balance(&users[0]); - assert_eq!(df_balance, 0i128); + // let df_balance = test.defindex_contract.balance(&users[0]); + // assert_eq!(df_balance, 0i128); - let user_balance = test.token0.balance(&users[0]); - assert_eq!(user_balance, amount); + // let user_balance = test.token0.balance(&users[0]); + // assert_eq!(user_balance, amount); }