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

Feat/improve withdraw function and tests #191

Merged
merged 12 commits into from
Dec 9, 2024
Prev Previous commit
Next Next commit
test withdraw from_idle_two_assets_success
  • Loading branch information
esteblock committed Dec 5, 2024
commit eba01582af22180fa6ca6ae8d754eae50cfc4ad1
5 changes: 2 additions & 3 deletions apps/contracts/vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use deposit::{generate_and_execute_investments, process_deposit};
use soroban_sdk::{
contract, contractimpl, panic_with_error,
token::{TokenClient, TokenInterface},
token::{TokenClient},
Address, Env, Map, String, Vec,
};
use soroban_token_sdk::metadata::TokenMetadata;
Expand Down Expand Up @@ -44,7 +44,7 @@ use strategies::{
get_strategy_struct, invest_in_strategy, pause_strategy, unpause_strategy,
unwind_from_strategy,
};
use token::{internal_burn, write_metadata, VaultToken};
use token::{internal_burn, write_metadata};
use utils::{
calculate_asset_amounts_per_vault_shares,
check_initialized,
Expand Down Expand Up @@ -273,7 +273,6 @@ impl VaultTrait for DeFindexVault {
collect_fees(&e)?;

// Calculate the withdrawal amounts for each asset based on the share amounts
let assets = get_assets(&e); // TODO:
let total_managed_funds = fetch_total_managed_funds(&e);

let asset_withdrawal_amounts = calculate_asset_amounts_per_vault_shares(
Expand Down
145 changes: 49 additions & 96 deletions apps/contracts/vault/src/test/vault/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,107 +465,60 @@ fn from_idle_two_assets_success() {
paused: false,
}],
}];
// assert_eq!(test.defindex_contract.get_assets(), expected_asset_vec);
// let expected_result = sorobanvec![&test.env, 45070, 78385];
// assert_eq!(result, expected_result);
assert_eq!(test.defindex_contract.get_assets(), expected_asset_vec);
let expected_result = sorobanvec![&test.env, 45070, 78385];
assert_eq!(result, expected_result);

// Token balance of user
assert_eq!(test.token0.balance(&users[0]), amount - amount_to_deposit_0 + 45070);
assert_eq!(test.token1.balance(&users[0]), amount - amount_to_deposit_1 + 78385);

// // Token balance of vault (still idle)

// assert_eq!(test.token0.balance(&test.defindex_contract.address), amount_to_deposit_0 - 45070);
// assert_eq!(test.token1.balance(&test.defindex_contract.address), amount_to_deposit_1 - 78385);

// // Token balance of hodl strategy should be 0 (all in idle)
// assert_eq!(test.token0.balance(&test.strategy_client_token0.address), 0);
// assert_eq!(test.token1.balance(&test.strategy_client_token1.address), 0);

// // Df balance of user should be equal to amount_to_deposit_0+amount_to_deposit_1 - 1000 - 123456
// // 567890+987654-1000 -123456 = 1434088
// let df_balance = test.defindex_contract.balance(&users[0]);
// assert_eq!(df_balance, 1431088 );

// // Token balance of user should be amount - amount_to_deposit + amount_to_withdraw
// let user_balance = test.token0.balance(&users[0]);
// assert_eq!(
// user_balance,
// amount - amount_to_deposit + amount_to_withdraw
// );

// // Token balance of vault should be amount_to_deposit - amount_to_withdraw
// let vault_balance = test.token0.balance(&test.defindex_contract.address);
// assert_eq!(vault_balance, amount_to_deposit - amount_to_withdraw);

// // Token balance of hodl strategy should be 0 (all in idle)
// 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 - 1000
// let df_balance = test.defindex_contract.balance(&users[0]);
// assert_eq!(df_balance, amount_to_deposit - amount_to_withdraw - 1000);

// // check total manage funds
// let mut total_managed_funds_expected = Map::new(&test.env);
// let strategy_investments_expected = sorobanvec![&test.env, StrategyAllocation {
// strategy_address: test.strategy_client_token0.address.clone(),
// amount: 0, //funds has not been invested yet!
// }];
// total_managed_funds_expected.set(test.token0.address.clone(),
// CurrentAssetInvestmentAllocation {
// asset: test.token0.address.clone(),
// total_amount: amount_to_deposit - amount_to_withdraw,
// idle_amount: amount_to_deposit - amount_to_withdraw,
// invested_amount: 0i128,
// strategy_allocations: strategy_investments_expected,
// }
// );

// let total_managed_funds = test.defindex_contract.fetch_total_managed_funds();
// assert_eq!(total_managed_funds, total_managed_funds_expected);


// // 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]);

// assert_eq!(result,
// Err(Ok(ContractError::AmountOverTotalSupply)));


// // // withdraw remaining balance
// let result= test.defindex_contract
// .withdraw(&(amount_to_deposit - amount_to_withdraw - 1000), &users[0]);

// 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 user_balance = test.token0.balance(&users[0]);
// assert_eq!(user_balance, amount - 1000);

// // check total manage funds
// let mut total_managed_funds_expected = Map::new(&test.env);
// let strategy_investments_expected = sorobanvec![&test.env, StrategyAllocation {
// strategy_address: test.strategy_client_token0.address.clone(),
// amount: 0, //funds has not been invested yet!
// }];
// total_managed_funds_expected.set(test.token0.address.clone(),
// CurrentAssetInvestmentAllocation {
// asset: test.token0.address.clone(),
// total_amount: 1000,
// idle_amount: 1000,
// invested_amount: 0i128,
// strategy_allocations: strategy_investments_expected,
// }
// );

// let total_managed_funds = test.defindex_contract.fetch_total_managed_funds();
// assert_eq!(total_managed_funds, total_managed_funds_expected);
// Token balance of vault (still idle)

assert_eq!(test.token0.balance(&test.defindex_contract.address), amount_to_deposit_0 - 45070);
assert_eq!(test.token1.balance(&test.defindex_contract.address), amount_to_deposit_1 - 78385);

// Token balance of hodl strategy should be 0 (all in idle)
assert_eq!(test.token0.balance(&test.strategy_client_token0.address), 0);
assert_eq!(test.token1.balance(&test.strategy_client_token1.address), 0);

// Df balance of user should be equal to amount_to_deposit_0+amount_to_deposit_1 - 1000 - 123456
// 567890+987654-1000 -123456 = 1434088
let df_balance = test.defindex_contract.balance(&users[0]);
assert_eq!(df_balance, 1431088 );

// check total manage funds
let mut total_managed_funds_expected = Map::new(&test.env);
total_managed_funds_expected.set(test.token0.address.clone(),
CurrentAssetInvestmentAllocation {
asset: test.token0.address.clone(),
total_amount: 567890i128 - 45070,
idle_amount: 567890i128 - 45070,
invested_amount: 0i128,
strategy_allocations: sorobanvec![&test.env,
StrategyAllocation {
strategy_address: test.strategy_client_token0.address.clone(),
amount: 0, //funds has not been invested yet!
}],
}
);

total_managed_funds_expected.set(test.token1.address.clone(),
CurrentAssetInvestmentAllocation {
asset: test.token1.address.clone(),
total_amount: 987654i128 - 78385,
idle_amount: 987654i128 - 78385,
invested_amount: 0i128,
strategy_allocations: sorobanvec![&test.env,
StrategyAllocation {
strategy_address: test.strategy_client_token1.address.clone(),
amount: 0, //funds has not been invested yet!
}],
}
);

let total_managed_funds = test.defindex_contract.fetch_total_managed_funds();
assert_eq!(total_managed_funds, total_managed_funds_expected);

}

Expand Down
3 changes: 1 addition & 2 deletions apps/contracts/vault/src/token/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use crate::token::metadata::{read_decimal, read_name, read_symbol};
use crate::token::total_supply::{decrease_total_supply, increase_total_supply, read_total_supply};

#[cfg(test)]
use crate::token::storage_types::{AllowanceDataKey, AllowanceValue, DataKey};
use crate::token::storage_types::{AllowanceDataKey};
use crate::token::storage_types::{INSTANCE_BUMP_AMOUNT, INSTANCE_LIFETIME_THRESHOLD};
use soroban_sdk::token::{self, Interface as _};
use soroban_sdk::{contract, contractimpl, Address, Env, String};
use soroban_token_sdk::TokenUtils;
use crate::ContractError;

fn check_nonnegative_amount(amount: i128) {
if amount < 0 {
Expand Down