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

✅Add Hodl tests for deposit, events & withdraw #270

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/contracts/strategies/hodl/src/test/hodl/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Val> = (0,).into_val(&test.env);
test.strategy.initialize(&test.token.address, &init_fn_args);
test.env.mock_all_auths();
}

#[test]
Expand Down
117 changes: 114 additions & 3 deletions apps/contracts/strategies/hodl/src/test/hodl/events.rs
Original file line number Diff line number Diff line change
@@ -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<Val> = (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<Val> = (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<Val> = (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<Val> = (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)
)
]
);
}
56 changes: 53 additions & 3 deletions apps/contracts/strategies/hodl/src/test/hodl/withdraw.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@

use crate::test::HodlStrategyTest;
use crate::test::StrategyError;
use soroban_sdk::{IntoVal, Vec, Val};
#[test]
fn withdraw() {
todo!()
}
let test = HodlStrategyTest::setup();

// initialize
let init_fn_args: Vec<Val> = (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)));

}
Loading