Skip to content

Commit

Permalink
testing invest work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
esteblock committed Nov 8, 2024
1 parent 18fb645 commit a8deddb
Show file tree
Hide file tree
Showing 5 changed files with 544 additions and 19 deletions.
33 changes: 26 additions & 7 deletions apps/contracts/vault/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,34 @@ pub trait AdminInterfaceTrait {
}

pub trait VaultManagementTrait {
/// Invests the vault's idle funds into the specified strategies.
/// Executes the investment of the vault's idle funds based on the specified asset allocations.
/// This function allows partial investments by providing an optional allocation for each asset,
/// and it ensures proper authorization and validation checks before proceeding with investments.
///
/// # Arguments:
/// * `e` - The environment.
/// * `investment` - A vector of `AssetInvestmentAllocation` structs representing the amount to invest in each strategy.
/// * `caller` - The address of the caller.
/// # Arguments
/// * `e` - The current environment reference.
/// * `asset_investments` - A vector of optional `AssetInvestmentAllocation` structures, where each element
/// represents an allocation for a specific asset. The vector must match the number of vault assets in length.
///
/// # Returns:
/// * `Result<(), ContractError>` - Ok if successful, otherwise returns a ContractError.
/// # Returns
/// * `Result<(), ContractError>` - Returns `Ok(())` if the investments are successful or a `ContractError`
/// if any issue occurs during validation or execution.
///
/// # Function Flow
/// 1. **Extend Instance TTL**: Extends the contract instance's time-to-live to keep the instance active.
/// 2. **Check Initialization**: Verifies that the vault is properly initialized before proceeding.
/// 3. **Access Control**: Ensures the caller has the `Manager` role required to initiate investments.
/// 4. **Asset Count Validation**: Verifies that the length of the `asset_investments` vector matches
/// the number of assets managed by the vault. If they don't match, a `WrongInvestmentLength` error is returned.
/// 5. **Investment Execution**: Calls the `check_and_execute_investments` function to perform the investment
/// after validating the inputs and ensuring correct execution flows for each asset allocation.
///
/// # Errors
/// * Returns `ContractError::WrongInvestmentLength` if the length of `asset_investments` does not match the vault assets.
/// * Returns `ContractError` if access control validation fails or if investment execution encounters an issue.
///
/// # Security
/// - Only addresses with the `Manager` role can call this function, ensuring restricted access to managing investments.
fn invest(e: Env, asset_investments: Vec<Option<AssetInvestmentAllocation>>) -> Result<(), ContractError>;

/// Rebalances the vault by executing a series of instructions.
Expand Down
29 changes: 29 additions & 0 deletions apps/contracts/vault/src/investment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ use crate::{
ContractError,
};

/// Checks and executes the investments for each asset based on provided allocations.
/// The function iterates through the specified assets and asset investments to ensure validity
/// and executes investments accordingly.
///
/// # Arguments
/// * `e` - The current environment reference.
/// * `assets` - A vector of `AssetStrategySet` that holds information about assets and their associated strategies.
/// * `asset_investments` - A vector of optional investment allocations for each asset.
///s
/// # Returns
/// * `Result<(), ContractError>` - Returns `Ok(())` if all investments are successful or an appropriate `ContractError` if any issue is encountered.
///
/// # Function Flow
/// 1. **Iterate Over Asset Investments**: Loops through each asset investment allocation.
/// 2. **Validation**:
/// - **Asset Address Check**: Ensures that the asset's address matches the expected address in the allocation.
/// - **Strategy Length Check**: Verifies that the number of strategies matches between the asset and the corresponding allocation.
/// - **Note**: The total intended investment check has been removed as the subsequent operations inherently perform the same validation.
/// 3. **Process Strategy Investments**:
/// - For each strategy within an asset:
/// - **Non-Negative Amount Check**: Validates that the investment amount is non-negative.
/// - **Strategy Active Check**: Ensures that the strategy is not paused before proceeding with the investment.
/// - **Execute Investment**: Calls the `invest_in_strategy` function if all checks pass.
///
/// # Errors
/// * Returns `ContractError::WrongAssetAddress` if an asset's address does not match the expected address.
/// * Returns `ContractError::WrongStrategiesLength` if the number of strategies in the asset and allocation do not match.
/// * Returns `ContractError::StrategyPaused` if an investment targets a paused strategy.
///
pub fn check_and_execute_investments(
e: Env,
assets: Vec<AssetStrategySet>,
Expand Down
37 changes: 25 additions & 12 deletions apps/contracts/vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,22 +660,34 @@ impl AdminInterfaceTrait for DeFindexVault {

#[contractimpl]
impl VaultManagementTrait for DeFindexVault {
/// Invests the vault's idle funds into the specified strategies, based on provided allocations.
/// The investment allocations allow for selective investments across assets and strategies.
/// Executes the investment of the vault's idle funds based on the specified asset allocations.
/// This function allows partial investments by providing an optional allocation for each asset,
/// and it ensures proper authorization and validation checks before proceeding with investments.
///
/// # Arguments
/// * `e` - The current environment.
/// * `asset_investments` - A vector of optional investment allocations, each corresponding to an asset.
/// * `e` - The current environment reference.
/// * `asset_investments` - A vector of optional `AssetInvestmentAllocation` structures, where each element
/// represents an allocation for a specific asset. The vector must match the number of vault assets in length.
///
/// # Returns
/// Returns `Ok(())` if all investments are successful, or an error if there are any issues.
/// * `Result<(), ContractError>` - Returns `Ok(())` if the investments are successful or a `ContractError`
/// if any issue occurs during validation or execution.
///
/// # Function Flow
/// 1. **Extend Instance TTL**: Extends the contract instance's time-to-live to keep the instance active.
/// 2. **Check Initialization**: Verifies that the vault is properly initialized before proceeding.
/// 3. **Access Control**: Ensures the caller has the `Manager` role required to initiate investments.
/// 4. **Asset Count Validation**: Verifies that the length of the `asset_investments` vector matches
/// the number of assets managed by the vault. If they don't match, a `WrongInvestmentLength` error is returned.
/// 5. **Investment Execution**: Calls the `check_and_execute_investments` function to perform the investment
/// after validating the inputs and ensuring correct execution flows for each asset allocation.
///
/// # Errors
/// Returns errors if:
/// - The number of asset allocations does not match the assets in the vault.
/// - The asset or strategy allocations are invalid.
/// - Insufficient idle funds exist for an allocation.
/// - Any strategy targeted for investment is currently paused.
/// * Returns `ContractError::WrongInvestmentLength` if the length of `asset_investments` does not match the vault assets.
/// * Returns `ContractError` if access control validation fails or if investment execution encounters an issue.
///
/// # Security
/// - Only addresses with the `Manager` role can call this function, ensuring restricted access to managing investments.
fn invest(
e: Env,
asset_investments: Vec<Option<AssetInvestmentAllocation>>
Expand All @@ -686,7 +698,6 @@ impl VaultManagementTrait for DeFindexVault {
// Access control: ensure caller has the required manager role
let access_control = AccessControl::new(&e);
access_control.require_role(&RolesDataKey::Manager);
e.current_contract_address().require_auth();

let assets = get_assets(&e);

Expand All @@ -695,12 +706,14 @@ impl VaultManagementTrait for DeFindexVault {
panic_with_error!(&e, ContractError::WrongInvestmentLength);
}

// Check and execute investments for each asset allocation
check_and_execute_investments(e, assets, asset_investments)?;

Ok(())
}



/// Rebalances the vault by executing a series of instructions.
///
/// # Arguments:
Expand Down
1 change: 1 addition & 0 deletions apps/contracts/vault/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl<'a> DeFindexVaultTest<'a> {
mod initialize;
mod deposit;
mod admin;
mod invest;
// mod emergency_withdraw;
// mod rebalance;
// mod withdraw;
Loading

0 comments on commit a8deddb

Please sign in to comment.