diff --git a/apps/contracts/strategies/hodl/src/test/hodl/deposit.rs b/apps/contracts/strategies/hodl/src/test/hodl/deposit.rs index 3267f97..7f07599 100644 --- a/apps/contracts/strategies/hodl/src/test/hodl/deposit.rs +++ b/apps/contracts/strategies/hodl/src/test/hodl/deposit.rs @@ -18,7 +18,10 @@ fn deposit_with_negative_amount() { // check auth #[test] fn deposit_mock_auths() { - todo!() + let test = HodlStrategyTest::setup(); + let init_fn_args: Vec = (0,).into_val(&test.env); + test.strategy.initialize(&test.token.address, &init_fn_args); + test.env.mock_all_auths(); } #[test] diff --git a/apps/contracts/strategies/hodl/src/test/hodl/events.rs b/apps/contracts/strategies/hodl/src/test/hodl/events.rs index 239a9bd..bd90303 100644 --- a/apps/contracts/strategies/hodl/src/test/hodl/events.rs +++ b/apps/contracts/strategies/hodl/src/test/hodl/events.rs @@ -1,6 +1,117 @@ -// TODO: Write tests for events + +use soroban_sdk::{symbol_short, testutils::Events, vec, IntoVal, Vec, Val}; +use crate::test::HodlStrategyTest; +use crate::event::{InitializedEvent, DepositEvent, HarvestEvent, WithdrawEvent}; + +#[test] +fn initialized (){ + let test = HodlStrategyTest::setup(); + let init_fn_args: Vec = (0,).into_val(&test.env); + test.strategy.initialize(&test.token.address, &init_fn_args); + + let initialized_event = test.env.events().all().last().unwrap(); + let expected_initialized_event = InitializedEvent { + asset: test.token.address, + }; + + assert_eq!( + vec![&test.env, initialized_event.clone()], + vec![ + &test.env, + ( + test.strategy.address.clone(), + ("HodlStrategy", symbol_short!("init")).into_val(&test.env), + (expected_initialized_event).into_val(&test.env) + ) + ] + ); +} + +#[test] +fn deposit() { + let test = HodlStrategyTest::setup(); + let init_fn_args: Vec = (0,).into_val(&test.env); + test.strategy.initialize(&test.token.address, &init_fn_args); + + let amount = 123456; + test.strategy.deposit(&amount, &test.user); + + let deposit_event = test.env.events().all().last().unwrap(); + let expected_deposit_event = DepositEvent { + amount, + from: test.user, + }; + + assert_eq!( + vec![&test.env, deposit_event.clone()], + vec![ + &test.env, + ( + test.strategy.address.clone(), + ("HodlStrategy", symbol_short!("deposit")).into_val(&test.env), + (expected_deposit_event).into_val(&test.env) + ) + ] + ); +} #[test] -fn test_events() { - todo!() +fn withdraw() { + let test = HodlStrategyTest::setup(); + let init_fn_args: Vec = (0,).into_val(&test.env); + test.strategy.initialize(&test.token.address, &init_fn_args); + let amount_to_deposit = 987654321; + test.strategy.deposit(&amount_to_deposit, &test.user); + + let amount_to_withdraw = 123456; + test.strategy.withdraw(&amount_to_withdraw, &test.user); + let withdraw_event = test.env.events().all().last().unwrap(); + let expected_withdraw_event = WithdrawEvent { + amount: amount_to_withdraw, + from: test.user, + }; + + assert_eq!( + vec![&test.env, withdraw_event.clone()], + vec![ + &test.env, + ( + test.strategy.address.clone(), + ("HodlStrategy", symbol_short!("withdraw")).into_val(&test.env), + (expected_withdraw_event).into_val(&test.env) + ) + ] + ); + + + +} + +#[test] +fn harvest(){ + let test = HodlStrategyTest::setup(); + let init_fn_args: Vec = (0,).into_val(&test.env); + test.strategy.initialize(&test.token.address, &init_fn_args); + + let amount = 123456; + test.strategy.deposit(&amount, &test.user); + test.strategy.harvest(&test.user); + + let harvest_event = test.env.events().all().last().unwrap(); + let expected_harvest_event = HarvestEvent { + amount: 0i128, + from: test.user, + }; + + assert_eq!( + vec![&test.env, harvest_event.clone()], + vec![ + &test.env, + ( + test.strategy.address.clone(), + ("HodlStrategy", symbol_short!("harvest")).into_val(&test.env), + (expected_harvest_event).into_val(&test.env) + ) + ] + ); } \ No newline at end of file diff --git a/apps/contracts/strategies/hodl/src/test/hodl/withdraw.rs b/apps/contracts/strategies/hodl/src/test/hodl/withdraw.rs index dd8aa9d..d420122 100644 --- a/apps/contracts/strategies/hodl/src/test/hodl/withdraw.rs +++ b/apps/contracts/strategies/hodl/src/test/hodl/withdraw.rs @@ -1,5 +1,55 @@ - +use crate::test::HodlStrategyTest; +use crate::test::StrategyError; +use soroban_sdk::{IntoVal, Vec, Val}; #[test] fn withdraw() { - todo!() -} \ No newline at end of file + let test = HodlStrategyTest::setup(); + + // initialize + let init_fn_args: Vec = (0,).into_val(&test.env); + test.strategy.initialize(&test.token.address, &init_fn_args); + + let balance = test.token.balance(&test.user); + let amount = 123456; + + //Try to withdraw before depositing + let result = test.strategy.try_withdraw(&amount, &test.user); + assert_eq!(result, Err(Ok(StrategyError::InsufficientBalance))); + + // Deposit amount of token from the user to the strategy + test.strategy.deposit(&amount, &test.user); + + let user_balance_after_deposit = test.token.balance(&test.user); + assert_eq!(user_balance_after_deposit, balance - amount); + + // Reading strategy balance + let strategy_balance_after_deposit = test.token.balance(&test.strategy.address); + assert_eq!(strategy_balance_after_deposit, amount); + + // Reading user balance on strategy contract + let user_balance_on_strategy = test.strategy.balance(&test.user); + assert_eq!(user_balance_on_strategy, amount); + + + let amount_to_withdraw = 100_000; + // Withdrawing token from the strategy to user + test.strategy.withdraw(&amount_to_withdraw, &test.user); + + // Reading user balance in token + let balance = test.token.balance(&test.user); + assert_eq!(balance, user_balance_after_deposit + amount_to_withdraw); + + // Reading strategy balance in token + let balance = test.token.balance(&test.strategy.address); + assert_eq!(balance, amount - amount_to_withdraw); + + // Reading user balance on strategy contract + let user_balance = test.strategy.balance(&test.user); + assert_eq!(user_balance, amount - amount_to_withdraw); + + //withdraw more than the user has + let amount_to_withdraw = user_balance + 1; + let result = test.strategy.try_withdraw(&amount_to_withdraw, &test.user); + assert_eq!(result, Err(Ok(StrategyError::InsufficientBalance))); + +}