Skip to content

Commit

Permalink
test invest correct idle funds
Browse files Browse the repository at this point in the history
  • Loading branch information
esteblock committed Nov 11, 2024
1 parent 9b47823 commit 3baafa2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 38 deletions.
11 changes: 4 additions & 7 deletions apps/contracts/vault/src/investment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use soroban_sdk::{Address, Env, Map, Vec, panic_with_error};
use soroban_sdk::{Env, Vec, panic_with_error};

use crate::{
models::{AssetStrategySet, AssetInvestmentAllocation},
strategies::invest_in_strategy,

&asset,
utils::{check_nonnegative_amount},
ContractError,
};
Expand All @@ -31,9 +29,7 @@ use crate::{
/// - 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` fu
&asset,
ction if all checks pass.
/// - **Execute Investment**: Calls the `invest_in_strategy` fuction if all checks pass.
///
/// # Errors
/// * Returns `ContractError::WrongAssetAddress` if an asset's address does not match the expected address.
Expand Down Expand Up @@ -90,7 +86,8 @@ pub fn check_and_execute_investments(


// Execute the investment if checks pass
invest_in_strategy(&e,
invest_in_strategy(
&e,
&asset.address,
&strategy.address,
&strategy_investment.amount)?;
Expand Down
3 changes: 2 additions & 1 deletion apps/contracts/vault/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ impl VaultManagementTrait for DeFindexVault {
},
ActionType::Invest => match (&instruction.strategy, &instruction.amount) {
(Some(strategy_address), Some(amount)) => {
invest_in_strategy(&e, strategy_address, amount)?;
invest_in_strategy(
&e, strategy_address, strategy_address, amount)?; // TODO THIS WILL FAIUL FOR NOW
}
_ => return Err(ContractError::MissingInstructionData),
},
Expand Down
43 changes: 14 additions & 29 deletions apps/contracts/vault/src/strategies.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use defindex_strategy_core::DeFindexStrategyClient;
use soroban_sdk::{Address, Env, vec, IntoVal, Vec, Val, Symbol};
use soroban_sdk::{Address, Env, vec, IntoVal, Symbol};
use soroban_sdk::auth::{ContractContext, InvokerContractAuthEntry, SubContractInvocation};


Expand Down Expand Up @@ -137,37 +137,18 @@ pub fn withdraw_from_strategy(
}

pub fn invest_in_strategy(
e: &Env,\
e: &Env,
asset_address: &Address,
strategy_address: &Address,
amount: &i128,
) -> Result<(), ContractError> {

// e.authorize_as_current_contract(vec![
// &e,
// InvokerContractAuthEntry::Contract(SubContractInvocation {
// context: ContractContext {
// contract: token_a.clone(),
// fn_name: Symbol::new(&e, "transfer"),
// args: transfer_args_a.clone(),
// },
// sub_invocations: vec![&e],
// }),
// InvokerContractAuthEntry::Contract(SubContractInvocation {
// context: ContractContext {
// contract: token_b.clone(),
// fn_name: Symbol::new(&e, "transfer"),
// args: transfer_args_b.clone(),
// },
// sub_invocations: vec![&e],
// }),
// ]);

// Now we will handle funds on behalf of the contract, not the caller (manager or user)
let mut transfer_args: Vec<Val> = vec![&e];
transfer_args.push_back(e.current_contract_address().into_val(&e)); //from
transfer_args.push_back(strategy_address.into_val(&e)); //to
transfer_args.push_back(amount.into_val(&e)); //amount

// // Now we will handle funds on behalf of the contract, not the caller (manager or user)
// let mut transfer_args: Vec<Val> = vec![&e];
// transfer_args.push_back(e.current_contract_address().into_val(&e)); //from
// transfer_args.push_back(strategy_address.into_val(&e)); //to
// transfer_args.push_back(amount.into_val(&e)); //amount


e.authorize_as_current_contract(vec![
Expand All @@ -176,11 +157,15 @@ pub fn invest_in_strategy(
context: ContractContext {
contract: asset_address.clone(),
fn_name: Symbol::new(&e, "transfer"),
args: transfer_args.clone(),
args: (
e.current_contract_address(),
strategy_address,
amount.clone()).into_val(e),

},
sub_invocations: vec![&e],
}),
]);
]);


let strategy_client = get_strategy_client(&e, strategy_address.clone());
Expand Down
2 changes: 1 addition & 1 deletion apps/contracts/vault/src/test/invest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn test_invest_wrong_strategy_length() {
let test = DeFindexVaultTest::setup();

let strategy_params_token0 = create_strategy_params_token0(&test);
let strategy_params_token1 = create_strategy_params_token1(&test);
// let strategy_params_token1 = create_strategy_params_token1(&test);

let assets: Vec<AssetStrategySet> = sorobanvec![
&test.env,
Expand Down

0 comments on commit 3baafa2

Please sign in to comment.