Skip to content

Commit

Permalink
invest as deposit param boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinsoza committed Nov 26, 2024
1 parent a7825dd commit 996b5de
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 64 deletions.
6 changes: 3 additions & 3 deletions apps/contracts/vault/src/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::{funds::{fetch_invested_funds_for_asset, fetch_invested_funds_for_str
/// Common logic for processing deposits.
pub fn process_deposit(
e: &Env,
assets: &Vec<AssetStrategySet>,
amounts_desired: &Vec<i128>,
amounts_min: &Vec<i128>,
from: &Address,
) -> Result<(Vec<i128>, i128), ContractError> {
let assets = get_assets(&e);
let assets_length = assets.len();

// Validate inputs
Expand Down Expand Up @@ -97,7 +97,7 @@ fn mint_shares(
pub fn generate_and_execute_investments(
e: &Env,
amounts: &Vec<i128>,
assets: Vec<AssetStrategySet>,
assets: &Vec<AssetStrategySet>,
) -> Result<(), ContractError> {
let mut asset_investments = Vec::new(&e);

Expand Down Expand Up @@ -135,6 +135,6 @@ pub fn generate_and_execute_investments(
}));
}

check_and_execute_investments(e.clone(), assets, asset_investments)?;
check_and_execute_investments(e.clone(), assets.clone(), asset_investments)?;
Ok(())
}
21 changes: 1 addition & 20 deletions apps/contracts/vault/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,7 @@ pub trait VaultTrait {
amounts_desired: Vec<i128>,
amounts_min: Vec<i128>,
from: Address,
) -> Result<(Vec<i128>, i128), ContractError>;

/// Handles user deposits into the DeFindex Vault and invest them immediately.
///
///
/// # Parameters
/// * `e` - The current environment reference (`Env`), for access to the contract state and utilities.
/// * `amounts_desired` - A vector specifying the user's intended deposit amounts for each asset.
/// * `amounts_min` - A vector of minimum deposit amounts required for the transaction to proceed.
/// * `from` - The address of the user making the deposit.
///
/// # Returns
/// * `Result<(Vec<i128>, i128), ContractError>` - Returns the actual deposited `amounts` and `shares_to_mint` if successful,
/// otherwise a `ContractError`.
///
fn deposit_and_invest(
e: Env,
amounts_desired: Vec<i128>,
amounts_min: Vec<i128>,
from: Address,
invest: bool,
) -> Result<(Vec<i128>, i128), ContractError>;

/// Withdraws assets from the DeFindex Vault by burning dfTokens.
Expand Down
43 changes: 7 additions & 36 deletions apps/contracts/vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl VaultTrait for DeFindexVault {
amounts_desired: Vec<i128>,
amounts_min: Vec<i128>,
from: Address,
invest: bool,
) -> Result<(Vec<i128>, i128), ContractError> {
extend_instance_ttl(&e);
check_initialized(&e)?;
Expand All @@ -204,45 +205,15 @@ 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)?;

let (amounts, shares_to_mint) = process_deposit(&e, &amounts_desired, &amounts_min, &from)?;

events::emit_deposit_event(&e, from, amounts.clone(), shares_to_mint.clone());

Ok((amounts, shares_to_mint))
}

/// Handles user deposits into the DeFindex Vault and invest them immediately.
///
///
/// # Parameters
/// * `e` - The current environment reference (`Env`), for access to the contract state and utilities.
/// * `amounts_desired` - A vector specifying the user's intended deposit amounts for each asset.
/// * `amounts_min` - A vector of minimum deposit amounts required for the transaction to proceed.
/// * `from` - The address of the user making the deposit.
///
/// # Returns
/// * `Result<(Vec<i128>, i128), ContractError>` - Returns the actual deposited `amounts` and `shares_to_mint` if successful,
/// otherwise a `ContractError`.
///
fn deposit_and_invest(
e: Env,
amounts_desired: Vec<i128>,
amounts_min: Vec<i128>,
from: Address,
) -> Result<(Vec<i128>, i128), ContractError> {
extend_instance_ttl(&e);
check_initialized(&e)?;
from.require_auth();

// Collect Fees
collect_fees(&e)?;
let assets = get_assets(&e);

let (amounts, shares_to_mint) = process_deposit(&e, &amounts_desired, &amounts_min, &from)?;
let (amounts, shares_to_mint) = process_deposit(&e, &assets, &amounts_desired, &amounts_min, &from)?;
events::emit_deposit_event(&e, from, amounts.clone(), shares_to_mint.clone());

let assets = get_assets(&e);
// Generate investment allocations and execute them
generate_and_execute_investments(&e, &amounts, assets)?;
if invest {
// Generate investment allocations and execute them
generate_and_execute_investments(&e, &amounts, &assets)?;
}

Ok((amounts, shares_to_mint))
}
Expand Down
15 changes: 15 additions & 0 deletions apps/contracts/vault/src/test/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn test_deposit_not_yet_initialized() {
&sorobanvec![&test.env, 100i128],
&sorobanvec![&test.env, 100i128],
&users[0],
&false,
);

assert_eq!(result, Err(Ok(ContractError::NotInitialized)));
Expand Down Expand Up @@ -60,6 +61,7 @@ fn deposit_amounts_desired_less_length() {
&sorobanvec![&test.env, amount], // wrong amount desired
&sorobanvec![&test.env, amount, amount],
&users[0],
&false,
);

assert_eq!(response, Err(Ok(ContractError::WrongAmountsLength)));
Expand Down Expand Up @@ -101,6 +103,7 @@ fn deposit_amounts_desired_more_length() {
&sorobanvec![&test.env, amount, amount], // wrong amount desired
&sorobanvec![&test.env, amount],
&users[0],
&false,
);

assert_eq!(response, Err(Ok(ContractError::WrongAmountsLength)));
Expand Down Expand Up @@ -146,6 +149,7 @@ fn deposit_amounts_min_less_length() {
&sorobanvec![&test.env, amount, amount],
&sorobanvec![&test.env, amount], // wrong amount min
&users[0],
&false,
);

assert_eq!(response, Err(Ok(ContractError::WrongAmountsLength)));
Expand Down Expand Up @@ -192,6 +196,7 @@ fn deposit_amounts_min_more_length() {
&sorobanvec![&test.env, amount, amount],
&sorobanvec![&test.env, amount, amount, amount], // wrong amount min
&users[0],
&false,
);

assert_eq!(response, Err(Ok(ContractError::WrongAmountsLength)));
Expand Down Expand Up @@ -237,6 +242,7 @@ fn deposit_amounts_desired_negative() {
&sorobanvec![&test.env, -amount, amount],
&sorobanvec![&test.env, amount, amount],
&users[0],
&false,
);

assert_eq!(response, Err(Ok(ContractError::NegativeNotAllowed)));
Expand Down Expand Up @@ -286,6 +292,7 @@ fn deposit_one_asset_success() {
&sorobanvec![&test.env, amount],
&sorobanvec![&test.env, amount],
&users[0],
&false,
);

// check balances after deposit
Expand Down Expand Up @@ -330,6 +337,7 @@ fn deposit_one_asset_success() {
&sorobanvec![&test.env, amount2],
&sorobanvec![&test.env, amount2],
&users[0],
&false,
);

//map shuould be map
Expand Down Expand Up @@ -412,6 +420,7 @@ fn deposit_one_asset_min_more_than_desired() {
&sorobanvec![&test.env, amount],
&sorobanvec![&test.env, amount + 1],
&users[0],
&false,
);
// this should fail
assert_eq!(result, Err(Ok(ContractError::InsufficientAmount)));
Expand Down Expand Up @@ -471,6 +480,7 @@ fn deposit_several_assets_success() {
&sorobanvec![&test.env, amount0, amount1],
&sorobanvec![&test.env, amount0, amount1],
&users[0],
&false,
);

// check deposit result
Expand Down Expand Up @@ -541,6 +551,7 @@ fn deposit_several_assets_success() {
&sorobanvec![&test.env, amount0_new, amount1_new],
&sorobanvec![&test.env, 0i128, 0i128],
&users[1],
&false,
);

// check deposit result. Ok((amounts, shares_to_mint))
Expand Down Expand Up @@ -607,6 +618,7 @@ fn deposit_several_assets_success() {
&sorobanvec![&test.env, amount0_new, amount1_new],
&sorobanvec![&test.env, 0i128, 0i128],
&users[1],
&false,
);

// check deposit result. Ok((amounts, shares_to_mint))
Expand Down Expand Up @@ -668,6 +680,7 @@ fn deposit_several_assets_min_greater_than_optimal() {
&sorobanvec![&test.env, amount0, amount1],
&sorobanvec![&test.env, amount0 + 1, amount1],
&users[0],
&false,
);

// this should fail
Expand All @@ -678,6 +691,7 @@ fn deposit_several_assets_min_greater_than_optimal() {
&sorobanvec![&test.env, amount0, amount1],
&sorobanvec![&test.env, amount0, amount1],
&users[0],
&false,
);

// check deposit result
Expand All @@ -697,6 +711,7 @@ fn deposit_several_assets_min_greater_than_optimal() {
&sorobanvec![&test.env, amount0_new, amount1_new],
&sorobanvec![&test.env, amount0*2+1, amount1*2],
&users[0],
&false,
);

// this should fail
Expand Down
15 changes: 10 additions & 5 deletions apps/contracts/vault/src/test/deposit_and_invest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@ fn deposit_and_invest_one_asset_success() {
assert_eq!(df_balance, 0i128);

// deposit
test.defindex_contract.deposit_and_invest(
test.defindex_contract.deposit(
&sorobanvec![&test.env, amount],
&sorobanvec![&test.env, amount],
&users[0],
&true,
);

// check balances after deposit
Expand Down Expand Up @@ -88,10 +89,11 @@ fn deposit_and_invest_one_asset_success() {
assert_eq!(user_balance, amount2);

// deposit
test.defindex_contract.deposit_and_invest(
test.defindex_contract.deposit(
&sorobanvec![&test.env, amount2],
&sorobanvec![&test.env, amount2],
&users[0],
&true,
);

//map shuould be map
Expand Down Expand Up @@ -174,10 +176,11 @@ fn deposit_and_invest_several_assets_success() {
assert_eq!(df_balance, 0i128);

// deposit
let deposit_result=test.defindex_contract.deposit_and_invest(
let deposit_result=test.defindex_contract.deposit(
&sorobanvec![&test.env, amount0, amount1],
&sorobanvec![&test.env, amount0, amount1],
&users[0],
&true,
);

// check deposit result
Expand Down Expand Up @@ -242,10 +245,11 @@ fn deposit_and_invest_several_assets_success() {


// user 1 deposits
let deposit_result=test.defindex_contract.deposit_and_invest(
let deposit_result=test.defindex_contract.deposit(
&sorobanvec![&test.env, amount0_new, amount1_new],
&sorobanvec![&test.env, 0i128, 0i128],
&users[1],
&true,
);

// check deposit result. Ok((amounts, shares_to_mint))
Expand Down Expand Up @@ -307,10 +311,11 @@ fn deposit_and_invest_several_assets_success() {
assert_eq!(user_balance1, amount1_new);

// user 1 deposits
let deposit_result=test.defindex_contract.deposit_and_invest(
let deposit_result=test.defindex_contract.deposit(
&sorobanvec![&test.env, amount0_new, amount1_new],
&sorobanvec![&test.env, 0i128, 0i128],
&users[1],
&true,
);

// check deposit result. Ok((amounts, shares_to_mint))
Expand Down
1 change: 1 addition & 0 deletions apps/contracts/vault/src/test/emergency_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn test_emergency_withdraw_success() {
&sorobanvec![&test.env, amount],
&sorobanvec![&test.env, amount],
&users[0],
&false
);

let df_balance = test.defindex_contract.balance(&users[0]);
Expand Down
3 changes: 3 additions & 0 deletions apps/contracts/vault/src/test/invest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ fn test_invest_in_strategy() {
&sorobanvec![&test.env, amount_0, amount_1], // asset 0
&sorobanvec![&test.env, amount_0, amount_1], // asset 1
&users[0],
&false,
);


Expand Down Expand Up @@ -609,6 +610,7 @@ fn test_invest_more_than_idle_funds() {
&sorobanvec![&test.env, amount_0, amount_1], // asset 0
&sorobanvec![&test.env, amount_0, amount_1], // asset 1
&users[0],
&false,
);

// check vault balances
Expand Down Expand Up @@ -753,6 +755,7 @@ fn test_invest_without_mock_all_auths() {
&sorobanvec![&test.env, amount_0, amount_1], // asset 0
&sorobanvec![&test.env, amount_0, amount_1], // asset 1
&users[0],
&false,
);

// TODO check that the blockchain saw this authorizations
Expand Down
1 change: 1 addition & 0 deletions apps/contracts/vault/src/test/rebalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ fn rebalance() {
&sorobanvec![&test.env, amount],
&sorobanvec![&test.env, amount],
&users[0],
&false
);

let df_balance = test.defindex_contract.balance(&users[0]);
Expand Down
2 changes: 2 additions & 0 deletions apps/contracts/vault/src/test/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn test_withdraw_from_idle_success() {
&sorobanvec![&test.env, amount_to_deposit],
&sorobanvec![&test.env, amount_to_deposit],
&users[0],
&false
);

// Check Balances after deposit
Expand Down Expand Up @@ -165,6 +166,7 @@ fn test_withdraw_from_strategy_success() {
&sorobanvec![&test.env, amount],
&sorobanvec![&test.env, amount],
&users[0],
&false
);

let df_balance = test.defindex_contract.balance(&users[0]);
Expand Down

0 comments on commit 996b5de

Please sign in to comment.