-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from paltalabs/fix/hodlTests
✅Add Hodl tests for deposit, events & withdraw
- Loading branch information
Showing
3 changed files
with
171 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
] | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
|
||
} |